Skip to content

Commit

Permalink
Validated svs rda orientations.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtclarke committed Mar 2, 2024
1 parent b915930 commit 8ba03eb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
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.3 (WIP)
-----------
- Siemens .rda format now had corrected and validated orientations (tested on VE11 baseline).
-

0.7.2 (Thursday 7th December 2023)
----------------------------------
- SpectralWidth now added to header extension automatically to match bids specification.
Expand Down
33 changes: 17 additions & 16 deletions spec2nii/Siemens/rda.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def convert_rda(rda_path, fname_out, verbose):
' Please contribute test data if you can!')

imagePositionPatient = np.asarray([
_locale_float(hdr['PositionVector[0]']),
_locale_float(hdr['PositionVector[1]']),
_locale_float(hdr['PositionVector[2]'])])
_locale_float(hdr['VOIPositionSag']),
_locale_float(hdr['VOIPositionCor']),
_locale_float(hdr['VOIPositionTra'])])

imageOrientationPatient = np.asarray([
[_locale_float(hdr['RowVector[0]']), _locale_float(hdr['ColumnVector[0]'])],
Expand Down Expand Up @@ -137,13 +137,14 @@ def extractRdaMetadata(hdr):
hdr['Nucleus'])

# Standard defined metadata
def set_standard_def(nifti_mrs_key, location, key, cast=None):
if key in location\
and location[key] is not None:
def set_standard_def(nifti_mrs_key, key, cast=None):
if key in hdr\
and hdr[key] is not None\
and hdr[key]:
if cast is not None:
obj.set_standard_def(nifti_mrs_key, cast(location[key]))
obj.set_standard_def(nifti_mrs_key, cast(hdr[key]))
else:
obj.set_standard_def(nifti_mrs_key, location[key])
obj.set_standard_def(nifti_mrs_key, hdr[key])

# # 5.1 MRS specific Tags
# 'EchoTime'
Expand Down Expand Up @@ -180,22 +181,22 @@ def set_standard_def(nifti_mrs_key, location, key, cast=None):

# # 5.3 Sequence information
# 'SequenceName'
obj.set_standard_def('SequenceName', hdr['SequenceName'])
set_standard_def('SequenceName', 'SequenceName')
# 'ProtocolName'
obj.set_standard_def('ProtocolName', hdr['ProtocolName'])
set_standard_def('ProtocolName', 'ProtocolName')
# # 5.4 Sequence information
# 'PatientPosition'
obj.set_standard_def('PatientPosition', hdr['PatientPosition'])
set_standard_def('PatientPosition', 'PatientPosition')
# 'PatientName'
obj.set_standard_def('PatientName', hdr['PatientName'])
set_standard_def('PatientName', 'PatientName')
# 'PatientID'
obj.set_standard_def('PatientID', hdr['PatientID'])
set_standard_def('PatientID', 'PatientID')
# 'PatientWeight'
obj.set_standard_def('PatientWeight', _locale_float(hdr['PatientWeight']))
set_standard_def('PatientWeight', 'PatientWeight', cast=_locale_float)
# 'PatientDoB'
obj.set_standard_def('PatientDoB', hdr['PatientBirthDate'])
set_standard_def('PatientDoB', 'PatientBirthDate')
# 'PatientSex'
obj.set_standard_def('PatientSex', hdr['PatientSex'])
set_standard_def('PatientSex', 'PatientSex')

# # 5.5 Provenance and conversion metadata
obj.set_standard_def('ConversionMethod', f'spec2nii v{spec2nii_ver}')
Expand Down
2 changes: 1 addition & 1 deletion tests/spec2nii_test_data
Submodule spec2nii_test_data updated from ca9850 to 9322e3
44 changes: 44 additions & 0 deletions tests/test_siemens_rda.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,50 @@

latin1_encoding = siemens_path / 'rda' / 'latin1.rda'

ve_path = siemens_path / 'VEData' / 'rda'
paired_ve_data = {
'svs_se_c_t15_s10_r10': 'svs_se_c>t15>s10_R10_12_1',
'svs_se_iso_tra_sat': 'svs_se_iso_tra_sat_13_1',
'svs_se_s_c10_t5_r10': 'svs_se_s>c10>t5_R10_11_1',
'svs_se_t_c15_s10_r10': 'svs_se_t>c15>s10_R10_10_1'}


def test_ve_against_dicom(tmp_path):
for rda_file in ve_path.glob("svs_se*.rda"):
print(rda_file.stem)
dcm_dir = rda_file.parent.parent / 'DICOM' / paired_ve_data[rda_file.stem]

subprocess.run([
'spec2nii', 'rda',
'-f', rda_file.stem + '_rda',
'-o', tmp_path,
'-j', rda_file])

subprocess.run([
'spec2nii', 'dicom',
'-f', rda_file.stem + '_dcm',
'-o', tmp_path,
'-j', dcm_dir])

rda_out = tmp_path / (rda_file.stem + '_rda.nii.gz')
dcm_out = tmp_path / (rda_file.stem + '_dcm.nii.gz')
assert rda_out.exists()
assert dcm_out.exists()

dcm = read_nifti_mrs(dcm_out)
rda = read_nifti_mrs(rda_out)
assert dcm.shape == rda.shape
assert np.allclose(dcm.get_fdata(dtype=complex), rda.get_fdata(dtype=complex))
assert np.allclose(dcm.get_sform(), rda.get_sform())

hdr_ext_codes = dcm.header.extensions.get_codes()
hdr_ext_dcm = json.loads(dcm.header.extensions[hdr_ext_codes.index(44)].get_content())
hdr_ext_codes = rda.header.extensions.get_codes()
hdr_ext_rda = json.loads(rda.header.extensions[hdr_ext_codes.index(44)].get_content())

for key in ['SpectrometerFrequency', 'ResonantNucleus', 'EchoTime', 'RepetitionTime']:
assert hdr_ext_dcm[key] == hdr_ext_rda[key]


def test_xa20_svs(tmp_path):

Expand Down

0 comments on commit 8ba03eb

Please sign in to comment.