Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hacky fix for files with subloops #193

Merged
merged 4 commits into from
Feb 22, 2024
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
18 changes: 16 additions & 2 deletions src/nd2/_parse/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@


def load_experiment(
level: int, src: RawExperimentDict, dest: list[ExpLoop] | None = None
src: RawExperimentDict, level: int = 0, dest: list[ExpLoop] | None = None
) -> list[ExpLoop]:
"""Parse the "ImageMetadata[LV]!" section of an nd2 file."""
dest = dest or []
Expand All @@ -208,10 +208,24 @@
if prev.count < loop.count:
dest[-1] = loop

# FIXME:
# hack for file in https://github.com/tlambert03/nd2/issues/190
# there is a better fix, but this is a very rare case
loop_params = src.get("uLoopPars", {})
if "pSubLoops" in loop_params:
loop_params = cast("NETimeLoopPars", loop_params)
subloops = loop_params["pSubLoops"]
i0 = "i0000000000"
if i0 in subloops:
subnext = subloops[i0]["ppNextLevelEx"]
if i0 in subnext:
experiment = subnext[i0]["SLxExperiment"]
dest.extend(load_experiment(experiment))

Check warning on line 223 in src/nd2/_parse/_parse.py

View check run for this annotation

Codecov / codecov/patch

src/nd2/_parse/_parse.py#L216-L223

Added lines #L216 - L223 were not covered by tests

next_level_src = src.get("ppNextLevelEx")
if next_level_src:
for item in next_level_src.values():
dest = load_experiment(level + 1, item, dest)
dest = load_experiment(item, level + 1, dest)

return dest

Expand Down
10 changes: 10 additions & 0 deletions src/nd2/_sdk_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ class SpectLoopPars(TypedDict):
pPlanes: PicturePlanesDict
uiCount: NotRequired[int]

class SLxExperimentDict(TypedDict):
SLxExperiment: RawExperimentDict

class SubLoopDict(TypedDict):
uiNextLevelCount: int
# this is a dict of keys 'i0000000000', 'i0000000001', etc.
ppNextLevelEx: dict[str, SLxExperimentDict]

class NETimeLoopPars(TypedDict):
# keys are '_00' or 'i0000000000' ...
pPeriod: dict[str, PeriodDict]
Expand All @@ -167,6 +175,8 @@ class NETimeLoopPars(TypedDict):
uiPeriodCount: int
wsCommandAfterPeriod: str
wsCommandBeforePeriod: str
# this is a dict of keys 'i0000000000', 'i0000000001', etc.
pSubLoops: NotRequired[dict[str, SubLoopDict]]

LoopParsDict: TypeAlias = Union[
TimeLoopPars, XYPosLoopPars, ZStackLoopPars, SpectLoopPars, NETimeLoopPars
Expand Down
2 changes: 1 addition & 1 deletion src/nd2/readers/_modern/modern_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def experiment(self) -> list[structures.ExpLoop]:
exp = self._decode_chunk(k, strip_prefix=False)
exp = exp.get("SLxExperiment", exp) # for v3 only
self._raw_experiment = cast("RawExperimentDict", exp)
self._experiment = load_experiment(0, self._raw_experiment)
self._experiment = load_experiment(self._raw_experiment)
return self._experiment

def loop_indices(self) -> list[dict[str, int]]:
Expand Down