Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/cytodataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5029,7 +5029,10 @@ def _generate_jupyter_dataframe_html( # noqa: C901, PLR0912, PLR0915
# Re-add bounding box columns if they are no longer available
bounding_box_externally_joined = False
if self._custom_attrs["data_bounding_box"] is not None and not all(
col in self.columns.tolist()
col
in (
data if self._custom_attrs["is_transposed"] else self
).columns.tolist()
for col in self._custom_attrs["data_bounding_box"].columns.tolist()
):
logger.debug("Re-adding bounding box columns.")
Expand All @@ -5049,15 +5052,18 @@ def _generate_jupyter_dataframe_html( # noqa: C901, PLR0912, PLR0915

# Re-add compartment center xy columns if they are no longer available
compartment_center_externally_joined = False
use_data_for_compartment_center = (
self._custom_attrs["is_transposed"] or bounding_box_externally_joined
)
if self._custom_attrs["compartment_center_xy"] is not None and not all(
col
in (data if bounding_box_externally_joined else self).columns.tolist()
in (data if use_data_for_compartment_center else self).columns.tolist()
for col in self._custom_attrs["compartment_center_xy"].columns.tolist()
):
logger.debug("Re-adding compartment center xy columns.")
data = (
data.join(other=self._custom_attrs["compartment_center_xy"])
if bounding_box_externally_joined
if use_data_for_compartment_center
else self.join(other=self._custom_attrs["compartment_center_xy"])
)
compartment_center_externally_joined = True
Expand All @@ -5072,11 +5078,13 @@ def _generate_jupyter_dataframe_html( # noqa: C901, PLR0912, PLR0915

# Re-add image path columns if they are no longer available
image_paths_externally_joined = False
use_data_for_image_paths = (
self._custom_attrs["is_transposed"]
or compartment_center_externally_joined
or bounding_box_externally_joined
)
if self._custom_attrs["data_image_paths"] is not None and not all(
col
in (
data if compartment_center_externally_joined else self
).columns.tolist()
col in (data if use_data_for_image_paths else self).columns.tolist()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
for col in self._custom_attrs["data_image_paths"].columns.tolist()
):
logger.debug("Re-adding image path columns.")
Expand All @@ -5087,8 +5095,7 @@ def _generate_jupyter_dataframe_html( # noqa: C901, PLR0912, PLR0915
)
data = (
data.join(other=self._custom_attrs["data_image_paths"])
if compartment_center_externally_joined
or bounding_box_externally_joined
if use_data_for_image_paths
else self.join(other=self._custom_attrs["data_image_paths"])
)
image_paths_externally_joined = True
Expand Down
49 changes: 49 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,55 @@ def displayed_image_sizes(frame: CytoDataFrame) -> list:
assert all(size == (expected_width, expected_height) for size in sizes)


def test_repr_html_transposed_render_whole_image_without_bounding_box_or_center(
cytotable_NF1_data_parquet_shrunken: str,
):
"""
Tests that ``.T`` on a whole-FOV CytoDataFrame (no bounding box or
compartment center columns) still renders a transposed table with the
embedded image intact, rather than falling back to an untransposed table
of raw filenames.

See https://github.com/cytomining/CytoDataFrame/issues/226.
"""

image_dir = (
f"{pathlib.Path(cytotable_NF1_data_parquet_shrunken).parent}/Plate_2_images"
)
image_filename = next(pathlib.Path(image_dir).glob("*.tif")).name

# a minimal whole-FOV frame with no bounding box or compartment
# center columns at all (mirrors the issue's reproduction).
df = pd.DataFrame(
{
"FileName_OrigDNA": [image_filename, image_filename],
"PathName_OrigDNA": [image_dir, image_dir],
"SomeMetric": [-2.5, -2.7],
}
)

whole_frame = CytoDataFrame(df, display_options={"render_whole_image": True})
assert whole_frame._custom_attrs["data_bounding_box"] is None
assert whole_frame._custom_attrs["compartment_center_xy"] is None

non_transposed_html = whole_frame.head(2)._repr_html_(debug=True)
transposed_html = whole_frame.head(2).T._repr_html_(debug=True)

non_transposed_images = re.findall(
r"data:image/png;base64,([^\"]+)", non_transposed_html
)
transposed_images = re.findall(r"data:image/png;base64,([^\"]+)", transposed_html)

# the transposed rendering must still embed the same images as the
# non-transposed rendering (previously it silently rendered none).
assert len(transposed_images) == len(non_transposed_images) > 0

# the transposed rendering must actually be transposed: the
# FileName_OrigDNA column becomes a row label instead of appearing
# in the header row.
assert ">FileName_OrigDNA<" in transposed_html


def test_repr_html_offset_bounding_box_warns_when_centers_missing_with_bbox(
cytotable_NF1_data_parquet_shrunken: str,
caplog: pytest.LogCaptureFixture,
Expand Down
Loading