PreibischLab / BigStitcher

ImgLib2/BDV implementation of Stitching for large datasets
GNU General Public License v2.0
64 stars 14 forks source link

Importing h5 file after registration to python #111

Closed kapoorlab closed 2 years ago

kapoorlab commented 2 years ago

Hi,

Many thanks for the big stitcher plugin. I have a feature request/if it already exists a pointer in the direction:

I was wondering if it is possible to export the h5 file in python using h5py for doing further dataprocessing in notebooks?

hoerldavid commented 2 years ago

Hi Varun,

I think the issue here is the same as in https://github.com/PreibischLab/BigStitcher/issues/112 - the h5 files contain only raw data, not a fused result.

Specifically, the images (for each timepoint and view setup) can be found in the h5 file under:

/t{timepoint id, 5 digits}/s{view setup id, 2 digits}/{downsampling level}/cells

e.g. the image of setup 0 at tp 0 and downsampling level 0 is at:

/t00000/s00/0/cells

Also, you can find the downsampling factors of each view setup under

/s{setup id}/resolutions

You can use h5py to access all of this, e.g. the following code will load the first image from our test dataset (http://preibischlab.mdc-berlin.de/BigStitcher/Grid_3d_h5_aligned.zip), plot a max-projection and print the resolution levels.

import h5py
from matplotlib import pyplot as plt

with h5py.File('C:/Users/david/Downloads/grid-3d-stitched-h5/dataset.h5', 'r') as fd:
    print(fd['/s00/resolutions'][...])
    img = fd['/t00000/s00/0/cells'][...]

plt.imshow(img.max(axis=0))

So if you want to load fused images with h5py, you can save the fusion results as a new h5 file and load from there.

Alternatively, one could load the raw images and transformations from the .xml dataset file and transform them in Python using things like https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.affine_transform.html, but be aware that this involves a lot of manual work, as you essentially would have to re-create the entire fusion process in Python.

Best, David

kapoorlab commented 2 years ago

Hi David, Yes that works very well indeed. Many thanks.