Commit e72166cd authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Merge branch 'p1' into 'master'

Keep the order of FrameContainers when saving

Closes #14

See merge request !37
parents 135d3898 8194610d
Pipeline #25077 passed with stages
in 6 minutes and 34 seconds
......@@ -19,9 +19,9 @@ def test_frame_container():
# create random test data
test_data = [numpy.random.randn(20,20) for i in range(5)]
test_data = [numpy.random.randn(20,20) for i in range(15)]
frames =
for i in range(5):
for i in range(15):
frames.add(i, test_data[i], i/5.)
# write to file
......@@ -29,14 +29,14 @@ def test_frame_container():
# read from file using the dummy class
read =, 'r'))
assert len(read) == 5
assert len(read) == 15
# assert that the data hasn't changed
for index, data, quality in read:
# assert that the data hasn't changed and also order is kept
for i, (index, data, quality) in enumerate(read):
index = int(index)
assert index in range(5)
assert index == i
assert abs(quality - index/5.) < 1e-8
assert numpy.allclose(test_data[index], data)
assert numpy.allclose(test_data[i], data)
# test as_array method
assert numpy.allclose(read.as_array(), test_data)
......@@ -35,8 +35,16 @@ class FrameContainer:
def load(self, hdf5, load_function =
self._frames = []
if hdf5.has_group("FrameIndexes"):"FrameIndexes")
indices = sorted(int(i) for i in hdf5.keys(relative=True))
frame_ids = [hdf5[str(i)] for i in indices]"..")
frame_ids = hdf5.sub_groups(relative=True, recursive=False)
# Read content (frames) from HDF5File
for path in hdf5.sub_groups(relative=True, recursive=False):
for path in frame_ids:
# extract frame_id
if path[:6] == 'Frame_':
frame_id = str(path[6:])
......@@ -56,13 +64,21 @@ class FrameContainer:
The contained data will be written using the given save_function."""
if not len(self):
logger.warn("Saving empty FrameContainer '%s'", hdf5.filename)
frame_ids = []
for frame_id, data, quality in self:
hdf5.create_group("Frame_%s" % frame_id)"Frame_%s" % frame_id)
frame_ids.append("Frame_%s" % frame_id)
save_function(data, hdf5)
if quality is not None:
hdf5.set("FrameQuality", quality)"..")
# save the order of frames too so we can load them correctly later
for i, v in enumerate(frame_ids):
hdf5[str(i)] = v"..")
def is_similar_to(self, other):
if len(self) != len(other): return False
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment