Skip to content

Commit 50214ff

Browse files
committed
Fix missing rounding from rebase
1 parent 0fda6a2 commit 50214ff

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

chart_data_extractor/e2b_charts/graphs/bars.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pydantic import BaseModel, Field
55

66
from .base import Graph2D, GraphType
7+
from ..utils.rounding import dynamic_round
78

89

910
class BarData(BaseModel):
@@ -48,6 +49,7 @@ class BoxAndWhiskerData(BaseModel):
4849
median: float
4950
third_quartile: float
5051
max: float
52+
outliers: List[float]
5153

5254

5355
class BoxAndWhiskerGraph(Graph2D):
@@ -56,22 +58,23 @@ class BoxAndWhiskerGraph(Graph2D):
5658
elements: List[BoxAndWhiskerData] = Field(default_factory=list)
5759

5860
def _extract_info(self, ax: Axes) -> None:
59-
super()._extract_info(ax)
61+
labels = [item.get_text() for item in ax.get_xticklabels()]
6062

6163
boxes = []
62-
for box in ax.patches:
64+
for label, box in zip(labels, ax.patches):
6365
vertices = box.get_path().vertices
64-
x_vertices = vertices[:, 0]
65-
y_vertices = vertices[:, 1]
66+
x_vertices = [dynamic_round(x) for x in vertices[:, 0]]
67+
y_vertices = [dynamic_round(y) for y in vertices[:, 1]]
6668
x = min(x_vertices)
6769
y = min(y_vertices)
6870
boxes.append(
6971
{
7072
"x": x,
7173
"y": y,
72-
"label": box.get_label(),
73-
"width": round(max(x_vertices) - x, 4),
74-
"height": round(max(y_vertices) - y, 4),
74+
"label": label,
75+
"width": max(x_vertices) - x,
76+
"height": max(y_vertices) - y,
77+
"outliers": [],
7578
}
7679
)
7780

@@ -85,13 +88,21 @@ def _extract_info(self, ax: Axes) -> None:
8588
box["x"], box["y"] = box["y"], box["x"]
8689
box["width"], box["height"] = box["height"], box["width"]
8790

88-
for line in ax.lines:
89-
xdata = line.get_xdata()
90-
ydata = line.get_ydata()
91+
for i, line in enumerate(ax.lines):
92+
xdata = [dynamic_round(x) for x in line.get_xdata()]
93+
ydata = [dynamic_round(y) for y in line.get_ydata()]
9194

9295
if orientation == "vertical":
9396
xdata, ydata = ydata, xdata
9497

98+
if len(xdata) == 1:
99+
for box in boxes:
100+
if box["x"] <= xdata[0] <= box["x"] + box["width"]:
101+
break
102+
else:
103+
continue
104+
105+
box["outliers"].append(ydata[0])
95106
if len(ydata) != 2:
96107
continue
97108
for box in boxes:
@@ -101,6 +112,7 @@ def _extract_info(self, ax: Axes) -> None:
101112
continue
102113

103114
if (
115+
# Check if the line is inside the box, prevent floating point errors
104116
ydata[0] == ydata[1]
105117
and box["y"] <= ydata[0] <= box["y"] + box["height"]
106118
):
@@ -122,6 +134,7 @@ def _extract_info(self, ax: Axes) -> None:
122134
median=box["median"],
123135
third_quartile=box["y"] + box["height"],
124136
max=box["whisker_upper"],
137+
outliers=box["outliers"],
125138
)
126139
for box in boxes
127140
]

chart_data_extractor/e2b_charts/graphs/pie.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from decimal import Decimal
12
from typing import Literal, List
23

34
from matplotlib.axes import Axes
45
from pydantic import BaseModel, Field
56

67
from .base import Graph, GraphType
8+
from ..utils.rounding import dynamic_round
79

810

911
class PieData(BaseModel):
@@ -23,7 +25,7 @@ def _extract_info(self, ax: Axes) -> None:
2325
for wedge in ax.patches:
2426
pie_data = PieData(
2527
label=wedge.get_label(),
26-
angle=abs(round(wedge.theta2 - wedge.theta1, 4)),
28+
angle=abs(dynamic_round(Decimal(wedge.theta2) - Decimal(wedge.theta1))),
2729
radius=wedge.r,
2830
)
2931

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from decimal import Decimal, localcontext
2+
3+
4+
def dynamic_round(number):
5+
# Convert to Decimal for precise control
6+
decimal_number = Decimal(str(number))
7+
8+
# Dynamically determine precision based on magnitude
9+
precision = max(1, 16 - decimal_number.adjusted()) # 16 digits of precision
10+
11+
with localcontext() as ctx:
12+
ctx.prec = precision # Set the dynamic precision
13+
return +decimal_number # The + operator applies rounding

0 commit comments

Comments
 (0)