Skip to content

Commit 81eba53

Browse files
committed
ENH: Update Nifti extension codes, implement ASCII and JSON types
1 parent 2c26fa5 commit 81eba53

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

nibabel/nifti1.py

Lines changed: 46 additions & 2 deletions
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,34 @@ def _mangle(self, dataset):
505506
return dio.read(ds_len)
506507

507508

509+
class NiftiASCIIExtension(Nifti1Extension):
510+
"""Generic ASCII-based NIfTI header extension
511+
512+
This class handles serialization and deserialization of ASCII contents
513+
without any further validation or processing.
514+
"""
515+
516+
def _unmangle(self, value: bytes) -> str:
517+
return value.decode('ascii')
518+
519+
def _mangle(self, value: str) -> bytes:
520+
return value.encode('ascii')
521+
522+
523+
class NiftiJSONExtension(Nifti1Extension):
524+
"""Generic JSON-based NIfTI header extension
525+
526+
This class handles serialization and deserialization of JSON contents
527+
without any further validation or processing.
528+
"""
529+
530+
def _unmangle(self, value: bytes) -> dict:
531+
return json.loads(value.decode('utf-8'))
532+
533+
def _mangle(self, value: dict) -> bytes:
534+
return json.dumps(value).encode('utf-8')
535+
536+
508537
# NIfTI header extension type codes (ECODE)
509538
# see nifti1_io.h for a complete list of all known extensions and
510539
# references to their description or contacts of the respective
@@ -514,12 +543,27 @@ def _mangle(self, dataset):
514543
(0, 'ignore', Nifti1Extension),
515544
(2, 'dicom', Nifti1DicomExtension if have_dicom else Nifti1Extension),
516545
(4, 'afni', Nifti1Extension),
517-
(6, 'comment', Nifti1Extension),
546+
(6, 'comment', NiftiASCIIExtension),
518547
(8, 'xcede', Nifti1Extension),
519548
(10, 'jimdiminfo', Nifti1Extension),
520549
(12, 'workflow_fwds', Nifti1Extension),
521550
(14, 'freesurfer', Nifti1Extension),
522551
(16, 'pypickle', Nifti1Extension),
552+
(18, 'mind_ident', Nifti1Extension),
553+
(20, 'b_value', Nifti1Extension),
554+
(22, 'spherical_direction', Nifti1Extension),
555+
(24, 'dt_component', Nifti1Extension),
556+
(26, 'shc_degreeorder', Nifti1Extension),
557+
(28, 'voxbo', Nifti1Extension),
558+
(30, 'caret', Nifti1Extension),
559+
## Defined in nibabel.cifti2.parse_cifti2
560+
# (32, 'cifti', Cifti2Extension),
561+
(34, 'variable_frame_timing', Nifti1Extension),
562+
(36, 'unassigned', Nifti1Extension),
563+
(38, 'eval', Nifti1Extension),
564+
(40, 'matlab', Nifti1Extension),
565+
(42, 'quantiphyse', Nifti1Extension),
566+
(44, 'mrs', NiftiJSONExtension),
523567
),
524568
fields=('code', 'label', 'handler'),
525569
)

0 commit comments

Comments
 (0)