Skip to content

Commit

Permalink
feat(chart_sdk): add regression distribution chart + utils to get buc…
Browse files Browse the repository at this point in the history
…ket data fromatted
  • Loading branch information
dvalleri committed Dec 10, 2024
1 parent 85d6ea5 commit 2e97615
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 149 deletions.
5 changes: 4 additions & 1 deletion chart_sdk/radicalbit_platform_chart_sdk/charts/chart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ipecharts import EChartsRawWidget
from radicalbit_platform_chart_sdk.charts import ChartData, NumericalBarChartData
from .utils import get_formatted_bucket_data


class Chart:
Expand All @@ -20,6 +21,8 @@ def placeholder_chart(self, data: ChartData) -> EChartsRawWidget:
return EChartsRawWidget(option=option)

def numerical_bar_chart(self, data: NumericalBarChartData) -> EChartsRawWidget:
bucket_data_formatted = get_formatted_bucket_data(bucket_data=data.bucket_data)

reference_data_json = {
"title": "reference",
"type": "bar",
Expand Down Expand Up @@ -65,7 +68,7 @@ def numerical_bar_chart(self, data: NumericalBarChartData) -> EChartsRawWidget:
"color": "#9B99A1",
"rotate": 20
},
"data": data.bucket_data
"data": bucket_data_formatted
},
"yAxis": {
"type": "value",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from ipecharts import EChartsRawWidget
from .multi_class_chart_data import MultiClasssificationChartData
from .multi_class_chart_data import MultiClassificationChartData


class MultiClassificationChart:
def __init__(self) -> None:
pass

def distribution_chart(self, data: MultiClasssificationChartData) -> EChartsRawWidget:
def distribution_chart(self, data: MultiClassificationChartData) -> EChartsRawWidget:
reference_json_data = data.model_dump().get('reference_data')
current_data_json = data.model_dump().get('current_data')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MultiClassificationData(BaseModel):
count: float
value: float

class MultiClasssificationChartData(BaseModel):
class MultiClassificationChartData(BaseModel):
title: str
reference_data: List[MultiClassificationData]
current_data: Optional[List[MultiClassificationData]] = None
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .regression_chart import RegressionChart
from .regression_chart_data import RegressionChartData

__all__ = [
'RegressionChart',
'RegressionChartData'
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from ipecharts import EChartsRawWidget
from .regression_chart_data import RegressionChartData
from ..utils import get_formatted_bucket_data


class RegressionChart:
def __init__(self) -> None:
pass

def distribution_chart(self, data: RegressionChartData) -> EChartsRawWidget:
bucket_data_formatted = get_formatted_bucket_data(bucket_data=data.bucket_data)

reference_json_data = data.model_dump().get('reference_data')
current_data_json = data.model_dump().get('current_data')

reference_series_data = {
"title": "reference",
"type": "bar",
"itemStyle": {
"color": "#9B99A1"
},
"data": reference_json_data
}

current_series_data = {
"title": "current",
"type": "bar",
"itemStyle": {
"color": "#3695d9"
},
"data": current_data_json
}

series = [reference_series_data] if not data.current_data else [
reference_series_data, current_series_data]

option = {
"grid": {
"left": 0,
"right": 20,
"bottom": 0,
"top": 10,
"containLabel": True
},
"xAxis": {
"type": "category",
"axisTick": {
"show": False
},
"axisLine": {
"show": False
},
"splitLine": {
"show": False
},
"axisLabel": {
"fontSize": 12,
"interval": 0,
"color": "#9b99a1",
"rotate": 20
},
"data":bucket_data_formatted,
},
"yAxis": {
"type": "value",
"axisLabel": {
"fontSize": 9,
"color": "#9b99a1"
},
"splitLine": {
"lineStyle": {
"color": "#9f9f9f54"
}
}
},
"emphasis": {
"disabled": True
},
"barCategoryGap": "0",
"barGap": "0",
"itemStyle": {
"borderWidth": 1,
"borderColor": "rgba(201, 25, 25, 1)"
},
"series": series
}

return EChartsRawWidget(option=option)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Optional, List
from pydantic import BaseModel


class RegressionChartData(BaseModel):
title: str
bucket_data: List[str]
reference_data: List[float]
current_data: Optional[List[float]] = None
15 changes: 15 additions & 0 deletions chart_sdk/radicalbit_platform_chart_sdk/charts/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import List



def get_formatted_bucket_data(bucket_data:List[str]):
bucket_data_formatted = []
bucket_data_len = len(bucket_data) -1

for idx, d in enumerate(bucket_data):
close_bracket = ']' if idx == bucket_data_len - 1 else ')'
if idx < bucket_data_len:
element = '['+ d + ',' + bucket_data[ idx + 1 ] + close_bracket
bucket_data_formatted.append(element)

return bucket_data_formatted
12 changes: 6 additions & 6 deletions chart_sdk/test/multi_class.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "9ded8395-69cd-4c5e-82dd-69e95d6b8182",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -30,10 +30,10 @@
}
],
"source": [
"from radicalbit_platform_chart_sdk.charts.multi_classification import MultiClassificationChart, MultiClasssificationChartData\n",
"from radicalbit_platform_chart_sdk.charts.multi_classification import MultiClassificationChart, MultiClassificationChartData\n",
"\n",
"MultiClassificationChart().distribution_chart(data=\n",
" MultiClasssificationChartData(\n",
" MultiClassificationChartData(\n",
" title=\"ground_truth\",\n",
" reference_data=[\n",
" { \"percentage\": 31.443298969072163, \"count\": 305, \"value\": 305 },\n",
Expand All @@ -46,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "c1f952cc-af88-4479-9111-1b3d6a6cc705",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -74,10 +74,10 @@
}
],
"source": [
"from radicalbit_platform_chart_sdk.charts.multi_classification import MultiClassificationChart, MultiClasssificationChartData\n",
"from radicalbit_platform_chart_sdk.charts.multi_classification import MultiClassificationChart, MultiClassificationChartData\n",
"\n",
"MultiClassificationChart().distribution_chart(data=\n",
" MultiClasssificationChartData(\n",
" MultiClassificationChartData(\n",
" title=\"ground_truth\",\n",
" reference_data=[\n",
" { \"percentage\": 31.443298969072163, \"count\": 305, \"value\": 305 },\n",
Expand Down
Loading

0 comments on commit 2e97615

Please sign in to comment.