Skip to content

Commit

Permalink
Automatically add spectralwidth
Browse files Browse the repository at this point in the history
  • Loading branch information
wtclarke committed Dec 6, 2023
1 parent 5d166e3 commit 18f6590
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
This document contains the Spec2nii release history in reverse chronological order.

0.7.2 (WIP)
---------------------------------
- SpectralWidth now added to header extension automatically to match bids specification.
- NIfTI-MRS V0.8 now generated.

0.7.1 (Tuesday 7th November 2023)
---------------------------------
- The --anon flag can be passed with any call to anonymise after writing files.
Expand Down
2 changes: 1 addition & 1 deletion requirements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ dependencies:
- scipy
- brukerapi>=0.1.8
- pandas
- nifti-mrs>=1.0.2
- nifti-mrs>=1.1.1
10 changes: 10 additions & 0 deletions spec2nii/spec2nii.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ def add_common_parameters(subparser):
if self.imageOut:
self.implement_overrides(args)

self.insert_spectralwidth()

if args.anon:
from spec2nii.anonymise import anon_nifti_mrs
for idx, nifti_mrs_img in enumerate(self.imageOut):
Expand Down Expand Up @@ -325,6 +327,14 @@ def implement_overrides(self, args):
nifti_mrs_img.header.extensions.clear()
nifti_mrs_img.header.extensions.append(new_ext)

def insert_spectralwidth(self):
"""Ensure that the correct spectral width is inserted into the header extension"""
for nifti_mrs_img in self.imageOut:
if 'SpectralWidth' in nifti_mrs_img.hdr_ext:
nifti_mrs_img.hdr_ext['SpectralWidth'] = 1 / nifti_mrs_img.dwelltime
else:
nifti_mrs_img.add_hdr_field('SpectralWidth', 1 / nifti_mrs_img.dwelltime)

def validate_output(self):
"""Run NIfTI MRS validation on output."""
import nifti_mrs.validator as validate
Expand Down
29 changes: 29 additions & 0 deletions tests/test_spectralwidth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'''Simple test for SpectralWidth compliance'''

import subprocess
from pathlib import Path
import json

import numpy as np

from .io_for_tests import read_nifti_mrs

# Data paths
siemens_path = Path(__file__).parent / 'spec2nii_test_data' / 'Siemens'
vb_path = siemens_path / 'VBData' / 'Twix/meas_MID151_svs_se_C_T15_S10_10_FID108741.dat'


def test_insertion_spectralwidth(tmp_path):
subprocess.check_call(['spec2nii', 'twix',
'-e', 'image',
'-f', 'vb',
'-o', tmp_path,
'-j', str(vb_path)])

img_t = read_nifti_mrs(tmp_path / 'vb.nii.gz')

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

assert 'SpectralWidth' in hdr_ext
assert np.isclose(hdr_ext['SpectralWidth'], 1 / img_t.header['pixdim'][4])

0 comments on commit 18f6590

Please sign in to comment.