From 4e23aa8f69a9e60323f9ff17042019f3fb0e9c44 Mon Sep 17 00:00:00 2001 From: jim Date: Thu, 13 Nov 2014 12:48:17 -0600 Subject: [PATCH] image validity test, iterator for all images --- nd2reader/__init__.py | 17 +++++++--- nd2reader/model/__init__.py | 4 +++ nd2reader/service/__init__.py | 60 +++++++++++------------------------ run.py | 15 +++------ 4 files changed, 40 insertions(+), 56 deletions(-) diff --git a/nd2reader/__init__.py b/nd2reader/__init__.py index b415879..7feb90e 100644 --- a/nd2reader/__init__.py +++ b/nd2reader/__init__.py @@ -12,10 +12,19 @@ class Nd2(BaseNd2): super(Nd2, self).__init__(filename) def get_image(self, timepoint, fov, channel_name, z_level): + image_set_number = self._calculate_image_set_number(timepoint, fov, z_level) + timestamp, raw_image_data = self._reader.get_raw_image_data(image_set_number, self.channel_offset[channel_name]) + return Image(timestamp, raw_image_data, self.height, self.width) + + def __iter__(self): """ - Everything here is zero-indexed. + Just return every image. """ - image_set_number = self._calculate_image_set_number(timepoint, fov, z_level) - timestamp, raw_image_data = self._reader.get_raw_image_data(image_set_number, self.channel_offset[channel_name]) - return Image(timestamp, raw_image_data, self.height, self.width) \ No newline at end of file + for i in range(self._image_count): + for fov in range(self._field_of_view_count): + for z_level in range(self._z_level_count): + for channel in self.channels: + image = self.get_image(i, fov, channel.name, z_level) + if image.is_valid: + yield image \ No newline at end of file diff --git a/nd2reader/model/__init__.py b/nd2reader/model/__init__.py index fff975d..caf6f6b 100644 --- a/nd2reader/model/__init__.py +++ b/nd2reader/model/__init__.py @@ -57,6 +57,10 @@ class Image(object): def data(self): return self._data + @property + def is_valid(self): + return np.any(self._data) + def show(self): skimage.io.imshow(self.data) skimage.io.show() \ No newline at end of file diff --git a/nd2reader/service/__init__.py b/nd2reader/service/__init__.py index c4e401f..ade86f9 100644 --- a/nd2reader/service/__init__.py +++ b/nd2reader/service/__init__.py @@ -25,6 +25,25 @@ class BaseNd2(object): def width(self): return self._metadata['ImageAttributes']['SLxImageAttributes']['uiWidth'] + @property + def channels(self): + metadata = self._metadata['ImageMetadataSeq']['SLxPictureMetadata']['sPicturePlanes'] + try: + validity = self._metadata['ImageMetadata']['SLxExperiment']['ppNextLevelEx']['']['ppNextLevelEx']['']['pItemValid'] + except KeyError: + # If none of the channels have been deleted, there is no validity list, so we just make one + validity = [True for i in metadata] + # Channel information is contained in dictionaries with the keys a0, a1...an where the number + # indicates the order in which the channel is stored. So by sorting the dicts alphabetically + # we get the correct order. + for (label, chan), valid in zip(sorted(metadata['sPlaneNew'].items()), validity): + if not valid: + continue + name = chan['sDescription'] + exposure_time = metadata['sSampleSetting'][label]['dExposureTime'] + camera = metadata['sSampleSetting'][label]['pCameraSetting']['CameraUserName'] + yield Channel(name, camera, exposure_time) + @property def _image_count(self): return self._metadata['ImageAttributes']['SLxImageAttributes']['uiSequenceCount'] @@ -37,28 +56,6 @@ class BaseNd2(object): def _timepoint_count(self): return self._image_count / self._field_of_view_count / self._z_level_count - # @property - # def _fields_of_view(self): - # """ - # Fields of view are the various places in the xy-plane where images were taken. - # - # """ - # # Grab all the metadata about fields of view - # fov_metadata = self._metadata['ImageMetadata']['SLxExperiment']['ppNextLevelEx'][''] - # # The attributes include x, y, and z coordinates, and perfect focus (PFS) offset - # fov_attributes = fov_metadata['uLoopPars']['Points'][''] - # # If you crop fields of view from your ND2 file, the metadata is retained and only this list is - # # updated to indicate that the fields of view have been deleted. - # fov_validity = fov_metadata['pItemValid'] - # # We only yield valid (i.e. uncropped) fields of view - # for number, (fov, valid) in enumerate(zip(fov_attributes, fov_validity)): - # if valid: - # yield field_of_view(number=number + 1, - # x=fov['dPosX'], - # y=fov['dPosY'], - # z=fov['dPosZ'], - # pfs_offset=fov['dPFSOffset']) - @property def _z_level_count(self): return self._image_count / self._sequence_count @@ -74,25 +71,6 @@ class BaseNd2(object): """ return sum(self._metadata['ImageMetadata']['SLxExperiment']['ppNextLevelEx']['']['pItemValid']) - @property - def channels(self): - metadata = self._metadata['ImageMetadataSeq']['SLxPictureMetadata']['sPicturePlanes'] - try: - validity = self._metadata['ImageMetadata']['SLxExperiment']['ppNextLevelEx']['']['ppNextLevelEx']['']['pItemValid'] - except KeyError: - # If none of the channels have been deleted, there is no validity list, so we just make one - validity = [True for i in metadata] - # Channel information is contained in dictionaries with the keys a0, a1...an where the number - # indicates the order in which the channel is stored. So by sorting the dicts alphabetically - # we get the correct order. - for (label, chan), valid in zip(sorted(metadata['sPlaneNew'].items()), validity): - if not valid: - continue - name = chan['sDescription'] - exposure_time = metadata['sSampleSetting'][label]['dExposureTime'] - camera = metadata['sSampleSetting'][label]['pCameraSetting']['CameraUserName'] - yield Channel(name, camera, exposure_time) - @property def channel_offset(self): if self._channel_offset is None: diff --git a/run.py b/run.py index 6c2ca53..bd45aea 100644 --- a/run.py +++ b/run.py @@ -4,16 +4,9 @@ import numpy as np from skimage import io -n = Nd2("/home/jim/Desktop/nd2hacking/YFP-dsRed-GFP-BF.nd2") +for image in Nd2("/home/jim/Desktop/nd2hacking/YFP-dsRed-GFP-BF.nd2"): + # n = Nd2("/home/jim/Desktop/nd2hacking/test-141111.nd2") -# for chan in n.channels: -# print(chan.name) -print(n._reader.channel_count) -print(n._z_level_count) -print(n._field_of_view_count) -print(n._timepoint_count) -# z = n._reader.read_coordinates() -# print(z) -# pprint(n._metadata) -image = n._reader.get_raw_image_data(71, 0) + + image.show() \ No newline at end of file