Skip to content

Commit

Permalink
Merge pull request #477 from tsalo/rec-to-part
Browse files Browse the repository at this point in the history
ENH: Replace rec entity with part entity for complex-valued data
  • Loading branch information
yarikoptic authored Dec 8, 2020
2 parents 1413824 + b1a3e53 commit b6eedc7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
21 changes: 12 additions & 9 deletions heudiconv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,

def update_complex_name(metadata, filename, suffix):
"""
Insert `_rec-<magnitude|phase>` entity into filename if data are from a
Insert `_part-<mag|phase>` entity into filename if data are from a
sequence with magnitude/phase part.
Parameters
Expand All @@ -257,15 +257,18 @@ def update_complex_name(metadata, filename, suffix):
Updated filename with rec entity added in appropriate position.
"""
# Some scans separate magnitude/phase differently
unsupported_types = ['_bold', '_phase',
# A small note: _phase is deprecated, but this may add part-mag to
# magnitude data while leaving phase data with a separate suffix,
# depending on how one sets up their heuristic.
unsupported_types = ['_phase',
'_magnitude', '_magnitude1', '_magnitude2',
'_phasediff', '_phase1', '_phase2']
if any(ut in filename for ut in unsupported_types):
return filename

# Check to see if it is magnitude or phase part:
if 'M' in metadata.get('ImageType'):
mag_or_phase = 'magnitude'
mag_or_phase = 'mag'
elif 'P' in metadata.get('ImageType'):
mag_or_phase = 'phase'
else:
Expand All @@ -275,19 +278,19 @@ def update_complex_name(metadata, filename, suffix):
filetype = '_' + filename.split('_')[-1]

# Insert rec label
if not ('_rec-%s' % mag_or_phase) in filename:
# If "_rec-" is specified, prepend the 'mag_or_phase' value.
if '_rec-' in filename:
if not ('_part-%s' % mag_or_phase) in filename:
# If "_part-" is specified, prepend the 'mag_or_phase' value.
if '_part-' in filename:
raise BIDSError(
"Reconstruction label for images will be automatically set, "
"Part label for images will be automatically set, "
"remove from heuristic"
)

# Insert it **before** the following string(s), whichever appears first.
for label in ['_dir', '_run', '_mod', '_echo', '_recording', '_proc', '_space', filetype]:
for label in ['_recording', '_proc', '_space', filetype]:
if (label == filetype) or (label in filename):
filename = filename.replace(
label, "_rec-%s%s" % (mag_or_phase, label)
label, "_part-%s%s" % (mag_or_phase, label)
)
break

Expand Down
8 changes: 4 additions & 4 deletions heudiconv/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

def test_update_complex_name():
"""Unit testing for heudiconv.convert.update_complex_name(), which updates
filenames with the rec field if appropriate.
filenames with the part field if appropriate.
"""
# Standard name update
fn = 'sub-X_ses-Y_task-Z_run-01_sbref'
metadata = {'ImageType': ['ORIGINAL', 'PRIMARY', 'P', 'MB', 'TE3', 'ND', 'MOSAIC']}
suffix = 3
out_fn_true = 'sub-X_ses-Y_task-Z_rec-phase_run-01_sbref'
out_fn_true = 'sub-X_ses-Y_task-Z_run-01_part-phase_sbref'
out_fn_test = update_complex_name(metadata, fn, suffix)
assert out_fn_test == out_fn_true
# Catch an unsupported type and *do not* update
Expand All @@ -26,12 +26,12 @@ def test_update_complex_name():
# Data type is missing from metadata so use suffix
fn = 'sub-X_ses-Y_task-Z_run-01_sbref'
metadata = {'ImageType': ['ORIGINAL', 'PRIMARY', 'MB', 'TE3', 'ND', 'MOSAIC']}
out_fn_true = 'sub-X_ses-Y_task-Z_rec-3_run-01_sbref'
out_fn_true = 'sub-X_ses-Y_task-Z_run-01_part-3_sbref'
out_fn_test = update_complex_name(metadata, fn, suffix)
assert out_fn_test == out_fn_true
# Catch existing field with value that *does not match* metadata
# and raise Exception
fn = 'sub-X_ses-Y_task-Z_rec-magnitude_run-01_sbref'
fn = 'sub-X_ses-Y_task-Z_run-01_part-mag_sbref'
metadata = {'ImageType': ['ORIGINAL', 'PRIMARY', 'P', 'MB', 'TE3', 'ND', 'MOSAIC']}
suffix = 3
with pytest.raises(BIDSError):
Expand Down

0 comments on commit b6eedc7

Please sign in to comment.