Skip to content

Commit

Permalink
Merge pull request #647 from dbic/pr-540
Browse files Browse the repository at this point in the history
strip non-alphanumeric from session ids too
  • Loading branch information
yarikoptic authored Feb 21, 2023
2 parents c6c3f3d + 3d668a6 commit 260f0bc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
39 changes: 29 additions & 10 deletions heudiconv/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from random import sample
from glob import glob
import errno
import warnings

from .external.pydicom import dcm

Expand Down Expand Up @@ -502,7 +503,7 @@ def get_formatted_scans_key_row(dcm_fn):


def convert_sid_bids(subject_id):
"""Strips any non-BIDS compliant characters within subject_id
"""Shim for stripping any non-BIDS compliant characters within subject_id
Parameters
----------
Expand All @@ -515,15 +516,10 @@ def convert_sid_bids(subject_id):
subject_id : string
Original subject ID
"""
cleaner = lambda y: ''.join([x for x in y if x.isalnum()])
sid = cleaner(subject_id)
if not sid:
raise ValueError(
"Subject ID became empty after cleanup. Please provide manually "
"a suitable alphanumeric subject ID")
lgr.warning('{0} contained nonalphanumeric character(s), subject '
'ID was cleaned to be {1}'.format(subject_id, sid))
return sid, subject_id
warnings.warn('convert_sid_bids() is deprecated, '
'please use sanitize_label() instead.',
DeprecationWarning)
return sanitize_label(subject_id)


def get_shim_setting(json_file):
Expand Down Expand Up @@ -1028,3 +1024,26 @@ def suffix(self):
@property
def extension(self):
return self._extension


def sanitize_label(label):
"""Strips any non-BIDS compliant characters within label
Parameters
----------
label : string
Returns
-------
clean_label : string
New, sanitized label
"""
clean_label = ''.join(x for x in label if x.isalnum())
if not clean_label:
raise ValueError(
"Label became empty after cleanup. Please provide manually "
"a suitable alphanumeric label.")
if clean_label != label:
lgr.warning('%r label contained non-alphanumeric character(s), it '
'was cleaned to be %r', label, clean_label)
return clean_label
7 changes: 4 additions & 3 deletions heudiconv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
file_md5sum
)
from .bids import (
convert_sid_bids,
sanitize_label,
populate_bids_templates,
populate_intended_for,
save_scans_key,
Expand Down Expand Up @@ -102,8 +102,9 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
if not sid:
raise ValueError(
"BIDS requires alphanumeric subject ID. Got an empty value")
if not sid.isalnum(): # alphanumeric only
sid, old_sid = convert_sid_bids(sid)
sid = sanitize_label(sid)
if ses:
ses = sanitize_label(ses)

if not anon_sid:
anon_sid = sid
Expand Down
6 changes: 6 additions & 0 deletions heudiconv/tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
AllowedCriteriaForFmapAssignment,
KeyInfoForForce,
BIDSFile,
sanitize_label,
)
from heudiconv.cli.run import main as runner

Expand Down Expand Up @@ -1155,3 +1156,8 @@ def test_ME_mag_phase_conversion(tmpdir, subID='MEGRE', heuristic='bids_ME.py'):
% (subID, subID, e, part, ext)
)


def test_sanitize_label():
assert sanitize_label('az XZ-@09') == 'azXZ09'
with pytest.raises(ValueError):
sanitize_label(' @ ')

0 comments on commit 260f0bc

Please sign in to comment.