Skip to content

Commit

Permalink
feat: Add current extension codes
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Sep 7, 2024
1 parent 1936d24 commit 061fbf5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 51 deletions.
15 changes: 15 additions & 0 deletions nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,21 @@ def _mangle(self, dataset: DicomDataset) -> bytes:
(12, 'workflow_fwds', Nifti1Extension),
(14, 'freesurfer', Nifti1Extension),
(16, 'pypickle', Nifti1Extension),
(18, 'mind_ident', NiftiExtension),
(20, 'b_value', NiftiExtension),
(22, 'spherical_direction', NiftiExtension),
(24, 'dt_component', NiftiExtension),
(26, 'shc_degreeorder', NiftiExtension),
(28, 'voxbo', NiftiExtension),
(30, 'caret', NiftiExtension),
## Defined in nibabel.cifti2.parse_cifti2
# (32, 'cifti', Cifti2Extension),
(34, 'variable_frame_timing', NiftiExtension),
(36, 'unassigned', NiftiExtension),
(38, 'eval', NiftiExtension),
(40, 'matlab', NiftiExtension),
(42, 'quantiphyse', NiftiExtension),
(44, 'mrs', NiftiExtension[ty.Dict[str, ty.Any]]),
),
fields=('code', 'label', 'handler'),
)
Expand Down
51 changes: 0 additions & 51 deletions nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
Nifti1Image,
Nifti1Pair,
Nifti1PairHeader,
NiftiJSONExtension,
data_type_codes,
extension_codes,
load,
Expand Down Expand Up @@ -1415,56 +1414,6 @@ def test_nifti_dicom_extension():
Nifti1DicomExtension(2, 0)


def test_json_extension(tmp_path):
nim = load(image_file)
hdr = nim.header
exts_container = hdr.extensions

# Test basic functionality
json_ext = NiftiJSONExtension('ignore', b'{"key": "value"}')
assert json_ext.get_content() == {'key': 'value'}
byte_content = json_ext._mangle(json_ext.get_content())
assert byte_content == b'{"key": "value"}'
json_obj = json_ext._unmangle(byte_content)
assert json_obj == {'key': 'value'}
size = 16 * ((len(byte_content) + 7) // 16 + 1)
assert json_ext.get_sizeondisk() == size

def ext_to_bytes(ext, byteswap=False):
bio = BytesIO()
ext.write_to(bio, byteswap)
return bio.getvalue()

# Check serialization
bytestring = ext_to_bytes(json_ext)
assert bytestring[:8] == struct.pack('<2I', size, extension_codes['ignore'])
assert bytestring[8:].startswith(byte_content)
assert len(bytestring) == size

# Save to file and read back
exts_container.append(json_ext)
nim.to_filename(tmp_path / 'test.nii')

# We used ignore, so it comes back as a Nifti1Extension
rt_img = Nifti1Image.from_filename(tmp_path / 'test.nii')
assert len(rt_img.header.extensions) == 3
rt_ext = rt_img.header.extensions[-1]
assert rt_ext.get_code() == extension_codes['ignore']
assert rt_ext.get_content() == byte_content

# MRS is currently the only JSON extension
json_ext._code = extension_codes['mrs']
nim.to_filename(tmp_path / 'test.nii')

# Check that the extension is read back as a NiftiJSONExtension
rt_img = Nifti1Image.from_filename(tmp_path / 'test.nii')
assert len(rt_img.header.extensions) == 3
rt_ext = rt_img.header.extensions[-1]
assert rt_ext.get_code() == extension_codes['mrs']
assert isinstance(rt_ext, NiftiJSONExtension)
assert rt_ext.get_content() == json_obj


class TestNifti1General:
"""Test class to test nifti1 in general
Expand Down

0 comments on commit 061fbf5

Please sign in to comment.