Skip to content

Commit 611066f

Browse files
committed
🥅 Guardrail freesurfer_abcd_preproc
1 parent 1cbf464 commit 611066f

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

CPAC/anat_preproc/anat_preproc.py

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,8 +2905,8 @@ def fnirt_based_brain_extraction(config=None, wf_name='fnirt_based_brain_extract
29052905
return preproc
29062906

29072907

2908-
def fast_bias_field_correction(config=None, wf_name='fast_bias_field_correction'):
2909-
2908+
def fast_bias_field_correction(config=None,
2909+
wf_name='fast_bias_field_correction'):
29102910
### ABCD Harmonization - FAST bias field correction ###
29112911
# Ref: https://github.com/DCAN-Labs/DCAN-HCP/blob/master/PreFreeSurfer/PreFreeSurferPipeline.sh#L688-L694
29122912

@@ -2931,10 +2931,8 @@ def fast_bias_field_correction(config=None, wf_name='fast_bias_field_correction'
29312931

29322932
preproc.connect(inputnode, 'anat_brain',
29332933
fast_bias_field_correction, 'in_files')
2934-
29352934
preproc.connect(fast_bias_field_correction, 'restored_image',
29362935
outputnode, 'anat_brain_restore')
2937-
29382936
preproc.connect(fast_bias_field_correction, 'bias_field',
29392937
outputnode, 'bias_field')
29402938

@@ -2944,7 +2942,7 @@ def fast_bias_field_correction(config=None, wf_name='fast_bias_field_correction'
29442942

29452943
# fslmaths ${T1wFolder}/T1w_acpc_brain_mask.nii.gz -mul -1 -add 1 ${T1wFolder}/T1w_acpc_inverse_brain_mask.nii.gz
29462944
inverse_brain_mask = pe.Node(interface=fsl.ImageMaths(),
2947-
name='inverse_brain_mask')
2945+
name='inverse_brain_mask')
29482946
inverse_brain_mask.inputs.op_string = '-mul -1 -add 1'
29492947

29502948
preproc.connect(inputnode, 'anat_brain_mask',
@@ -2955,25 +2953,19 @@ def fast_bias_field_correction(config=None, wf_name='fast_bias_field_correction'
29552953
name='apply_mask')
29562954
apply_mask.inputs.op_string = '-mul %s'
29572955

2958-
preproc.connect(inputnode, 'anat_data',
2959-
apply_mask, 'in_file')
2960-
2956+
preproc.connect(inputnode, 'anat_data', apply_mask, 'in_file')
29612957
preproc.connect(inverse_brain_mask, 'out_file',
29622958
apply_mask, 'operand_files')
29632959

29642960
# fslmaths ${T1wFolder}/T1w_fast_restore.nii.gz -add ${T1wFolder}/T1w_acpc_dc_skull.nii.gz ${T1wFolder}/${T1wImage}_acpc_dc_restore
29652961
anat_restore = pe.Node(interface=fsl.MultiImageMaths(),
2966-
name='get_anat_restore')
2962+
name='get_anat_restore')
29672963
anat_restore.inputs.op_string = '-add %s'
29682964

29692965
preproc.connect(fast_bias_field_correction, 'restored_image',
29702966
anat_restore, 'in_file')
2971-
2972-
preproc.connect(apply_mask, 'out_file',
2973-
anat_restore, 'operand_files')
2974-
2975-
preproc.connect(anat_restore, 'out_file',
2976-
outputnode, 'anat_restore')
2967+
preproc.connect(apply_mask, 'out_file', anat_restore, 'operand_files')
2968+
preproc.connect(anat_restore, 'out_file', outputnode, 'anat_restore')
29772969

29782970
return preproc
29792971

@@ -3014,34 +3006,40 @@ def freesurfer_abcd_preproc(wf, cfg, strat_pool, pipe_num, opt=None):
30143006
'''
30153007

30163008
# fnirt-based brain extraction
3017-
brain_extraction = fnirt_based_brain_extraction(config=cfg,
3018-
wf_name=f'fnirt_based_brain_extraction_{pipe_num}')
3009+
fn_brain_extraction = fnirt_based_brain_extraction(
3010+
config=cfg, wf_name=f'fnirt_based_brain_extraction_{pipe_num}')
30193011

30203012
node, out = strat_pool.get_data('desc-preproc_T1w')
3021-
wf.connect(node, out, brain_extraction, 'inputspec.anat_data')
3013+
wf.connect(node, out, fn_brain_extraction, 'inputspec.anat_data')
30223014

30233015
node, out = strat_pool.get_data('template-ref-mask-res-2')
3024-
wf.connect(node, out, brain_extraction, 'inputspec.template-ref-mask-res-2')
3016+
wf.connect(node, out,
3017+
fn_brain_extraction, 'inputspec.template-ref-mask-res-2')
30253018

30263019
node, out = strat_pool.get_data('T1w-template')
3027-
wf.connect(node, out, brain_extraction, 'inputspec.template_skull_for_anat')
3020+
wf.connect(node, out,
3021+
fn_brain_extraction, 'inputspec.template_skull_for_anat')
30283022

30293023
node, out = strat_pool.get_data('T1w-template-res-2')
3030-
wf.connect(node, out, brain_extraction, 'inputspec.template_skull_for_anat_2mm')
3024+
wf.connect(node, out,
3025+
fn_brain_extraction, 'inputspec.template_skull_for_anat_2mm')
30313026

30323027
node, out = strat_pool.get_data('T1w-brain-template-mask')
3033-
wf.connect(node, out, brain_extraction, 'inputspec.template_brain_mask_for_anat')
3028+
wf.connect(node, out,
3029+
fn_brain_extraction, 'inputspec.template_brain_mask_for_anat')
30343030

30353031
# fast bias field correction
3036-
fast_correction = fast_bias_field_correction(config=cfg,
3037-
wf_name=f'fast_bias_field_correction_{pipe_num}')
3032+
fast_correction = fast_bias_field_correction(
3033+
config=cfg, wf_name=f'fast_bias_field_correction_{pipe_num}')
30383034

30393035
node, out = strat_pool.get_data('desc-preproc_T1w')
30403036
wf.connect(node, out, fast_correction, 'inputspec.anat_data')
30413037

3042-
wf.connect(brain_extraction, 'outputspec.anat_brain', fast_correction, 'inputspec.anat_brain')
3038+
wf.connect(fn_brain_extraction, 'outputspec.anat_brain',
3039+
fast_correction, 'inputspec.anat_brain')
30433040

3044-
wf.connect(brain_extraction, 'outputspec.anat_brain_mask', fast_correction, 'inputspec.anat_brain_mask')
3041+
wf.connect(fn_brain_extraction, 'outputspec.anat_brain_mask',
3042+
fast_correction, 'inputspec.anat_brain_mask')
30453043

30463044
### ABCD Harmonization ###
30473045
# Ref: https://github.com/DCAN-Labs/DCAN-HCP/blob/master/FreeSurfer/FreeSurferPipeline.sh#L140-L144
@@ -3051,80 +3049,82 @@ def freesurfer_abcd_preproc(wf, cfg, strat_pool, pipe_num, opt=None):
30513049
name=f'resample_anat_head_1mm_{pipe_num}')
30523050
resample_head_1mm.inputs.interp = 'spline'
30533051
resample_head_1mm.inputs.apply_isoxfm = 1
3052+
head_1mm_nodes, head_1mm_guardrails = wf.nodes_and_guardrails(
3053+
resample_head_1mm, registered='out_file')
30543054

30553055
node, out = strat_pool.get_data('desc-preproc_T1w')
3056-
wf.connect(node, out, resample_head_1mm, 'in_file')
3057-
3058-
wf.connect(node, out, resample_head_1mm, 'reference')
3059-
3056+
wf.connect_retries(head_1mm_nodes, [(node, out, 'in_file'),
3057+
(node, out, 'reference')])
3058+
wf.connect_retries(head_1mm_guardrails, [(node, out, 'reference')])
3059+
head_1mm = guardrail_selection(wf, *head_1mm_guardrails)
30603060
# applywarp --rel --interp=spline -i "$T1wImage" -r "$T1wImageFile"_1mm.nii.gz --premat=$FSLDIR/etc/flirtsch/ident.mat -o "$T1wImageFile"_1mm.nii.gz
3061-
applywarp_head_to_head_1mm = pe.Node(interface=fsl.ApplyWarp(),
3062-
name=f'applywarp_head_to_head_1mm_{pipe_num}')
3061+
applywarp_head_to_head_1mm = pe.Node(
3062+
interface=fsl.ApplyWarp(),
3063+
name=f'applywarp_head_to_head_1mm_{pipe_num}')
30633064
applywarp_head_to_head_1mm.inputs.relwarp = True
30643065
applywarp_head_to_head_1mm.inputs.interp = 'spline'
3065-
applywarp_head_to_head_1mm.inputs.premat = cfg.registration_workflows['anatomical_registration']['registration']['FSL-FNIRT']['identity_matrix']
3066+
applywarp_head_to_head_1mm.inputs.premat = cfg.registration_workflows[
3067+
'anatomical_registration'
3068+
]['registration']['FSL-FNIRT']['identity_matrix']
30663069

30673070
wf.connect(node, out, applywarp_head_to_head_1mm, 'in_file')
3068-
3069-
wf.connect(resample_head_1mm, 'out_file',
3070-
applywarp_head_to_head_1mm, 'ref_file')
3071+
wf.connect(head_1mm, 'out', applywarp_head_to_head_1mm, 'ref_file')
30713072

30723073
# applywarp --rel --interp=nn -i "$T1wImageBrain" -r "$T1wImageFile"_1mm.nii.gz --premat=$FSLDIR/etc/flirtsch/ident.mat -o "$T1wImageBrainFile"_1mm.nii.gz
3073-
applywarp_brain_to_head_1mm = pe.Node(interface=fsl.ApplyWarp(),
3074-
name=f'applywarp_brain_to_head_1mm_{pipe_num}')
3074+
applywarp_brain_to_head_1mm = pe.Node(
3075+
interface=fsl.ApplyWarp(),
3076+
name=f'applywarp_brain_to_head_1mm_{pipe_num}')
30753077
applywarp_brain_to_head_1mm.inputs.relwarp = True
30763078
applywarp_brain_to_head_1mm.inputs.interp = 'nn'
3077-
applywarp_brain_to_head_1mm.inputs.premat = cfg.registration_workflows['anatomical_registration']['registration']['FSL-FNIRT']['identity_matrix']
3079+
applywarp_brain_to_head_1mm.inputs.premat = cfg.registration_workflows[
3080+
'anatomical_registration'
3081+
]['registration']['FSL-FNIRT']['identity_matrix']
30783082

30793083
wf.connect(fast_correction, 'outputspec.anat_brain_restore',
3080-
applywarp_brain_to_head_1mm, 'in_file')
3081-
3082-
wf.connect(resample_head_1mm, 'out_file',
3083-
applywarp_brain_to_head_1mm, 'ref_file')
3084+
applywarp_brain_to_head_1mm, 'in_file')
3085+
wf.connect(head_1mm, 'out', applywarp_brain_to_head_1mm, 'ref_file')
30843086

30853087
# fslstats $T1wImageBrain -M
30863088
average_brain = pe.Node(interface=fsl.ImageStats(),
3087-
name=f'average_brain_{pipe_num}')
3089+
name=f'average_brain_{pipe_num}')
30883090
average_brain.inputs.op_string = '-M'
30893091
average_brain.inputs.output_type = 'NIFTI_GZ'
30903092

30913093
wf.connect(fast_correction, 'outputspec.anat_brain_restore',
30923094
average_brain, 'in_file')
30933095

30943096
# fslmaths "$T1wImageFile"_1mm.nii.gz -div $Mean -mul 150 -abs "$T1wImageFile"_1mm.nii.gz
3095-
normalize_head = pe.Node(util.Function(input_names=['in_file', 'number', 'out_file_suffix'],
3097+
normalize_head = pe.Node(util.Function(input_names=['in_file', 'number',
3098+
'out_file_suffix'],
30963099
output_names=['out_file'],
30973100
function=fslmaths_command),
30983101
name=f'normalize_head_{pipe_num}')
30993102
normalize_head.inputs.out_file_suffix = '_norm'
31003103

3101-
wf.connect(applywarp_head_to_head_1mm, 'out_file',
3104+
wf.connect(applywarp_head_to_head_1mm, 'out_file',
31023105
normalize_head, 'in_file')
3103-
3104-
wf.connect(average_brain, 'out_stat',
3105-
normalize_head, 'number')
3106+
wf.connect(average_brain, 'out_stat', normalize_head, 'number')
31063107

31073108
### recon-all -all step ###
31083109
reconall = pe.Node(interface=freesurfer.ReconAll(),
31093110
name=f'anat_freesurfer_{pipe_num}',
31103111
mem_gb=2.7)
31113112

31123113
sub_dir = cfg.pipeline_setup['working_directory']['path']
3113-
freesurfer_subject_dir = os.path.join(sub_dir,
3114-
'cpac_'+cfg['subject_id'],
3115-
f'anat_preproc_freesurfer_{pipe_num}',
3116-
'anat_freesurfer')
3114+
freesurfer_subject_dir = os.path.join(
3115+
sub_dir, 'cpac_'+cfg['subject_id'],
3116+
f'anat_preproc_freesurfer_{pipe_num}', 'anat_freesurfer')
31173117

31183118
# create the directory for FreeSurfer node
31193119
if not os.path.exists(freesurfer_subject_dir):
31203120
os.makedirs(freesurfer_subject_dir)
31213121

31223122
reconall.inputs.directive = 'all'
31233123
reconall.inputs.subjects_dir = freesurfer_subject_dir
3124-
reconall.inputs.openmp = cfg.pipeline_setup['system_config']['num_OMP_threads']
3124+
reconall.inputs.openmp = cfg.pipeline_setup['system_config'][
3125+
'num_OMP_threads']
31253126

3126-
wf.connect(normalize_head, 'out_file',
3127-
reconall, 'T1_files')
3127+
wf.connect(normalize_head, 'out_file', reconall, 'T1_files')
31283128

31293129
wf, hemisphere_outputs = freesurfer_hemispheres(wf, reconall, pipe_num)
31303130

@@ -3138,10 +3138,11 @@ def freesurfer_abcd_preproc(wf, cfg, strat_pool, pipe_num, opt=None):
31383138
**hemisphere_outputs
31393139
}
31403140

3141-
return (wf, outputs)
3141+
return wf, outputs
31423142

31433143

3144-
def correct_restore_brain_intensity_abcd(wf, cfg, strat_pool, pipe_num, opt=None):
3144+
def correct_restore_brain_intensity_abcd(wf, cfg, strat_pool, pipe_num,
3145+
opt=None):
31453146
'''
31463147
{"name": "correct_restore_brain_intensity_abcd",
31473148
"config": ["anatomical_preproc", "brain_extraction"],

0 commit comments

Comments
 (0)