Skip to content

Commit 8ad4bf9

Browse files
committed
Merge pull request #963 from oesteban/bug/TVTKDisplayError
Improve tvtk import
2 parents 4569385 + b3daa05 commit 8ad4bf9

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

nipype/algorithms/mesh.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ class P2PDistanceInputSpec(BaseInterfaceInputSpec):
3434
'"surface": edge distance is weighted by the '
3535
'corresponding surface area'))
3636

37+
3738
class P2PDistanceOutputSpec(TraitedSpec):
3839
distance = traits.Float(desc="computed distance")
3940

41+
4042
class P2PDistance(BaseInterface):
43+
4144
"""Calculates a point-to-point (p2p) distance between two corresponding
4245
VTK-readable meshes or contours.
4346
@@ -57,41 +60,52 @@ class P2PDistance(BaseInterface):
5760
output_spec = P2PDistanceOutputSpec
5861

5962
def _triangle_area(self, A, B, C):
60-
ABxAC = euclidean(A,B) * euclidean(A,C)
61-
prod = np.dot(np.array(B)-np.array(A),np.array(C)-np.array(A))
62-
angle = np.arccos( prod / ABxAC )
63-
area = 0.5 * ABxAC * np.sin( angle )
63+
ABxAC = euclidean(A, B) * euclidean(A, C)
64+
prod = np.dot(np.array(B) - np.array(A), np.array(C) - np.array(A))
65+
angle = np.arccos(prod / ABxAC)
66+
area = 0.5 * ABxAC * np.sin(angle)
6467
return area
6568

6669
def _run_interface(self, runtime):
67-
from tvtk.api import tvtk
68-
r1 = tvtk.PolyDataReader( file_name=self.inputs.surface1 )
69-
r2 = tvtk.PolyDataReader( file_name=self.inputs.surface2 )
70+
try:
71+
from tvtk.api import tvtk
72+
except ImportError:
73+
raise ImportError('Interface P2PDistance requires tvtk')
74+
75+
try:
76+
from enthought.etsconfig.api import ETSConfig
77+
ETSConfig.toolkit = 'null'
78+
except ImportError:
79+
iflogger.warn(('ETS toolkit could not be imported'))
80+
pass
81+
82+
r1 = tvtk.PolyDataReader(file_name=self.inputs.surface1)
83+
r2 = tvtk.PolyDataReader(file_name=self.inputs.surface2)
7084
vtk1 = r1.output
7185
vtk2 = r2.output
7286
r1.update()
7387
r2.update()
74-
assert( len(vtk1.points) == len(vtk2.points) )
88+
assert(len(vtk1.points) == len(vtk2.points))
7589
d = 0.0
7690
totalWeight = 0.0
7791

7892
points = vtk1.points
79-
faces = vtk1.polys.to_array().reshape(-1,4).astype(int)[:,1:]
93+
faces = vtk1.polys.to_array().reshape(-1, 4).astype(int)[:, 1:]
8094

81-
for p1,p2 in zip( points, vtk2.points ):
95+
for p1, p2 in zip(points, vtk2.points):
8296
weight = 1.0
8397
if (self.inputs.weighting == 'surface'):
84-
#compute surfaces, set in weight
98+
# compute surfaces, set in weight
8599
weight = 0.0
86-
point_faces = faces[ (faces[:,:]==0).any(axis=1) ]
100+
point_faces = faces[(faces[:, :] == 0).any(axis=1)]
87101

88102
for idset in point_faces:
89-
p1 = points[ int(idset[0]) ]
90-
p2 = points[ int(idset[1]) ]
91-
p3 = points[ int(idset[2]) ]
103+
p1 = points[int(idset[0])]
104+
p2 = points[int(idset[1])]
105+
p3 = points[int(idset[2])]
92106
weight = weight + self._triangle_area(p1, p2, p3)
93107

94-
d+= weight*euclidean( p1, p2 )
108+
d += weight * euclidean(p1, p2)
95109
totalWeight = totalWeight + weight
96110

97111
self._distance = d / totalWeight
@@ -101,4 +115,3 @@ def _list_outputs(self):
101115
outputs = self._outputs().get()
102116
outputs['distance'] = self._distance
103117
return outputs
104-

0 commit comments

Comments
 (0)