Browse Source

Merge pull request #22 from WMAPernice/fix-missing-frames-issue_to-current-master

Fix to allow reading of ND2-files with missing frames.
feature/load_slices
Ruben Verweij 5 years ago
committed by GitHub
parent
commit
e182d2e73a
2 changed files with 10 additions and 15 deletions
  1. +0
    -11
      nd2reader/exceptions.py
  2. +10
    -4
      nd2reader/parser.py

+ 0
- 11
nd2reader/exceptions.py View File

@ -6,17 +6,6 @@ class InvalidVersionError(Exception):
""" """
pass pass
class NoImageError(Exception):
"""No image found.
Some apparent images in ND2s are just completely blank placeholders. These are used when the number of images per
cycle are unequal (e.g. if you take fluorescent images every 2 minutes, and bright field images every minute).
"""
pass
class EmptyFileError(Exception): class EmptyFileError(Exception):
"""This .nd2 file seems to be empty. """This .nd2 file seems to be empty.


+ 10
- 4
nd2reader/parser.py View File

@ -3,11 +3,12 @@ import struct
import array import array
import six import six
import warnings
from pims.base_frames import Frame from pims.base_frames import Frame
import numpy as np import numpy as np
from nd2reader.common import get_version, read_chunk from nd2reader.common import get_version, read_chunk
from nd2reader.exceptions import InvalidVersionError, NoImageError
from nd2reader.exceptions import InvalidVersionError
from nd2reader.label_map import LabelMap from nd2reader.label_map import LabelMap
from nd2reader.raw_metadata import RawMetadata from nd2reader.raw_metadata import RawMetadata
@ -72,7 +73,7 @@ class Parser(object):
try: try:
timestamp, image = self._get_raw_image_data(image_group_number, channel_offset, self.metadata["height"], timestamp, image = self._get_raw_image_data(image_group_number, channel_offset, self.metadata["height"],
self.metadata["width"]) self.metadata["width"])
except (TypeError, NoImageError):
except (TypeError):
return Frame([], frame_no=frame_number, metadata=self._get_frame_metadata()) return Frame([], frame_no=frame_number, metadata=self._get_frame_metadata())
else: else:
return image return image
@ -101,7 +102,7 @@ class Parser(object):
try: try:
timestamp, raw_image_data = self._get_raw_image_data(image_group_number, channel, timestamp, raw_image_data = self._get_raw_image_data(image_group_number, channel,
height, width) height, width)
except (TypeError, NoImageError):
except (TypeError):
return Frame([], frame_no=frame_number, metadata=self._get_frame_metadata()) return Frame([], frame_no=frame_number, metadata=self._get_frame_metadata())
else: else:
return raw_image_data return raw_image_data
@ -284,7 +285,12 @@ class Parser(object):
if np.any(image_data): if np.any(image_data):
return timestamp, Frame(image_data, metadata=self._get_frame_metadata()) return timestamp, Frame(image_data, metadata=self._get_frame_metadata())
raise NoImageError
# If a blank "gap" image is encountered, generate an array of corresponding height and width to avoid
# errors with ND2-files with missing frames. Array is filled with nan to reflect that data is missing.
else:
empty_frame = np.full((height, width), np.nan)
warnings.warn('ND2 file contains gap frames which are represented by np.nan-filled arrays; to convert to zeros use e.g. np.nan_to_num(array)')
return timestamp, Frame(empty_frame, metadata=self._get_frame_metadata())
def _get_frame_metadata(self): def _get_frame_metadata(self):
"""Get the metadata for one frame """Get the metadata for one frame


Loading…
Cancel
Save