Browse Source

got image set iteration working

master
jim 10 years ago
parent
commit
57940efae0
4 changed files with 44 additions and 10 deletions
  1. +22
    -4
      nd2reader/__init__.py
  2. +14
    -4
      nd2reader/model/__init__.py
  3. +4
    -0
      nd2reader/service/__init__.py
  4. +4
    -2
      run.py

+ 22
- 4
nd2reader/__init__.py View File

@ -1,6 +1,6 @@
import logging import logging
from nd2reader.service import BaseNd2 from nd2reader.service import BaseNd2
from nd2reader.model import Image
from nd2reader.model import Image, ImageSet
log = logging.getLogger("nd2reader") log = logging.getLogger("nd2reader")
log.addHandler(logging.StreamHandler()) log.addHandler(logging.StreamHandler())
@ -14,11 +14,12 @@ class Nd2(BaseNd2):
def get_image(self, timepoint, fov, channel_name, z_level): def get_image(self, timepoint, fov, channel_name, z_level):
image_set_number = self._calculate_image_set_number(timepoint, fov, 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]) 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)
return Image(timestamp, raw_image_data, fov, channel_name, z_level, self.height, self.width)
def __iter__(self): def __iter__(self):
""" """
Just return every image.
Just return every image in order (might not be exactly the order that the images were physically taken, but it will
be within a few seconds). A better explanation is probably needed here.
""" """
for i in range(self._image_count): for i in range(self._image_count):
@ -27,4 +28,21 @@ class Nd2(BaseNd2):
for channel in self.channels: for channel 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_valid:
yield image
yield image
def image_sets(self, field_of_view, timepoints=None, channels=None, z_levels=None):
"""
Gets all the images for a given field of view and
"""
timepoint_set = xrange(self._timepoint_count) if timepoints is None else timepoints
channel_set = [channel.name for channel in self.channels] if channels is None else channels
z_level_set = xrange(self._z_level_count) if z_levels is None else z_levels
for timepoint in timepoint_set:
image_set = ImageSet()
for channel_name in channel_set:
for z_level in z_level_set:
image = self.get_image(timepoint, field_of_view, channel_name, z_level)
if image.is_valid:
image_set.add(image)
yield image_set

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

@ -1,5 +1,8 @@
import numpy as np import numpy as np
import skimage.io import skimage.io
import logging
log = logging.getLogger("nd2reader")
class Channel(object): class Channel(object):
@ -43,23 +46,30 @@ class ImageSet(object):
class Image(object): class Image(object):
def __init__(self, timestamp, raw_array, height, width):
def __init__(self, timestamp, raw_array, field_of_view, channel, z_level, height, width):
self._timestamp = timestamp self._timestamp = timestamp
self._raw_data = raw_array self._raw_data = raw_array
self._data = np.reshape(self._raw_data, (height, width))
self._field_of_view = field_of_view
self._channel = channel
self._z_level = z_level
self._height = height
self._width = width
self._data = None
@property @property
def timestamp(self): def timestamp(self):
# TODO: Convert to datetime object # TODO: Convert to datetime object
return self._timestamp
return self._timestamp / 1000.0
@property @property
def data(self): def data(self):
if self._data is None:
self._data = np.reshape(self._raw_data, (self._height, self._width))
return self._data return self._data
@property @property
def is_valid(self): def is_valid(self):
return np.any(self._data)
return np.any(self.data)
def show(self): def show(self):
skimage.io.imshow(self.data) skimage.io.imshow(self.data)

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

@ -71,6 +71,10 @@ class BaseNd2(object):
""" """
return sum(self._metadata['ImageMetadata']['SLxExperiment']['ppNextLevelEx']['']['pItemValid']) return sum(self._metadata['ImageMetadata']['SLxExperiment']['ppNextLevelEx']['']['pItemValid'])
@property
def _channel_count(self):
return self._reader.channel_count
@property @property
def channel_offset(self): def channel_offset(self):
if self._channel_offset is None: if self._channel_offset is None:


+ 4
- 2
run.py View File

@ -4,9 +4,11 @@ import numpy as np
from skimage import io from skimage import io
for image in Nd2("/home/jim/Desktop/nd2hacking/YFP-dsRed-GFP-BF.nd2"):
for image_set in Nd2("/home/jim/Desktop/nd2hacking/YFP-dsRed-GFP-BF.nd2").image_sets(0):
for image in image_set:
print(image.timestamp, image._channel, image._z_level)
print("")
# n = Nd2("/home/jim/Desktop/nd2hacking/test-141111.nd2") # n = Nd2("/home/jim/Desktop/nd2hacking/test-141111.nd2")
image.show()

Loading…
Cancel
Save