Skip to content

LoadImage can not read .mgz files #2743

@Spenhouet

Description

@Spenhouet

Is your feature request related to a problem? Please describe.
If one wants to work with Freesurfer data it is necessary to read .mgz files.
Nibabel can do so.

Describe the solution you'd like
One step is to add the .mgz extension to the suffixes defined for the NibabelReader.

suffixes: Sequence[str] = ["nii", "nii.gz"] # <- add mgz here

I tested this by monkey patching NibabelReader.verify_suffix:

from monai.data.utils import is_supported_format
from monai.data.image_reader import NibabelReader


def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
    """
    Verify whether the specified file or files format is supported by Nibabel reader.

    Args:
        filename: file name or a list of file names to read.
            if a list of files, verify all the suffixes.

    """
    suffixes: Sequence[str] = ["nii", "nii.gz", "mgz"]
    return is_supported_format(filename, suffixes)


NibabelReader.verify_suffix = verify_suffix

But there seem to be more adjustments needed. With this change I get the following exception:

~\AppData\Local\Programs\Python\Python37\lib\site-packages\monai\data\image_reader.py in read(self, data, **kwargs)
    363             img = nib.load(name, **kwargs_)
--> 364             img = correct_nifti_header_if_necessary(img)
    365             img_.append(img)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\monai\data\utils.py in correct_nifti_header_if_necessary(img_nii)
    489     """
--> 490     dim = img_nii.header["dim"][0]
    491     if dim >= 5:

~\AppData\Local\Programs\Python\Python37\lib\site-packages\nibabel\freesurfer\mghformat.py in __getitem__(self, item)
    495             return self._header_data[item]
--> 496         return super(MGHHeader, self).__getitem__(item)
    497 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\nibabel\wrapstruct.py in __getitem__(self, item)
    313         """
--> 314         return self._structarr[item]
    315 

ValueError: no field of name dim

Describe alternatives you've considered
One can convert the .mgz files to .nii.gz beforehand like this:

img = nib.load(f"{filename}.mgz")
nib.save(img , f"{filename}.nii.gz")

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions