From 40aa6c8e4960fc49ce6316dcdc678dfe3d98c24b Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Tue, 20 Jun 2017 09:36:52 +0200 Subject: [PATCH] Cache timesteps and add frame rate property --- nd2reader/reader.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/nd2reader/reader.py b/nd2reader/reader.py index 33f1ed9..2ab2c85 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -27,6 +27,9 @@ class ND2Reader(FramesSequenceND): # Setup the axes self._setup_axes() + # Other properties + self._timesteps = None + @classmethod def class_exts(cls): """Let PIMS open function use this reader for opening .nd2 files @@ -89,6 +92,27 @@ class ND2Reader(FramesSequenceND): """ return self._dtype + @property + def timesteps(self): + """Get the timesteps of the experiment + + Returns: + np.ndarray: an array of times in milliseconds. + + """ + if self._timesteps is None: + return self.get_timesteps() + return self._timesteps + + @property + def frame_rate(self): + """The (average) frame rate + + Returns: + float: the (average) frame rate in frames per second + """ + return 1000. / np.mean(np.diff(self.timesteps)) + def _get_metadata_property(self, key, default=None): if self.metadata is None: return default @@ -149,6 +173,9 @@ class ND2Reader(FramesSequenceND): np.ndarray: an array of times in milliseconds. """ + if self._timesteps is not None: + return self._timesteps + timesteps = np.array([]) current_time = 0.0 for loop in self.metadata['experiment']['loops']: @@ -165,4 +192,5 @@ class ND2Reader(FramesSequenceND): current_time += loop['duration'] # if experiment did not finish, number of timesteps is wrong. Take correct amount of leading timesteps. - return timesteps[:self.metadata['num_frames']] + self._timesteps = timesteps[:self.metadata['num_frames']] + return self._timesteps