diff --git a/src/ndr/format/smartspim/ndr_matlab_python_bridge.yaml b/src/ndr/format/smartspim/ndr_matlab_python_bridge.yaml index 8ea41e9..4a9878f 100644 --- a/src/ndr/format/smartspim/ndr_matlab_python_bridge.yaml +++ b/src/ndr/format/smartspim/ndr_matlab_python_bridge.yaml @@ -125,12 +125,17 @@ functions: - name: readAllChannelsInfo matlab_path: "+ndr/+format/+smartspim/readAllChannelsInfo.m" status: regular_port - matlab_last_sync_hash: "b8f0e4b" + matlab_last_sync_hash: "15d82e1" python_path: "ndr/format/smartspim/readAllChannelsInfo.py" decision_log: >- Indexes SmartSPIM AutoStitch's stitched output All_Channels/___ch.tiff (ferret-lightsheet - formats/SmartSPIM.md). Channel -> ch comes from the single + formats/SmartSPIM.md), or _ch.tiff as other pipeline + versions name the slices (NDR-matlab ffcf678, ported: the origins + are optional in _SLICE_RE, z is the number before _ch). A listing + that names a file twice (macOS SMB directory caching) or two files + for one channel and z is refused (NDR-matlab 15d82e1, ported: + RuntimeError where MATLAB raises DuplicateListing / DuplicateSlice). Channel -> ch comes from the single _ch suffix in _MIP_stitched, else the stable rank of the filter-channel number (same rule as ferret-lightsheet's SmartSPIMVolume.all_channels_ch_index). Slices sort by the numeric diff --git a/src/ndr/format/smartspim/readAllChannelsInfo.py b/src/ndr/format/smartspim/readAllChannelsInfo.py index 2cbf5c0..a9ee12a 100644 --- a/src/ndr/format/smartspim/readAllChannelsInfo.py +++ b/src/ndr/format/smartspim/readAllChannelsInfo.py @@ -11,7 +11,9 @@ ``All_Channels`` is a flat folder of 2-D TIFFs, one per (z, channel), named ``___ch.tiff`` (e.g. -``675050_455000_000110_ch1.tiff``), plus an ``ImageList.txt`` for +``675050_455000_000110_ch1.tiff``) or, by other versions of the +pipeline, ``_ch.tiff`` (e.g. ``000110_ch1.tiff``), plus an +``ImageList.txt`` for Imaris. Every slice spans the whole stitched canvas (height = Y, width = X), so there are no tiles. The ``ch`` index follows the filter-channel number, NOT the acquisition order. It is taken from the @@ -36,8 +38,9 @@ __all__ = ["readAllChannelsInfo"] -# e.g. 675050_455000_000110_ch0.tiff -> (Y origin, X origin, z*10, k) -_SLICE_RE = re.compile(r"^(\d+)_(\d+)_(\d+)_ch(\d+)\.tiff?$", re.IGNORECASE) +# e.g. 675050_455000_000110_ch0.tiff -> (z*10, k), and, as other versions +# of the pipeline name them, without the origins: 000110_ch0.tiff +_SLICE_RE = re.compile(r"^(?:\d+_\d+_)?(\d+)_ch(\d+)\.tiff?$", re.IGNORECASE) _MATLAB_CLASS = {"float32": "single", "float64": "double", "bool": "logical"} @@ -62,7 +65,23 @@ def _index_slices(folder: Path) -> list[tuple[int, int, str]]: continue m = _SLICE_RE.match(e.name) if m: - out.append((int(m.group(4)), int(m.group(3)), str(folder / e.name))) + out.append((int(m.group(2)), int(m.group(1)), str(folder / e.name))) + # A folder cannot hold one name twice, but a network share's listing can + # return it twice (and leave others out): macOS SMB directory caching + # does, on large folders. A volume built from it would be wrong. + paths = [p for _, _, p in out] + if len(set(paths)) < len(paths): + raise RuntimeError( + f"The listing of {folder} names some files more than once, so it " + "cannot be trusted (on a macOS SMB share, turn off directory " + "caching: dir_cache_off=yes in /etc/nsmb.conf, then remount)." + ) + keys = [(k, z) for k, z, _ in out] + if len(set(keys)) < len(keys): + raise RuntimeError( + f"{folder} holds more than one file for the same channel and z " + "(tiles rather than stitched slices?)." + ) return out diff --git a/tests/test_smartspim_all_channels.py b/tests/test_smartspim_all_channels.py index 79e4df8..ee562d1 100644 --- a/tests/test_smartspim_all_channels.py +++ b/tests/test_smartspim_all_channels.py @@ -83,6 +83,27 @@ def test_channels_by_filter_channel_rank(self, fixture_dir): assert info["voxelSizeUm"] == [4, 4, 4] assert Path(info["channels"][1]["slicePaths"][-1]).name == "200000_100000_000030_ch1.tiff" + def test_slices_named_without_origins(self, fixture_dir): + # Some versions of the pipeline name the slices _ch.tiff. + before = readAllChannelsInfo(str(fixture_dir)) + for p in list(_all_dir(fixture_dir).glob("*_ch*.tiff")): + y, x, rest = p.name.split("_", 2) + p.rename(p.with_name(rest)) + assert hasAllChannels(str(fixture_dir)) is True + info = readAllChannelsInfo(str(fixture_dir)) + assert info["channelNames"] == before["channelNames"] + assert [c["numSlices"] for c in info["channels"]] == [4, 4] + assert Path(info["channels"][1]["slicePaths"][-1]).name == "000030_ch1.tiff" + + def test_two_files_for_one_slice_are_refused(self, fixture_dir): + # Same channel and z under two names (tiles, not stitched slices). + d = _all_dir(fixture_dir) + src = next(d.glob("*_000000_ch0.tiff")) + shutil.copy(src, d / "000000_ch0.tiff") + with pytest.raises(RuntimeError, match="same channel and z"): + readAllChannelsInfo(str(fixture_dir)) + assert hasAllChannels(str(fixture_dir)) is False + def test_without_header(self, fixture_dir): info = readAllChannelsInfo(str(fixture_dir), ReadHeader=False) assert math.isnan(info["height"])