diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e75b23..84797b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) -------------------------------- diff --git a/spec2nii/Siemens/rda.py b/spec2nii/Siemens/rda.py index a3cc25e..294948f 100644 --- a/spec2nii/Siemens/rda.py +++ b/spec2nii/Siemens/rda.py @@ -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) diff --git a/tests/spec2nii_test_data b/tests/spec2nii_test_data index 6f205d3..e0ad587 160000 --- a/tests/spec2nii_test_data +++ b/tests/spec2nii_test_data @@ -1 +1 @@ -Subproject commit 6f205d389085a420b38689f573045166517bb445 +Subproject commit e0ad5870cd5891bbe3fdcf9415845cde1f194884 diff --git a/tests/test_siemens_rda.py b/tests/test_siemens_rda.py index aac48e4..b8f2be8 100644 --- a/tests/test_siemens_rda.py +++ b/tests/test_siemens_rda.py @@ -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): @@ -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)