Mri_convert fails with "cannot allocate memory"

Hello neurostars! :star2: :star2:

I am trying to use mri_convert on recon-all outputs which were processed a few years ago (probably in 2014/2015, and I don’t know what version of Freesurfer these people used at the time) and get the following error:

〉 mri_convert aseg.mgz aseg.nii.gz
mri_convert aseg.mgz aseg.nii.gz 
reading from aseg.mgz...
error: Cannot allocate memory
error: znzTAGskip: tag=1697605229, failed to calloc 1953461248 bytes!

1953461248 bytes (= 1.8 Gbytes) indeed sounds like a lot (although it should easily fit in RAM on the machine I’m using, considering that this file is 100Ko…

I found a workaround using Python were I unzip the mgz file and later convert it to nii.gz:

import nibabel as nib
img = nib.load("aseg.mgz")
〉 mri_convert aseg.mgh aseg.nii.gz
mri_convert aseg.mgh aseg.nii.gz
reading from aseg.mgh...
TR=2700.00, TE=3.35, TI=850.00, flip angle=9.00
i_ras = (-1, 0, 0)
j_ras = (0, 0, -1)
k_ras = (0, 1, 0)
writing to aseg.nii.gz...

I am simply curious as to why mri_convert doesn’t seem to work out-of-the box for these files (it worked for me on more recent outputs of recon-all I computed on my machine). In particular, I looked at a previous topic on Neurostars which mentions a similar error message that was supposedly solved in fmriprep 1.5.4 ; I looked at the diff on github between versions 1.5.4 and 1.5.3 but couldn’t find any clue on how to solve this issue in a systematic way.

Thank you in advance for your help :blush:

NB: initially, I’m doing all this because I want to open this .mgz file with freeview, which weirdly enough doesn’t seem to handle this format while it correctly deals with .mgh and .nii.gz (fsleyes does open all of these formats though).

Presumably nibabel ignores the variable length tags that are stored in an unspecified format while mri_convert gets confused by them. Since this clearly sounds like a bug with FreeSurfer, you may want to generate a Github issue.

However, you will probably want to ensure that this issue has not been resolved by a more recent release of FreeSurfer:

$ mri_convert --version
mri_convert freesurfer 7.3.2

One tip is to launch an instance of NeuroDesk. An elegant feature of NeuroDesk is that the start menu allows you to rapidly start instances of different version of FeeSurfer (7.1.1, 7.2.0, 7.3.2) which allows you to determine if this bug is historical, recent or recently fixed.

It looks like the mgz files are just gzipped mgh files, so you could do something similar to your python processing with just the gunzip command. Most of my aseg.mgz files are only around 360 KB, and some are only 50KB, so I’m guessing that the size is the main problem. It could be the particular way the unzipping is done internally in mri_convert that is running into that limit. I don’t know why, but that particular kind of output is stored as float when it could be stored more efficiently as integer or byte.

cp aseg.mgz aseg.temp.gz
gunzip aseg.temp.gz
mv aseg.temp aseg.notcompressed.mgh

test to compare if there’s anything different in the NIFTI data

mri_info aseg.notcompressed.mgh
mri_convert aseg.mgz aseg.fromcompressed.nii.gz

AFNI comparison of the NIFTI files both ways

3dDiff -a aseg.notcompressed.nii.gz -b aseg.fromcompressed.nii.gz