Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions src/ndr/format/smartspim/ndr_matlab_python_bridge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/<Y>_<X>_<z*10>_ch<k>.tiff (ferret-lightsheet
formats/SmartSPIM.md). Channel -> ch<k> comes from the single
formats/SmartSPIM.md), or <z*10>_ch<k>.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<k>). 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<k> comes from the single
_ch<k> suffix in <channel>_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
Expand Down
27 changes: 23 additions & 4 deletions src/ndr/format/smartspim/readAllChannelsInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

``All_Channels`` is a flat folder of 2-D TIFFs, one per (z, channel),
named ``<Y origin>_<X origin>_<z*10>_ch<k>.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, ``<z*10>_ch<k>.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<k>`` index follows the
filter-channel number, NOT the acquisition order. It is taken from the
Expand All @@ -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"}

Expand All @@ -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


Expand Down
21 changes: 21 additions & 0 deletions tests/test_smartspim_all_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <z*10>_ch<k>.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"])
Expand Down
Loading