|
| 1 | +""" |
| 2 | +Tests for napari_lattice.reader's channel-axis splitting decision and the reader -> GUI |
| 3 | +dimension-order contract. |
| 4 | +
|
| 5 | +bioio fabricates a full ``TCZYX`` order for plain TIFFs (padding missing axes with |
| 6 | +size-1 and auto-naming channels ``Channel:i:j``). The reader must only split a layer |
| 7 | +along its channel axis when that axis is genuine; otherwise single-channel stacks |
| 8 | +collapse in dimensionality and frame axes that bioio mislabelled "C" get carved into |
| 9 | +spurious channels - which is what hides the TCZYX/CTZYX dimension-order options the user |
| 10 | +needs to correct the interpretation. |
| 11 | +""" |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import pytest |
| 16 | +import tifffile |
| 17 | +from bioio import BioImage |
| 18 | +from importlib_resources import as_file |
| 19 | + |
| 20 | +from lls_core.sample import resources |
| 21 | +from napari_lattice.fields import dimension_order_options |
| 22 | +from napari_lattice.reader import _AUTO_CHANNEL_NAME, bioio_reader |
| 23 | + |
| 24 | + |
| 25 | +def _plain_tiff(tmp_path, shape) -> str: |
| 26 | + p = tmp_path / "plain.tif" |
| 27 | + tifffile.imwrite(str(p), np.zeros(shape, dtype=np.uint16)) |
| 28 | + return str(p) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("on_disk", [ |
| 32 | + pytest.param((5, 1, 30, 16, 16), id="single_channel"), # bioio -> TCZYX, C=1 |
| 33 | + pytest.param((4, 30, 16, 16), id="frames_mislabelled_C"), # bioio -> TCZYX, C=4 |
| 34 | + pytest.param((3, 2, 30, 16, 16), id="multichannel_no_meta"), # placeholder names |
| 35 | +]) |
| 36 | +def test_plain_tiff_is_not_split(tmp_path, on_disk): |
| 37 | + # No genuine channel metadata -> keep the layer whole so the GUI can offer the |
| 38 | + # full TCZYX/CTZYX dimension-order options for the user to correct. |
| 39 | + layers = bioio_reader(_plain_tiff(tmp_path, on_disk)) |
| 40 | + assert len(layers) == 1 |
| 41 | + _data, add_kwargs, _ = layers[0] |
| 42 | + assert "channel_axis" not in add_kwargs |
| 43 | + |
| 44 | + |
| 45 | +def test_genuine_multichannel_is_split(): |
| 46 | + # A real acquisition (a format-specific reader with descriptive channel names) is |
| 47 | + # still split into one layer per channel for convenient per-channel viewing. |
| 48 | + with as_file(resources / "LLS7_t1_ch3.czi") as p: |
| 49 | + image = BioImage(str(p)) |
| 50 | + layers = bioio_reader(str(p)) |
| 51 | + _data, add_kwargs, _ = layers[0] |
| 52 | + assert add_kwargs["channel_axis"] == image.dims.order.index("C") |
| 53 | + assert add_kwargs["name"] == list(image.channel_names) |
| 54 | + # The channel axis is dropped from the per-channel dimension metadata. |
| 55 | + assert "C" not in add_kwargs["metadata"]["dimensions"] |
| 56 | + |
| 57 | + |
| 58 | +def test_single_channel_tiff_yields_5d_dimension_options(tmp_path): |
| 59 | + # The reader -> GUI contract: a single-channel stack stays 5D, and the dimension |
| 60 | + # dropdown must then offer the 5D orders so the user can set/confirm the order. |
| 61 | + data, _add_kwargs, _ = bioio_reader(_plain_tiff(tmp_path, (5, 1, 30, 16, 16)))[0] |
| 62 | + options = dimension_order_options(len(data.shape)) |
| 63 | + assert "TCZYX" in options and "CTZYX" in options |
| 64 | + |
| 65 | + |
| 66 | +def test_bioio_placeholder_channel_name_format_unchanged(tmp_path): |
| 67 | + # Canary: our split decision relies on recognising bioio's placeholder channel |
| 68 | + # names for plain TIFFs. If a bioio upgrade changes this format, fail loudly here |
| 69 | + # rather than silently reverting to splitting every plain TIFF. |
| 70 | + names = BioImage(_plain_tiff(tmp_path, (3, 2, 30, 16, 16))).channel_names |
| 71 | + assert names and all(_AUTO_CHANNEL_NAME.match(str(n)) for n in names), names |
0 commit comments