Skip to content

Commit 63f0647

Browse files
committed
ENH: Update Nifti extension codes, implement JSON type
1 parent 2c26fa5 commit 63f0647

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

nibabel/nifti1.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from __future__ import annotations
1515

16+
import json
1617
import warnings
1718
from io import BytesIO
1819

@@ -377,7 +378,7 @@ def __repr__(self):
377378
# deal with unknown codes
378379
code = self._code
379380

380-
s = f"Nifti1Extension('{code}', '{self._content}')"
381+
s = f"{self.__class__.__name__}('{code}', '{self._content}')"
381382
return s
382383

383384
def __eq__(self, other):
@@ -505,6 +506,20 @@ def _mangle(self, dataset):
505506
return dio.read(ds_len)
506507

507508

509+
class NiftiJSONExtension(Nifti1Extension):
510+
"""Generic JSON-based NIfTI header extension
511+
512+
This class handles serialization and deserialization of JSON contents
513+
without any further validation or processing.
514+
"""
515+
516+
def _unmangle(self, value: bytes) -> dict:
517+
return json.loads(value.decode('utf-8'))
518+
519+
def _mangle(self, value: dict) -> bytes:
520+
return json.dumps(value).encode('utf-8')
521+
522+
508523
# NIfTI header extension type codes (ECODE)
509524
# see nifti1_io.h for a complete list of all known extensions and
510525
# references to their description or contacts of the respective
@@ -520,6 +535,21 @@ def _mangle(self, dataset):
520535
(12, 'workflow_fwds', Nifti1Extension),
521536
(14, 'freesurfer', Nifti1Extension),
522537
(16, 'pypickle', Nifti1Extension),
538+
(18, 'mind_ident', Nifti1Extension),
539+
(20, 'b_value', Nifti1Extension),
540+
(22, 'spherical_direction', Nifti1Extension),
541+
(24, 'dt_component', Nifti1Extension),
542+
(26, 'shc_degreeorder', Nifti1Extension),
543+
(28, 'voxbo', Nifti1Extension),
544+
(30, 'caret', Nifti1Extension),
545+
## Defined in nibabel.cifti2.parse_cifti2
546+
# (32, 'cifti', Cifti2Extension),
547+
(34, 'variable_frame_timing', Nifti1Extension),
548+
(36, 'unassigned', Nifti1Extension),
549+
(38, 'eval', Nifti1Extension),
550+
(40, 'matlab', Nifti1Extension),
551+
(42, 'quantiphyse', Nifti1Extension),
552+
(44, 'mrs', NiftiJSONExtension),
523553
),
524554
fields=('code', 'label', 'handler'),
525555
)

0 commit comments

Comments
 (0)