Skip to content

Commit

Permalink
Allow, but correct, non compliant user defined fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtclarke committed Mar 27, 2024
1 parent 62e0bab commit d2004f8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 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.1.2 (WIP)
-----------------------------------
- When reading files any user defined parameters without a description will print a warning to the user and generate an empty description key.

1.1.1 (Wednesday 6th December 2023)
-----------------------------------
- Validator checks consistency of any `SpectraWidth` header extension value with the dwell time in `pixdim[4]`.
Expand Down
19 changes: 17 additions & 2 deletions src/nifti_mrs/hdr_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def from_header_ext(cls, hdr_ext_dict):
if key in standard_defined:
obj.set_standard_def(key, hdr_ext_dict[key])
else:
if 'Value' in hdr_ext_dict[key]\
if isinstance(hdr_ext_dict[key], dict)\
and 'Value' in hdr_ext_dict[key]\
and 'Description' in hdr_ext_dict[key]:
obj.set_user_def(
key,
Expand All @@ -103,7 +104,21 @@ def from_header_ext(cls, hdr_ext_dict):
hdr_ext_dict[key],
hdr_ext_dict[key]['Description'])
else:
raise ValueError(f'User-defined key {key} must contain a "Description" field"')
print(
"This file's header extension is currently invalid. "
f"Reason: User-defined key {key} does not contain a 'Description' field. "
"Setting empty 'Description'.")
if isinstance(hdr_ext_dict[key], dict)\
and 'Value' in hdr_ext_dict[key]:
obj.set_user_def(
key,
hdr_ext_dict[key]['Value'],
'')
else:
obj.set_user_def(
key,
hdr_ext_dict[key],
'')

return obj

Expand Down
6 changes: 6 additions & 0 deletions tests/test_hdr_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def test_to_from_dict():
assert hdr2._dim_info[1] == {"tag": "DIM_EDIT", "info": None, "hdr": None}
assert 'RepetitionTime' in hdr2._standard_data

dict_rep['my_value3'] = 5.0
dict_rep['my_value4'] = {'foo': 5.0, }
hdr3 = Hdr_Ext.from_header_ext(dict_rep)
assert hdr3['my_value3'] == {'Value': 5.0, 'Description': ''}
assert hdr3['my_value4'] == {'foo': 5.0, 'Description': ''}


def test_to_json():
hdr = Hdr_Ext(100., '1H', dimensions=5)
Expand Down
1 change: 0 additions & 1 deletion tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,3 @@ def test_spectralwidth():
validator.validate_spectralwidth(
json.dumps(test_dict),
0.001)

0 comments on commit d2004f8

Please sign in to comment.