read a 3d image dataset in python

Sorry for the basic question, but I want to read a 32-bit
floating-point, 1000X2048X2048 dataset in python. I want to specify
the dimensions just like the hdfview utility does when we use "open
as".

How I do that in python? I tried the following code, but it results in
a segmentation fault:

import sys

import numpy as np
import h5py

FILE = "tomo.h5"
DATASET = "images"

# Strings are handled very differently between python2 and python3.
if sys.hexversion >= 0x03000000:
    FILE = FILE.encode()
    DATASET = DATASET.encode()

DIM0 = 0
DIM1 = 2047

def run():

    # Reopen the file and dataset using default properties.
    fid = h5py.h5f.open(FILE)
    dset = h5py.h5d.open(fid, DATASET)

    # Read the data using default properties.
    rdata = np.zeros((DIM0, DIM1), dtype=np.int32)
    dset.read(h5py.h5s.ALL, h5py.h5s.ALL, rdata)

    print("%s:" % DATASET)
    print(rdata)

if __name__ == "__main__":
    run()

Thank you very much,

Marcio

Hi Marcio,

Sorry for the basic question, but I want to read a 32-bit
floating-point, 1000X2048X2048 dataset in python. I want to specify
the dimensions just like the hdfview utility does when we use "open
as".

How I do that in python? I tried the following code, but it results in
a segmentation fault:

It's actually much simpler than the code you posted; for example, to
open the file and access the dataset (both Py2 and Py3):

f = h5py.File("tomo.h5")
dset = f["images"]

To read a subset of the data, use the normal Python-style slicing access:

one_data_point = dset[0, 0, 0] # Load a single element from our 3D dataset
example_slice = dset[0, 0:10, 0:20] # 200-element slice

Writing uses the same syntax:

dset[0,0,0] = 42 # Update one element

You can find additional documentation and examples at h5py.org.

If you don't mind my asking, how did you come up with the code you
posted? It uses h5py's C-style low-level interface, which is
perfectly fine but (as you saw) challenging for newcomers.

Andrew

thank you for the response, I reached that code by googling hdf python
read array

···

On Fri, Dec 6, 2013 at 2:18 PM, Andrew Collette <andrew.collette@gmail.com> wrote:

Hi Marcio,

Sorry for the basic question, but I want to read a 32-bit
floating-point, 1000X2048X2048 dataset in python. I want to specify
the dimensions just like the hdfview utility does when we use "open
as".

How I do that in python? I tried the following code, but it results in
a segmentation fault:

It's actually much simpler than the code you posted; for example, to
open the file and access the dataset (both Py2 and Py3):

f = h5py.File("tomo.h5")
dset = f["images"]

To read a subset of the data, use the normal Python-style slicing access:

one_data_point = dset[0, 0, 0] # Load a single element from our 3D dataset
example_slice = dset[0, 0:10, 0:20] # 200-element slice

Writing uses the same syntax:

dset[0,0,0] = 42 # Update one element

You can find additional documentation and examples at h5py.org.

If you don't mind my asking, how did you come up with the code you
posted? It uses h5py's C-style low-level interface, which is
perfectly fine but (as you saw) challenging for newcomers.

Andrew

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@lists.hdfgroup.org
http://mail.lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org