def merge_h5(in_filenames, out_filename):
""" Merge a list of h5 files """
out_h5 = h5.File(out_filename, 'a')
for filename in in_filenames:
if filename is None:
continue
in_h5 = h5.File(filename, 'r')
for name in in_h5.keys():
# If the dataset already exists,
# They must be equal or one must be all-zero.
if name in out_h5.keys():
src_data, dest_data = in_h5[name][()], out_h5[name][()]
if src_data.dtype.kind != 'S' and dest_data.dtype.kind != 'S':
# Both numeric
if not np.any(src_data):
# Source is all zero. Do nothing.
continue
elif not np.any(dest_data):
# Dest is all zero. Overwrite.
del out_h5[name]
h5.h5o.copy(in_h5.id, name, out_h5.id, name)
else:
# Both non-zero. Assert equality and do nothing.
assert np.array_equal(src_data, dest_data)
else:
# Either are non-numeric. Assert equality and do nothing.
assert np.array_equal(src_data, dest_data)
else:
# Only exists in src. Copy to dest.
h5.h5o.copy(in_h5.id, name, out_h5.id, name)
out_h5.flush()
out_h5.close()
评论列表
文章目录