Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does h5py offer a neat simple way to create or use a group or dataset?

Tags:

h5py

I'm fixing a python script using h5py. It contains code like this:

hdf = h5py.File(hdf5_filename, 'a')
... 
g = hdf.create_group('foo')
g.create_dataset('bar', ...whatever...)

Sometimes this runs on a file which already has a group named 'foo', in which case I see "ValueError: Unable to create group (Name already exists)"

One way to fix this is to replace the one simple line with create_group with four lines, like this:

if 'foo' in hdf.keys():
    g = hdf['foo']
else:
    g = hdf.create_group['foo']

g.create_dataset(...etc...)

Is there a neater way to do this, maybe in only one line? Like how with files in the standard C library, 'a' mode will either append to an existing file, or create a file if it's not already there.

Same goes for datasets - I have

create_dataset('bar', ...) 

but should check first:

if 'bar' in g.keys():
   d = g['bar']
else:
   d = g.create_dataset('bar')

My wish: to find h5py has methods named create_or_use_group() and create_or_use_dataset(). What actually exists?

like image 649
DarenW Avatar asked Oct 19 '22 21:10

DarenW


1 Answers

Yes: require_group and require_dataset:

with h5py.File("/tmp/so_hdf5/test2.h5", 'w') as f:
    a = f.create_dataset('a',data=np.random.random((10, 10)))

with h5py.File("/tmp/so_hdf5/test2.h5", 'r+') as f:
    a = f.require_dataset('a', shape=(10, 10), dtype='float64')
    d = f.require_dataset('d', shape=(20, 20), dtype='float64')
    g = f.require_group('foo')
    print(a)
    print(d)
    print(g)

Note that you do need to know the shape and dtype of the dataset, otherwise require_dataset throws a TypeError. In that case, you could do something like:

try:
    a = f.require_dataset('a', shape=(10, 10), dtype='float64')
except TypeError:
    a = f['a']

If you don't already know the shape and dtype, I don't think there's much advantage to require_dataset over using try ... except ...

like image 174
Yossarian Avatar answered Oct 21 '22 21:10

Yossarian