Skip to content

Commit 6eae51f

Browse files
committed
enh: de/serialize processing groups
1 parent 85a9118 commit 6eae51f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

fmriprep/config.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ def load(cls, settings, init=True, ignore=None):
232232
else:
233233
setattr(cls, k, Path(v).absolute())
234234
elif hasattr(cls, k):
235+
match k:
236+
# Handle special deserializations
237+
case 'processing_groups':
238+
v = _deserialize_pg(v)
239+
case _:
240+
pass
235241
setattr(cls, k, v)
236242

237243
if init:
@@ -785,6 +791,10 @@ def get(flat=False):
785791
'nipype': nipype.get(),
786792
'seeds': seeds.get(),
787793
}
794+
795+
if (pg := settings['execution'].get('processing_groups')):
796+
settings['execution']['processing_groups'] = _serialize_pg(pg)
797+
788798
if not flat:
789799
return settings
790800

@@ -836,3 +846,50 @@ def init_spaces(checkpoint=True):
836846

837847
# Make the SpatialReferences object available
838848
workflow.spaces = spaces
849+
850+
def _serialize_pg(value: list[tuple[str, list[str] | None]]) -> list[str]:
851+
"""
852+
Serialize a list of participant-session tuples to be TOML-compatible.
853+
854+
Examples
855+
--------
856+
>>> _serialize_pg([('01', ['pre']), ('01', ['post'])])
857+
['01:pre', '01:post']
858+
>>> _serialize_pg([('01', ['pre', 'post']), ('02', ['post'])])
859+
['01:pre,post', '02:post']
860+
>>> _serialize_pg([('01', None), ('02', ['pre'])])
861+
['01', '02:pre']
862+
"""
863+
serial = []
864+
for val in value:
865+
if val[1] is None:
866+
serial.append(val[0])
867+
else:
868+
if not isinstance(val[1], list):
869+
val[1] = [val[1]]
870+
serial.append(f'{val[0]}:{",".join(val[1])}')
871+
return serial
872+
873+
874+
def _deserialize_pg(value: list[str]) -> list[tuple[str, list[str] | None]]:
875+
"""
876+
Deserialize a list of participant-session tuples to be TOML-compatible.
877+
878+
Examples
879+
--------
880+
>>> _deserialize_pg(['01:pre', '01:post'])
881+
[('01', ['pre']), ('01', ['post'])]
882+
>>> _deserialize_pg(['01:pre,post', '02:post'])
883+
[('01', ['pre', 'post']), ('02', ['post'])]
884+
>>> _deserialize_pg(['01', '02:pre'])
885+
[('01', None), ('02', ['pre'])]
886+
"""
887+
deserial = []
888+
for val in value:
889+
vals = val.split(':', 1)
890+
if len(vals) == 1:
891+
vals.append(None)
892+
else:
893+
vals[1] = vals[1].split(',')
894+
deserial.append(tuple(vals))
895+
return deserial

0 commit comments

Comments
 (0)