-
Notifications
You must be signed in to change notification settings - Fork 532
/
Copy pathtessellation_tutorial.py
111 lines (86 loc) · 3.54 KB
/
tessellation_tutorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
"""
=================================================
sMRI: Regional Tessellation and Surface Smoothing
=================================================
Introduction
============
This script, tessellation_tutorial.py, demonstrates the use of create_tessellation_flow from nipype.workflows.smri.freesurfer, and it can be run with::
python tessellation_tutorial.py
This example requires that the user has Freesurfer installed, and that the Freesurfer directory for 'fsaverage' is present.
.. seealso::
ConnectomeViewer
The Connectome Viewer connects Multi-Modal Multi-Scale Neuroimaging and Network Datasets For Analysis and Visualization in Python.
http://www.geuz.org/gmsh/
Gmsh: a three-dimensional finite element mesh generator with built-in pre- and post-processing facilities
http://www.blender.org/
Blender is the free open source 3D content creation suite, available for all major operating systems under the GNU General Public License.
.. warning::
This workflow will take several hours to finish entirely, since smoothing the larger cortical surfaces is very time consuming.
Packages and Data Setup
=======================
Import the necessary modules and workflow from nipype.
"""
import nipype.pipeline.engine as pe # pypeline engine
import nipype.interfaces.cmtk as cmtk
import nipype.interfaces.io as nio # Data i/o
import os
import os.path as op
from nipype.workflows.smri.freesurfer import create_tessellation_flow
"""
Directories
===========
Set the default directory and lookup table (LUT) paths
"""
fs_dir = os.environ['FREESURFER_HOME']
lookup_file = op.join(fs_dir, 'FreeSurferColorLUT.txt')
subjects_dir = op.join(fs_dir, 'subjects/')
output_dir = './tessellate_tutorial'
"""
Inputs
======
Create the tessellation workflow and set inputs
Here we will choose Gifti (gii) as the output format, because
we want to able to view the surface in ConnectomeViewer.
In you intend to view the meshes in gmsh or Blender, you should change
the workflow creation to use stereolithographic (stl) format.
"""
tessflow = create_tessellation_flow(name='tessflow', out_format='gii')
tessflow.inputs.inputspec.subject_id = 'fsaverage'
tessflow.inputs.inputspec.subjects_dir = subjects_dir
tessflow.inputs.inputspec.lookup_file = lookup_file
"""
We also create a conditional node to package the surfaces for ConnectomeViewer.
Simply set cff to "False" to ignore this step.
"""
cff = True
if cff:
cff = pe.Node(interface=cmtk.CFFConverter(), name='cff')
cff.inputs.out_file = 'Meshes.cff'
"""
Outputs
=======
Create a datasink to organize the smoothed meshes
Using regular-expression substitutions we can remove the extraneous folders generated by the mapnode.
"""
datasink = pe.Node(interface=nio.DataSink(), name="datasink")
datasink.inputs.base_directory = 'meshes'
datasink.inputs.regexp_substitutions = [('_smoother[\d]*/', '')]
"""
Execution
=========
Finally, create and run another pipeline that connects the workflow and datasink
"""
tesspipe = pe.Workflow(name='tessellate_tutorial')
tesspipe.base_dir = output_dir
tesspipe.connect([(tessflow, datasink, [('outputspec.meshes',
'@meshes.all')])])
"""
If the surfaces are to be packaged, this will connect the CFFConverter
node to the tessellation and smoothing workflow, as well as to the datasink.
"""
if cff:
tesspipe.connect([(tessflow, cff, [('outputspec.meshes',
'gifti_surfaces')])])
tesspipe.connect([(cff, datasink, [('connectome_file', '@cff')])])
tesspipe.run()