From 45c148a1d31ea6a9c4b09fd445bdad2af89afa70 Mon Sep 17 00:00:00 2001 From: jim Date: Sun, 27 Sep 2015 19:50:16 -0500 Subject: [PATCH] #66 even more refactoring --- nd2reader/exc.py | 2 ++ nd2reader/imreader/v3.py | 6 ++---- nd2reader/model/image.py | 14 +++++++++----- nd2reader/parser/parser.py | 10 +++++++++- nd2reader/version.py | 5 +---- 5 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 nd2reader/exc.py diff --git a/nd2reader/exc.py b/nd2reader/exc.py new file mode 100644 index 0000000..eda1f2d --- /dev/null +++ b/nd2reader/exc.py @@ -0,0 +1,2 @@ +class InvalidVersionError(Exception): + pass diff --git a/nd2reader/imreader/v3.py b/nd2reader/imreader/v3.py index 28f448a..9285e1b 100644 --- a/nd2reader/imreader/v3.py +++ b/nd2reader/imreader/v3.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- import array -from datetime import datetime import numpy as np -import re import struct import six from nd2reader.model.image import Image @@ -59,7 +57,7 @@ class V3ImageReader(object): """ channel_offset = {} - for n, channel in enumerate(self._channels): + for n, channel in enumerate(self._metadata.channels): channel_offset[channel] = n return channel_offset @@ -86,7 +84,7 @@ class V3ImageReader(object): # The images for the various channels are interleaved within the same array. For example, the second image # of a four image group will be composed of bytes 2, 6, 10, etc. If you understand why someone would design # a data structure that way, please send the author of this library a message. - image_data = np.reshape(image_group_data[image_data_start::len(self.channels)], (height, width)) + image_data = np.reshape(image_group_data[image_data_start::len(self._metadata.channels)], (height, width)) # Skip images that are all zeros! This is important, since NIS Elements creates blank "gap" images if you # don't have the same number of images each cycle. We discovered this because we only took GFP images every # other cycle to reduce phototoxicity, but NIS Elements still allocated memory as if we were going to take diff --git a/nd2reader/model/image.py b/nd2reader/model/image.py index 31ad0c6..5637870 100644 --- a/nd2reader/model/image.py +++ b/nd2reader/model/image.py @@ -21,10 +21,6 @@ class Image(np.ndarray): :type channel: str :param z_level: The label for the location in the Z-plane where this image was taken. :type z_level: int - :param height: The height of the image in pixels. - :type height: int - :param width: The width of the image in pixels. - :type width: int """ self._timestamp = timestamp @@ -37,12 +33,20 @@ class Image(np.ndarray): return "\n".join(["", "%sx%s (HxW)" % (self.height, self.width), "Timestamp: %s" % self.timestamp, - "Frame: %s" % self._frame_number, + "Frame: %s" % self.frame_number, "Field of View: %s" % self.field_of_view, "Channel: %s" % self.channel, "Z-Level: %s" % self.z_level, ]) + @property + def height(self): + return self.shape[1] + + @property + def width(self): + return self.shape[0] + @property def field_of_view(self): """ diff --git a/nd2reader/parser/parser.py b/nd2reader/parser/parser.py index cc09374..d3e7488 100644 --- a/nd2reader/parser/parser.py +++ b/nd2reader/parser/parser.py @@ -1,2 +1,10 @@ +from nd2reader.parser.v3 import V3Parser +from nd2reader.exc import InvalidVersionError + + def get_parser(filename, major_version, minor_version): - parsers = {} + parsers = {(3, None): V3Parser} + parser = parsers.get((major_version, minor_version)) or parsers.get((major_version, None)) + if not parser: + raise InvalidVersionError("No parser is available for that version.") + return parser(filename) diff --git a/nd2reader/version.py b/nd2reader/version.py index d4a02fc..9434d2f 100644 --- a/nd2reader/version.py +++ b/nd2reader/version.py @@ -1,8 +1,5 @@ import re - - -class InvalidVersionError(Exception): - pass +from nd2reader.exc import InvalidVersionError def get_version(filename):