From 27ed229f54ce7805ddbb13b553f1e0681307e5f0 Mon Sep 17 00:00:00 2001 From: Gabriele Girelli Date: Sat, 15 Aug 2020 11:58:57 +0200 Subject: [PATCH] Fixed complexity --- nd2reader/parser.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/nd2reader/parser.py b/nd2reader/parser.py index 8df38cd..010de1e 100644 --- a/nd2reader/parser.py +++ b/nd2reader/parser.py @@ -247,6 +247,18 @@ class Parser(object): """ return {channel: n for n, channel in enumerate(self.metadata["channels"])} + def _remove_unwanted_bytes(self, image_group_data, image_data_start, height, width): + # Remove unwanted 0-bytes that can appear in stitched images + number_of_true_channels = int(len(image_group_data[4:]) / (height * width)) + unwanted_bytes_len = (len(image_group_data[image_data_start:]))%(height*width) + if unwanted_bytes_len: + warnings.warn('Identified unwanted bytes in the ND2 file, possibly stitched.') + byte_ids = range(image_data_start+height*number_of_true_channels, len(image_group_data)-unwanted_bytes_len+1, height*number_of_true_channels) + if all([0 == image_group_data[byte_ids[i]+i] for i in range(len(byte_ids))]): + warnings.warn('All unwanted bytes are zero-bytes, correctly removed.') + for i in range(len(byte_ids)): + del image_group_data[byte_ids[i]] + def _get_raw_image_data(self, image_group_number, channel_offset, height, width): """Reads the raw bytes and the timestamp of an image. @@ -273,17 +285,7 @@ class Parser(object): # 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. number_of_true_channels = int(len(image_group_data[4:]) / (height * width)) - - # Remove unwanted 0-bytes that can appear in stitched images - unwanted_bytes_len = (len(image_group_data[image_data_start:]))%(height*width) - if unwanted_bytes_len: - warnings.warn('Identified unwanted bytes in the ND2 file, possibly stitched.') - byte_ids = range(image_data_start+height*number_of_true_channels, len(image_group_data)-unwanted_bytes_len+1, height*number_of_true_channels) - if all([0 == image_group_data[byte_ids[i]+i] for i in range(len(byte_ids))]): - warnings.warn('All unwanted bytes are zero-bytes, correctly removed.') - for i in range(len(byte_ids)): - del image_group_data[byte_ids[i]] - + self._remove_unwanted_bytes(image_group_data, image_data_start, height, width) try: image_data = np.reshape(image_group_data[image_data_start::number_of_true_channels], (height, width)) except ValueError: