Read/write Ilastik .h5 into/from numpy array

I would like to start read and write images using the ilastik .h5 format instead of .tif in my own python script, so my scripts can better work with ilastik. Is there example/document on how to convert between ndarray and .h5 (i.e., what’s the minimum required structure of .h5)?

Hi @Mark,

the minimal thing you should add to your numpy array is currently axistags:

import vigra
import h5py

# assuming the data is loaded already
image = ...

# it is cool to help ilastik and tell it the meanings of the axes, we use vigra axistags objects for that

# with your data I am assuming axisorder z y x c
axistags = vigra.defaultAxistags("zyxc")

# write out the file
with h5py.File("path/to/file.h5", "w") as f:
    ds = f.create_dataset(name="mydata", data=image, chunks=(64, 64, 64, 1))
    ds.attrs["axistags"] = axistags.toJSON()

that’s basically the nicest you can be to ilastik :slight_smile:

This is super helpful and it works very well. Thank you!

I have one recommendation, the example code does not compress data. I am now using

ds = f.create_dataset(name=“mydata”, data=image, chunks=(64, 64, 64, 1), compression=“gzip”, compression_opts=4)

This often significantly reduces the file size, without much impact on the speed. Compression is particularly effective on the probability image and 3D images. So if the compression can be turned on by default in ilastik, it should address the other topic:

1 Like