forked from sbu-python-class/python-science
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh5_example.py
More file actions
36 lines (23 loc) · 919 Bytes
/
h5_example.py
File metadata and controls
36 lines (23 loc) · 919 Bytes
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
import numpy as np
import h5py
a = np.arange(1000, dtype=np.float64).reshape(25, 40)
with h5py.File("test.h5", "w") as f:
# attributes are meta-data that can help describe the data
# in h5py, they are dictionary keys
f.attrs["about"] = "a test HDF5 file with h5py"
# groups can be thought of as subdirectories in the output file,
# and can help you organize data together logically
grp = f.create_group("data")
# a numpy array is a dataset in HDF5 -- it will figure out the
# dimensions and type from the array itself
grp.create_dataset("a", data=a)
# there can be attributes in the group too
grp.attrs["array info"] = "a simple array"
# now we'll try to read it in
with h5py.File("test.h5", "r") as f:
# access the group we stored our data in
g = f["data"]
aread = np.array(g["a"])
# are they the same?
da = a - aread
print(np.max(abs(da)))