Skip to content

Commit 6a8f6d6

Browse files
committed
Merge pull request #1219 from oesteban/fix/TVTKPolyDataWriter
[FIX] Created TVTKBaseInterface. Should fix #1218
2 parents 80e284f + 9c2749d commit 6a8f6d6

File tree

3 files changed

+65
-50
lines changed

3 files changed

+65
-50
lines changed

CHANGES

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
Next Release
1+
Next release
22
============
33

4+
* FIX: VTK version check missing when using tvtk (https://github.com/nipy/nipype/pull/1219)
45
* ENH: Added an OAR scheduler plugin (https://github.com/nipy/nipype/pull/1259)
56
* ENH: New ANTs interface: antsBrainExtraction (https://github.com/nipy/nipype/pull/1231)
67
* API: Default model level for the bedpostx workflow has been set to "2" following FSL 5.0.9 lead
78
* ENH: New interfaces for interacting with AWS S3: S3DataSink and S3DataGrabber (https://github.com/nipy/nipype/pull/1201)
89

10+
911
Release 0.11.0 (September 15, 2015)
1012
============
1113

nipype/algorithms/mesh.py

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
iflogger = logging.getLogger('interface')
2727

2828

29+
class TVTKBaseInterface(BaseInterface):
30+
_redirect_x = True
31+
_vtk_major = 6
32+
33+
def __init__(self, **inputs):
34+
try:
35+
from tvtk.tvtk_classes.vtk_version import vtk_build_version
36+
self._vtk_major = int(vtk_build_version[0])
37+
except ImportError:
38+
iflogger.warning('VTK version-major inspection using tvtk failed.')
39+
40+
super(TVTKBaseInterface, self).__init__(**inputs)
41+
42+
2943
class WarpPointsInputSpec(BaseInterfaceInputSpec):
3044
points = File(exists=True, mandatory=True,
3145
desc=('file containing the point set'))
@@ -42,7 +56,7 @@ class WarpPointsOutputSpec(TraitedSpec):
4256
out_points = File(desc='the warped point set')
4357

4458

45-
class WarpPoints(BaseInterface):
59+
class WarpPoints(TVTKBaseInterface):
4660

4761
"""
4862
Applies a displacement field to a point set given in vtk format.
@@ -62,7 +76,6 @@ class WarpPoints(BaseInterface):
6276
"""
6377
input_spec = WarpPointsInputSpec
6478
output_spec = WarpPointsOutputSpec
65-
_redirect_x = True
6679

6780
def _gen_fname(self, in_file, suffix='generated', ext=None):
6881
import os.path as op
@@ -81,30 +94,15 @@ def _gen_fname(self, in_file, suffix='generated', ext=None):
8194
return op.abspath('%s_%s.%s' % (fname, suffix, ext))
8295

8396
def _run_interface(self, runtime):
84-
vtk_major = 6
85-
try:
86-
import vtk
87-
vtk_major = vtk.VTK_MAJOR_VERSION
88-
except ImportError:
89-
iflogger.warn(('python-vtk could not be imported'))
97+
import nibabel as nb
98+
import numpy as np
99+
from scipy import ndimage
90100

91101
try:
92102
from tvtk.api import tvtk
93103
except ImportError:
94104
raise ImportError('Interface requires tvtk')
95105

96-
try:
97-
from enthought.etsconfig.api import ETSConfig
98-
ETSConfig.toolkit = 'null'
99-
except ImportError:
100-
iflogger.warn(('ETS toolkit could not be imported'))
101-
except ValueError:
102-
iflogger.warn(('ETS toolkit could not be set to null'))
103-
104-
import nibabel as nb
105-
import numpy as np
106-
from scipy import ndimage
107-
108106
r = tvtk.PolyDataReader(file_name=self.inputs.points)
109107
r.update()
110108
mesh = r.output
@@ -134,7 +132,7 @@ def _run_interface(self, runtime):
134132
newpoints = [p + d for p, d in zip(points, disps)]
135133
mesh.points = newpoints
136134
w = tvtk.PolyDataWriter()
137-
if vtk_major <= 5:
135+
if self._vtk_major <= 5:
138136
w.input = mesh
139137
else:
140138
w.set_input_data_object(mesh)
@@ -182,7 +180,7 @@ class ComputeMeshWarpOutputSpec(TraitedSpec):
182180
desc='numpy file keeping computed distances and weights')
183181

184182

185-
class ComputeMeshWarp(BaseInterface):
183+
class ComputeMeshWarp(TVTKBaseInterface):
186184

187185
"""
188186
Calculates a the vertex-wise warping to get surface2 from surface1.
@@ -207,7 +205,6 @@ class ComputeMeshWarp(BaseInterface):
207205

208206
input_spec = ComputeMeshWarpInputSpec
209207
output_spec = ComputeMeshWarpOutputSpec
210-
_redirect_x = True
211208

212209
def _triangle_area(self, A, B, C):
213210
A = np.array(A)
@@ -223,15 +220,7 @@ def _run_interface(self, runtime):
223220
try:
224221
from tvtk.api import tvtk
225222
except ImportError:
226-
raise ImportError('Interface ComputeMeshWarp requires tvtk')
227-
228-
try:
229-
from enthought.etsconfig.api import ETSConfig
230-
ETSConfig.toolkit = 'null'
231-
except ImportError:
232-
iflogger.warn(('ETS toolkit could not be imported'))
233-
except ValueError:
234-
iflogger.warn(('ETS toolkit is already set'))
223+
raise ImportError('Interface requires tvtk')
235224

236225
r1 = tvtk.PolyDataReader(file_name=self.inputs.surface1)
237226
r2 = tvtk.PolyDataReader(file_name=self.inputs.surface2)
@@ -280,7 +269,12 @@ def _run_interface(self, runtime):
280269
out_mesh.point_data.vectors.name = 'warpings'
281270
writer = tvtk.PolyDataWriter(
282271
file_name=op.abspath(self.inputs.out_warp))
283-
writer.set_input_data(out_mesh)
272+
273+
if self._vtk_major <= 5:
274+
writer.input = mesh
275+
else:
276+
writer.set_input_data_object(mesh)
277+
284278
writer.write()
285279

286280
self._distance = np.average(errvector, weights=weights)
@@ -322,7 +316,7 @@ class MeshWarpMathsOutputSpec(TraitedSpec):
322316
desc='vtk with surface warped')
323317

324318

325-
class MeshWarpMaths(BaseInterface):
319+
class MeshWarpMaths(TVTKBaseInterface):
326320

327321
"""
328322
Performs the most basic mathematical operations on the warping field
@@ -348,21 +342,12 @@ class MeshWarpMaths(BaseInterface):
348342

349343
input_spec = MeshWarpMathsInputSpec
350344
output_spec = MeshWarpMathsOutputSpec
351-
_redirect_x = True
352345

353346
def _run_interface(self, runtime):
354347
try:
355348
from tvtk.api import tvtk
356349
except ImportError:
357-
raise ImportError('Interface ComputeMeshWarp requires tvtk')
358-
359-
try:
360-
from enthought.etsconfig.api import ETSConfig
361-
ETSConfig.toolkit = 'null'
362-
except ImportError:
363-
iflogger.warn(('ETS toolkit could not be imported'))
364-
except ValueError:
365-
iflogger.warn(('ETS toolkit is already set'))
350+
raise ImportError('Interface requires tvtk')
366351

367352
r1 = tvtk.PolyDataReader(file_name=self.inputs.in_surf)
368353
vtk1 = r1.output
@@ -412,14 +397,21 @@ def _run_interface(self, runtime):
412397
vtk1.point_data.vectors = warping
413398
writer = tvtk.PolyDataWriter(
414399
file_name=op.abspath(self.inputs.out_warp))
415-
writer.set_input_data(vtk1)
400+
if self._vtk_major <= 5:
401+
writer.input = vtk1
402+
else:
403+
writer.set_input_data_object(vtk1)
416404
writer.write()
417405

418406
vtk1.point_data.vectors = None
419407
vtk1.points = points1 + warping
420408
writer = tvtk.PolyDataWriter(
421409
file_name=op.abspath(self.inputs.out_file))
422-
writer.set_input_data(vtk1)
410+
411+
if self._vtk_major <= 5:
412+
writer.input = vtk1
413+
else:
414+
writer.set_input_data_object(vtk1)
423415
writer.write()
424416

425417
return runtime

nipype/interfaces/fsl/utils.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,9 +1870,18 @@ def _vtk_to_coords(self, in_file, out_file=None):
18701870
except ImportError:
18711871
raise ImportError('This interface requires tvtk to run.')
18721872

1873+
vtk_major = 5
1874+
try:
1875+
from tvtk.tvtk_classes.vtk_version import vtk_build_version
1876+
vtk_major = int(vtk_build_version[0])
1877+
except ImportError:
1878+
iflogger.warning('VTK version-major inspection using tvtk failed.')
1879+
18731880
reader = tvtk.PolyDataReader(file_name=in_file + '.vtk')
18741881
reader.update()
1875-
points = reader.output.points
1882+
1883+
mesh = reader.output if vtk_major < 6 else reader.get_output()
1884+
points = mesh.points
18761885

18771886
if out_file is None:
18781887
out_file, _ = op.splitext(in_file) + '.txt'
@@ -1887,12 +1896,24 @@ def _coords_to_vtk(self, points, out_file):
18871896
except ImportError:
18881897
raise ImportError('This interface requires tvtk to run.')
18891898

1899+
vtk_major = 5
1900+
try:
1901+
from tvtk.tvtk_classes.vtk_version import vtk_build_version
1902+
vtk_major = int(vtk_build_version[0])
1903+
except ImportError:
1904+
iflogger.warning('VTK version-major inspection using tvtk failed.')
1905+
18901906
reader = tvtk.PolyDataReader(file_name=self.inputs.in_file)
18911907
reader.update()
1892-
mesh = reader.output
1908+
1909+
mesh = reader.output if vtk_major < 6 else reader.get_output()
18931910
mesh.points = points
18941911

1895-
writer = tvtk.PolyDataWriter(file_name=out_file, input=mesh)
1912+
writer = tvtk.PolyDataWriter(file_name=out_file)
1913+
if vtk_major < 6:
1914+
writer.input = mesh
1915+
else:
1916+
writer.set_input_data_object(mesh)
18961917
writer.write()
18971918

18981919
def _trk_to_coords(self, in_file, out_file=None):

0 commit comments

Comments
 (0)