From d0855564f051beabbce16fcea552e46f8a7a8325 Mon Sep 17 00:00:00 2001 From: Benjamin Thyreau Date: Wed, 14 May 2025 20:16:17 +0900 Subject: [PATCH 1/4] adds ushort to the mgz reader --- nibabel/freesurfer/mghformat.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nibabel/freesurfer/mghformat.py b/nibabel/freesurfer/mghformat.py index 0adcb88e2..22400e0b0 100644 --- a/nibabel/freesurfer/mghformat.py +++ b/nibabel/freesurfer/mghformat.py @@ -62,6 +62,7 @@ (4, 'int16', '>i2', '2', 'MRI_SHORT', np.int16, np.dtype('i2'), np.dtype('>i2')), (1, 'int32', '>i4', '4', 'MRI_INT', np.int32, np.dtype('i4'), np.dtype('>i4')), (3, 'float', '>f4', '4', 'MRI_FLOAT', np.float32, np.dtype('f4'), np.dtype('>f4')), + (10, 'int16', '>i2', '2', 'MRI_SHORT', np.int16, np.dtype('i2'), np.dtype('>i2')), ) # make full code alias bank, including dtype column From dd17469cfb8b791d4be9b59f768fadb690a24cbc Mon Sep 17 00:00:00 2001 From: Benjamin Thyreau Date: Thu, 15 May 2025 10:16:02 +0900 Subject: [PATCH 2/4] Update nibabel/freesurfer/mghformat.py Co-authored-by: Chris Markiewicz --- nibabel/freesurfer/mghformat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nibabel/freesurfer/mghformat.py b/nibabel/freesurfer/mghformat.py index 22400e0b0..f5642d630 100644 --- a/nibabel/freesurfer/mghformat.py +++ b/nibabel/freesurfer/mghformat.py @@ -59,10 +59,10 @@ # caveat 2: Note that the bytespervox you get is in str ( not an int) _dtdefs = ( # code, conversion function, dtype, bytes per voxel (0, 'uint8', '>u1', '1', 'MRI_UCHAR', np.uint8, np.dtype('u1'), np.dtype('>u1')), - (4, 'int16', '>i2', '2', 'MRI_SHORT', np.int16, np.dtype('i2'), np.dtype('>i2')), (1, 'int32', '>i4', '4', 'MRI_INT', np.int32, np.dtype('i4'), np.dtype('>i4')), (3, 'float', '>f4', '4', 'MRI_FLOAT', np.float32, np.dtype('f4'), np.dtype('>f4')), - (10, 'int16', '>i2', '2', 'MRI_SHORT', np.int16, np.dtype('i2'), np.dtype('>i2')), + (4, 'int16', '>i2', '2', 'MRI_SHORT', np.int16, np.dtype('i2'), np.dtype('>i2')), + (10, 'uint16', '>u2', '2', 'MRI_USHRT', np.uint16, np.dtype('u2'), np.dtype('>u2')), ) # make full code alias bank, including dtype column From e03cacc26dfa1e9513283b5501c64017afe3a5f3 Mon Sep 17 00:00:00 2001 From: Benjamin Thyreau Date: Fri, 16 May 2025 11:07:41 +0900 Subject: [PATCH 3/4] add a comment regarding datatype support --- nibabel/freesurfer/mghformat.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nibabel/freesurfer/mghformat.py b/nibabel/freesurfer/mghformat.py index f5642d630..1c97fd566 100644 --- a/nibabel/freesurfer/mghformat.py +++ b/nibabel/freesurfer/mghformat.py @@ -57,6 +57,10 @@ # caveat: Note that it's ambiguous to get the code given the bytespervoxel # caveat 2: Note that the bytespervox you get is in str ( not an int) +# FreeSurfer historically defines codes 0-10 [1], but only a subset is well supported. +# Here we use FreeSurfer's MATLAB loader [2] as an indication of current support. +# [1] https://github.com/freesurfer/freesurfer/blob/v8.0.0/include/mri.h#L53-L63 +# [2] https://github.com/freesurfer/freesurfer/blob/v8.0.0/matlab/load_mgh.m#L195-L207 _dtdefs = ( # code, conversion function, dtype, bytes per voxel (0, 'uint8', '>u1', '1', 'MRI_UCHAR', np.uint8, np.dtype('u1'), np.dtype('>u1')), (1, 'int32', '>i4', '4', 'MRI_INT', np.int32, np.dtype('i4'), np.dtype('>i4')), From 643f7a1dea0804bad87f506d3ac3e1170f5cd065 Mon Sep 17 00:00:00 2001 From: Benjamin Thyreau Date: Fri, 16 May 2025 11:37:02 +0900 Subject: [PATCH 4/4] TEST: mgz should fail on f64, no more fail on u16 --- nibabel/freesurfer/tests/test_mghformat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nibabel/freesurfer/tests/test_mghformat.py b/nibabel/freesurfer/tests/test_mghformat.py index d69587811..660d3dee9 100644 --- a/nibabel/freesurfer/tests/test_mghformat.py +++ b/nibabel/freesurfer/tests/test_mghformat.py @@ -172,11 +172,11 @@ def test_set_zooms(): def bad_dtype_mgh(): """This function raises an MGHError exception because - uint16 is not a valid MGH datatype. + float64 is not a valid MGH datatype. """ # try to write an unsigned short and make sure it # raises MGHError - v = np.ones((7, 13, 3, 22), np.uint16) + v = np.ones((7, 13, 3, 22), np.float64) # form a MGHImage object using data # and the default affine matrix (Note the "None") MGHImage(v, None)