|
| 1 | +# Import modules |
| 2 | +from os.path import join as opj |
| 3 | +from nipype.interfaces.ants import Registration |
| 4 | +from nipype.interfaces.utility import IdentityInterface |
| 5 | +from nipype.interfaces.io import SelectFiles, DataSink |
| 6 | +from nipype.pipeline.engine import Workflow, Node |
| 7 | +from nipype.interfaces.fsl import Info |
| 8 | + |
| 9 | +# Specify variables |
| 10 | +experiment_dir = '/output' |
| 11 | +output_dir = 'antsdir' |
| 12 | +working_dir = 'workingdir' |
| 13 | +subject_list = ['sub-01', 'sub-02', 'sub-03', 'sub-04', 'sub-05', |
| 14 | + 'sub-06', 'sub-07', 'sub-08', 'sub-09', 'sub-10'] |
| 15 | + |
| 16 | +# Location of template file |
| 17 | +template = '/data/ds000114/derivatives/fmriprep/mni_icbm152_nlin_asym_09c/1mm_T1.nii.gz' |
| 18 | +# or alternatively template = Info.standard_image('MNI152_T1_1mm.nii.gz') |
| 19 | + |
| 20 | +# Registration - computes registration between subject's anatomy & the MNI template |
| 21 | +antsreg = Node(Registration(args='--float', |
| 22 | + collapse_output_transforms=True, |
| 23 | + fixed_image=template, |
| 24 | + initial_moving_transform_com=True, |
| 25 | + num_threads=4, |
| 26 | + output_inverse_warped_image=True, |
| 27 | + output_warped_image=True, |
| 28 | + sigma_units=['vox'] * 3, |
| 29 | + transforms=['Rigid', 'Affine', 'SyN'], |
| 30 | + terminal_output='file', |
| 31 | + winsorize_lower_quantile=0.005, |
| 32 | + winsorize_upper_quantile=0.995, |
| 33 | + convergence_threshold=[1e-06], |
| 34 | + convergence_window_size=[10], |
| 35 | + metric=['MI', 'MI', 'CC'], |
| 36 | + metric_weight=[1.0] * 3, |
| 37 | + number_of_iterations=[[1000, 500, 250, 100], |
| 38 | + [1000, 500, 250, 100], |
| 39 | + [100, 70, 50, 20]], |
| 40 | + radius_or_number_of_bins=[32, 32, 4], |
| 41 | + sampling_percentage=[0.25, 0.25, 1], |
| 42 | + sampling_strategy=['Regular', 'Regular', 'None'], |
| 43 | + shrink_factors=[[8, 4, 2, 1]] * 3, |
| 44 | + smoothing_sigmas=[[3, 2, 1, 0]] * 3, |
| 45 | + transform_parameters=[(0.1,), (0.1,), |
| 46 | + (0.1, 3.0, 0.0)], |
| 47 | + use_histogram_matching=True, |
| 48 | + write_composite_transform=True), |
| 49 | + name='antsreg') |
| 50 | + |
| 51 | +### |
| 52 | +# Input & Output Stream |
| 53 | + |
| 54 | +# Infosource - a function free node to iterate over the list of subject names |
| 55 | +infosource = Node(IdentityInterface(fields=['subject_id']), |
| 56 | + name="infosource") |
| 57 | +infosource.iterables = [('subject_id', subject_list)] |
| 58 | + |
| 59 | +# SelectFiles - to grab the data (alternative to DataGrabber) |
| 60 | +anat_file = opj('{subject_id}', 'ses-test', 'anat', '{subject_id}_ses-test_T1w.nii.gz') |
| 61 | +templates = {'anat': anat_file} |
| 62 | + |
| 63 | +selectfiles = Node(SelectFiles(templates, |
| 64 | + base_directory='/data/ds000114'), |
| 65 | + name="selectfiles") |
| 66 | + |
| 67 | +# Datasink - creates output folder for important outputs |
| 68 | +datasink = Node(DataSink(base_directory=experiment_dir, |
| 69 | + container=output_dir), |
| 70 | + name="datasink") |
| 71 | + |
| 72 | +# Use the following DataSink output substitutions |
| 73 | +substitutions = [('_subject_id_', '')] |
| 74 | +datasink.inputs.substitutions = substitutions |
| 75 | + |
| 76 | +### |
| 77 | +# Specify Normalization Workflow & Connect Nodes |
| 78 | + |
| 79 | +# Initiation of the ANTS normalization workflow |
| 80 | +regflow = Workflow(name='regflow') |
| 81 | +regflow.base_dir = opj(experiment_dir, working_dir) |
| 82 | + |
| 83 | +# Connect workflow nodes |
| 84 | +regflow.connect([(infosource, selectfiles, [('subject_id', 'subject_id')]), |
| 85 | + (selectfiles, antsreg, [('anat', 'moving_image')]), |
| 86 | + (antsreg, datasink, [('warped_image', |
| 87 | + 'antsreg.@warped_image'), |
| 88 | + ('inverse_warped_image', |
| 89 | + 'antsreg.@inverse_warped_image'), |
| 90 | + ('composite_transform', |
| 91 | + 'antsreg.@transform'), |
| 92 | + ('inverse_composite_transform', |
| 93 | + 'antsreg.@inverse_transform')]), |
| 94 | + ]) |
| 95 | + |
| 96 | +### |
| 97 | +# Run Workflow |
| 98 | +regflow.write_graph(graph2use='flat') |
| 99 | +regflow.run('Linear') |
0 commit comments