From e55029c1de4cf61894c765129f3d616abccf3e06 Mon Sep 17 00:00:00 2001 From: jim Date: Fri, 9 Oct 2015 19:20:35 -0500 Subject: [PATCH] resolves #90 --- nd2reader/interface.py | 9 +++++++-- nd2reader/model/metadata.py | 7 ++++++- nd2reader/parser/v3.py | 5 +++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/nd2reader/interface.py b/nd2reader/interface.py index a54b5dd..29813f4 100644 --- a/nd2reader/interface.py +++ b/nd2reader/interface.py @@ -36,7 +36,7 @@ class Nd2(object): :rtype: int """ - return self._driver.total_images_per_channel * len(self.channels) + return self._metadata.total_images_per_channel * len(self.channels) def __getitem__(self, item): """ @@ -56,7 +56,12 @@ class Nd2(object): """ if isinstance(item, int): - return self._driver.get_image(item) + try: + image = self._driver.get_image(item) + except KeyError: + raise IndexError + else: + return image elif isinstance(item, slice): return self._slice(item.start, item.stop, item.step) raise IndexError diff --git a/nd2reader/model/metadata.py b/nd2reader/model/metadata.py index 838dcd7..5cb8ca7 100644 --- a/nd2reader/model/metadata.py +++ b/nd2reader/model/metadata.py @@ -1,6 +1,6 @@ class Metadata(object): """ A simple container for ND2 metadata. """ - def __init__(self, height, width, channels, date, fields_of_view, frames, z_levels): + def __init__(self, height, width, channels, date, fields_of_view, frames, z_levels, total_images_per_channel): self._height = height self._width = width self._channels = channels @@ -8,6 +8,7 @@ class Metadata(object): self._fields_of_view = fields_of_view self._frames = frames self._z_levels = z_levels + self._total_images_per_channel = total_images_per_channel @property def height(self): @@ -70,3 +71,7 @@ class Metadata(object): """ return self._z_levels + + @property + def total_images_per_channel(self): + return self._total_images_per_channel diff --git a/nd2reader/parser/v3.py b/nd2reader/parser/v3.py index fa1cf89..a7dfd75 100644 --- a/nd2reader/parser/v3.py +++ b/nd2reader/parser/v3.py @@ -52,7 +52,8 @@ class V3Parser(BaseParser): fields_of_view = self._parse_fields_of_view(metadata_dict) frames = self._parse_frames(metadata_dict) z_levels = self._parse_z_levels(metadata_dict) - self._metadata = Metadata(height, width, channels, date, fields_of_view, frames, z_levels) + total_images_per_channel = self._parse_total_images_per_channel(metadata_dict) + self._metadata = Metadata(height, width, channels, date, fields_of_view, frames, z_levels, total_images_per_channel) def _parse_date(self, metadata_dict): """ @@ -122,7 +123,7 @@ class V3Parser(BaseParser): :rtype: list """ - return self._parse_dimension(r""".*?T'\((\d+)\).*?""", metadata_dict) + return self._parse_dimension(r""".*?T'?\((\d+)\).*?""", metadata_dict) def _parse_z_levels(self, metadata_dict): """