Skip to content

Commit c674fc1

Browse files
committed
RF: Add sanitized_id field to FieldmapEstimation
1 parent 6fe5b38 commit c674fc1

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

sdcflows/fieldmaps.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ class FieldmapEstimation:
300300
bids_id = attr.ib(default=None, kw_only=True, type=str, on_setattr=_id_setter)
301301
"""The unique ``B0FieldIdentifier`` field of this fieldmap."""
302302

303+
sanitized_id = attr.ib(init=False, repr=False)
304+
"""Sanitized version of the bids_id with special characters replaced by underscores."""
305+
303306
_wf = attr.ib(init=False, default=None, repr=False)
304307
"""Internal pointer to a workflow."""
305308

@@ -436,6 +439,10 @@ def __attrs_post_init__(self):
436439
for intent_file in intents_meta:
437440
_intents[intent_file].add(self.bids_id)
438441

442+
# Provide a sanitized identifier that can be used in cases where
443+
# special characters are not allowed.
444+
self.sanitized_id = re.sub(r'[^a-zA-Z0-9]', '_', self.bids_id)
445+
439446
def paths(self):
440447
"""Return a tuple of paths that are sorted."""
441448
return tuple(sorted(str(f.path) for f in self.sources))
@@ -446,8 +453,7 @@ def get_workflow(self, set_inputs=True, **kwargs):
446453
return self._wf
447454

448455
# Override workflow name
449-
clean_bids_id = re.sub(r'[^a-zA-Z0-9]', '', self.bids_id)
450-
kwargs["name"] = f"wf_{clean_bids_id}"
456+
kwargs["name"] = f"wf_{self.sanitized_id}"
451457

452458
if self.method in (EstimatorType.MAPPED, EstimatorType.PHASEDIFF):
453459
from .workflows.fit.fieldmap import init_fmap_wf

sdcflows/workflows/base.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
# https://www.nipreps.org/community/licensing/
2222
#
2323
"""Estimate fieldmaps for :abbr:`SDC (susceptibility distortion correction)`."""
24-
import re
25-
2624
from nipype import logging
2725
from nipype.pipeline import engine as pe
2826
from nipype.interfaces import utility as niu
@@ -108,7 +106,6 @@ def init_fmap_preproc_wf(
108106
)
109107

110108
for n, estimator in enumerate(estimators, 1):
111-
clean_bids_id = re.sub(r'[^a-zA-Z0-9]', '', estimator.bids_id)
112109
est_wf = estimator.get_workflow(
113110
omp_nthreads=omp_nthreads,
114111
debug=debug,
@@ -119,15 +116,15 @@ def init_fmap_preproc_wf(
119116
]
120117

121118
out_map = pe.Node(
122-
niu.IdentityInterface(fields=out_fields), name=f"out_{clean_bids_id}"
119+
niu.IdentityInterface(fields=out_fields), name=f"out_{estimator.bids_id}"
123120
)
124121
out_map.inputs.fmap_id = estimator.bids_id
125122

126123
fmap_derivatives_wf = init_fmap_derivatives_wf(
127124
output_dir=str(output_dir),
128125
write_coeff=True,
129126
bids_fmap_id=estimator.bids_id,
130-
name=f"fmap_derivatives_wf_{clean_bids_id}",
127+
name=f"fmap_derivatives_wf_{estimator.sanitized_id}",
131128
)
132129
fmap_derivatives_wf.inputs.inputnode.source_files = source_files
133130
fmap_derivatives_wf.inputs.inputnode.fmap_meta = [
@@ -138,15 +135,15 @@ def init_fmap_preproc_wf(
138135
output_dir=str(output_dir),
139136
fmap_type=str(estimator.method).rpartition(".")[-1].lower(),
140137
bids_fmap_id=estimator.bids_id,
141-
name=f"fmap_reports_wf_{clean_bids_id}",
138+
name=f"fmap_reports_wf_{estimator.sanitized_id}",
142139
)
143140
fmap_reports_wf.inputs.inputnode.source_files = source_files
144141

145142
if estimator.method not in (EstimatorType.MAPPED, EstimatorType.PHASEDIFF):
146143
fields = INPUT_FIELDS[estimator.method]
147144
inputnode = pe.Node(
148145
niu.IdentityInterface(fields=fields),
149-
name=f"in_{clean_bids_id}",
146+
name=f"in_{estimator.sanitized_id}",
150147
)
151148
# fmt:off
152149
workflow.connect([

sdcflows/workflows/fit/base.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
def init_sdcflows_wf():
2727
"""Create a multi-subject, multi-estimator *SDCFlows* workflow."""
28-
import re
29-
3028
from nipype.pipeline.engine import Workflow
3129
from niworkflows.utils.bids import collect_participants
3230

@@ -53,8 +51,6 @@ def init_sdcflows_wf():
5351

5452
for subject, sub_estimators in estimators_record.items():
5553
for estim in sub_estimators:
56-
clean_bids_id = re.sub(r'[^a-zA-Z0-9]', '', estim.bids_id)
57-
5854
estim_wf = estim.get_workflow(
5955
omp_nthreads=config.nipype.omp_nthreads,
6056
sloppy=False,
@@ -65,7 +61,7 @@ def init_sdcflows_wf():
6561
output_dir=config.execution.output_dir,
6662
bids_fmap_id=estim.bids_id,
6763
write_coeff=True,
68-
name=f"fmap_derivatives_{clean_bids_id}",
64+
name=f"fmap_derivatives_{estim.sanitized_id}",
6965
)
7066

7167
source_paths = [
@@ -80,7 +76,7 @@ def init_sdcflows_wf():
8076
fmap_type=estim.method,
8177
output_dir=config.execution.output_dir,
8278
bids_fmap_id=estim.bids_id,
83-
name=f"fmap_reports_{clean_bids_id}",
79+
name=f"fmap_reports_{estim.sanitized_id}",
8480
)
8581
reportlets_wf.inputs.inputnode.source_files = source_paths
8682

sdcflows/workflows/tests/test_base.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
"""Test the base workflow."""
2424
from pathlib import Path
2525
import os
26-
import re
27-
2826
import pytest
29-
3027
from sdcflows import fieldmaps as fm
3128
from sdcflows.utils.wrangler import find_estimators
3229
from sdcflows.workflows.base import init_fmap_preproc_wf
@@ -58,8 +55,7 @@ def test_fmap_wf(tmpdir, workdir, outdir, bids_layouts, dataset, subject):
5855
if estimator.method != fm.EstimatorType.PEPOLAR:
5956
continue
6057

61-
clean_bids_id = re.sub(r'[^a-zA-Z0-9]', '', estimator.bids_id)
62-
inputnode = wf.get_node(f"in_{clean_bids_id}")
58+
inputnode = wf.get_node(f"in_{estimator.bids_id}")
6359
inputnode.inputs.in_data = [str(f.path) for f in estimator.sources]
6460
inputnode.inputs.metadata = [f.metadata for f in estimator.sources]
6561

0 commit comments

Comments
 (0)