Skip to content

Commit

Permalink
WIP/STY: Ruff bold workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
mgxd committed May 15, 2024
1 parent 2778b41 commit 1743edf
Show file tree
Hide file tree
Showing 7 changed files with 618 additions and 582 deletions.
1 change: 0 additions & 1 deletion nibabies/workflows/bold/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .base import init_func_preproc_wf
228 changes: 114 additions & 114 deletions nibabies/workflows/bold/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from nibabies.interfaces.workbench import VolumeLabelImport


def init_subcortical_rois_wf(*, name="subcortical_rois_wf"):
def init_subcortical_rois_wf(*, name='subcortical_rois_wf'):
"""
Refine segmentations into volumes of expected CIFTI subcortical structures.
Expand Down Expand Up @@ -46,14 +46,14 @@ def init_subcortical_rois_wf(*, name="subcortical_rois_wf"):
# For now, just use the aseg

workflow = Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(fields=["MNIInfant_aseg"]), name="inputnode")
inputnode = pe.Node(niu.IdentityInterface(fields=['MNIInfant_aseg']), name='inputnode')
outputnode = pe.Node(
niu.IdentityInterface(fields=["MNIInfant_rois", "MNI152_rois"]),
name="outputnode",
niu.IdentityInterface(fields=['MNIInfant_rois', 'MNI152_rois']),
name='outputnode',
)
# Fetch the HCP volumetric template
tpl_rois = get_template(
"MNI152NLin6Asym", resolution=2, atlas="HCP", suffix="dseg", raise_empty=True
'MNI152NLin6Asym', resolution=2, atlas='HCP', suffix='dseg', raise_empty=True
)
outputnode.inputs.MNI152_rois = tpl_rois

Expand All @@ -68,30 +68,30 @@ def init_subcortical_rois_wf(*, name="subcortical_rois_wf"):
# )

map_labels = pe.Node(
MapLabels(mappings_file=load_data("FreeSurferLabelRemappings.json")),
MapLabels(mappings_file=load_data('FreeSurferLabelRemappings.json')),
name='map_labels',
)

refine_bold_rois = pe.Node(
VolumeLabelImport(
label_list_file=load_data("FreeSurferSubcorticalLabelTableLut.txt"),
label_list_file=load_data('FreeSurferSubcorticalLabelTableLut.txt'),
discard_others=True,
),
name="refine_bold_rois",
name='refine_bold_rois',
)

# fmt: off
workflow.connect([
(inputnode, map_labels, [("MNIInfant_aseg", "in_file")]),
(map_labels, refine_bold_rois, [("out_file", "in_file")]),
(inputnode, map_labels, [('MNIInfant_aseg', 'in_file')]),
(map_labels, refine_bold_rois, [('out_file', 'in_file')]),
# (applywarp_tpl, refine_std_rois, [("out_file", "in_file")]),
(refine_bold_rois, outputnode, [("out_file", "MNIInfant_rois")]),
(refine_bold_rois, outputnode, [('out_file', 'MNIInfant_rois')]),
])
# fmt: on
return workflow


def init_subcortical_mni_alignment_wf(*, vol_sigma=0.8, name="subcortical_mni_alignment_wf"):
def init_subcortical_mni_alignment_wf(*, vol_sigma=0.8, name='subcortical_mni_alignment_wf'):
"""
Align individual subcortical structures into MNI space.
Expand Down Expand Up @@ -139,29 +139,29 @@ def init_subcortical_mni_alignment_wf(*, vol_sigma=0.8, name="subcortical_mni_al
)

# reuse saved atlas to atlas transform
atlas_xfm = load_data("MNIInfant_to_MNI1526NLinAsym.mat")
atlas_xfm = load_data('MNIInfant_to_MNI1526NLinAsym.mat')
inputnode = pe.Node(
niu.IdentityInterface(fields=["MNIInfant_bold", "MNIInfant_rois", "MNI152_rois"]),
name="inputnode",
niu.IdentityInterface(fields=['MNIInfant_bold', 'MNIInfant_rois', 'MNI152_rois']),
name='inputnode',
)
outputnode = pe.Node(
niu.IdentityInterface(fields=["subcortical_volume", "subcortical_labels"]),
name="outputnode",
niu.IdentityInterface(fields=['subcortical_volume', 'subcortical_labels']),
name='outputnode',
)

applyxfm_atlas = pe.Node(fsl.ApplyXFM(in_matrix_file=atlas_xfm), name="applyxfm_atlas")
applyxfm_atlas = pe.Node(fsl.ApplyXFM(in_matrix_file=atlas_xfm), name='applyxfm_atlas')
vol_resample = pe.Node(
VolumeAffineResample(method="ENCLOSING_VOXEL", flirt=True, affine=atlas_xfm),
name="vol_resample",
VolumeAffineResample(method='ENCLOSING_VOXEL', flirt=True, affine=atlas_xfm),
name='vol_resample',
)
subj_rois = pe.Node(VolumeAllLabelsToROIs(label_map=1), name="subj_rois")
split_rois = pe.Node(fsl.Split(dimension="t"), name="split_rois")
atlas_rois = pe.Node(VolumeAllLabelsToROIs(label_map=1), name="atlas_rois")
split_atlas_rois = pe.Node(fsl.Split(dimension="t"), name="split_atlas_rois")
atlas_labels = pe.Node(VolumeLabelExportTable(label_map=1), name="atlas_labels")
subj_rois = pe.Node(VolumeAllLabelsToROIs(label_map=1), name='subj_rois')
split_rois = pe.Node(fsl.Split(dimension='t'), name='split_rois')
atlas_rois = pe.Node(VolumeAllLabelsToROIs(label_map=1), name='atlas_rois')
split_atlas_rois = pe.Node(fsl.Split(dimension='t'), name='split_atlas_rois')
atlas_labels = pe.Node(VolumeLabelExportTable(label_map=1), name='atlas_labels')
parse_labels = pe.Node(
niu.Function(function=parse_roi_labels, output_names=["structures", "label_ids"]),
name="parse_labels",
niu.Function(function=parse_roi_labels, output_names=['structures', 'label_ids']),
name='parse_labels',
)

# The following is wrapped in a for-loop, iterating across each roi
Expand All @@ -171,141 +171,141 @@ def init_subcortical_mni_alignment_wf(*, vol_sigma=0.8, name="subcortical_mni_al
searchr_x=[-20, 20],
searchr_y=[-20, 20],
searchr_z=[-20, 20],
interp="nearestneighbour",
interp='nearestneighbour',
),
name="roi2atlas",
iterfield=["in_file", "reference"],
name='roi2atlas',
iterfield=['in_file', 'reference'],
)
applyxfm_roi = pe.MapNode(
fsl.ApplyXFM(interp="spline"),
iterfield=["reference", "in_matrix_file"],
name="applyxfm_roi",
fsl.ApplyXFM(interp='spline'),
iterfield=['reference', 'in_matrix_file'],
name='applyxfm_roi',
mem_gb=4,
)
bold_mask_roi = pe.MapNode(
fsl.ApplyMask(),
iterfield=["in_file", "mask_file"],
name="bold_mask_roi",
iterfield=['in_file', 'mask_file'],
name='bold_mask_roi',
)
mul_roi = pe.MapNode(
fsl.BinaryMaths(operation="mul"),
iterfield=["in_file", "operand_value"],
name="mul_roi",
fsl.BinaryMaths(operation='mul'),
iterfield=['in_file', 'operand_value'],
name='mul_roi',
)
mul_atlas_roi = pe.MapNode(
fsl.BinaryMaths(operation="mul"),
iterfield=["in_file", "operand_value"],
name="mul_atlas_roi",
fsl.BinaryMaths(operation='mul'),
iterfield=['in_file', 'operand_value'],
name='mul_atlas_roi',
)
vol_label = pe.MapNode(
VolumeLabelImport(drop_unused_labels=True),
iterfield=["in_file"],
name="vol_label",
iterfield=['in_file'],
name='vol_label',
)
vol_atlas_label = pe.MapNode(
VolumeLabelImport(drop_unused_labels=True),
iterfield=["in_file"],
name="vol_atlas_label",
iterfield=['in_file'],
name='vol_atlas_label',
)
create_dtseries = pe.MapNode(
CiftiCreateDenseTimeseries(),
iterfield=["volume_data", "volume_structure_labels"],
name="create_dtseries",
iterfield=['volume_data', 'volume_structure_labels'],
name='create_dtseries',
)
create_label = pe.MapNode(
CiftiCreateLabel(),
iterfield=["volume_label", "structure_label_volume"],
name="create_label",
iterfield=['volume_label', 'structure_label_volume'],
name='create_label',
)
dilate = pe.MapNode(
CiftiDilate(direction="COLUMN", surface_distance=0, volume_distance=10),
iterfield=["in_file"],
name="dilate",
CiftiDilate(direction='COLUMN', surface_distance=0, volume_distance=10),
iterfield=['in_file'],
name='dilate',
)
resample = pe.MapNode(
CiftiResample(
direction="COLUMN",
template_direction="COLUMN",
surface_method="ADAP_BARY_AREA",
volume_method="CUBIC",
direction='COLUMN',
template_direction='COLUMN',
surface_method='ADAP_BARY_AREA',
volume_method='CUBIC',
volume_predilate=10,
),
iterfield=["in_file", "template"],
name="resample",
iterfield=['in_file', 'template'],
name='resample',
)
smooth = pe.MapNode(
CiftiSmooth(direction="COLUMN", fix_zeros_vol=True, sigma_surf=0, sigma_vol=vol_sigma),
iterfield=["in_file"],
name="smooth",
CiftiSmooth(direction='COLUMN', fix_zeros_vol=True, sigma_surf=0, sigma_vol=vol_sigma),
iterfield=['in_file'],
name='smooth',
)
separate = pe.MapNode(
CiftiSeparate(direction="COLUMN", volume_all_file="volume_all.nii.gz"),
iterfield=["in_file"],
name="separate",
CiftiSeparate(direction='COLUMN', volume_all_file='volume_all.nii.gz'),
iterfield=['in_file'],
name='separate',
)
fmt_agg_rois = pe.Node(
niu.Function(
function=format_agg_rois,
output_names=["first_image", "op_files", "op_string"],
output_names=['first_image', 'op_files', 'op_string'],
),
name="fmt_agg_rois",
name='fmt_agg_rois',
)
agg_rois = pe.Node(fsl.MultiImageMaths(), name="agg_rois")
merge_rois = pe.Node(MergeROIs(), name="merge_rois")
agg_rois = pe.Node(fsl.MultiImageMaths(), name='agg_rois')
merge_rois = pe.Node(MergeROIs(), name='merge_rois')

workflow = Workflow(name=name)
# fmt: off
workflow.connect([
(inputnode, applyxfm_atlas, [
("MNIInfant_bold", "in_file"),
("MNI152_rois", "reference")]),
('MNIInfant_bold', 'in_file'),
('MNI152_rois', 'reference')]),
(inputnode, vol_resample, [
("MNIInfant_rois", "in_file"),
("MNIInfant_rois", "flirt_source_volume")]),
('MNIInfant_rois', 'in_file'),
('MNIInfant_rois', 'flirt_source_volume')]),
(applyxfm_atlas, vol_resample, [
("out_file", "volume_space"),
("out_file", "flirt_target_volume")]),
(inputnode, subj_rois, [("MNIInfant_rois", "in_file")]),
(inputnode, atlas_rois, [("MNI152_rois", "in_file")]),
(subj_rois, split_rois, [("out_file", "in_file")]),
(atlas_rois, split_atlas_rois, [("out_file", "in_file")]),
(inputnode, atlas_labels, [("MNI152_rois", "in_file")]),
(atlas_labels, parse_labels, [("out_file", "label_file")]),
('out_file', 'volume_space'),
('out_file', 'flirt_target_volume')]),
(inputnode, subj_rois, [('MNIInfant_rois', 'in_file')]),
(inputnode, atlas_rois, [('MNI152_rois', 'in_file')]),
(subj_rois, split_rois, [('out_file', 'in_file')]),
(atlas_rois, split_atlas_rois, [('out_file', 'in_file')]),
(inputnode, atlas_labels, [('MNI152_rois', 'in_file')]),
(atlas_labels, parse_labels, [('out_file', 'label_file')]),
# for loop across ROIs
(split_rois, roi2atlas, [("out_files", "in_file")]),
(split_atlas_rois, roi2atlas, [("out_files", "reference")]),
(inputnode, applyxfm_roi, [("MNIInfant_bold", "in_file")]),
(split_atlas_rois, applyxfm_roi, [("out_files", "reference")]),
(roi2atlas, applyxfm_roi, [("out_matrix_file", "in_matrix_file")]),
(applyxfm_roi, bold_mask_roi, [("out_file", "in_file")]),
(roi2atlas, bold_mask_roi, [("out_file", "mask_file")]),
(roi2atlas, mul_roi, [("out_file", "in_file")]),
(parse_labels, mul_roi, [("label_ids", "operand_value")]),
(split_atlas_rois, mul_atlas_roi, [("out_files", "in_file")]),
(parse_labels, mul_atlas_roi, [("label_ids", "operand_value")]),
(mul_roi, vol_label, [("out_file", "in_file")]),
(atlas_labels, vol_label, [("out_file", "label_list_file")]),
(mul_atlas_roi, vol_atlas_label, [("out_file", "in_file")]),
(atlas_labels, vol_atlas_label, [("out_file", "label_list_file")]),
(bold_mask_roi, create_dtseries, [("out_file", "volume_data")]),
(vol_label, create_dtseries, [("out_file", "volume_structure_labels")]),
(split_rois, roi2atlas, [('out_files', 'in_file')]),
(split_atlas_rois, roi2atlas, [('out_files', 'reference')]),
(inputnode, applyxfm_roi, [('MNIInfant_bold', 'in_file')]),
(split_atlas_rois, applyxfm_roi, [('out_files', 'reference')]),
(roi2atlas, applyxfm_roi, [('out_matrix_file', 'in_matrix_file')]),
(applyxfm_roi, bold_mask_roi, [('out_file', 'in_file')]),
(roi2atlas, bold_mask_roi, [('out_file', 'mask_file')]),
(roi2atlas, mul_roi, [('out_file', 'in_file')]),
(parse_labels, mul_roi, [('label_ids', 'operand_value')]),
(split_atlas_rois, mul_atlas_roi, [('out_files', 'in_file')]),
(parse_labels, mul_atlas_roi, [('label_ids', 'operand_value')]),
(mul_roi, vol_label, [('out_file', 'in_file')]),
(atlas_labels, vol_label, [('out_file', 'label_list_file')]),
(mul_atlas_roi, vol_atlas_label, [('out_file', 'in_file')]),
(atlas_labels, vol_atlas_label, [('out_file', 'label_list_file')]),
(bold_mask_roi, create_dtseries, [('out_file', 'volume_data')]),
(vol_label, create_dtseries, [('out_file', 'volume_structure_labels')]),
(vol_atlas_label, create_label, [
("out_file", "volume_label"),
("out_file", "structure_label_volume")]),
(create_dtseries, dilate, [("out_file", "in_file")]),
(dilate, resample, [("out_file", "in_file")]),
(create_label, resample, [("out_file", "template")]),
(resample, smooth, [("out_file", "in_file")]),
(smooth, separate, [("out_file", "in_file")]),
('out_file', 'volume_label'),
('out_file', 'structure_label_volume')]),
(create_dtseries, dilate, [('out_file', 'in_file')]),
(dilate, resample, [('out_file', 'in_file')]),
(create_label, resample, [('out_file', 'template')]),
(resample, smooth, [('out_file', 'in_file')]),
(smooth, separate, [('out_file', 'in_file')]),
# end loop
(mul_roi, fmt_agg_rois, [("out_file", "rois")]),
(mul_roi, fmt_agg_rois, [('out_file', 'rois')]),
(fmt_agg_rois, agg_rois, [
("first_image", "in_file"),
("op_files", "operand_files"),
("op_string", "op_string")]),
(separate, merge_rois, [("volume_all_file", "in_files")]),
(merge_rois, outputnode, [("out_file", "subcortical_volume")]),
(inputnode, outputnode, [("MNI152_rois", "subcortical_labels")]),
('first_image', 'in_file'),
('op_files', 'operand_files'),
('op_string', 'op_string')]),
(separate, merge_rois, [('volume_all_file', 'in_files')]),
(merge_rois, outputnode, [('out_file', 'subcortical_volume')]),
(inputnode, outputnode, [('MNI152_rois', 'subcortical_labels')]),
])
# fmt: on
return workflow
Expand All @@ -329,13 +329,13 @@ def parse_roi_labels(label_file):
with open(label_file) as fp:
lines = fp.readlines()
if len(lines) % 2 == 1:
raise RuntimeError("Label file is incomplete or invalid")
raise RuntimeError('Label file is incomplete or invalid')
structs, label_ids = [], []
for idx, line in enumerate(lines):
if idx % 2 == 0:
structs.append(line.strip())
else:
label_ids.append(int(line.split(" ", 1)[0]))
label_ids.append(int(line.split(' ', 1)[0]))
return structs, label_ids


Expand All @@ -355,4 +355,4 @@ def format_agg_rois(rois):
op_string
"""
return rois[0], rois[1:], ("-add %s " * (len(rois) - 1)).strip()
return rois[0], rois[1:], ('-add %s ' * (len(rois) - 1)).strip()
Loading

0 comments on commit 1743edf

Please sign in to comment.