Skip to content

Commit

Permalink
Added MrPhoenixProtocol tests and bugfixes (but the code is still dodgy)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Jan 28, 2024
1 parent a936337 commit f9ecaa5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
12 changes: 7 additions & 5 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,21 +597,23 @@ def get_dicomfield(tagname: str, dicomfile: Path) -> Union[str, int]:
value = value if (value or value==0) else Image(dicomfile).header.get(csa)
for csatag in tagname.split('.'): # E.g. CSA tagname = 'SliceArray.Slice.instance_number.Position.Tra'
if isinstance(value, dict): # Final CSA header attributes in dictionary of dictionaries
print(f"{csa} tag: {csatag}")
value = value.get(csatag, '')
if 'value' in value: # Normal CSA (i.e. not MrPhoenixProtocol)
value = value['value']
if not isinstance(value, int):
value = str(value)
value = str(value if value is not None else '')

else:

for type in ('Series', 'Image'):
value = value if (value or value==0) else csareader.get_csa_header(dicomdata, type)['tags']
for csatag in tagname.split('.'): # E.g. CSA tagname = 'SliceArray.Slice.instance_number.Position.Tra'
if isinstance(value, dict): # Final CSA header attributes in dictionary of dictionaries
print(f"{type} tag: {csatag}")
value = value.get(csatag, '')
value = value.get(csatag, {}).get('items', '')
if isinstance(value, list) and len(value) == 1:
value = value[0]
if not isinstance(value, int):
value = str(value)
value = str(value if value not in (None,[]) else '')

if not value and value != 0 and 'Modality' not in dicomdata:
raise ValueError(f"Missing mandatory DICOM 'Modality' field in: {dicomfile}")
Expand Down
26 changes: 18 additions & 8 deletions tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_properties(self, datasource):
assert datasource.properties( 'filepath:.*/(.*?)_files/.*') == 'test' # path = [..]/pydicom/data/test_files/MR_small.dcm'
assert datasource.properties(r'filename:MR_(.*?)\.dcm') == 'small'
assert datasource.properties( 'filesize') == '9.60 kB'
assert datasource.properties( 'nrfiles') == 75
assert datasource.properties( 'nrfiles') in (75,76) # Depends on the pydicom version

def test_attributes(self, datasource, extdatasource):
assert datasource.attributes(r'PatientName:.*\^(.*?)1') == 'MR' # PatientName = 'CompressedSamples^MR1'
Expand Down Expand Up @@ -125,17 +125,27 @@ def test_get_datasource(dicomdir):

def test_get_dicomfield(dcm_file_csa):

value = bids.get_dicomfield('SeriesDescription', dcm_file_csa) # -> Standard DICOM
# -> Standard DICOM
value = bids.get_dicomfield('SeriesDescription', dcm_file_csa)
assert value == 'CBU_DTI_64D_1A'

value = bids.get_dicomfield('PhaseGradientAmplitude', dcm_file_csa) # -> CSA Series header
assert value == "{'index': 3, 'VR': 'DS', 'VM': 1, 'value': 0.0}"
# -> CSA Series header
value = bids.get_dicomfield('PhaseGradientAmplitude', dcm_file_csa)
assert value == '0.0'

value = bids.get_dicomfield('PhaseGradientAmplitude.index', dcm_file_csa)
assert value == 3
# -> CSA Image header
value = bids.get_dicomfield('B_matrix', dcm_file_csa)
assert value == ''

value = bids.get_dicomfield('B_matrix.index', dcm_file_csa) # -> CSA Image header
assert value == 77
# -> CSA MrPhoenixProtocol
value = bids.get_dicomfield('MrPhoenixProtocol.tProtocolName', dcm_file_csa)
assert value == 'CBU+AF8-DTI+AF8-64D+AF8-1A'

value = bids.get_dicomfield('MrPhoenixProtocol.sDiffusion', dcm_file_csa)
assert value == "{'lDiffWeightings': 2, 'alBValue': [None, 1000], 'lNoiseLevel': 40, 'lDiffDirections': 64, 'ulMode': 256}"

value = bids.get_dicomfield('MrPhoenixProtocol.sProtConsistencyInfo.tBaselineString', dcm_file_csa)
assert value == 'N4_VB17A_LATEST_20090307'


@pytest.mark.parametrize('template', bcoin.list_plugins()[1])
Expand Down

0 comments on commit f9ecaa5

Please sign in to comment.