How can I convert ecat(.v) file to nifti(.nii) file in python?

I want to convert ecat file to nifiti file or ecat file to dicom so I can change it to .nii later. I would like it in python if possible. Do you have any library suggestion? Thank you

Check out nibabel. There’s an introduction to the basic ideas at Nibabel images, and some documentation on the ecat format.

Ecat is not one of the more commonly used formats, so you may run into bugs or edge cases that are not well handled. Please feel free to open issues and pull requests on GitHub - nipy/nibabel: Python package to access a cacophony of neuro-imaging file formats.

When I look into document of nibabel.ecat it only have ecat.load function that can upload ecat and command about mange structure but no command to change format directly. So I think, I have to write a code on my own but I don’t know which and how to change a structure of format. I met this code but it error a bit because his nibabel library use incorrect and I know nothing about changing data structure. quick ecat to nifti · GitHub Another solution I met is Turku but I really want it in python. If you have any suggestion thank you.

I would expect the following to work:

import nibabel as nb

ecat = nb.load('orig.v')
nii = nb.Nifti1Image(
    ecat.get_fdata(),
    ecat.affine,
    dtype=ecat.header.get_data_dtype()
)
nii.to_filename('converted.nii')

I’ve never really worked with ecats, so I don’t know if there are wrinkles here that need to be dealt with. @bendhouseart has done more with them in recent years and might have suggestions. And this will obviously not copy any metadata over except for the affine and on-disk datatype.

@baitantan, effigies is right in that I do have experience (for better or worse) converting ecat to nifti. We wrote our own python based tool to do so, but it’s primarily focused on using the command line. That said, it has quite a few features that aren’t available in Nibabel; it was my intent to merge in those features to nibabel but that just hasn’t happened.

History lesson aside, I would encourage you determine what version of ecat file you are working with as the documentation is sparse and to date we only officially support versions 6.3, 7.2 and 7.3. We haven’t encountered any other versions out in the wild, but the numbering scheme implies that there are quite a few floating around. If you’re not sure what version of file you have I would suggest you try the following to determine:

pip install pypet2bids
ecatpet2bids <your ecat file> --dump
MAGIC_NUMBER: MATRIX73v
ORIGINAL_FILE_NAME: <your ecat file>
SW_VERSION: 73
SYSTEM TYPE: 328
FILE_TYPE: 7

The output from ecatpet2bids --dump will output some of the information from the main header of the file, and SW_VERSION should tell you what version of ecat you’re working with, it might error out if it’s not a 6.3, 7.2 or 7.3 version, but it should display what it’s found.

If it is a supported version you can use the python library directly to convert the file in your code:

from pypet2bids.ecat import Ecat
ecat = Ecat(ecat_file=your_ecat_file,
                nifti_file=nifti_file_destination,
                collect_pixel_data=True)

ecat.make_nifti()

# at this point you should have a nifti made with the help of Nibabel and pet2bids
# the additional code below exists if if you so wish to add in any additional bids into a sidecar json
# or create one at all
output_path = pathlib.Path(ecat.make_nifti())
ecat.populate_sidecar(**kwargs)
# clean up the sidecar file
ecat.prune_sidecar()
sidecar_path = pathlib.Path(join(str(output_path.parent), output_path.stem + '.json'))
ecat.show_sidecar(output_path=sidecar_path)

If it’s not a supported version, well, I’d be happy to work with you to support whatever number version of file it is you have so it’s included in our library at GitHub - openneuropet/PET2BIDS: PET2BIDS helps you convert your PET data into BIDS! raw PET scanner files (e.g. ecat, dicom) and additional side file like .e.g excel sheets

Thanks

1 Like