-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhdf5_format_use.py
More file actions
37 lines (29 loc) · 776 Bytes
/
hdf5_format_use.py
File metadata and controls
37 lines (29 loc) · 776 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
37
# -*- coding: utf-8 -*-
"""
hdf5 tutorial
example from uetke.com
"""
import h5py
import numpy as np
arr = np.random.randn(1000)
with h5py.File('random.hdf5', 'w') as f:
dset = f.create_dataset("default", data=arr)
with h5py.File('random.hdf5', 'r') as f:
data = f['default']
print(min(data))
print(max(data))
print(data[:15])
# code that raises an error
f = h5py.File('random.hdf5', 'r')
data = f['default']
f.close()
try:
print(data[1]) # ValueError: Not a dataset (Not a dataset)
except Exception as e:
print(e, " !!!!!!! error raised")
# No more access to dataset. file closed. f['default'] generates a pointer to data on hard drive.
# code that works
f = h5py.File('random.hdf5', 'r')
data = f['default'][:]
f.close()
print(data[10])