26
26
iflogger = logging .getLogger ('interface' )
27
27
28
28
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
+
29
43
class WarpPointsInputSpec (BaseInterfaceInputSpec ):
30
44
points = File (exists = True , mandatory = True ,
31
45
desc = ('file containing the point set' ))
@@ -42,7 +56,7 @@ class WarpPointsOutputSpec(TraitedSpec):
42
56
out_points = File (desc = 'the warped point set' )
43
57
44
58
45
- class WarpPoints (BaseInterface ):
59
+ class WarpPoints (TVTKBaseInterface ):
46
60
47
61
"""
48
62
Applies a displacement field to a point set given in vtk format.
@@ -62,7 +76,6 @@ class WarpPoints(BaseInterface):
62
76
"""
63
77
input_spec = WarpPointsInputSpec
64
78
output_spec = WarpPointsOutputSpec
65
- _redirect_x = True
66
79
67
80
def _gen_fname (self , in_file , suffix = 'generated' , ext = None ):
68
81
import os .path as op
@@ -81,30 +94,15 @@ def _gen_fname(self, in_file, suffix='generated', ext=None):
81
94
return op .abspath ('%s_%s.%s' % (fname , suffix , ext ))
82
95
83
96
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
90
100
91
101
try :
92
102
from tvtk .api import tvtk
93
103
except ImportError :
94
104
raise ImportError ('Interface requires tvtk' )
95
105
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
-
108
106
r = tvtk .PolyDataReader (file_name = self .inputs .points )
109
107
r .update ()
110
108
mesh = r .output
@@ -134,7 +132,7 @@ def _run_interface(self, runtime):
134
132
newpoints = [p + d for p , d in zip (points , disps )]
135
133
mesh .points = newpoints
136
134
w = tvtk .PolyDataWriter ()
137
- if vtk_major <= 5 :
135
+ if self . _vtk_major <= 5 :
138
136
w .input = mesh
139
137
else :
140
138
w .set_input_data_object (mesh )
@@ -182,7 +180,7 @@ class ComputeMeshWarpOutputSpec(TraitedSpec):
182
180
desc = 'numpy file keeping computed distances and weights' )
183
181
184
182
185
- class ComputeMeshWarp (BaseInterface ):
183
+ class ComputeMeshWarp (TVTKBaseInterface ):
186
184
187
185
"""
188
186
Calculates a the vertex-wise warping to get surface2 from surface1.
@@ -207,7 +205,6 @@ class ComputeMeshWarp(BaseInterface):
207
205
208
206
input_spec = ComputeMeshWarpInputSpec
209
207
output_spec = ComputeMeshWarpOutputSpec
210
- _redirect_x = True
211
208
212
209
def _triangle_area (self , A , B , C ):
213
210
A = np .array (A )
@@ -223,15 +220,7 @@ def _run_interface(self, runtime):
223
220
try :
224
221
from tvtk .api import tvtk
225
222
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' )
235
224
236
225
r1 = tvtk .PolyDataReader (file_name = self .inputs .surface1 )
237
226
r2 = tvtk .PolyDataReader (file_name = self .inputs .surface2 )
@@ -280,7 +269,12 @@ def _run_interface(self, runtime):
280
269
out_mesh .point_data .vectors .name = 'warpings'
281
270
writer = tvtk .PolyDataWriter (
282
271
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
+
284
278
writer .write ()
285
279
286
280
self ._distance = np .average (errvector , weights = weights )
@@ -322,7 +316,7 @@ class MeshWarpMathsOutputSpec(TraitedSpec):
322
316
desc = 'vtk with surface warped' )
323
317
324
318
325
- class MeshWarpMaths (BaseInterface ):
319
+ class MeshWarpMaths (TVTKBaseInterface ):
326
320
327
321
"""
328
322
Performs the most basic mathematical operations on the warping field
@@ -348,21 +342,12 @@ class MeshWarpMaths(BaseInterface):
348
342
349
343
input_spec = MeshWarpMathsInputSpec
350
344
output_spec = MeshWarpMathsOutputSpec
351
- _redirect_x = True
352
345
353
346
def _run_interface (self , runtime ):
354
347
try :
355
348
from tvtk .api import tvtk
356
349
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' )
366
351
367
352
r1 = tvtk .PolyDataReader (file_name = self .inputs .in_surf )
368
353
vtk1 = r1 .output
@@ -412,14 +397,21 @@ def _run_interface(self, runtime):
412
397
vtk1 .point_data .vectors = warping
413
398
writer = tvtk .PolyDataWriter (
414
399
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 )
416
404
writer .write ()
417
405
418
406
vtk1 .point_data .vectors = None
419
407
vtk1 .points = points1 + warping
420
408
writer = tvtk .PolyDataWriter (
421
409
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 )
423
415
writer .write ()
424
416
425
417
return runtime
0 commit comments