Skip to content

Commit

Permalink
Compatibility with nibabel changes to header extension access.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtclarke committed Oct 21, 2024
1 parent f2639a5 commit b0a26ed
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
This document contains the nifti_mrs_tools release history in reverse chronological order.

1.3.1 (Monday 21st October 2024)
--------------------------------
- Fix compatibility issue with nibabel > 5.3

1.3.0 (Wednesday 26th June 2024)
--------------------------------
- Added `--full-hdr` argumet to `mrs_tools info` which enables printing of the full header extension.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ author_email = william.clarke@ndcn.ox.ac.uk
[options]
install_requires =
numpy
nibabel
nibabel>=5.3
fslpy
importlib_resources;python_version<'3.10'
python_requires = >=3.8
Expand Down
3 changes: 1 addition & 2 deletions src/nifti_mrs/nifti_mrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ def __init__(self, *args, validate_on_creation=True, **kwargs):
raise NotNIFTI_MRS('NIFTI-MRS must have a header extension.')

self._hdr_ext = Hdr_Ext.from_header_ext(
json.loads(
self.header.extensions[hdr_ext_codes.index(44)].get_content()))
self.header.extensions[hdr_ext_codes.index(44)].json())

# Some validation upon creation
if validate_on_creation:
Expand Down
6 changes: 3 additions & 3 deletions src/nifti_mrs/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def validate_nifti_mrs(nifti_mrs):

# Validate header extension
validate_hdr_ext(
nifti_mrs.header.extensions[0].get_content(),
nifti_mrs.header.extensions[0],
nifti_mrs.shape)

# Validate that any SpectralWidth definition matches pixdim[4]
validate_spectralwidth(
nifti_mrs.header.extensions[0].get_content(),
nifti_mrs.header.extensions[0].json(),
nifti_mrs.header['pixdim'][4])


Expand Down Expand Up @@ -97,7 +97,7 @@ def validate_hdr_ext(header_ex, dimension_sizes, data_dimensions=None):
"""
# 1. Check that header_ext is json
try:
json_dict = json.loads(header_ex)
json_dict = header_ex.json()
except json.JSONDecodeError as exc:
raise headerExtensionError("Header extension is not json deserialisable.") from exc

Expand Down
10 changes: 3 additions & 7 deletions tests/test_script_mrs_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# Imports
import subprocess
from pathlib import Path
import json

import nibabel as nib
import pytest
Expand Down Expand Up @@ -163,8 +162,7 @@ def test_merge(tmp_path):
assert (tmp_path / 'test_newaxis_merge.nii.gz').exists()
fna = nib.load(tmp_path / 'test_newaxis_merge.nii.gz')
hdr_ext_codes = fna.header.extensions.get_codes()
hdr_ext = json.loads(
fna.header.extensions[hdr_ext_codes.index(44)].get_content())
hdr_ext = fna.header.extensions[hdr_ext_codes.index(44)].json()

assert fna.ndim == 7
assert fna.shape == (1, 1, 1, 4096, 4, 2, 2)
Expand Down Expand Up @@ -267,8 +265,7 @@ def test_reshape(tmp_path):
assert f1.shape == (1, 1, 1, 4096, 4, 4, 4)

hdr_ext_codes = f1.header.extensions.get_codes()
hdr_ext = json.loads(
f1.header.extensions[hdr_ext_codes.index(44)].get_content())
hdr_ext = f1.header.extensions[hdr_ext_codes.index(44)].json()

assert hdr_ext['dim_5'] == 'DIM_COIL'
assert hdr_ext['dim_6'] == 'DIM_DYN'
Expand All @@ -287,8 +284,7 @@ def test_reshape(tmp_path):
assert f2.shape == (1, 1, 1, 4096, 8, 8)

hdr_ext_codes = f2.header.extensions.get_codes()
hdr_ext = json.loads(
f2.header.extensions[hdr_ext_codes.index(44)].get_content())
hdr_ext = f2.header.extensions[hdr_ext_codes.index(44)].json()

assert hdr_ext['dim_5'] == 'DIM_COIL'
assert hdr_ext['dim_6'] == 'DIM_DYN'
Expand Down

0 comments on commit b0a26ed

Please sign in to comment.