|
|
@ -3,11 +3,12 @@ import struct |
|
|
|
|
|
|
|
import array |
|
|
|
import six |
|
|
|
import warnings |
|
|
|
from pims.base_frames import Frame |
|
|
|
import numpy as np |
|
|
|
|
|
|
|
from nd2reader.common import get_version, read_chunk |
|
|
|
from nd2reader.exceptions import InvalidVersionError, NoImageError |
|
|
|
from nd2reader.exceptions import InvalidVersionError |
|
|
|
from nd2reader.label_map import LabelMap |
|
|
|
from nd2reader.raw_metadata import RawMetadata |
|
|
|
|
|
|
@ -72,7 +73,7 @@ class Parser(object): |
|
|
|
try: |
|
|
|
timestamp, image = self._get_raw_image_data(image_group_number, channel_offset, self.metadata["height"], |
|
|
|
self.metadata["width"]) |
|
|
|
except (TypeError, NoImageError): |
|
|
|
except (TypeError): |
|
|
|
return Frame([], frame_no=frame_number, metadata=self._get_frame_metadata()) |
|
|
|
else: |
|
|
|
return image |
|
|
@ -101,7 +102,7 @@ class Parser(object): |
|
|
|
try: |
|
|
|
timestamp, raw_image_data = self._get_raw_image_data(image_group_number, channel, |
|
|
|
height, width) |
|
|
|
except (TypeError, NoImageError): |
|
|
|
except (TypeError): |
|
|
|
return Frame([], frame_no=frame_number, metadata=self._get_frame_metadata()) |
|
|
|
else: |
|
|
|
return raw_image_data |
|
|
@ -140,6 +141,7 @@ class Parser(object): |
|
|
|
self._label_map = self._build_label_map() |
|
|
|
self._raw_metadata = RawMetadata(self._fh, self._label_map) |
|
|
|
self.metadata = self._raw_metadata.__dict__ |
|
|
|
self.acquisition_times = self._raw_metadata.acquisition_times |
|
|
|
|
|
|
|
def _build_label_map(self): |
|
|
|
""" |
|
|
@ -283,7 +285,12 @@ class Parser(object): |
|
|
|
if np.any(image_data): |
|
|
|
return timestamp, Frame(image_data, metadata=self._get_frame_metadata()) |
|
|
|
|
|
|
|
raise NoImageError |
|
|
|
# If a blank "gap" image is encountered, generate an array of corresponding height and width to avoid |
|
|
|
# errors with ND2-files with missing frames. Array is filled with nan to reflect that data is missing. |
|
|
|
else: |
|
|
|
empty_frame = np.full((height, width), np.nan) |
|
|
|
warnings.warn('ND2 file contains gap frames which are represented by np.nan-filled arrays; to convert to zeros use e.g. np.nan_to_num(array)') |
|
|
|
return timestamp, Frame(empty_frame, metadata=self._get_frame_metadata()) |
|
|
|
|
|
|
|
def _get_frame_metadata(self): |
|
|
|
"""Get the metadata for one frame |
|
|
|