Search code examples
arrayshdf5pytables

Access subset of array in hdf5 with pytables


I am a newbie in using pytables.

I am trying to access an array stored in a hdf5-file.

For instance the array has the dimensions 100x2000x5000, Now, I am trying to access only the first 100 data entries along the dimensions 2 and 3, something like "[:,0,0]". But in the documentation of pytables I only find the following example:

import tables
h5file = tables.open_file('file.h5', 'r')
data = h5file.root.array.read()
h5file.close()

How I can tell the read-module only to load a subset of the array?

Thanks in advance for any help!


Solution

  • You can use any of the following methods to read a subset of the array. All methods return data as an object of the current flavor (same dtype as Array).

    array0 = Array.read(start=None, stop=None, step=None)
    

    You can also use NumPy-style slicing. NumPy-style point and boolean selections are supported as well as fancy indexing. [The latter uses a list of indices in a certain axis is specified.]
    Examples below.

    array1 = array[0:4]           # simple selection
    array2 = array[:,0]           # slice selection 
    array3 = array[1, ::2, 1:4]   # general slice selection
    

    Specific to your request, you can use the following. (It reads the first 100 data entries along the 2nd and 3rd indices, which are axis 1 and axis 2):

    your_slice = h5file.root.array[:,0:100,0:100]           # slice selection