Skip to content

Support ushort for the mgh/mgz reader #1415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion nibabel/freesurfer/mghformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@

# 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')),
(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')),
(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
Expand Down
4 changes: 2 additions & 2 deletions nibabel/freesurfer/tests/test_mghformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading