From 53737b69f0d773def893dda390e3d51359b8c062 Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Fri, 7 Jul 2017 12:30:23 +0200 Subject: [PATCH] Add try/except blocks to make code more robust + fix unit tests --- nd2reader/common.py | 7 ++++++- nd2reader/raw_metadata.py | 40 ++++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/nd2reader/common.py b/nd2reader/common.py index fbb1c4a..52fba85 100644 --- a/nd2reader/common.py +++ b/nd2reader/common.py @@ -255,7 +255,12 @@ def _get_value(data, data_type, cursor_position): 8: _parse_string, 9: _parse_char_array, 11: _parse_metadata_item} - return parser[data_type](data) if data_type < 11 else parser[data_type](data, cursor_position) + try: + value = parser[data_type](data) if data_type < 11 else parser[data_type](data, cursor_position) + except KeyError: + value = None + + return value def read_metadata(data, count): diff --git a/nd2reader/raw_metadata.py b/nd2reader/raw_metadata.py index f03587d..45c5b36 100644 --- a/nd2reader/raw_metadata.py +++ b/nd2reader/raw_metadata.py @@ -56,7 +56,10 @@ class RawMetadata(object): return self._metadata_parsed def _set_default_if_not_empty(self, entry): - if len(self._metadata_parsed[entry]) == 0 and self._metadata_parsed['total_images_per_channel'] > 0: + total_images = self._metadata_parsed['total_images_per_channel'] \ + if self._metadata_parsed['total_images_per_channel'] is not None else 0 + + if len(self._metadata_parsed[entry]) == 0 and total_images > 0: # if the file is not empty, we always have one of this entry self._metadata_parsed[entry] = [0] @@ -67,7 +70,12 @@ class RawMetadata(object): return None def _parse_width_or_height(self, key): - return self.image_attributes[six.b('SLxImageAttributes')][six.b(key)] + try: + length = self.image_attributes[six.b('SLxImageAttributes')][six.b(key)] + except KeyError: + length = None + + return length def _parse_height(self): return self._parse_width_or_height('uiHeight') @@ -76,10 +84,16 @@ class RawMetadata(object): return self._parse_width_or_height('uiWidth') def _parse_date(self): - return parse_date(self.image_text_info[six.b('SLxImageTextInfo')]) + try: + return parse_date(self.image_text_info[six.b('SLxImageTextInfo')]) + except KeyError: + return None def _parse_calibration(self): - return self.image_calibration.get(six.b('SLxCalibration'), {}).get(six.b('dCalibration')) + try: + return self.image_calibration.get(six.b('SLxCalibration'), {}).get(six.b('dCalibration')) + except KeyError: + return None def _parse_frames(self): """The number of cycles. @@ -99,7 +113,11 @@ class RawMetadata(object): if self.image_metadata_sequence is None: return [] - metadata = self.image_metadata_sequence[six.b('SLxPictureMetadata')][six.b('sPicturePlanes')] + try: + metadata = self.image_metadata_sequence[six.b('SLxPictureMetadata')][six.b('sPicturePlanes')] + except KeyError: + return [] + channels = self._process_channels_metadata(metadata) return channels @@ -148,7 +166,10 @@ class RawMetadata(object): if self.image_text_info is None: return dimension_text - textinfo = self.image_text_info[six.b('SLxImageTextInfo')].values() + try: + textinfo = self.image_text_info[six.b('SLxImageTextInfo')].values() + except KeyError: + return dimension_text for line in textinfo: entry = self._parse_dimension_text_line(line) @@ -186,7 +207,12 @@ class RawMetadata(object): """ if self.image_attributes is None: return 0 - return self.image_attributes[six.b('SLxImageAttributes')][six.b('uiSequenceCount')] + try: + total_images = self.image_attributes[six.b('SLxImageAttributes')][six.b('uiSequenceCount')] + except KeyError: + total_images = None + + return total_images def _parse_roi_metadata(self): """Parse the raw ROI metadata.