From 9d2188aa47fd2907b1e18decfe57a7268af6cd63 Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Thu, 30 Mar 2017 14:36:00 +0200 Subject: [PATCH] Bug fixes by testing different .nd2 files --- nd2reader/common.py | 25 ++++++++++++++++++++++++- nd2reader/raw_metadata.py | 13 +++++++------ tests/test_data/.gitignore | 1 - 3 files changed, 31 insertions(+), 8 deletions(-) delete mode 100644 tests/test_data/.gitignore diff --git a/nd2reader/common.py b/nd2reader/common.py index 0e3c129..3316bf9 100644 --- a/nd2reader/common.py +++ b/nd2reader/common.py @@ -196,7 +196,10 @@ def parse_date(text_info): except (TypeError, ValueError): absolute_start = None - return absolute_start + if absolute_start is not None: + return absolute_start + + return None def _parse_metadata_item(data, cursor_position): @@ -309,3 +312,23 @@ def _add_to_metadata(metadata, name, value): metadata[name].append(value) return metadata + + +def get_from_dict_if_exists(key, dictionary, convert_key_to_binary=True): + """ + Get the entry from the dictionary if it exists + Args: + key: key to lookup + dictionary: dictionary to look in + convert_key_to_binary: convert the key from string to binary if true + + Returns: + the value of dictionary[key] or None + + """ + if convert_key_to_binary: + key = six.b(key) + + if not key in dictionary: + return None + return dictionary[key] diff --git a/nd2reader/raw_metadata.py b/nd2reader/raw_metadata.py index 20870a7..2a72663 100644 --- a/nd2reader/raw_metadata.py +++ b/nd2reader/raw_metadata.py @@ -1,5 +1,5 @@ import re -from nd2reader.common import read_chunk, read_array, read_metadata, parse_date +from nd2reader.common import read_chunk, read_array, read_metadata, parse_date, get_from_dict_if_exists import xmltodict import six import numpy as np @@ -189,6 +189,9 @@ class RawMetadata(object): raw_roi_data = self.roi_metadata[six.b('RoiMetadata_v1')] + if not six.b('m_vectGlobal_Size') in raw_roi_data: + return + number_of_rois = raw_roi_data[six.b('m_vectGlobal_Size')] roi_objects = [] @@ -333,15 +336,13 @@ class RawMetadata(object): for loop in loops: # duration of this loop - duration = loop[six.b('dDuration')] + duration = get_from_dict_if_exists('dDuration', loop) or 0 # uiLoopType == 6 is a stimulation loop - is_stimulation = False - if six.b('uiLoopType') in loop: - is_stimulation = loop[six.b('uiLoopType')] == 6 + is_stimulation = get_from_dict_if_exists('uiLoopType', loop) == 6 # sampling interval in ms - interval = loop[six.b('dAvgPeriodDiff')] + interval = get_from_dict_if_exists('dAvgPeriodDiff', loop) parsed_loop = { 'start': time_offset, diff --git a/tests/test_data/.gitignore b/tests/test_data/.gitignore deleted file mode 100644 index c6e2647..0000000 --- a/tests/test_data/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.nd2 \ No newline at end of file