Jupyter Snippet CB2nd 09_hdf5_array
Jupyter Snippet CB2nd 09_hdf5_array
4.9. Manipulating large arrays with HDF5
import numpy as np
import h5py
f = h5py.File('myfile.h5', 'w')
f.create_group('/experiment1')
<HDF5 group "/experiment1" (0 members)>
f['/experiment1'].attrs['date'] = '2018-01-01'
x = np.random.rand(1000, 1000)
f['/experiment1'].create_dataset('array1', data=x)
<HDF5 dataset "array1": shape (1000, 1000), type "<f8">
f.close()
f = h5py.File('myfile.h5', 'r')
f['/experiment1'].attrs['date']
'2018-01-01'
y = f['/experiment1/array1']
type(y)
h5py._hl.dataset.Dataset
np.array_equal(x[0, :], y[0, :])
True
f.close()
import os
os.remove('myfile.h5')