13
13
14
14
from __future__ import annotations
15
15
16
+ import json
16
17
import warnings
17
18
from io import BytesIO
18
19
@@ -377,7 +378,7 @@ def __repr__(self):
377
378
# deal with unknown codes
378
379
code = self ._code
379
380
380
- s = f"Nifti1Extension ('{ code } ', '{ self ._content } ')"
381
+ s = f"{ self . __class__ . __name__ } ('{ code } ', '{ self ._content } ')"
381
382
return s
382
383
383
384
def __eq__ (self , other ):
@@ -505,6 +506,20 @@ def _mangle(self, dataset):
505
506
return dio .read (ds_len )
506
507
507
508
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
+
508
523
# NIfTI header extension type codes (ECODE)
509
524
# see nifti1_io.h for a complete list of all known extensions and
510
525
# references to their description or contacts of the respective
@@ -520,6 +535,21 @@ def _mangle(self, dataset):
520
535
(12 , 'workflow_fwds' , Nifti1Extension ),
521
536
(14 , 'freesurfer' , Nifti1Extension ),
522
537
(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 ),
523
553
),
524
554
fields = ('code' , 'label' , 'handler' ),
525
555
)
0 commit comments