Skip to content
Snippets Groups Projects
Commit 46f307ec authored by David GEISSBUHLER's avatar David GEISSBUHLER
Browse files

Merge branch 'timestamps_writing' into 'master'

Write timestamps to file as dataset attributes along with data

See merge request !19
parents 4669efb1 48b7b75f
No related branches found
No related tags found
1 merge request!19Write timestamps to file as dataset attributes along with data
Pipeline #54349 failed
...@@ -862,7 +862,16 @@ class StreamSave(StreamFilter): ...@@ -862,7 +862,16 @@ class StreamSave(StreamFilter):
raise ValueError("Output file is not a valid StreamFile.") raise ValueError("Output file is not a valid StreamFile.")
def put(self, data, timestamp=None): 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)
################################################################################ ################################################################################
......
...@@ -200,7 +200,7 @@ class StreamFile: ...@@ -200,7 +200,7 @@ class StreamFile:
# TODO rotate if requested # TODO rotate if requested
return data 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. """Appends `data` (a frame of a stream) to the hdf5 file.
Parameters Parameters
...@@ -211,3 +211,9 @@ class StreamFile: ...@@ -211,3 +211,9 @@ class StreamFile:
Data frame to append. Data frame to append.
""" """
self.hdf5_file.append(name, data) 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)
...@@ -38,19 +38,30 @@ def test_stream_write(): ...@@ -38,19 +38,30 @@ def test_stream_write():
np.arange(12 * 52).astype(np.float64).reshape((12, 52)), np.arange(12 * 52).astype(np.float64).reshape((12, 52)),
] ]
for a_test_data in test_data: test_timestamps = [
np.linspace(0, 20*test_data[0].shape[0], test_data[0].shape[0]),
np.linspace(10, 10*test_data[1].shape[0], test_data[1].shape[0]),
np.linspace(100, 200*test_data[2].shape[0], test_data[2].shape[0]),
np.linspace(5, 6*test_data[3].shape[0], test_data[3].shape[0]),
]
for a_test_data, a_test_timestamps in zip(test_data, test_timestamps):
with StreamFile(resource_path("test/data/save_test.h5"), mode="w") as output_file: with StreamFile(resource_path("test/data/save_test.h5"), mode="w") as output_file:
stream = Stream("test_data") stream = Stream("test_data")
save_filter = stream.save(output_file) save_filter = stream.save(output_file)
for i in range(a_test_data.shape[0]): for i in range(a_test_data.shape[0]):
stream.put(a_test_data[i]) stream.put(a_test_data[i], a_test_timestamps[i])
with StreamFile(resource_path("test/data/save_test.h5"), mode="r") as input_file: with StreamFile(resource_path("test/data/save_test.h5"), mode="r") as input_file:
stream = Stream("test_data", input_file) stream = Stream("test_data", input_file)
data = stream.load() data = stream.load()
timestamps = input_file.get_stream_timestamps("test_data")
if np.isscalar(timestamps): # if there is only 1 frame, timestamps are returned as a scalar
timestamps = np.array([timestamps])
assert np.array_equal(data, a_test_data) assert np.array_equal(data, a_test_data)
assert data.dtype == a_test_data.dtype assert data.dtype == a_test_data.dtype
assert np.array_equal(timestamps, a_test_timestamps)
os.remove(resource_path("test/data/save_test.h5")) os.remove(resource_path("test/data/save_test.h5"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment