Skip to content

Commit

Permalink
RF: Add sanitized_id field to FieldmapEstimation
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jun 6, 2024
1 parent 6fe5b38 commit 743bb1b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
10 changes: 8 additions & 2 deletions sdcflows/fieldmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,18 @@ class FieldmapEstimation:
bids_id = attr.ib(default=None, kw_only=True, type=str, on_setattr=_id_setter)
"""The unique ``B0FieldIdentifier`` field of this fieldmap."""

sanitized_id = attr.ib(init=False, repr=False)
"""Sanitized version of the bids_id with special characters replaced by underscores."""

_wf = attr.ib(init=False, default=None, repr=False)
"""Internal pointer to a workflow."""

def __attrs_post_init__(self):
"""Determine the intended fieldmap estimation type and check for data completeness."""
# Provide a sanitized identifier that can be used in cases where
# special characters are not allowed.
self.sanitized_id = re.sub(r'[^a-zA-Z0-9]', '_', self.bids_id)

suffix_list = [f.suffix for f in self.sources]
suffix_set = set(suffix_list)

Expand Down Expand Up @@ -446,8 +453,7 @@ def get_workflow(self, set_inputs=True, **kwargs):
return self._wf

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

if self.method in (EstimatorType.MAPPED, EstimatorType.PHASEDIFF):
from .workflows.fit.fieldmap import init_fmap_wf
Expand Down
11 changes: 4 additions & 7 deletions sdcflows/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
# https://www.nipreps.org/community/licensing/
#
"""Estimate fieldmaps for :abbr:`SDC (susceptibility distortion correction)`."""
import re

from nipype import logging
from nipype.pipeline import engine as pe
from nipype.interfaces import utility as niu
Expand Down Expand Up @@ -108,7 +106,6 @@ def init_fmap_preproc_wf(
)

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

out_map = pe.Node(
niu.IdentityInterface(fields=out_fields), name=f"out_{clean_bids_id}"
niu.IdentityInterface(fields=out_fields), name=f"out_{estimator.bids_id}"
)
out_map.inputs.fmap_id = estimator.bids_id

fmap_derivatives_wf = init_fmap_derivatives_wf(
output_dir=str(output_dir),
write_coeff=True,
bids_fmap_id=estimator.bids_id,
name=f"fmap_derivatives_wf_{clean_bids_id}",
name=f"fmap_derivatives_wf_{estimator.sanitized_id}",
)
fmap_derivatives_wf.inputs.inputnode.source_files = source_files
fmap_derivatives_wf.inputs.inputnode.fmap_meta = [
Expand All @@ -138,15 +135,15 @@ def init_fmap_preproc_wf(
output_dir=str(output_dir),
fmap_type=str(estimator.method).rpartition(".")[-1].lower(),
bids_fmap_id=estimator.bids_id,
name=f"fmap_reports_wf_{clean_bids_id}",
name=f"fmap_reports_wf_{estimator.sanitized_id}",
)
fmap_reports_wf.inputs.inputnode.source_files = source_files

if estimator.method not in (EstimatorType.MAPPED, EstimatorType.PHASEDIFF):
fields = INPUT_FIELDS[estimator.method]
inputnode = pe.Node(
niu.IdentityInterface(fields=fields),
name=f"in_{clean_bids_id}",
name=f"in_{estimator.sanitized_id}",
)
# fmt:off
workflow.connect([
Expand Down
8 changes: 2 additions & 6 deletions sdcflows/workflows/fit/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

def init_sdcflows_wf():
"""Create a multi-subject, multi-estimator *SDCFlows* workflow."""
import re

from nipype.pipeline.engine import Workflow
from niworkflows.utils.bids import collect_participants

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

for subject, sub_estimators in estimators_record.items():
for estim in sub_estimators:
clean_bids_id = re.sub(r'[^a-zA-Z0-9]', '', estim.bids_id)

estim_wf = estim.get_workflow(
omp_nthreads=config.nipype.omp_nthreads,
sloppy=False,
Expand All @@ -65,7 +61,7 @@ def init_sdcflows_wf():
output_dir=config.execution.output_dir,
bids_fmap_id=estim.bids_id,
write_coeff=True,
name=f"fmap_derivatives_{clean_bids_id}",
name=f"fmap_derivatives_{estim.sanitized_id}",
)

source_paths = [
Expand All @@ -80,7 +76,7 @@ def init_sdcflows_wf():
fmap_type=estim.method,
output_dir=config.execution.output_dir,
bids_fmap_id=estim.bids_id,
name=f"fmap_reports_{clean_bids_id}",
name=f"fmap_reports_{estim.sanitized_id}",
)
reportlets_wf.inputs.inputnode.source_files = source_paths

Expand Down
6 changes: 1 addition & 5 deletions sdcflows/workflows/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
"""Test the base workflow."""
from pathlib import Path
import os
import re

import pytest

from sdcflows import fieldmaps as fm
from sdcflows.utils.wrangler import find_estimators
from sdcflows.workflows.base import init_fmap_preproc_wf
Expand Down Expand Up @@ -58,8 +55,7 @@ def test_fmap_wf(tmpdir, workdir, outdir, bids_layouts, dataset, subject):
if estimator.method != fm.EstimatorType.PEPOLAR:
continue

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

Expand Down

0 comments on commit 743bb1b

Please sign in to comment.