Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An XML file inside HDF5, h5py

Tags:

python

hdf5

h5py

I am using h5py to save data (float numbers), in groups. In addition to the data itself, I need to include an additional file (an .xml file, containing necessary information) within the hdf5. How do i do this? Is my approach wrong?

f = h5py.File('filename.h5')
f.create_dataset('/data/1',numpy_array_1)
f.create_dataset('/data/2',numpy_array_2)
.
.

my h5 tree should look thus:

/ 
/data
/data/1 (numpy_array_1)
/data/2 (numpy_array_2)
.
.
/morphology.xml (?)
like image 986
chaitu Avatar asked Feb 22 '23 03:02

chaitu


1 Answers

One option is to add it as a variable-length string dataset.

http://code.google.com/p/h5py/wiki/HowTo#Variable-length_strings

E.g.:

import h5py
xmldata = """<xml>
<something>
    <else>Text</else>
</something>
</xml>
"""

# Write the xml file...
f = h5py.File('test.hdf5', 'w')
str_type = h5py.new_vlen(str)
ds = f.create_dataset('something.xml', shape=(1,), dtype=str_type)
ds[:] = xmldata
f.close()

# Read the xml file back...
f = h5py.File('test.hdf5', 'r')
print f['something.xml'][0]
like image 70
Joe Kington Avatar answered Feb 24 '23 16:02

Joe Kington