Hi
To change my dataset from a 2D array to a 3D one, the only way I found is:
-
first to get it and chnage it to an array
-
to reshape the array
-
to delete the original dataset
-
then the new dataset is recorded
Is there another way ?
Thanks for any hint
Paul
# -*- coding: utf-8 -*-
import h5py, os
import numpy as np
m, n = 10, 5
M = np.arange(m*n).reshape(m, n)
# step1: creation
with h5py.File(os.getcwd() + '/test.h5', 'w') as h5:
MyGroup = h5.require_group('/MyGroup')
MyDataset = MyGroup.create_dataset(name = '/MyGroup/M',
data = M,
maxshape = (None, None),
dtype='d',
compression='gzip')
h5.flush()
# step2: update
with h5py.File(os.getcwd() + '/test.h5', 'a') as h5:
MyGroup = h5.require_group('/MyGroup')
M = np.array(h5.get('/MyGroup/M'))
del h5['/MyGroup/M']
# the dataset is resized/reshaped from 2D array to 3D
# M.resize() ?
M = M.reshape(1, m, n)
N = np.arange(m*n+1, 2*m*n+1).reshape(1, m, n)
M = np.concatenate( (M, N), axis = 0)
MyDataset = MyGroup.create_dataset(name = '/MyGroup/SubGroup',
data = M,
maxshape = (None, None, None),
dtype='d',
compression='gzip')
h5.flush()
, ,