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: improve roi parsing for some files #150

Merged
merged 1 commit into from
Jun 26, 2023
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
2 changes: 0 additions & 2 deletions src/nd2/_pysdk/_pysdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ def _decode_chunk(self, name: bytes, strip_prefix: bool = True) -> dict | Any:
data = self._load_chunk(name)
if data.startswith(b"<"):
decoded: Any = json_from_clx_variant(data, strip_prefix=strip_prefix)
elif self.version < (3, 0):
decoded = json_from_clx_variant(data, strip_prefix=strip_prefix)
else:
decoded = json_from_clx_lite_variant(data, strip_prefix=strip_prefix)
self._cached_decoded_chunks[name] = decoded
Expand Down
11 changes: 9 additions & 2 deletions src/nd2/nd2file.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,16 @@ def rois(self) -> dict[int, ROI]:

data = cast("LatestSDKReader", self._rdr)._decode_chunk(key)
data = data.get("RoiMetadata_v1", {}).copy()
data.pop("Global_Size", None)
dicts: list[dict] = []
if "Global_Size" in data:
dicts.extend(data[f"Global_{i}"] for i in range(data["Global_Size"]))
if "2PerMPoint_Size" in data:
for i in range(data.get("2PerMPoint_Size", 0)):
item: dict = data[f"2PerMPoint_{i}"]
dicts.extend(item[str(idx)] for idx in range(item.get("Size", 0)))

try:
_rois = [ROI._from_meta_dict(d) for d in data.values()]
_rois = [ROI._from_meta_dict(d) for d in dicts]
except Exception as e: # pragma: no cover
raise ValueError(f"Could not parse ROI metadata: {e}") from e
return {r.id: r for r in _rois}
Expand Down
16 changes: 11 additions & 5 deletions src/nd2/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,25 @@ class ROI:
def __post_init__(self) -> None:
if isinstance(self.info, dict):
self.info = RoiInfo(**self.info)
self.animParams = [AnimParam(**i) for i in self.animParams] # type: ignore
self.animParams = [
AnimParam(**i) if isinstance(i, dict) else i for i in self.animParams
]

@classmethod
def _from_meta_dict(cls, val: dict) -> ROI:
# val has keys:
# 'Id', 'Info', 'GUID', 'AnimParams_Size', 'AnimParams_{i}'
# where GUID and AnimParams keys are optional
anim_params = [
{_lower0(k): v for k, v in val[f"AnimParams_{i}"].items()}
for i in range(val.pop("AnimParams_Size", 0))
AnimParam(**{_lower0(k): v for k, v in val[f"AnimParams_{i}"].items()})
for i in range(val.get("AnimParams_Size", 0))
]
info = RoiInfo(**{_lower0(k): v for k, v in val["Info"].items()})
return cls(
id=val["Id"],
info={_lower0(k): v for k, v in val["Info"].items()}, # type: ignore
info=info,
guid=val.get("GUID", ""),
animParams=anim_params, # type: ignore
animParams=anim_params,
)


Expand Down
1 change: 1 addition & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def test_metadata_extraction(new_nd2: Path):
assert isinstance(nd.closed, bool)
assert isinstance(nd.ndim, int)
_bd = nd.binary_data
assert all(isinstance(x, structures.ROI) for x in nd.rois.values())
assert isinstance(nd.is_rgb, bool)
assert isinstance(nd.nbytes, int)

Expand Down