Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for HDF5/NWB #683

Open
DeanTM opened this issue Jul 17, 2024 · 1 comment
Open

Support for HDF5/NWB #683

DeanTM opened this issue Jul 17, 2024 · 1 comment

Comments

@DeanTM
Copy link

DeanTM commented Jul 17, 2024

I'm looking to save transforms in the same HDF5 file that contains my data, but in a different data group (so I can keep them together, share it as a single file, etc.).

Currently I'm building a workaround similar to that descibed in issue 318 but with nibabel in the middle

import ants, h5py, nibabel
reg=ants.registration( ants.image_read( ants.get_data("r16" ) ),  ants.image_read( ants.get_data("r16" ) ), 'SyN')

# Saving Transform
elastic_ni = nibabel.load(reg['fwdtransforms'][0])
with h5py.File("mydata.h5", "a") as my_h5:
    transform_group = my_h5.create_group("ants_transforms")
    elastic_group = transform_group.create_group('elastic')
    elastic_group.create_dataset("fdata", data=elastic_ni..get_fdata())
    elastic_group.create_dataset("affine", data=elastic_ni.affine)
    # EDIT: Some extra stuff needs to come in for the header too...

## Loading Transform
# instead of image_read, I can use nibabel to create a spatial image 
# warp=ants.image_read( reg['fwdtransforms'][0] )  # image not transform
with h5.File("mydata.h5", "r") as my_h5:
    nibabel_header = nibabel.Nifti1Header()
    for key in nibabel_header.keys():
        # ... populate header here
    warp = ants.from_nibabel(
            nibabel.spatialimages.SpatialImage(
                dataobj=my_h5['ants_transforms']['elastic']['fdata'],
                affine=my_h5['ants_transforms']['elastic']['affine'],
                header=nibabel_header))
warptx = ants.transform_from_displacement_field( warp )

However,

  • this process is slow;
  • the use of nibabel seems excessive but I can't determine how to construct an ANTsTransform directly from the data in the 'fwdtransforms'][0] file; and
  • I'm not sure if I'm missing cases where the fdata and affine values are insufficient to describe a transform.

I also need to do a similar, but different, procedure for saving and loading the .mat affine transforms.

Would it be possible to save transforms as HDF5 file groups and load them directly? Something like,

## Saving Transform
with h5py.File("mydata.h5", "a") as my_h5:
    transform_group = my_h5.create_group("ants_transforms")
    _ = transform_group.create_group('elastic')
    warptx = save_transform_to_h5(my_h5, group='elastic')

## Loading Transform
with h5py.File("mydata.h5", "r") as my_h5:
    warptx = load_transform_from_h5(my_h5, group="elastic")
@cookpa
Copy link
Member

cookpa commented Jul 26, 2024

I don't know if ITK supports image data and warps being in the same file. I would ask on the ITK discourse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants