|
| 1 | +#!/usr/bin/env python |
| 2 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 3 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 4 | +""" |
| 5 | +====================================================== |
| 6 | +sMRI: Using new ANTS for creating a T1 template (ITK4) |
| 7 | +====================================================== |
| 8 | +
|
| 9 | +In this tutorial we will use ANTS (new ITK4 version aka "antsRegistration") based workflow to |
| 10 | +create a template out of multiple T1 volumes. We will also showcase how to fine tune SGE jobs requirements. |
| 11 | +
|
| 12 | +1. Tell python where to find the appropriate functions. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import nipype.interfaces.utility as util |
| 17 | +import nipype.interfaces.ants as ants |
| 18 | +import nipype.interfaces.io as io |
| 19 | +import nipype.pipeline.engine as pe # pypeline engine |
| 20 | + |
| 21 | +from nipype.workflows.smri.ants import antsRegistrationTemplateBuildSingleIterationWF |
| 22 | + |
| 23 | +""" |
| 24 | +2. Download T1 volumes into home directory |
| 25 | +""" |
| 26 | + |
| 27 | +import urllib2 |
| 28 | +homeDir=os.getenv("HOME") |
| 29 | +requestedPath=os.path.join(homeDir,'nipypeTestPath') |
| 30 | +mydatadir=os.path.realpath(requestedPath) |
| 31 | +if not os.path.exists(mydatadir): |
| 32 | + os.makedirs(mydatadir) |
| 33 | +print mydatadir |
| 34 | + |
| 35 | +MyFileURLs=[ |
| 36 | + ('http://slicer.kitware.com/midas3/download?bitstream=13121','01_T1_half.nii.gz'), |
| 37 | + ('http://slicer.kitware.com/midas3/download?bitstream=13122','02_T1_half.nii.gz'), |
| 38 | + ('http://slicer.kitware.com/midas3/download?bitstream=13124','03_T1_half.nii.gz'), |
| 39 | + ('http://slicer.kitware.com/midas3/download?bitstream=13128','01_T1_inv_half.nii.gz'), |
| 40 | + ('http://slicer.kitware.com/midas3/download?bitstream=13123','02_T1_inv_half.nii.gz'), |
| 41 | + ('http://slicer.kitware.com/midas3/download?bitstream=13125','03_T1_inv_half.nii.gz'), |
| 42 | + ] |
| 43 | +for tt in MyFileURLs: |
| 44 | + myURL=tt[0] |
| 45 | + localFilename=os.path.join(mydatadir,tt[1]) |
| 46 | + if not os.path.exists(localFilename): |
| 47 | + remotefile = urllib2.urlopen(myURL) |
| 48 | + |
| 49 | + localFile = open(localFilename, 'wb') |
| 50 | + localFile.write(remotefile.read()) |
| 51 | + localFile.close() |
| 52 | + print("Downloaded file: {0}".format(localFilename)) |
| 53 | + else: |
| 54 | + print("File previously downloaded {0}".format(localFilename)) |
| 55 | + |
| 56 | + |
| 57 | +""" |
| 58 | +ListOfImagesDictionaries - a list of dictionaries where each dictionary is |
| 59 | +for one scan session, and the mappings in the dictionary are for all the |
| 60 | +co-aligned images for that one scan session |
| 61 | +""" |
| 62 | +ListOfImagesDictionaries=[ |
| 63 | +{'T1':os.path.join(mydatadir,'01_T1_half.nii.gz'),'INV_T1':os.path.join(mydatadir,'01_T1_inv_half.nii.gz'),'LABEL_MAP':os.path.join(mydatadir,'01_T1_inv_half.nii.gz')}, |
| 64 | +{'T1':os.path.join(mydatadir,'02_T1_half.nii.gz'),'INV_T1':os.path.join(mydatadir,'02_T1_inv_half.nii.gz'),'LABEL_MAP':os.path.join(mydatadir,'02_T1_inv_half.nii.gz')}, |
| 65 | +{'T1':os.path.join(mydatadir,'03_T1_half.nii.gz'),'INV_T1':os.path.join(mydatadir,'03_T1_inv_half.nii.gz'),'LABEL_MAP':os.path.join(mydatadir,'03_T1_inv_half.nii.gz')} |
| 66 | +] |
| 67 | +input_passive_images=[ |
| 68 | +{'INV_T1':os.path.join(mydatadir,'01_T1_inv_half.nii.gz')}, |
| 69 | +{'INV_T1':os.path.join(mydatadir,'02_T1_inv_half.nii.gz')}, |
| 70 | +{'INV_T1':os.path.join(mydatadir,'03_T1_inv_half.nii.gz')} |
| 71 | +] |
| 72 | + |
| 73 | +""" |
| 74 | +registrationImageTypes - A list of the image types to be used actively during |
| 75 | +the estimation process of registration, any image type not in this list |
| 76 | +will be passively resampled with the estimated transforms. |
| 77 | +['T1','T2'] |
| 78 | +""" |
| 79 | +registrationImageTypes=['T1'] |
| 80 | + |
| 81 | +""" |
| 82 | +interpolationMap - A map of image types to interpolation modes. If an |
| 83 | +image type is not listed, it will be linearly interpolated. |
| 84 | +{ 'labelmap':'NearestNeighbor', 'FLAIR':'WindowedSinc' } |
| 85 | +""" |
| 86 | +interpolationMapping={'INV_T1':'LanczosWindowedSinc','LABEL_MAP':'NearestNeighbor','T1':'Linear'} |
| 87 | + |
| 88 | +""" |
| 89 | +3. Define the workflow and its working directory |
| 90 | +""" |
| 91 | +tbuilder=pe.Workflow(name="antsRegistrationTemplateBuilder") |
| 92 | +tbuilder.base_dir=requestedPath |
| 93 | + |
| 94 | +""" |
| 95 | +4. Define data sources. In real life these would be replace by DataGrabbers |
| 96 | +""" |
| 97 | +InitialTemplateInputs=[ mdict['T1'] for mdict in ListOfImagesDictionaries ] |
| 98 | + |
| 99 | +datasource = pe.Node(interface=util.IdentityInterface(fields= |
| 100 | + ['InitialTemplateInputs', 'ListOfImagesDictionaries', |
| 101 | + 'registrationImageTypes','interpolationMapping']), |
| 102 | + run_without_submitting=True, |
| 103 | + name='InputImages' ) |
| 104 | +datasource.inputs.InitialTemplateInputs=InitialTemplateInputs |
| 105 | +datasource.inputs.ListOfImagesDictionaries=ListOfImagesDictionaries |
| 106 | +datasource.inputs.registrationImageTypes=registrationImageTypes |
| 107 | +datasource.inputs.interpolationMapping=interpolationMapping |
| 108 | + |
| 109 | +""" |
| 110 | +5. Template is initialized by a simple average in this simple example, |
| 111 | + any reference image could be used (i.e. a previously created template) |
| 112 | +""" |
| 113 | +initAvg = pe.Node(interface=ants.AverageImages(), name ='initAvg') |
| 114 | +initAvg.inputs.dimension = 3 |
| 115 | +initAvg.inputs.normalize = True |
| 116 | + |
| 117 | +tbuilder.connect(datasource, "InitialTemplateInputs", initAvg, "images") |
| 118 | + |
| 119 | +""" |
| 120 | +6. Define the first iteration of template building |
| 121 | +""" |
| 122 | + |
| 123 | +buildTemplateIteration1=antsRegistrationTemplateBuildSingleIterationWF('iteration01') |
| 124 | +""" |
| 125 | +Here we are fine tuning parameters of the SGE job (memory limit, numebr of cores etc.) |
| 126 | +""" |
| 127 | +BeginANTS = buildTemplateIteration1.get_node("BeginANTS") |
| 128 | +BeginANTS.plugin_args={'qsub_args': '-S /bin/bash -pe smp1 8-12 -l mem_free=6000M -o /dev/null -e /dev/null queue_name', 'overwrite': True} |
| 129 | + |
| 130 | +tbuilder.connect(initAvg, 'output_average_image', buildTemplateIteration1, 'inputspec.fixed_image') |
| 131 | +tbuilder.connect(datasource, 'ListOfImagesDictionaries', buildTemplateIteration1, 'inputspec.ListOfImagesDictionaries') |
| 132 | +tbuilder.connect(datasource, 'registrationImageTypes', buildTemplateIteration1, 'inputspec.registrationImageTypes') |
| 133 | +tbuilder.connect(datasource, 'interpolationMapping', buildTemplateIteration1, 'inputspec.interpolationMapping') |
| 134 | +""" |
| 135 | +7. Define the second iteration of template building |
| 136 | +""" |
| 137 | + |
| 138 | +buildTemplateIteration2 = antsRegistrationTemplateBuildSingleIterationWF('iteration02') |
| 139 | +BeginANTS = buildTemplateIteration2.get_node("BeginANTS") |
| 140 | +BeginANTS.plugin_args={'qsub_args': '-S /bin/bash -pe smp1 8-12 -l mem_free=6000M -o /dev/null -e /dev/null queue_name', 'overwrite': True} |
| 141 | +tbuilder.connect(buildTemplateIteration1, 'outputspec.template', buildTemplateIteration2, 'inputspec.fixed_image') |
| 142 | +tbuilder.connect(datasource, 'ListOfImagesDictionaries', buildTemplateIteration2, 'inputspec.ListOfImagesDictionaries') |
| 143 | +tbuilder.connect(datasource, 'registrationImageTypes', buildTemplateIteration2, 'inputspec.registrationImageTypes') |
| 144 | +tbuilder.connect(datasource, 'interpolationMapping', buildTemplateIteration2, 'inputspec.interpolationMapping') |
| 145 | + |
| 146 | +""" |
| 147 | +8. Move selected files to a designated results folder |
| 148 | +""" |
| 149 | + |
| 150 | +datasink = pe.Node(io.DataSink(), name="datasink") |
| 151 | +datasink.inputs.base_directory = os.path.join(requestedPath, "results") |
| 152 | + |
| 153 | +tbuilder.connect(buildTemplateIteration2, 'outputspec.template',datasink,'PrimaryTemplate') |
| 154 | +tbuilder.connect(buildTemplateIteration2, 'outputspec.passive_deformed_templates',datasink,'PassiveTemplate') |
| 155 | +tbuilder.connect(initAvg, 'output_average_image', datasink,'PreRegisterAverage') |
| 156 | + |
| 157 | +""" |
| 158 | +8. Run the workflow |
| 159 | +""" |
| 160 | + |
| 161 | +tbuilder.run(plugin="SGE") |
0 commit comments