|
| 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 |
| 7 | +=============================================== |
| 8 | +
|
| 9 | +In this tutorial we will use ANTS (old version aka "ANTS") based workflow to |
| 10 | +create a template out of multiple T1 volumes. |
| 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 ANTSTemplateBuildSingleIterationWF |
| 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 | +input_images=[ |
| 57 | +os.path.join(mydatadir,'01_T1_half.nii.gz'), |
| 58 | +os.path.join(mydatadir,'02_T1_half.nii.gz'), |
| 59 | +os.path.join(mydatadir,'03_T1_half.nii.gz') |
| 60 | +] |
| 61 | +input_passive_images=[ |
| 62 | +{'INV_T1':os.path.join(mydatadir,'01_T1_inv_half.nii.gz')}, |
| 63 | +{'INV_T1':os.path.join(mydatadir,'02_T1_inv_half.nii.gz')}, |
| 64 | +{'INV_T1':os.path.join(mydatadir,'03_T1_inv_half.nii.gz')} |
| 65 | +] |
| 66 | + |
| 67 | + |
| 68 | +""" |
| 69 | +3. Define the workflow and its working directory |
| 70 | +""" |
| 71 | +tbuilder=pe.Workflow(name="ANTSTemplateBuilder") |
| 72 | +tbuilder.base_dir=requestedPath |
| 73 | + |
| 74 | +""" |
| 75 | +4. Define data sources. In real life these would be replace by DataGrabbers |
| 76 | +""" |
| 77 | +datasource = pe.Node(interface=util.IdentityInterface(fields= |
| 78 | + ['imageList', 'passiveImagesDictionariesList']), |
| 79 | + run_without_submitting=True, |
| 80 | + name='InputImages' ) |
| 81 | +datasource.inputs.imageList=input_images |
| 82 | +datasource.inputs.passiveImagesDictionariesList=input_passive_images |
| 83 | + |
| 84 | +""" |
| 85 | +5. Template is initialized by a simple average |
| 86 | +""" |
| 87 | +initAvg = pe.Node(interface=ants.AverageImages(), name ='initAvg') |
| 88 | +initAvg.inputs.dimension = 3 |
| 89 | +initAvg.inputs.normalize = True |
| 90 | + |
| 91 | +tbuilder.connect(datasource, "imageList", initAvg, "images") |
| 92 | + |
| 93 | +""" |
| 94 | +6. Define the first iteration of template building |
| 95 | +""" |
| 96 | + |
| 97 | +buildTemplateIteration1=ANTSTemplateBuildSingleIterationWF('iteration01') |
| 98 | +tbuilder.connect(initAvg, 'output_average_image', buildTemplateIteration1, 'inputspec.fixed_image') |
| 99 | +tbuilder.connect(datasource, 'imageList', buildTemplateIteration1, 'inputspec.images') |
| 100 | +tbuilder.connect(datasource, 'passiveImagesDictionariesList', buildTemplateIteration1, 'inputspec.ListOfPassiveImagesDictionaries') |
| 101 | + |
| 102 | +""" |
| 103 | +7. Define the second iteration of template building |
| 104 | +""" |
| 105 | + |
| 106 | +buildTemplateIteration2 = ANTSTemplateBuildSingleIterationWF('iteration02') |
| 107 | +tbuilder.connect(buildTemplateIteration1, 'outputspec.template', buildTemplateIteration2, 'inputspec.fixed_image') |
| 108 | +tbuilder.connect(datasource, 'imageList', buildTemplateIteration2, 'inputspec.images') |
| 109 | +tbuilder.connect(datasource, 'passiveImagesDictionariesList', buildTemplateIteration2, 'inputspec.ListOfPassiveImagesDictionaries') |
| 110 | + |
| 111 | +""" |
| 112 | +8. Move selected files to a designated results folder |
| 113 | +""" |
| 114 | + |
| 115 | +datasink = pe.Node(io.DataSink(), name="datasink") |
| 116 | +datasink.inputs.base_directory = os.path.join(requestedPath, "results") |
| 117 | + |
| 118 | +tbuilder.connect(buildTemplateIteration2, 'outputspec.template',datasink,'PrimaryTemplate') |
| 119 | +tbuilder.connect(buildTemplateIteration2, 'outputspec.passive_deformed_templates',datasink,'PassiveTemplate') |
| 120 | +tbuilder.connect(initAvg, 'output_average_image', datasink,'PreRegisterAverage') |
| 121 | + |
| 122 | +""" |
| 123 | +8. Run the workflow |
| 124 | +""" |
| 125 | + |
| 126 | +tbuilder.run() |
0 commit comments