1
votes

I am new to HDF5 file format and I have a data(images) saved in HDF5 format. The images are saved undere a group called 'data' which is under the root group as Carrays. what I want to do is to retrive a slice of the saved images. for example the first 400 or somthing like that. The following is what I did.

 h5f = h5py.File('images.h5f', 'r')
 image_grp= h5f['/data/']  #the image group (data) is opened
 print(image_grp[0:400])

but I am getting the following error

Traceback (most recent call last):
File "fgf.py", line 32, in <module>
print(image_grp[0:40])
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
    (/feedstock_root/build_artefacts/h5py_1496410723014/work/h5py-2.7.0/h5py/_objects.c:2846)
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
    (/feedstock_root/build_artefacts/h5py_1496410723014/work/h5py
    2.7.0/h5py/_objects.c:2804)
File "/..../python2.7/site-packages/h5py/_hl/group.py", line 169, in
    __getitem__oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
File "/..../python2.7/site-packages/h5py/_hl/base.py", line 133, in _e name = name.encode('ascii')
AttributeError: 'slice' object has no attribute 'encode'

I am not sure why I am getting this error but I am not even sure if I can slice the images which are saved as individual datasets.

2
Please ad a bit more information. Do you have multiple Datasets in your group data, or is your group actually a dataset? If there are multiple Datasets in your group 'data', you have to retrieve the dataset names first and then iterate over the datasets to get your data. It would be also quite comftable for a beginner to inspect the file with hdfview first. support.hdfgroup.org/products/java/release/download.html - max9111
Thanks @max9111 yes actually my group 'data' contains multiple Datasets and after I post this question I tried iterating over the dataset names to get the data and it works, eventhough it is slow.....! - D_negn
Can you provide the code showing what you do exactly? What do you mean by slow 200-400/sec or less? Are the Arrays of the same size, if yes why are they actually stored in such a manner? - max9111

2 Answers

0
votes

I know this is an old question, but it is the first hit when searching for 'slice' object has no attribute 'encode' and it has no solution.

The error happens because the "group" is a group which does not have the encoding attribute. You are looking for the dataset element.

You need to find/know the key for the item that contains the dataset.

One suggestion is to list all keys in the group, and then guess which one it is:

print(list(image_grp.keys()))

This will give you the keys in the group. A common case is that the first element is the image, so you can do this:

image_grp= h5f['/data/']
image= image_grp(image_grp.keys[0])
print(image[0:400])
0
votes

yesterday I had a similar error and wrote this little piece of code to take my desired slice of h5py file.

import h5py

def h5py_slice(h5py_file, begin_index, end_index):
    slice_list = []
    with h5py.File(h5py_file, 'r') as f:
        for i in range(begin_index, end_index):
            slice_list.append(f[str(i)][...])
    return slice_list

and it can be used like

the_desired_slice_list = h5py_slice('images.h5f', 0, 400)