diff --git a/nd2reader/__init__.py b/nd2reader/__init__.py index 0d202fe..06e8709 100644 --- a/nd2reader/__init__.py +++ b/nd2reader/__init__.py @@ -34,7 +34,7 @@ class Nd2(Nd2Parser): for z_level in range(self.z_level_count): for channel_name in self.channels: image = self.get_image(i, fov, channel_name, z_level) - if image.is_valid: + if image is not None: yield image @property @@ -45,14 +45,19 @@ class Nd2(Nd2Parser): for channel_name in self.channels: for z_level in xrange(self.z_level_count): image = self.get_image(time_index, fov, channel_name, z_level) - if image.is_valid: + if image is not None: image_set.add(image) yield image_set def get_image(self, time_index, fov, channel_name, z_level): image_set_number = self._calculate_image_set_number(time_index, fov, z_level) - timestamp, raw_image_data = self._get_raw_image_data(image_set_number, self._channel_offset[channel_name]) - return Image(timestamp, raw_image_data, fov, channel_name, z_level, self.height, self.width) + try: + timestamp, raw_image_data = self._get_raw_image_data(image_set_number, self._channel_offset[channel_name]) + image = Image(timestamp, raw_image_data, fov, channel_name, z_level, self.height, self.width) + except TypeError: + return None + else: + return image @property def channels(self): @@ -178,7 +183,9 @@ class Nd2(Nd2Parser): # The images for the various channels are interleaved within each other. image_data = array.array("H", data) image_data_start = 4 + channel_offset - return timestamp, image_data[image_data_start::self.channel_count] + if any(image_data): + return timestamp, image_data[image_data_start::self.channel_count] + return None def _calculate_image_set_number(self, time_index, fov, z_level): return time_index * self.field_of_view_count * self.z_level_count + (fov * self.z_level_count + z_level) \ No newline at end of file diff --git a/nd2reader/model/__init__.py b/nd2reader/model/__init__.py index d660b3c..796b96e 100644 --- a/nd2reader/model/__init__.py +++ b/nd2reader/model/__init__.py @@ -46,20 +46,6 @@ class Image(object): self._data = np.reshape(self._raw_data, (self._height, self._width)) return self._data - @property - def is_valid(self): - """ - Not every image stored in an ND2 is a real image! If you take 4 images at one field of view and 2 at another - in a repeating cycle, there will be 4 images at BOTH field of view. The 2 non-images are the same size as all - the other images, only pure black (i.e. every pixel has a value of zero). - - This is probably an artifact of some algorithm in NIS Elements determining the maximum number of possible - images and pre-allocating the space with zeros. Regardless of why they exit, we can't tell that they're - not actual images until we examine the data. If every pixel value is exactly 0, it's a gap image. - - """ - return np.any(self._raw_data) - class ImageSet(object): """