Skip to content

Commit 7158b2a

Browse files
committed
Enhance _add_table_results_to_axes to support geometry results and update coordinate label extraction
1 parent 6265cc6 commit 7158b2a

File tree

1 file changed

+78
-11
lines changed

1 file changed

+78
-11
lines changed

datalab_kernel/plotter.py

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,26 @@ def _extract_table_results_from_metadata(obj) -> list:
124124
return results
125125

126126

127-
def _add_table_results_to_axes(ax: Axes, table_results: list) -> None:
128-
"""Add table results as text annotation to matplotlib axes.
127+
def _add_table_results_to_axes(
128+
ax: Axes, table_results: list, geometry_results: list | None = None
129+
) -> None:
130+
"""Add table and geometry results as text annotation to matplotlib axes.
129131
130-
Formats TableResult objects as a text box displayed in the upper-left
131-
corner of the axes, similar to DataLab's result label display.
132+
Formats TableResult and GeometryResult objects as a text box displayed in
133+
the upper-left corner of the axes, similar to DataLab's result label display.
132134
133135
Args:
134136
ax: Matplotlib axes object
135137
table_results: List of TableResult objects to display
138+
geometry_results: Optional list of GeometryResult objects to display
136139
"""
137-
if not table_results:
140+
if not table_results and not geometry_results:
138141
return
139142

140-
# Build text content from all table results
143+
# Build text content from all results
141144
text_lines = []
145+
146+
# Add table results first (statistics)
142147
for table in table_results:
143148
# Add table title as header
144149
text_lines.append(f"{table.title}:")
@@ -160,7 +165,33 @@ def _add_table_results_to_axes(ax: Axes, table_results: list) -> None:
160165
formatted = str(value)
161166
text_lines.append(f" {header}: {formatted}")
162167

163-
text_lines.append("") # Empty line between tables
168+
text_lines.append("") # Empty line between results
169+
170+
# Add geometry results after table results
171+
if geometry_results:
172+
for geometry in geometry_results:
173+
text_lines.append(f"{geometry.title}:")
174+
text_lines.append(" Value")
175+
176+
# Get coordinate labels based on geometry kind
177+
coord_labels = _get_geometry_coord_labels(geometry)
178+
179+
# Display first row of coords (most geometry results have one row)
180+
if len(geometry.coords) > 0:
181+
coords = (
182+
geometry.coords[0] if geometry.coords.ndim > 1 else geometry.coords
183+
)
184+
for label, value in zip(coord_labels, coords):
185+
if isinstance(value, float):
186+
if abs(value) < 0.001 or abs(value) >= 10000:
187+
formatted = f"{value:.3g}"
188+
else:
189+
formatted = f"{value:.3f}"
190+
else:
191+
formatted = str(value)
192+
text_lines.append(f" {label}: {formatted}")
193+
194+
text_lines.append("") # Empty line between results
164195

165196
# Remove trailing empty line
166197
if text_lines and text_lines[-1] == "":
@@ -187,6 +218,42 @@ def _add_table_results_to_axes(ax: Axes, table_results: list) -> None:
187218
)
188219

189220

221+
def _get_geometry_coord_labels(geometry) -> list[str]:
222+
"""Get coordinate labels for a geometry result based on its kind.
223+
224+
Args:
225+
geometry: GeometryResult object
226+
227+
Returns:
228+
List of coordinate labels (e.g., ["x", "y", "r"] for circle)
229+
"""
230+
# Delayed import
231+
# pylint: disable=import-outside-toplevel
232+
from sigima.objects import KindShape
233+
234+
if geometry.kind == KindShape.POINT:
235+
return ["x", "y"]
236+
if geometry.kind == KindShape.MARKER:
237+
return ["x", "y"]
238+
if geometry.kind == KindShape.RECTANGLE:
239+
return ["x0", "y0", "dx", "dy"]
240+
if geometry.kind == KindShape.CIRCLE:
241+
return ["x", "y", "r"]
242+
if geometry.kind == KindShape.SEGMENT:
243+
return ["x0", "y0", "x1", "y1"]
244+
if geometry.kind == KindShape.ELLIPSE:
245+
return ["x", "y", "a", "b", "θ"]
246+
# Default for POLYGON and others
247+
return [
248+
f"c{i}"
249+
for i in range(
250+
len(geometry.coords[0])
251+
if geometry.coords.ndim > 1
252+
else len(geometry.coords)
253+
)
254+
]
255+
256+
190257
def _add_single_roi_to_axes(ax: Axes, roi, obj=None) -> None:
191258
"""Add single ROI overlay to matplotlib axes.
192259
@@ -682,7 +749,7 @@ def _render_signal(self, ax: Axes) -> None:
682749

683750
# Auto-extract and display table results (statistics) from metadata
684751
table_results = _extract_table_results_from_metadata(obj)
685-
_add_table_results_to_axes(ax, table_results)
752+
_add_table_results_to_axes(ax, table_results, metadata_results)
686753

687754
def _render_image(self, ax: Axes, fig) -> None:
688755
"""Render image data to axes.
@@ -754,7 +821,7 @@ def _render_image(self, ax: Axes, fig) -> None:
754821

755822
# Auto-extract and display table results (statistics) from metadata
756823
table_results = _extract_table_results_from_metadata(obj)
757-
_add_table_results_to_axes(ax, table_results)
824+
_add_table_results_to_axes(ax, table_results, results_to_display)
758825

759826
def __repr__(self) -> str:
760827
"""Return string representation."""
@@ -881,7 +948,7 @@ def _render_to_png(self) -> bytes:
881948

882949
# Auto-extract and display table results (statistics) from metadata
883950
table_results = _extract_table_results_from_metadata(obj)
884-
_add_table_results_to_axes(ax, table_results)
951+
_add_table_results_to_axes(ax, table_results, metadata_results)
885952

886953
elif isinstance(data_or_obj, tuple) and len(data_or_obj) == 2:
887954
# Tuple of (x, y) arrays
@@ -1145,7 +1212,7 @@ def _render_to_png(self) -> bytes:
11451212
# Auto-extract and display table results (statistics) from metadata
11461213
if is_image_obj:
11471214
table_results = _extract_table_results_from_metadata(img)
1148-
_add_table_results_to_axes(ax, table_results)
1215+
_add_table_results_to_axes(ax, table_results, results_to_display)
11491216

11501217
# Hide unused subplots
11511218
for idx in range(n_images, len(axes_flat)):

0 commit comments

Comments
 (0)