14
14
from __future__ import division
15
15
from builtins import zip
16
16
17
- import os
18
17
import os .path as op
19
18
from warnings import warn
20
19
25
24
from ..external .six import string_types
26
25
from ..interfaces .base import (BaseInterface , traits , TraitedSpec , File ,
27
26
BaseInterfaceInputSpec )
28
-
27
+ from ..interfaces .vtkbase import tvtk
28
+ from ..interfaces import vtkbase as VTKInfo
29
29
iflogger = logging .getLogger ('interface' )
30
30
31
- # Ensure that tvtk is loaded with the appropriate ETS_TOOLKIT env var
32
- old_ets = os .getenv ('ETS_TOOLKIT' )
33
- os .environ ['ETS_TOOLKIT' ] = 'null'
34
- have_tvtk = False
35
- try :
36
- from tvtk .api import tvtk
37
- have_tvtk = True
38
- except ImportError :
39
- iflogger .warning ('tvtk wasn\' t found' )
40
- finally :
41
- if old_ets is not None :
42
- os .environ ['ETS_TOOLKIT' ] = old_ets
43
- else :
44
- del os .environ ['ETS_TOOLKIT' ]
45
-
46
-
47
- class Info (object ):
48
- """ Handle VTK version information """
49
- _vtk_version = None
50
-
51
- @staticmethod
52
- def vtk_version ():
53
- """ Get VTK version """
54
- if not Info .no_tvtk ():
55
- return None
56
-
57
- if Info ._vtk_version is None :
58
- try :
59
- from tvtk .tvtk_classes .vtk_version import vtk_build_version
60
- vsplits = vtk_build_version .split ('.' )
61
- Info ._vtk_version = tuple ([int (vsplits [0 ]), int (vsplits [1 ])] + vsplits [2 :])
62
- except ImportError :
63
- iflogger .warning (
64
- 'VTK version-major inspection using tvtk failed, assuming VTK == 4.0.' )
65
- Info ._vtk_version = (4 , 0 )
66
-
67
- return Info ._vtk_version
68
-
69
- @staticmethod
70
- def no_tvtk ():
71
- global have_tvtk
72
- return not have_tvtk
73
-
74
31
75
32
class TVTKBaseInterface (BaseInterface ):
76
33
@@ -79,13 +36,10 @@ class TVTKBaseInterface(BaseInterface):
79
36
_redirect_x = True
80
37
81
38
def __init__ (self , ** inputs ):
82
- if Info .no_tvtk ():
39
+ if VTKInfo .no_tvtk ():
83
40
raise ImportError ('This interface requires tvtk to run.' )
84
41
super (TVTKBaseInterface , self ).__init__ (** inputs )
85
42
86
- def vtk_version (self ):
87
- return Info .vtk_version ()
88
-
89
43
90
44
class WarpPointsInputSpec (BaseInterfaceInputSpec ):
91
45
points = File (exists = True , mandatory = True ,
@@ -144,17 +98,15 @@ def _run_interface(self, runtime):
144
98
import nibabel as nb
145
99
import numpy as np
146
100
from scipy import ndimage
147
- from tvtk .common import configure_input_data
148
- from tvtk .common import is_old_pipeline as vtk_old
149
101
150
102
r = tvtk .PolyDataReader (file_name = self .inputs .points )
151
103
r .update ()
152
- mesh = r . output if vtk_old () else r . get_output ( )
104
+ mesh = VTKInfo . vtk_output ( r )
153
105
points = np .array (mesh .points )
154
106
warp_dims = nb .funcs .four_to_three (nb .load (self .inputs .warp ))
155
107
156
108
affine = warp_dims [0 ].affine
157
- voxsize = warp_dims [0 ].header .get_zooms ()
109
+ # voxsize = warp_dims[0].header.get_zooms()
158
110
vox2ras = affine [0 :3 , 0 :3 ]
159
111
ras2vox = np .linalg .inv (vox2ras )
160
112
origin = affine [0 :3 , 3 ]
@@ -176,7 +128,7 @@ def _run_interface(self, runtime):
176
128
newpoints = [p + d for p , d in zip (points , disps )]
177
129
mesh .points = newpoints
178
130
w = tvtk .PolyDataWriter ()
179
- configure_input_data (w , mesh )
131
+ VTKInfo . configure_input_data (w , mesh )
180
132
w .file_name = self ._gen_fname (self .inputs .points , suffix = 'warped' , ext = '.vtk' )
181
133
w .write ()
182
134
return runtime
@@ -253,13 +205,10 @@ def _triangle_area(self, A, B, C):
253
205
return area
254
206
255
207
def _run_interface (self , runtime ):
256
- from tvtk .common import configure_input_data
257
- from tvtk .common import is_old_pipeline as vtk_old
258
-
259
208
r1 = tvtk .PolyDataReader (file_name = self .inputs .surface1 )
260
209
r2 = tvtk .PolyDataReader (file_name = self .inputs .surface2 )
261
- vtk1 = r1 . output if vtk_old () else r1 . get_output ( )
262
- vtk2 = r2 . output if vtk_old () else r2 . get_output ( )
210
+ vtk1 = VTKInfo . vtk_output ( r1 )
211
+ vtk2 = VTKInfo . vtk_output ( r2 )
263
212
r1 .update ()
264
213
r2 .update ()
265
214
assert (len (vtk1 .points ) == len (vtk2 .points ))
@@ -303,7 +252,7 @@ def _run_interface(self, runtime):
303
252
out_mesh .point_data .vectors .name = 'warpings'
304
253
writer = tvtk .PolyDataWriter (
305
254
file_name = op .abspath (self .inputs .out_warp ))
306
- configure_input_data (writer , out_mesh )
255
+ VTKInfo . configure_input_data (writer , out_mesh )
307
256
writer .write ()
308
257
309
258
self ._distance = np .average (errvector , weights = weights )
@@ -372,11 +321,8 @@ class MeshWarpMaths(TVTKBaseInterface):
372
321
output_spec = MeshWarpMathsOutputSpec
373
322
374
323
def _run_interface (self , runtime ):
375
- from tvtk .common import configure_input_data
376
- from tvtk .common import is_old_pipeline as vtk_old
377
-
378
324
r1 = tvtk .PolyDataReader (file_name = self .inputs .in_surf )
379
- vtk1 = r1 . output if vtk_old () else r1 . get_output ( )
325
+ vtk1 = VTKInfo . vtk_output ( r1 )
380
326
r1 .update ()
381
327
points1 = np .array (vtk1 .points )
382
328
@@ -388,7 +334,7 @@ def _run_interface(self, runtime):
388
334
389
335
if isinstance (operator , string_types ):
390
336
r2 = tvtk .PolyDataReader (file_name = self .inputs .surface2 )
391
- vtk2 = r2 . output if vtk_old () else r2 . get_output ( )
337
+ vtk2 = VTKInfo . vtk_output ( r2 )
392
338
r2 .update ()
393
339
assert (len (points1 ) == len (vtk2 .points ))
394
340
@@ -399,7 +345,7 @@ def _run_interface(self, runtime):
399
345
400
346
if opfield is None :
401
347
raise RuntimeError (
402
- ( 'No operator values found in operator file' ) )
348
+ 'No operator values found in operator file' )
403
349
404
350
opfield = np .array (opfield )
405
351
@@ -422,13 +368,13 @@ def _run_interface(self, runtime):
422
368
423
369
vtk1 .point_data .vectors = warping
424
370
writer = tvtk .PolyDataWriter (file_name = op .abspath (self .inputs .out_warp ))
425
- configure_input_data (writer , vtk1 )
371
+ VTKInfo . configure_input_data (writer , vtk1 )
426
372
writer .write ()
427
373
428
374
vtk1 .point_data .vectors = None
429
375
vtk1 .points = points1 + warping
430
376
writer = tvtk .PolyDataWriter (file_name = op .abspath (self .inputs .out_file ))
431
- configure_input_data (writer , vtk1 )
377
+ VTKInfo . configure_input_data (writer , vtk1 )
432
378
writer .write ()
433
379
return runtime
434
380
0 commit comments