From e9ff0915bffc6a0dab7726b72c3130cd5a6f7f0a Mon Sep 17 00:00:00 2001 From: jim Date: Sat, 7 Nov 2015 15:16:23 -0600 Subject: [PATCH] #123 switched to the new LabelMap object, added support for another metadata field --- nd2reader/model/label.py | 11 ++++----- nd2reader/parser/v3.py | 49 +++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/nd2reader/model/label.py b/nd2reader/model/label.py index adbac16..daa6a16 100644 --- a/nd2reader/model/label.py +++ b/nd2reader/model/label.py @@ -1,12 +1,8 @@ import six import struct -from collections import namedtuple import re -data_location = namedtuple("DataLocation", ["location", "length"]) - - class LabelMap(object): """ """ @@ -22,12 +18,16 @@ class LabelMap(object): def _parse_data_location(self, label_location): location, length = struct.unpack("QQ", self._data[label_location: label_location + 16]) - return data_location(location=location, length=length) + return location @property def image_text_info(self): return self._get_location(six.b("ImageTextInfoLV!")) + @property + def image_metadata(self): + return self._get_location(six.b("ImageMetadataLV!")) + @property def image_metadata_sequence(self): # there is always only one of these, even though it has a pipe followed by a zero, which is how they do indexes @@ -39,7 +39,6 @@ class LabelMap(object): regex = re.compile(six.b("""ImageDataSeq\|(\d+)!""")) for match in regex.finditer(self._data): if match: - print(match.start(), match.end()) location = self._parse_data_location(match.end()) image_data[int(match.group(1))] = location return image_data diff --git a/nd2reader/parser/v3.py b/nd2reader/parser/v3.py index e831f62..4e11927 100644 --- a/nd2reader/parser/v3.py +++ b/nd2reader/parser/v3.py @@ -42,14 +42,34 @@ class V3Parser(BaseParser): return V3Driver(self.metadata, self._label_map, self._fh) def _build_metadata_dict(self): - metadata_dict = {} self._label_map = self._build_label_map() - for label in self._label_map.keys(): - if label.endswith(six.b("LV!")) or six.b("LV|") in label: - data = read_chunk(self._fh, self._label_map[label]) - stop = label.index(six.b("LV")) - metadata_dict[label[:stop]] = self._read_metadata(data, 1) - return metadata_dict + raw_data = {"image_text_info": read_chunk(self._fh, self._label_map.image_text_info), + "image_metadata_sequence": read_chunk(self._fh, self._label_map.image_metadata_sequence), + # "image_data": read_chunk(self._fh, self._label_map.image_data), + "image_calibration": read_chunk(self._fh, self._label_map.image_calibration), + "image_attributes": read_chunk(self._fh, self._label_map.image_attributes), + # "x_data": read_chunk(self._fh, self._label_map.x_data), + # "y_data": read_chunk(self._fh, self._label_map.y_data), + # "z_data": read_chunk(self._fh, self._label_map.z_data), + # "roi_metadata": read_chunk(self._fh, self._label_map.roi_metadata), + # "pfs_status": read_chunk(self._fh, self._label_map.pfs_status), + # "pfs_offset": read_chunk(self._fh, self._label_map.pfs_offset), + # "guid": read_chunk(self._fh, self._label_map.guid), + # "description": read_chunk(self._fh, self._label_map.description), + # "camera_exposure_time": read_chunk(self._fh, self._label_map.camera_exposure_time), + # "camera_temp": read_chunk(self._fh, self._label_map.camera_temp), + # "acquisition_times": read_chunk(self._fh, self._label_map.acquisition_times), + # "acquisition_times_2": read_chunk(self._fh, self._label_map.acquisition_times_2), + # "acquisition_frames": read_chunk(self._fh, self._label_map.acquisition_frames), + # "lut_data": read_chunk(self._fh, self._label_map.lut_data), + # "grabber_settings": read_chunk(self._fh, self._label_map.grabber_settings), + # "custom_data": read_chunk(self._fh, self._label_map.custom_data), + # "app_info": read_chunk(self._fh, self._label_map.app_info) + } + if self._label_map.image_metadata: + raw_data["image_metadata"] = read_chunk(self._fh, self._label_map.image_metadata) + + return {key: self._read_metadata(data, 1) for key, data in raw_data.items()} def _parse_metadata(self): """ @@ -209,28 +229,15 @@ class V3Parser(BaseParser): as some of the bytes contain the value 33, which is the ASCII code for "!". So we iteratively find each label, grab the subsequent data (always 16 bytes long), advance to the next label and repeat. - :rtype: dict + :rtype: LabelMap """ - # label_map = {} self._fh.seek(-8, 2) chunk_map_start_location = struct.unpack("Q", self._fh.read(8))[0] self._fh.seek(chunk_map_start_location) raw_text = self._fh.read(-1) - # label_start = raw_text.index(V3Parser.CHUNK_MAP_START) + 32 return LabelMap(raw_text) - # while True: - # data_start = raw_text.index(six.b("!"), label_start) + 1 - # key = raw_text[label_start: data_start] - # location, length = struct.unpack("QQ", raw_text[data_start: data_start + 16]) - # if key == V3Parser.CHUNK_MAP_END: - # # We've reached the end of the chunk map - # break - # label_map[key] = location - # label_start = data_start + 16 - # return label_map - def _parse_unsigned_char(self, data): return struct.unpack("B", data.read(1))[0]