Skip to content

Commit e0ae56a

Browse files
authored
Merge pull request #1415 from bthyreau/mgz_add_ushort
Support ushort for the mgh/mgz reader
2 parents db4c859 + 643f7a1 commit e0ae56a

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

nibabel/freesurfer/mghformat.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@
5757

5858
# caveat: Note that it's ambiguous to get the code given the bytespervoxel
5959
# caveat 2: Note that the bytespervox you get is in str ( not an int)
60+
# FreeSurfer historically defines codes 0-10 [1], but only a subset is well supported.
61+
# Here we use FreeSurfer's MATLAB loader [2] as an indication of current support.
62+
# [1] https://github.com/freesurfer/freesurfer/blob/v8.0.0/include/mri.h#L53-L63
63+
# [2] https://github.com/freesurfer/freesurfer/blob/v8.0.0/matlab/load_mgh.m#L195-L207
6064
_dtdefs = ( # code, conversion function, dtype, bytes per voxel
6165
(0, 'uint8', '>u1', '1', 'MRI_UCHAR', np.uint8, np.dtype('u1'), np.dtype('>u1')),
62-
(4, 'int16', '>i2', '2', 'MRI_SHORT', np.int16, np.dtype('i2'), np.dtype('>i2')),
6366
(1, 'int32', '>i4', '4', 'MRI_INT', np.int32, np.dtype('i4'), np.dtype('>i4')),
6467
(3, 'float', '>f4', '4', 'MRI_FLOAT', np.float32, np.dtype('f4'), np.dtype('>f4')),
68+
(4, 'int16', '>i2', '2', 'MRI_SHORT', np.int16, np.dtype('i2'), np.dtype('>i2')),
69+
(10, 'uint16', '>u2', '2', 'MRI_USHRT', np.uint16, np.dtype('u2'), np.dtype('>u2')),
6570
)
6671

6772
# make full code alias bank, including dtype column

nibabel/freesurfer/tests/test_mghformat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ def test_set_zooms():
172172

173173
def bad_dtype_mgh():
174174
"""This function raises an MGHError exception because
175-
uint16 is not a valid MGH datatype.
175+
float64 is not a valid MGH datatype.
176176
"""
177177
# try to write an unsigned short and make sure it
178178
# raises MGHError
179-
v = np.ones((7, 13, 3, 22), np.uint16)
179+
v = np.ones((7, 13, 3, 22), np.float64)
180180
# form a MGHImage object using data
181181
# and the default affine matrix (Note the "None")
182182
MGHImage(v, None)

0 commit comments

Comments
 (0)