Skip to content

Commit c3ee587

Browse files
committed
FIX: Hash multi session runs, filter by session when collecting
1 parent 94a01ac commit c3ee587

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/smriprep/utils/misc.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,20 @@ def fs_isRunning(subjects_dir, subject_id, mtime_tol=86400, logger=None):
9595
if logger:
9696
logger.warn(f'Removed "IsRunning*" files found under {subj_dir}')
9797
return subjects_dir
98+
99+
100+
def hash_list(lst: list[str], digest_size: int = 2) -> str:
101+
"""
102+
Hash a list of strings into a string.
103+
104+
Example
105+
-------
106+
>>> hash_list(['a', 'b', 'c'])
107+
'1279'
108+
>>> hash_list(['a', 'b', 'c'], digest_size=3)
109+
'fb7bf6'
110+
111+
"""
112+
from hashlib import blake2b
113+
114+
return blake2b(','.join(lst).encode('utf-8'), digest_size=digest_size).hexdigest()

src/smriprep/workflows/base.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,14 @@ def init_smriprep_wf(
182182

183183
name = f'sub-{subject_id}_wf'
184184
if session_ids:
185-
if isinstance(session_ids, str):
186-
ses_str = session_ids
185+
ses_str = session_ids
186+
if isinstance(session_ids, list):
187+
if len(session_ids) == 1:
188+
ses_str = session_ids[0]
189+
else:
190+
from smriprep.utils.misc import hash_list
191+
192+
ses_str = hash_list(session_ids, digest_size=2)
187193

188194
name = f'sub-{subject_id}_ses-{ses_str}_wf'
189195

@@ -358,7 +364,9 @@ def init_single_subject_wf(
358364
'flair': [],
359365
}
360366
else:
361-
subject_data = collect_data(layout, subject_id, bids_filters=bids_filters)[0]
367+
subject_data = collect_data(
368+
layout, subject_id, session_id=session_id, bids_filters=bids_filters
369+
)[0]
362370

363371
if not subject_data['t1w']:
364372
raise Exception(
@@ -492,9 +500,17 @@ def _prefix(subject_id, session_id=None):
492500
"""Create FreeSurfer subject ID."""
493501
if not subject_id.startswith('sub-'):
494502
subject_id = f'sub-{subject_id}'
495-
if session_id and not isinstance(session_id, list):
496-
# For now, drop ses label if multiple sessions
497-
if not session_id.startswith('ses-'):
498-
session_id = f'ses-{session_id}'
499-
subject_id = f'{subject_id}_{session_id}'
503+
504+
if session_id:
505+
ses_str = session_id
506+
if isinstance(session_id, list):
507+
if len(session_id) == 1:
508+
ses_str = session_id[0]
509+
else:
510+
from smriprep.utils.misc import hash_list
511+
512+
ses_str = f'multi{hash_list(session_id, digest_size=2)}'
513+
if not ses_str.startswith('ses-'):
514+
ses_str = f'ses-{ses_str}'
515+
subject_id += f'_{ses_str}'
500516
return subject_id

0 commit comments

Comments
 (0)