Skip to content

Commit d4632b8

Browse files
committed
example of hdf5 via h5py
1 parent 96c107a commit d4632b8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

examples/h5py/h5_example.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
import h5py
3+
4+
5+
a = np.arange(1000, dtype=np.float64).reshape(25, 40)
6+
7+
with h5py.File("test.h5", "w") as f:
8+
9+
# attributes are meta-data that can help describe the data
10+
# in h5py, they are dictionary keys
11+
f.attrs["about"] = "a test HDF5 file with h5py"
12+
13+
# groups can be thought of as subdirectories in the output file,
14+
# and can help you organize data together logically
15+
grp = f.create_group("data")
16+
17+
# a numpy array is a dataset in HDF5 -- it will figure out the
18+
# dimensions and type from the array itself
19+
grp.create_dataset("a", data=a)
20+
21+
# there can be attributes in the group too
22+
grp.attrs["array info"] = "a simple array"
23+
24+
25+
# now we'll try to read it in
26+
with h5py.File("test.h5", "r") as f:
27+
28+
# access the group we stored our data in
29+
g = f["data"]
30+
31+
aread = np.array(g["a"])
32+
33+
34+
# are they the same?
35+
da = a - aread
36+
print(np.max(abs(da)))

0 commit comments

Comments
 (0)