question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LoadImage can not read .mgz files

See original GitHub issue

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")

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
Spenhouetcommented, Aug 13, 2021

I tested the fix with LoadImaged(FILE_KEYS, "NibabelReader") and can confirm that it solved my issue.

1reaction
Nic-Macommented, Aug 11, 2021

Hi @wyli ,

I see your idea, that makes sense, we should only check extension in “auto” mode.

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[FieldTrip] Problem reading .mgz files
So do you think the solution would be running the matlab script with ... ERROR: could not open /tmp/tmp7914012.mgh for reading >>> ERROR: ......
Read more >
loadImage does not Load Image from data folder - Beginners
I read that “the image file must be in the data folder of the current sketch.” I quadruple checked & the image file...
Read more >
[Freesurfer] Able to load surfaces but not *.mgz files in ... - Re
I have attempted to > use the load_mgh.m and MRIread.m commands. The error returned is the > following: > > The system cannot...
Read more >
Surf Ice: help - NITRC
The first issue here is that the files you provided are not valid gzip format files, which explains why Surfice, FSLeyes, MRIcroGL, etc...
Read more >
Chapter 4 - Introducing heterogeneities - Springer Link
mgz file using Freeview. Doing so, we see that a value of '15' is assigned by FreeSurfer to the region that its segmentation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found