Skip to content

Diffusion toolkit #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
17 commits merged into from
Sep 7, 2010
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
A pipeline example that uses several interfaces to
perform analysis on diffusion weighted images using
FSL FDT tools.
Diffusion Toolkit and FSL.

This tutorial is based on the 2010 FSL course and uses
data freely available at the FSL website at:
Expand Down Expand Up @@ -69,15 +69,7 @@

info = dict(dwi=[['subject_id', 'data']],
bvecs=[['subject_id','bvecs']],
bvals=[['subject_id','bvals']],
seed_file = [['subject_id','MASK_average_thal_right']],
target_masks = [['subject_id',['MASK_average_M1_right',
'MASK_average_S1_right',
'MASK_average_occipital_right',
'MASK_average_pfc_right',
'MASK_average_pmc_right',
'MASK_average_ppc_right',
'MASK_average_temporal_right']]])
bvals=[['subject_id','bvals']])

infosource = pe.Node(interface=util.IdentityInterface(fields=['subject_id']),
name="infosource")
Expand Down Expand Up @@ -111,9 +103,7 @@
# http://www.fmrib.ox.ac.uk/fslcourse/fsl_course_data2.tar.gz
datasource.inputs.base_directory = os.path.abspath('fsl_course_data/fdt/')

datasource.inputs.field_template = dict(dwi='%s/%s.nii.gz',
seed_file="%s.bedpostX/%s.nii.gz",
target_masks="%s.bedpostX/%s.nii.gz")
datasource.inputs.field_template = dict(dwi='%s/%s.nii.gz')
datasource.inputs.template_args = info


Expand Down Expand Up @@ -160,69 +150,31 @@

computeTensor.connect([
(fslroi,bet,[('roi_file','in_file')]),
(eddycorrect,dtifit,[('eddy_corrected','dwi')])
(eddycorrect,dtifit,[('eddy_corrected','DWI')])
])



"""
Setup for Tracktography
-----------------------
Here we will create a workflow to enable probabilistic tracktography
and hard segmentation of the seed region
Here we will create a workflow to enable deterministic tracktography
"""

tractography = pe.Workflow(name='tractography')
tractography.base_dir = os.path.abspath('diffusion_toolkit_tutorial')

"""
estimate the diffusion parameters: phi, theta, and so on
"""

bedpostx = pe.Node(interface=fsl.BEDPOSTX(),name='bedpostx')
bedpostx.inputs.fibres = 1

bedpostx_2f = pe.Node(interface=fsl.BEDPOSTX(),name='bedpostx_2f')
bedpostx_2f.inputs.fibres = 2


flirt = pe.Node(interface=fsl.FLIRT(), name='flirt')
flirt.inputs.reference = fsl.Info.standard_image('MNI152_T1_2mm_brain.nii.gz')
flirt.inputs.dof = 12

"""
perform probabilistic tracktography
"""

probtrackx = pe.Node(interface=fsl.ProbTrackX(),name='probtrackx')
probtrackx.inputs.mode='seedmask'
probtrackx.inputs.loop_check=True
probtrackx.inputs.c_thresh = 0.2
probtrackx.inputs.n_steps=2000
probtrackx.inputs.step_length=0.5
probtrackx.inputs.n_samples=5000
probtrackx.inputs.force_dir=True
probtrackx.inputs.opd=True
probtrackx.inputs.os2t=True


"""
perform hard segmentation on the output of probtrackx
"""

findthebiggest = pe.Node(interface=fsl.FindTheBiggest(),name='findthebiggest')

dtk_tracker = pe.Node(interface=dtk.DTITracker(), name="dtk_tracker")
dtk_tracker.inputs.invert_x = True

smooth_trk = pe.Node(interface=dtk.SplineFilter(), name="smooth_trk")
smooth_trk.inputs.step_length = 0.5
"""
connect all the nodes for this workflow
"""

tractography.connect([
(bedpostx,probtrackx,[('bpx_out_directory','bpx_directory')]),
(bedpostx,probtrackx,[('bpx_out_directory','out_dir')]),
(probtrackx,findthebiggest,[('targets','in_files')]),
(flirt, probtrackx, [('out_matrix_file','xfm')])
])
(dtk_tracker, smooth_trk, [('track_file', 'track_file')])
])


"""
Expand All @@ -242,24 +194,16 @@ def getstripdir(subject_id):
"""

dwiproc = pe.Workflow(name="dwiproc")
dwiproc.base_dir = os.path.abspath('dti_tutorial')
dwiproc.base_dir = os.path.abspath('dtk_dti_tutorial')
dwiproc.connect([
(infosource,datasource,[('subject_id', 'subject_id')]),
(datasource,computeTensor,[('dwi','fslroi.in_file'),
('bvals','dtifit.bvals'),
('bvecs','dtifit.bvecs'),
('dwi','eddycorrect.in_file')]),
(datasource,tractography,[('bvals','bedpostx.bvals'),
('bvecs','bedpostx.bvecs'),
('seed_file','probtrackx.seed_file'),
('target_masks','probtrackx.target_masks')]),
(computeTensor,tractography,[('eddycorrect.eddy_corrected','bedpostx.dwi'),
('bet.mask_file','bedpostx.mask'),
('bet.mask_file','probtrackx.mask'),
('fslroi.roi_file','flirt.in_file')]),
(infosource, datasink,[('subject_id','container'),
(('subject_id', getstripdir),'strip_dir')]),
(tractography,datasink,[('findthebiggest.out_file','fbiggest.@biggestsegmentation')])
(computeTensor,tractography,[('bet.mask_file','dtk_tracker.mask1_file'),
('dtifit.tensor','dtk_tracker.tensor_file')
])
])

dwiproc.run()
Expand Down
203 changes: 203 additions & 0 deletions examples/dtk_odf_tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@

"""
A pipeline example that uses several interfaces to
perform analysis on diffusion weighted images using
Diffusion Toolkit and FSL.

This tutorial uses data from out nipype-tutorial package.
"""


"""
Tell python where to find the appropriate functions.
"""

import nipype.interfaces.io as nio # Data i/o
import nipype.interfaces.fsl as fsl # fsl
import nipype.interfaces.diffusion_toolkit as dtk
import nipype.interfaces.utility as util # utility
import nipype.pipeline.engine as pe # pypeline engine
import os # system functions

"""
Confirm package dependencies are installed. (This is only for the
tutorial, rarely would you put this in your own code.)
"""

from nipype.utils.misc import package_check

package_check('numpy', '1.3', 'tutorial1')
package_check('scipy', '0.7', 'tutorial1')
package_check('networkx', '1.0', 'tutorial1')
package_check('IPython', '0.10', 'tutorial1')


"""
Setting up workflows
--------------------
This is a generic workflow for DTI data analysis using the FSL
"""

"""
Data specific components
------------------------

The nipype tutorial contains data for two subjects. Subject data
is in two subdirectories, ``dwis1`` and ``dwis2``. Each subject directory
contains each of the following files: bvec, bval, diffusion weighted data, a set of target masks,
a seed file, and a transformation matrix.

Below we set some variables to inform the ``datasource`` about the
layout of our data. We specify the location of the data, the subject
sub-directories and a dictionary that maps each run to a mnemonic (or
field) for the run type (``dwi`` or ``bvals``). These fields become
the output fields of the ``datasource`` node in the pipeline.

Specify the subject directories
"""

subject_list = ['siemens_hardi_test']


"""
Map field names to individual subject runs
"""

info = dict(dwi=[['subject_id', 'siemens_hardi_test_data']],
bvecs=[['subject_id','siemens_hardi_test_data.bvec']],
bvals=[['subject_id','siemens_hardi_test_data.bval']])

infosource = pe.Node(interface=util.IdentityInterface(fields=['subject_id']),
name="infosource")

"""Here we set up iteration over all the subjects. The following line
is a particular example of the flexibility of the system. The
``datasource`` attribute ``iterables`` tells the pipeline engine that
it should repeat the analysis on each of the items in the
``subject_list``. In the current example, the entire first level
preprocessing and estimation will be repeated for each subject
contained in subject_list.
"""

infosource.iterables = ('subject_id', subject_list)

"""
Now we create a :class:`nipype.interfaces.io.DataGrabber` object and
fill in the information from above about the layout of our data. The
:class:`nipype.pipeline.engine.Node` module wraps the interface object
and provides additional housekeeping and pipeline specific
functionality.
"""

datasource = pe.Node(interface=nio.DataGrabber(infields=['subject_id'],
outfields=info.keys()),
name = 'datasource')

datasource.inputs.template = "%s/%s"

# This needs to point to the fdt folder you can find after extracting
# http://www.fmrib.ox.ac.uk/fslcourse/fsl_course_data2.tar.gz
datasource.inputs.base_directory = os.path.abspath('data')

datasource.inputs.field_template = dict(dwi='%s/%s.nii')
datasource.inputs.template_args = info


"""
Setup for ODF Computation
--------------------------------------
Here we will create a generic workflow for ODF computation
"""

compute_ODF = pe.Workflow(name='compute_ODF')

"""
extract the volume with b=0 (nodif_brain)
"""

fslroi = pe.Node(interface=fsl.ExtractROI(),name='fslroi')
fslroi.inputs.t_min=0
fslroi.inputs.t_size=1

"""
create a brain mask from the nodif_brain
"""

bet = pe.Node(interface=fsl.BET(),name='bet')
bet.inputs.mask=True
bet.inputs.frac=0.34

"""
correct the diffusion weighted images for eddy_currents
"""

eddycorrect = pe.Node(interface=fsl.EddyCorrect(),name='eddycorrect')
eddycorrect.inputs.ref_num=0


hardi_mat = pe.Node(interface=dtk.HARDIMat(),name='hardi_mat')

odf_recon = pe.Node(interface=dtk.ODFRecon(),name='odf_recon')

"""
connect all the nodes for this workflow
"""

compute_ODF.connect([
(fslroi,bet,[('roi_file','in_file')]),
(eddycorrect, odf_recon,[('eddy_corrected','DWI')]),
(eddycorrect, hardi_mat,[('eddy_corrected','reference_file')]),
(hardi_mat, odf_recon, [('out_file', 'matrix')])
])



"""
Setup for Tracktography
-----------------------
Here we will create a workflow to enable deterministic tracktography
"""

tractography = pe.Workflow(name='tractography')

odf_tracker = pe.Node(interface=dtk.ODFTracker(), name="odf_tracker")

smooth_trk = pe.Node(interface=dtk.SplineFilter(), name="smooth_trk")
smooth_trk.inputs.step_length = 1
"""
connect all the nodes for this workflow
"""

tractography.connect([
(odf_tracker, smooth_trk, [('track_file', 'track_file')])
])


"""
Setup the pipeline that combines the two workflows: tractography and compute_ODF
----------------------------------------------------------------------------------
"""

dwiproc = pe.Workflow(name="dwiproc")
dwiproc.base_dir = os.path.abspath('dtk_odf_tutorial')
dwiproc.connect([
(infosource,datasource,[('subject_id', 'subject_id')]),
(datasource,compute_ODF,[('dwi','fslroi.in_file'),
('bvals','hardi_mat.bvals'),
('bvecs','hardi_mat.bvecs'),
('dwi','eddycorrect.in_file')]),
(compute_ODF,tractography,[('bet.mask_file','odf_tracker.mask1_file'),
('odf_recon.ODF','odf_tracker.ODF'),
('odf_recon.max','odf_tracker.max')
])
])

dwiproc.inputs.compute_ODF.hardi_mat.oblique_correction = True
dwiproc.inputs.compute_ODF.odf_recon.n_directions = 31
dwiproc.inputs.compute_ODF.odf_recon.n_b0 = 5
dwiproc.inputs.compute_ODF.odf_recon.n_output_directions = 181

dwiproc.run()
dwiproc.write_graph()


4 changes: 2 additions & 2 deletions examples/dti_tutorial.py → examples/fsl_dti_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
"""

tractography = pe.Workflow(name='tractography')
tractography.base_dir = os.path.abspath('dti_tutorial')
tractography.base_dir = os.path.abspath('fsl_dti_tutorial')

"""
estimate the diffusion parameters: phi, theta, and so on
Expand Down Expand Up @@ -243,7 +243,7 @@ def getstripdir(subject_id):
"""

dwiproc = pe.Workflow(name="dwiproc")
dwiproc.base_dir = os.path.abspath('dti_tutorial')
dwiproc.base_dir = os.path.abspath('fsl_dti_tutorial')
dwiproc.connect([
(infosource,datasource,[('subject_id', 'subject_id')]),
(datasource,computeTensor,[('dwi','fslroi.in_file'),
Expand Down
4 changes: 3 additions & 1 deletion nipype/interfaces/diffusion_toolkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from nipype.interfaces.diffusion_toolkit.preproc import DTIRecon
from nipype.interfaces.diffusion_toolkit.postproc import SplineFilter
from nipype.interfaces.diffusion_toolkit.dti import (DTIRecon, DTITracker)
from nipype.interfaces.diffusion_toolkit.odf import (HARDIMat, ODFRecon, ODFTracker)
Loading