Skip to content

Commit c91b517

Browse files
committed
RF: Session stringification
1 parent 1a46fa1 commit c91b517

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/smriprep/utils/misc.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,35 @@ def fs_isRunning(subjects_dir, subject_id, mtime_tol=86400, logger=None):
9797
return subjects_dir
9898

9999

100-
def hash_list(lst: list[str], digest_size: int = 2) -> str:
100+
def stringify_sessions(lst: list[str], max_length: int = 10, digest_size: int = 2) -> str:
101101
"""
102-
Hash a list of strings into a string.
102+
Convert a list of session into a string identifier.
103+
If the list has only one element, it returns that element.
104+
If the list has more than one element, it concatenates them with '+'.
105+
If the concatenated string exceeds `max_length`, it returns a
106+
string starting with 'multi+' followed by a BLAKE2b hash of the concatenated string.
103107
104108
Example
105109
-------
106-
>>> hash_list(['a', 'b', 'c'])
107-
'1279'
108-
>>> hash_list(['a', 'b', 'c'], digest_size=3)
109-
'fb7bf6'
110+
>>> stringify_sessions(['a'])
111+
'a'
112+
>>> stringify_sessions(['a', 'b', 'c'])
113+
'a+b+c'
114+
>>> stringify_sessions(['a', 'b', 'toolong'])
115+
'multi+7c22'
116+
>>> stringify_sessions(['a', 'b', 'toolong'], max_length=12)
117+
'a+b+toolong'
118+
>>> stringify_sessions(['a', 'b', 'toolong'], digest_size=4)
119+
'multi+dd8bb349'
120+
>>>
110121
111122
"""
112-
from hashlib import blake2b
123+
if len(lst) == 1:
124+
return lst[0]
113125

114-
return blake2b(','.join(lst).encode('utf-8'), digest_size=digest_size).hexdigest()
126+
ses_str = '+'.join(lst)
127+
if len(ses_str) > max_length:
128+
from hashlib import blake2b
129+
130+
ses_str = f'multi+{blake2b(ses_str.encode(), digest_size=digest_size).hexdigest()}'
131+
return ses_str

src/smriprep/workflows/base.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,9 @@ def init_smriprep_wf(
184184
if session_ids:
185185
ses_str = session_ids
186186
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
187+
from smriprep.utils.misc import stringify_sessions
191188

192-
ses_str = hash_list(session_ids, digest_size=2)
189+
ses_str = stringify_sessions(session_ids)
193190

194191
name = f'sub-{subject_id}_ses-{ses_str}_wf'
195192

@@ -501,12 +498,9 @@ def _prefix(subject_id, session_id=None):
501498
if session_id:
502499
ses_str = session_id
503500
if isinstance(session_id, list):
504-
if len(session_id) == 1:
505-
ses_str = session_id[0]
506-
else:
507-
from smriprep.utils.misc import hash_list
501+
from smriprep.utils.misc import stringify_sessions
508502

509-
ses_str = f'multi{hash_list(session_id, digest_size=2)}'
503+
ses_str = stringify_sessions(session_id)
510504
if not ses_str.startswith('ses-'):
511505
ses_str = f'ses-{ses_str}'
512506
subject_id += f'_{ses_str}'

0 commit comments

Comments
 (0)