Browse Source

Fix to take into account real acquisition times when calculating framerate instead of the set interval (which is sometimes inaccurate)

feature/load_slices
Ruben Verweij 6 years ago
parent
commit
1311e2df58
2 changed files with 9 additions and 19 deletions
  1. +7
    -0
      nd2reader/raw_metadata.py
  2. +2
    -19
      nd2reader/reader.py

+ 7
- 0
nd2reader/raw_metadata.py View File

@ -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)


+ 2
- 19
nd2reader/reader.py View File

@ -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

Loading…
Cancel
Save