Browse Source

refactored the way images are validated

zolfa-add_slices_loading
Jim Rybarski 10 years ago
parent
commit
98391dc3c0
2 changed files with 12 additions and 19 deletions
  1. +12
    -5
      nd2reader/__init__.py
  2. +0
    -14
      nd2reader/model/__init__.py

+ 12
- 5
nd2reader/__init__.py View File

@ -34,7 +34,7 @@ class Nd2(Nd2Parser):
for z_level in range(self.z_level_count): for z_level in range(self.z_level_count):
for channel_name in self.channels: for channel_name in self.channels:
image = self.get_image(i, fov, channel_name, z_level) image = self.get_image(i, fov, channel_name, z_level)
if image.is_valid:
if image is not None:
yield image yield image
@property @property
@ -45,14 +45,19 @@ class Nd2(Nd2Parser):
for channel_name in self.channels: for channel_name in self.channels:
for z_level in xrange(self.z_level_count): for z_level in xrange(self.z_level_count):
image = self.get_image(time_index, fov, channel_name, z_level) image = self.get_image(time_index, fov, channel_name, z_level)
if image.is_valid:
if image is not None:
image_set.add(image) image_set.add(image)
yield image_set yield image_set
def get_image(self, time_index, fov, channel_name, z_level): def get_image(self, time_index, fov, channel_name, z_level):
image_set_number = self._calculate_image_set_number(time_index, fov, 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 @property
def channels(self): def channels(self):
@ -178,7 +183,9 @@ class Nd2(Nd2Parser):
# The images for the various channels are interleaved within each other. # The images for the various channels are interleaved within each other.
image_data = array.array("H", data) image_data = array.array("H", data)
image_data_start = 4 + channel_offset 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): 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) return time_index * self.field_of_view_count * self.z_level_count + (fov * self.z_level_count + z_level)

+ 0
- 14
nd2reader/model/__init__.py View File

@ -46,20 +46,6 @@ class Image(object):
self._data = np.reshape(self._raw_data, (self._height, self._width)) self._data = np.reshape(self._raw_data, (self._height, self._width))
return self._data 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): class ImageSet(object):
""" """


Loading…
Cancel
Save