… slice a dataset based on a condition?#
Good practice is to have dimension attached to the datasets. The dimension scales can be used to decide what to slice exactly. The following example first generates a dataset and the slices it depending on the value in the first dimension, in this case the time:
import h5rdmtoolbox as h5tbx
import numpy as np
h5tbx.use(None)
with h5tbx.File() as h5:
h5.create_dataset('time', data=range(0, 100), make_scale=True)
h5.create_dataset('x', data=range(0, 100), make_scale=True)
h5.create_dataset('y', data=range(0, 200), make_scale=True)
h5.create_dataset('data', np.random.rand(100, 200, 100), attach_scale=('time', 'y', 'x'))
data = h5.data[:]
h5.dump()
-
-
(time: 100, y: 200, x: 100) [float64]
-
(100) [int64]
-
(100) [int64]
-
(200) [int64]
with h5tbx.File(h5.hdf_filename) as h5:
h5.data[h5.data.time > 5.4, :, :].plot()
Matplotlib is building the font cache; this may take a moment.