Skip to content

Commit

Permalink
BF: RDA decoding issue (#115)
Browse files Browse the repository at this point in the history
* Include potential fix for a decoding issuereported. Awaiting test data.

* Add tests for rda with latin1 encoding.
  • Loading branch information
wtclarke authored Oct 12, 2023
1 parent ba20108 commit 933008e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This document contains the Spec2nii release history in reverse chronological ord
--------------------------------
- The --anon flag can be passed with any call to anonymise after writing files.
- The Siemens enhanced dicom filetype pathway now handles CSI data.
- Fixed issue with RDA files having latin1 encoding. Thanks to gaunab on github. Fixes Issue #96.

0.7.0 (Saturday 5th August 2023)
--------------------------------
Expand Down
37 changes: 26 additions & 11 deletions spec2nii/Siemens/rda.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,33 @@ def convert_rda(rda_path, fname_out, verbose):

with open(rda_path, 'rb') as fp:
for line in fp:
if hdr_st.search(line.decode()):
pass
# print('header found')
elif hdr_end.search(line.decode()):
# print('header end')
break
else:
match = hdr_val.search(line.decode())
if len(match.groups()) < 2:
hdr[match[1]] = None
try:
if hdr_st.search(line.decode()):
pass
# print('header found')
elif hdr_end.search(line.decode()):
# print('header end')
break
else:
match = hdr_val.search(line.decode())
if len(match.groups()) < 2:
hdr[match[1]] = None
else:
hdr[match[1]] = match[2]
except UnicodeDecodeError:
print('Trying latin-1 encoding.')
if hdr_st.search(line.decode('latin-1')):
pass
# print('header found')
elif hdr_end.search(line.decode('latin-1')):
# print('header end')
break
else:
hdr[match[1]] = match[2]
match = hdr_val.search(line.decode('latin-1'))
if len(match.groups()) < 2:
hdr[match[1]] = None
else:
hdr[match[1]] = match[2]
if verbose:
print(hdr)

Expand Down
2 changes: 1 addition & 1 deletion tests/spec2nii_test_data
Submodule spec2nii_test_data updated from 6f205d to e0ad58
20 changes: 20 additions & 0 deletions tests/test_siemens_rda.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
xa20_svs_path = siemens_path / 'XAData' / 'XA20/rda/spct_002.MR.MRI-LAB Test_Dir.5.1.114540.rda'
xa31_locale_svs_path = siemens_path / 'XAData' / 'XA31/rda/locale_XA31.rda'

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


def test_xa20_svs(tmp_path):

Expand Down Expand Up @@ -53,3 +55,21 @@ def test_xa31_locale_svs(tmp_path):

assert img_t.shape == (1, 1, 1, 1024)
assert np.iscomplexobj(img_t.dataobj)


def test_latin_encoding(tmp_path):

subprocess.check_call(['spec2nii', 'rda',
'-f', 'rda_latin1',
'-o', tmp_path,
'-j', str(latin1_encoding)])

img_t = read_nifti_mrs(tmp_path / 'rda_latin1.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())

hdr_ext['ResonantNucleus'] = ['1H', ]

assert img_t.shape == (1, 1, 1, 2048)
assert np.iscomplexobj(img_t.dataobj)

0 comments on commit 933008e

Please sign in to comment.