-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.py
66 lines (51 loc) · 1.59 KB
/
groups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
import h5py
import shutil
PATH = "example_groups.h5"
NUM_DATASETS = 3
NUM_ROWS = 100
NUM_COLS = 10
def random_data() -> list:
"""
Generates random data.
Returns a list of numpy arrays.
"""
return [np.random.random(size=(NUM_ROWS, NUM_COLS)) for i in range(NUM_DATASETS)]
def save_data(file_path: str, data: list) -> None:
"""
Saves provided data to hdf5 file. Data is stored in groups.
File is created if it does not exist.
File is overwritten if it exists.
File path is relative to the current working directory.
"""
with h5py.File(file_path, "w") as hdf:
group = hdf.create_group("Group1")
subgroups = [
group.create_group(name)
for name in ["Group1/SubGroup1", "Group1/SubGroup2"]
]
for subgroup in subgroups:
for i, matrix in enumerate(data):
subgroup.create_dataset(f"dataset {i}", data=matrix)
def read_data(path: str) -> None:
"""
Reads data from hdf5 file and prints it to standard output.
"""
with h5py.File(path, "r") as hdf:
base_items = list(hdf.items())
print(f"Items in the base directory: {base_items}")
dataset_list = list(hdf.keys())
print(f"List of datasets in this file: \n {dataset_list}")
for key in dataset_list:
for item in hdf.get(key).items():
print(list(item[1].keys()))
def main():
"""
Main function.
"""
data = random_data()
save_data(PATH, data)
read_data(PATH)
shutil.rmtree(PATH)
if __name__ == "__main__":
main()