Skip to content

Commit

Permalink
fix: Use removeprefix instead of lstrip or ternary operator
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Dec 10, 2024
1 parent 0218f4f commit 5c55606
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
5 changes: 1 addition & 4 deletions fmriprep/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ def _to_gb(value):
units = value[len(digits) :] or 'M'
return int(digits) * scale[units[0]]

def _drop_sub(value):
return value[4:] if value.startswith('sub-') else value

def _process_value(value):
import bids

Expand Down Expand Up @@ -205,7 +202,7 @@ def _slice_time_ref(value, parser):
'--participant_label',
action='store',
nargs='+',
type=_drop_sub,
type=lambda label: label.removeprefix('sub-'),
help='A space delimited list of participant identifiers or a single '
'identifier (the sub- prefix can be removed)',
)
Expand Down
8 changes: 4 additions & 4 deletions fmriprep/reports/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def generate_reports(

errors = []
for subject_label in subject_list:
subject_label = subject_label.removeprefix('sub-')
# The number of sessions is intentionally not based on session_list but
# on the total number of sessions, because I want the final derivatives
# folder to be the same whether sessions were run one at a time or all-together.
Expand All @@ -95,7 +96,7 @@ def generate_reports(
else:
# Beyond a threshold, we separate the anatomical report from the functional.
bootstrap_file = data.load('reports-spec-anat.yml')
html_report = f'sub-{subject_label.lstrip("sub-")}_anat.html'
html_report = f'sub-{subject_label}_anat.html'

report_error = run_reports(
output_dir,
Expand All @@ -121,12 +122,11 @@ def generate_reports(
subject=subject_label, **filters
)

# Drop ses- prefixes
session_list = [ses[4:] if ses.startswith('ses-') else ses for ses in session_list]
session_list = [ses.removeprefix('ses-') for ses in session_list]

for session_label in session_list:
bootstrap_file = data.load('reports-spec-func.yml')
html_report = f'sub-{subject_label.lstrip("sub-")}_ses-{session_label}_func.html'
html_report = f'sub-{subject_label}_ses-{session_label}_func.html'

report_error = run_reports(
output_dir,
Expand Down
2 changes: 1 addition & 1 deletion fmriprep/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def validate_input_dir(exec_env, bids_dir, participant_label, need_T1w=True):
# Limit validation only to data from requested participants
if participant_label:
all_subs = {s.name[4:] for s in bids_dir.glob('sub-*')}
selected_subs = {s[4:] if s.startswith('sub-') else s for s in participant_label}
selected_subs = {s.removeprefix('sub-') for s in participant_label}

Check warning on line 220 in fmriprep/utils/bids.py

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/bids.py#L220

Added line #L220 was not covered by tests
bad_labels = selected_subs.difference(all_subs)
if bad_labels:
error_msg = (
Expand Down

0 comments on commit 5c55606

Please sign in to comment.