Browse Source

image validity test, iterator for all images

master
jim 10 years ago
parent
commit
4e23aa8f69
4 changed files with 40 additions and 56 deletions
  1. +13
    -4
      nd2reader/__init__.py
  2. +4
    -0
      nd2reader/model/__init__.py
  3. +19
    -41
      nd2reader/service/__init__.py
  4. +4
    -11
      run.py

+ 13
- 4
nd2reader/__init__.py View File

@ -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)
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

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

@ -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()

+ 19
- 41
nd2reader/service/__init__.py View File

@ -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:


+ 4
- 11
run.py View File

@ -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()

Loading…
Cancel
Save