diff --git a/nd2reader/exceptions.py b/nd2reader/exceptions.py index 55c598b..cb1b1fb 100644 --- a/nd2reader/exceptions.py +++ b/nd2reader/exceptions.py @@ -6,17 +6,6 @@ class InvalidVersionError(Exception): """ pass - -class NoImageError(Exception): - """No image found. - - Some apparent images in ND2s are just completely blank placeholders. These are used when the number of images per - cycle are unequal (e.g. if you take fluorescent images every 2 minutes, and bright field images every minute). - - """ - pass - - class EmptyFileError(Exception): """This .nd2 file seems to be empty. diff --git a/nd2reader/parser.py b/nd2reader/parser.py index 5472535..baea4e8 100644 --- a/nd2reader/parser.py +++ b/nd2reader/parser.py @@ -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 @@ -284,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