From 1311e2df58f13cdfd2ca86f6aeff380c095e02c6 Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Wed, 22 Aug 2018 13:58:51 +0200 Subject: [PATCH] Fix to take into account real acquisition times when calculating framerate instead of the set interval (which is sometimes inaccurate) --- nd2reader/raw_metadata.py | 7 +++++++ nd2reader/reader.py | 21 ++------------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/nd2reader/raw_metadata.py b/nd2reader/raw_metadata.py index b0d3485..4487acd 100644 --- a/nd2reader/raw_metadata.py +++ b/nd2reader/raw_metadata.py @@ -3,6 +3,7 @@ from nd2reader.common import read_chunk, read_array, read_metadata, parse_date, import xmltodict import six import numpy as np +import warnings class RawMetadata(object): @@ -434,6 +435,12 @@ class RawMetadata(object): if interval is None or interval <= 0: # Use a fallback if it is still not found interval = get_from_dict_if_exists('dAvgPeriodDiff', loop) + else: + avg_interval = get_from_dict_if_exists('dAvgPeriodDiff', loop) + if round(avg_interval) != round(interval): + warnings.warn("Reported average frame interval (%.1f ms) doesn't match the set interval (%.1f ms). Using the average now." % (avg_interval, interval), RuntimeWarning) + interval = avg_interval + if interval is None or interval <= 0: # In some cases, both keys are not saved. Then try to calculate it. interval = RawMetadata._guess_sampling_from_loops(duration, loop) diff --git a/nd2reader/reader.py b/nd2reader/reader.py index c2ffcc5..1a36e5a 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -177,26 +177,9 @@ class ND2Reader(FramesSequenceND): np.ndarray: an array of times in milliseconds. """ - if self._timesteps is not None and len(timesteps) > 0: + if self._timesteps is not None and len(self._timesteps) > 0: return self._timesteps - timesteps = np.array([]) - current_time = 0.0 - - for loop in self.metadata['experiment']['loops']: - if loop['stimulation']: - continue - - if loop['sampling_interval'] == 0: - # This is a loop were no data is acquired - current_time += loop['duration'] - continue - - timesteps = np.concatenate( - (timesteps, np.arange(current_time, current_time + loop['duration'], loop['sampling_interval']))) - current_time += loop['duration'] - - # if experiment did not finish, number of timesteps is wrong. Take correct amount of leading timesteps. - self._timesteps = timesteps[:self.metadata['num_frames']] + self._timesteps = np.array(list(self._parser._raw_metadata.acquisition_times), dtype=np.float) * 1000.0 return self._timesteps