2626iflogger = 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+
2943class 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
0 commit comments