Browse Source

resolves #110: added comments and parameter types to every method with a few minor exceptions where it's super obvious and unimportant what's happening

feature/load_slices
jim 9 years ago
parent
commit
09173fcb9e
7 changed files with 126 additions and 46 deletions
  1. +22
    -12
      nd2reader/driver/v3.py
  2. +22
    -20
      nd2reader/interface.py
  3. +30
    -9
      nd2reader/model/image.py
  4. +6
    -0
      nd2reader/model/metadata.py
  5. +8
    -0
      nd2reader/parser/base.py
  6. +10
    -0
      nd2reader/parser/parser.py
  7. +28
    -5
      nd2reader/parser/v3.py

+ 22
- 12
nd2reader/driver/v3.py View File

@ -83,27 +83,27 @@ class V3Driver(object):
def get_image(self, index):
"""
Creates an Image object and adds its metadata, based on the index, which is simply the order in which the image was acquired. May return None if the ND2 contains
Creates an Image object and adds its metadata, based on the index (which is simply the order in which the image was acquired). May return None if the ND2 contains
multiple channels and not all were taken in each cycle (for example, if you take bright field images every minute, and GFP images every five minutes, there will be some
indexes that do not contain an image. The reason for this is complicated, but suffice it to say that we hope to eliminate this possibility in future releases. For now,
you'll need to check if your image is None if you're doing anything as described above.
you'll need to check if your image is None if you're doing anything out of the ordinary.
:type index: int
:rtype: Image or None
"""
channel_offset = index % len(self._metadata.channels)
fov = self._calculate_field_of_view(index)
field_of_view = self._calculate_field_of_view(index)
channel = self._calculate_channel(index)
z_level = self._calculate_z_level(index)
image_group_number = int(index / len(self._metadata.channels))
frame_number = self._calculate_frame_number(image_group_number, fov, z_level)
frame_number = self._calculate_frame_number(image_group_number, field_of_view, z_level)
try:
timestamp, image = self._get_raw_image_data(image_group_number, channel_offset, self._metadata.height, self._metadata.width)
except NoImageError:
return None
else:
image.add_params(timestamp, frame_number, fov, channel, z_level)
image.add_params(timestamp, frame_number, field_of_view, channel, z_level)
return image
@property
@ -115,21 +115,19 @@ class V3Driver(object):
:rtype: dict
"""
channel_offset = {}
for n, channel in enumerate(self._metadata.channels):
channel_offset[channel] = n
return channel_offset
return {channel: n for n, channel in enumerate(self._metadata.channels)}
def _get_raw_image_data(self, image_group_number, channel_offset, height, width):
"""
Reads the raw bytes and the timestamp of an image.
:param image_group_number: groups are made of images with the same time index, field of view and z-level.
:param image_group_number: groups are made of images with the same time index, field of view and z-level
:type image_group_number: int
:param channel_offset: the offset in the array where the bytes for this image are found.
:param channel_offset: the offset in the array where the bytes for this image are found
:type channel_offset: int
:return: (int, array.array()) or None
:rtype: (int, Image)
:raises: NoImageError
"""
chunk = self._label_map[six.b("ImageDataSeq|%d!" % image_group_number)]
@ -153,6 +151,18 @@ class V3Driver(object):
raise NoImageError
def get_image_by_attributes(self, frame_number, field_of_view, channel_name, z_level, height, width):
"""
Attempts to get Image based on attributes alone.
:type frame_number: int
:type field_of_view: int
:type channel_name: str
:type z_level: int
:type height: int
:type width: int
:rtype: Image or None
"""
image_group_number = self._calculate_image_group_number(frame_number, field_of_view, z_level)
try:
timestamp, raw_image_data = self._get_raw_image_data(image_group_number,


+ 22
- 20
nd2reader/interface.py View File

@ -5,10 +5,8 @@ from nd2reader.version import get_version
class Nd2(object):
"""
Allows easy access to NIS Elements .nd2 image files.
""" Allows easy access to NIS Elements .nd2 image files. """
"""
def __init__(self, filename):
self._filename = filename
self._fh = open(filename, "rb")
@ -39,7 +37,7 @@ class Nd2(object):
This should be the total number of images in the ND2, but it may be inaccurate. If the ND2 contains a
different number of images in a cycle (i.e. there are "gap" images) it will be higher than reality.
:rtype: int
:rtype: int
"""
return self._metadata.total_images_per_channel * len(self.channels)
@ -48,8 +46,8 @@ class Nd2(object):
"""
Allows slicing ND2s.
:type item: int or slice
:rtype: nd2reader.model.Image() or generator
:type item: int or slice
:rtype: nd2reader.model.Image() or generator
"""
if isinstance(item, int):
@ -67,10 +65,10 @@ class Nd2(object):
"""
Allows for iteration over a selection of the entire dataset.
:type start: int
:type stop: int
:type step: int
:rtype: nd2reader.model.Image()
:type start: int
:type stop: int
:type step: int
:rtype: nd2reader.model.Image()
"""
start = start if start is not None else 0
@ -85,7 +83,7 @@ class Nd2(object):
"""
The date and time that the acquisition began. Not guaranteed to have been recorded.
:rtype: datetime.datetime() or None
:rtype: datetime.datetime() or None
"""
return self._metadata.date
@ -138,7 +136,7 @@ class Nd2(object):
"""
The height of each image in pixels.
:rtype: int
:rtype: int
"""
return self._metadata.height
@ -148,7 +146,7 @@ class Nd2(object):
"""
The width of each image in pixels.
:rtype: int
:rtype: int
"""
return self._metadata.width
@ -157,13 +155,13 @@ class Nd2(object):
"""
Attempts to return the image with the unique combination of given attributes. None will be returned if a match is not found.
:type frame_number: int
:param field_of_view: the label for the place in the XY-plane where this image was taken.
:type field_of_view: int
:param channel_name: the name of the color of this image
:type channel_name: str
:param z_level: the label for the location in the Z-plane where this image was taken.
:type z_level: int
:type frame_number: int
:param field_of_view: the label for the place in the XY-plane where this image was taken.
:type field_of_view: int
:param channel_name: the name of the color of this image
:type channel_name: str
:param z_level: the label for the location in the Z-plane where this image was taken.
:type z_level: int
:rtype: nd2reader.model.Image() or None
@ -171,4 +169,8 @@ class Nd2(object):
return self._driver.get_image_by_attributes(frame_number, field_of_view, channel_name, z_level, self.height, self.width)
def close(self):
"""
Closes the file handle to the image. This actually sometimes will prevent problems so it's good to do this or use Nd2 as a context manager.
"""
self._fh.close()

+ 30
- 9
nd2reader/model/image.py View File

@ -4,6 +4,10 @@ import numpy as np
class Image(np.ndarray):
"""
Holds the raw pixel data of an image and provides access to some metadata.
"""
def __new__(cls, array):
return np.asarray(array).view(cls)
@ -16,8 +20,6 @@ class Image(np.ndarray):
def add_params(self, timestamp, frame_number, field_of_view, channel, z_level):
"""
A wrapper around the raw pixel data of an image.
:param timestamp: The number of milliseconds after the beginning of the acquisition that this image was taken.
:type timestamp: float
:param frame_number: The order in which this image was taken, with images of different channels/z-levels
@ -39,18 +41,30 @@ class Image(np.ndarray):
@property
def height(self):
"""
The height in pixels.
:rtype: int
"""
return self.shape[0]
@property
def width(self):
"""
The width in pixels.
:rtype: int
"""
return self.shape[1]
@property
def field_of_view(self):
"""
Which of the fixed locations this image was taken at.
The index of the stage location where this image was acquired.
:rtype int:
:rtype: int
"""
return self._field_of_view
@ -60,16 +74,23 @@ class Image(np.ndarray):
"""
The number of seconds after the beginning of the acquisition that the image was taken. Note that for a given
field of view and z-level offset, if you have images of multiple channels, they will all be given the same
timestamp. No, this doesn't make much sense. But that's how ND2s are structured, so if your experiment depends
on millisecond accuracy, you need to find an alternative imaging system.
timestamp. That's just how ND2s are structured, so if your experiment depends on millisecond accuracy,
you need to find an alternative imaging system.
:rtype float:
:rtype: float
"""
# data is actually stored in milliseconds
return self._timestamp / 1000.0
@property
def frame_number(self):
"""
The index of the group of images taken sequentially that all have the same group number and field of view.
:rtype: int
"""
return self._frame_number
@property
@ -77,7 +98,7 @@ class Image(np.ndarray):
"""
The name of the filter used to acquire this image. These are user-supplied in NIS Elements.
:rtype str:
:rtype: str
"""
return self._channel
@ -94,7 +115,7 @@ class Image(np.ndarray):
0 µm: 1
+3 µm: 2
:rtype int:
:rtype: int
"""
return self._z_level

+ 6
- 0
nd2reader/model/metadata.py View File

@ -86,4 +86,10 @@ class Metadata(object):
@property
def total_images_per_channel(self):
"""
The total number of images of a particular channel (wavelength, filter, etc) in the entire ND2.
:rtype: int
"""
return self._total_images_per_channel

+ 8
- 0
nd2reader/parser/base.py View File

@ -4,8 +4,16 @@ from abc import abstractproperty
class BaseParser(object):
@abstractproperty
def metadata(self):
"""
Instantiates a Metadata object.
"""
raise NotImplementedError
@abstractproperty
def driver(self):
"""
Instantiates a driver object.
"""
raise NotImplementedError

+ 10
- 0
nd2reader/parser/parser.py View File

@ -3,6 +3,16 @@ from nd2reader.exc import InvalidVersionError
def get_parser(fh, major_version, minor_version):
"""
Picks the appropriate parser based on the ND2 version.
:type fh: file
:type major_version: int
:type minor_version: int
:rtype: a parser object
"""
parsers = {(3, None): V3Parser}
parser = parsers.get((major_version, minor_version)) or parsers.get((major_version, None))
if not parser:


+ 28
- 5
nd2reader/parser/v3.py View File

@ -12,18 +12,26 @@ import struct
class V3Parser(BaseParser):
""" Parses ND2 files and creates a Metadata and ImageReader object. """
""" Parses ND2 files and creates a Metadata and driver object. """
CHUNK_HEADER = 0xabeceda
CHUNK_MAP_START = six.b("ND2 FILEMAP SIGNATURE NAME 0001!")
CHUNK_MAP_END = six.b("ND2 CHUNK MAP SIGNATURE 0000001!")
def __init__(self, fh):
"""
:type fh: file
"""
self._fh = fh
self._metadata = None
self._label_map = None
@property
def metadata(self):
"""
:rtype: Metadata
"""
if not self._metadata:
self._parse_metadata()
return self._metadata
@ -34,7 +42,7 @@ class V3Parser(BaseParser):
def _parse_metadata(self):
"""
Reads all metadata.
Reads all metadata and instantiates the Metadata object.
"""
metadata_dict = {}
@ -59,6 +67,7 @@ class V3Parser(BaseParser):
"""
The date and time when acquisition began.
:type metadata_dict: dict
:rtype: datetime.datetime() or None
"""
@ -85,6 +94,7 @@ class V3Parser(BaseParser):
These are labels created by the NIS Elements user. Typically they may a short description of the filter cube
used (e.g. "bright field", "GFP", etc.)
:type metadata_dict: dict
:rtype: list
"""
@ -111,7 +121,8 @@ class V3Parser(BaseParser):
in the image data, so we have to calculate it. There probably is something somewhere, since
NIS Elements can figure it out, but we haven't found it yet.
:rtype: list
:type metadata_dict: dict
:rtype: list
"""
return self._parse_dimension(r""".*?XY\((\d+)\).*?""", metadata_dict)
@ -120,6 +131,7 @@ class V3Parser(BaseParser):
"""
The number of cycles.
:type metadata_dict: dict
:rtype: list
"""
@ -129,7 +141,8 @@ class V3Parser(BaseParser):
"""
The different levels in the Z-plane. Just a sequence from 0 to n.
:rtype: list
:type metadata_dict: dict
:rtype: list
"""
return self._parse_dimension(r""".*?Z\((\d+)\).*?""", metadata_dict)
@ -140,7 +153,8 @@ class V3Parser(BaseParser):
Sometimes certain elements don't exist, or change their data type randomly. However, the human-readable text
is always there and in the same exact format, so we just parse that instead.
:rtype: str
:type metadata_dict: dict
:rtype: str
"""
for line in metadata_dict[six.b('ImageTextInfo')][six.b('SLxImageTextInfo')].values():
@ -158,6 +172,14 @@ class V3Parser(BaseParser):
return dimension_text
def _parse_dimension(self, pattern, metadata_dict):
"""
:param pattern: a valid regex pattern
:type pattern: str
:type metadata_dict: dict
:rtype: list of int
"""
dimension_text = self._parse_dimension_text(metadata_dict)
if six.PY3:
dimension_text = dimension_text.decode("utf8")
@ -171,6 +193,7 @@ class V3Parser(BaseParser):
"""
The total number of images per channel. Warning: this may be inaccurate as it includes "gap" images.
:type metadata_dict: dict
:rtype: int
"""


Loading…
Cancel
Save