H5py: dataset resizing from 2D to 3D

Hi

To change my dataset from a 2D array to a 3D one, the only way I found is:

  1. first to get it and chnage it to an array

  2. to reshape the array

  3. to delete the original dataset

  4. 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()

, ,

1 Like

The only other way is to create that dataset as 3D initially and store the initial 2D data like, for example, MyDataset[0,:,:] = <2D numpy array>.

Ok it confirms what i’ve been thinking
Thanks for the help