Open-Science-Tools / nd2reader

Pure Python library for reading NIS Elements ND2 images and metadata
http://www.lighthacking.nl/nd2reader
GNU General Public License v3.0
45 stars 28 forks source link

Single time point exported from time series appears to have as many frames as original #53

Open jni opened 3 years ago

jni commented 3 years ago

I have an nd2 from a collaborator that contains a single frame out of a longer time series. When I open it with nd2reader, it appears to have 193 time points, as the original time series did. However when I try to access any frame other than [0] I (understandably) get a KeyError. Is there any way for me to introspect this file to know that there is only one time frame within it?

example file

Script to reproduce the issue with the provided file:

from nd2reader import ND2Reader

f = ND2Reader('train_TR67_Inj7_fr50.nd2')
f.bundle_axes = 'zyx'
print(f'{len(f)=}')  # 193
print(f'{f.iter_axes=}')  # 't'
print(f'{f.sizes=}')  # 193 time points
print(f'{f[0].shape=}')  # 33 x 512 x 512
print(f'{f.frame_shape=}')  # 33 x 512 x 512
print(f'{f[1].shape=}')  # KeyError

Output on current master:

len(f)=193
f.iter_axes=['t']
f.sizes={'x': 512, 'y': 512, 'c': 4, 't': 193, 'z': 33}
f[0].shape=(33, 512, 512)
f.frame_shape=(33, 512, 512)
Traceback (most recent call last):
  File "/Users/jni/projects/useful-histories/nd2-single-frame.py", line 10, in <module>
    print(f'{f[1].shape=}')  # KeyError
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/slicerator/__init__.py", line 188, in __getitem__
    return self._get(indices)
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/pims/base_frames.py", line 98, in __getitem__
    return self.get_frame(key)
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/pims/base_frames.py", line 592, in get_frame
    result = self._get_frame_wrapped(**coords)
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/pims/base_frames.py", line 265, in get_frame_bundled
    frame = get_frame(**ind)
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/pims/base_frames.py", line 303, in get_frame_dropped
    result = get_frame(**ind)
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/nd2reader/reader.py", line 88, in get_frame_2D
    return self._parser.get_image_by_attributes(t, v, c, z, y, x)
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/nd2reader/parser.py", line 103, in get_image_by_attributes
    timestamp, raw_image_data = self._get_raw_image_data(image_group_number, channel,
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/nd2reader/parser.py", line 261, in _get_raw_image_data
    chunk = self._label_map.get_image_data_location(image_group_number)
  File "/Users/jni/conda/envs/napari-proofreading/lib/python3.9/site-packages/nd2reader/label_map.py", line 80, in get_image_data_location
    return self._image_data[index]
KeyError: 33

Thank you!

rbnvrw commented 2 years ago

Thanks for the report. Yes, this is currently a known limitation, the metadata is used exclusively to access frames. It would be good to solve this, but it requires an extensive rewrite that I currently don't have time for unfortunately. I'll leave this as a reminder so I can hopefully look at it again at a later time. Of course, pull request are also more than welcome.