Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize an HDF5 array with `h5py`

Tags:

python

hdf5

h5py

How can I resize an HDF5 array using the h5py Python library ?

I've tried using the .resize method and on an array with chunks set to True. Alas, I'm still missing something.

In [1]: import h5py

In [2]: f = h5py.File('foo.hdf5', 'w')

In [3]: d = f.create_dataset('data', (3, 3), dtype='i8', chunks=True)

In [4]: d.resize((6, 3))
/home/mrocklin/Software/anaconda/lib/python2.7/site-packages/h5py/_hl/dataset.pyc in resize(self, size, axis)
--> 277         self.id.set_extent(size)
ValueError: unable to set extend dataset (Dataset: Unable to initialize object)

In [11]: h5py.__version__ 
Out[11]: '2.2.1'
like image 401
MRocklin Avatar asked Apr 10 '14 20:04

MRocklin


2 Answers

As mentioned by Oren, you need to use maxshape when creating the dataset if you want to change the array size later. Setting a dimension to None allows you to resize that dimension up to 2**64 (h5's limit) later:

In [1]: import h5py

In [2]: f = h5py.File('foo.hdf5', 'w')

In [3]: d = f.create_dataset('data', (3, 3), maxshape=(None, 3), dtype='i8', chunks=True)

In [4]: d.resize((6, 3))

In [5]: h5py.__version__
Out[5]: '2.2.1'

See the docs for more.

like image 65
user130604 Avatar answered Sep 23 '22 11:09

user130604


You need to change this line:

d = f.create_dataset('data', (3, 3), dtype='i8', chunks=True)

To

d = f.create_dataset('data', (3, 3), maxshape=(?, ?), dtype='i8', chunks=True) 

d.resize((?, ?))

Change the ? to whatever size you what (You can also set it to None)

Read here: http://docs.h5py.org/en/latest/high/dataset.html#resizable-datasets

like image 34
Oren Avatar answered Sep 23 '22 11:09

Oren