From ec1880364659c531281d4ae896b88ca09edc6b7c Mon Sep 17 00:00:00 2001
From: vpollet <vincent.pollet@idiap.ch>
Date: Mon, 27 Sep 2021 15:43:50 +0200
Subject: [PATCH] Write timestamps to file as dataset attributes along with
 data

---
 bob/io/stream/stream.py      | 11 ++++++++++-
 bob/io/stream/stream_file.py |  8 +++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/bob/io/stream/stream.py b/bob/io/stream/stream.py
index 505aefd..70cf3df 100644
--- a/bob/io/stream/stream.py
+++ b/bob/io/stream/stream.py
@@ -862,7 +862,16 @@ class StreamSave(StreamFilter):
             raise ValueError("Output file is not a valid StreamFile.")
 
     def put(self, data, timestamp=None):
-        self.file.put_frame(self.name, data)
+        """Pass data and timestamp to the :obj:`~bob.io.stream.StreamFile` to write to disk.
+
+        Parameters
+        ----------
+        data : :obj:`numpy.ndarray`
+            data to write to file.
+        timestamp : int or float
+            Timestamp of `data`, by default None.
+        """
+        self.file.put_frame(self.name, data, timestamp)
 
 
 ################################################################################
diff --git a/bob/io/stream/stream_file.py b/bob/io/stream/stream_file.py
index e31ad49..34f8e29 100644
--- a/bob/io/stream/stream_file.py
+++ b/bob/io/stream/stream_file.py
@@ -200,7 +200,7 @@ class StreamFile:
         # TODO rotate if requested
         return data
 
-    def put_frame(self, name, data):
+    def put_frame(self, name, data, timestamp=None):
         """Appends `data` (a frame of a stream) to the hdf5 file.
 
         Parameters
@@ -211,3 +211,9 @@ class StreamFile:
             Data frame to append.
         """
         self.hdf5_file.append(name, data)
+        if timestamp is not None:
+            try:
+                previous_timestamps = self.hdf5_file.get_attribute("timestamps", name)
+            except RuntimeError:  # No previous timestamps
+                previous_timestamps = []
+            self.hdf5_file.set_attribute("timestamps", np.append(previous_timestamps, timestamp), name)
-- 
GitLab