Skip to content

Commit 82b0b05

Browse files
committed
FIX: Constrain permissible dtypes in CIFTI-2
1 parent 4bd0027 commit 82b0b05

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

nibabel/cifti2/cifti2.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ class Cifti2HeaderError(Exception):
4141
"""
4242

4343

44+
_dtdefs = ( # code, label, dtype definition, niistring
45+
(2, 'uint8', np.uint8, "NIFTI_TYPE_UINT8"),
46+
(4, 'int16', np.int16, "NIFTI_TYPE_INT16"),
47+
(8, 'int32', np.int32, "NIFTI_TYPE_INT32"),
48+
(16, 'float32', np.float32, "NIFTI_TYPE_FLOAT32"),
49+
(64, 'float64', np.float64, "NIFTI_TYPE_FLOAT64"),
50+
(256, 'int8', np.int8, "NIFTI_TYPE_INT8"),
51+
(512, 'uint16', np.uint16, "NIFTI_TYPE_UINT16"),
52+
(768, 'uint32', np.uint32, "NIFTI_TYPE_UINT32"),
53+
(1024, 'int64', np.int64, "NIFTI_TYPE_INT64"),
54+
(1280, 'uint64', np.uint64, "NIFTI_TYPE_UINT64"),
55+
)
56+
57+
# Make full code alias bank, including dtype column
58+
data_type_codes = make_dt_codes(_dtdefs)
59+
4460
CIFTI_MAP_TYPES = ('CIFTI_INDEX_TYPE_BRAIN_MODELS',
4561
'CIFTI_INDEX_TYPE_PARCELS',
4662
'CIFTI_INDEX_TYPE_SERIES',
@@ -103,6 +119,10 @@ def _underscore(string):
103119
return re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', string).lower()
104120

105121

122+
class LimitedNifti2Header(Nifti2Header):
123+
_data_type_codes = data_type_codes
124+
125+
106126
class Cifti2MetaData(CaretMetaData):
107127
""" A list of name-value pairs
108128
@@ -1392,7 +1412,7 @@ def __init__(self,
13921412
header = Cifti2Header.from_axes(header)
13931413
super(Cifti2Image, self).__init__(dataobj, header=header,
13941414
extra=extra, file_map=file_map)
1395-
self._nifti_header = Nifti2Header.from_header(nifti_header)
1415+
self._nifti_header = LimitedNifti2Header.from_header(nifti_header)
13961416

13971417
# if NIfTI header not specified, get data type from input array
13981418
if nifti_header is None:

0 commit comments

Comments
 (0)