Skip to content

Commit 8938187

Browse files
committed
removed doctests, added exception testing when vtk is unavailable
1 parent 7c7b719 commit 8938187

File tree

2 files changed

+71
-113
lines changed

2 files changed

+71
-113
lines changed

nipype/algorithms/mesh.py

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
66
.. testsetup::
77
# Change directory to provide relative paths for doctests
8-
import os
9-
filepath = os.path.dirname(os.path.realpath( __file__ ))
10-
datadir = os.path.realpath(os.path.join(filepath, '../testing/data'))
11-
os.chdir(datadir)
8+
>>> import os
9+
>>> filepath = os.path.dirname(os.path.realpath( __file__ ))
10+
>>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data'))
11+
>>> os.chdir(datadir)
1212
1313
"""
1414
from __future__ import division
@@ -91,25 +91,13 @@ class WarpPoints(TVTKBaseInterface):
9191
``warp`` file. FSL interfaces are compatible, for instance any
9292
field computed with :class:`nipype.interfaces.fsl.utils.ConvertWarp`.
9393
94-
Example
95-
-------
96-
97-
>>> from nipype.algorithms.mesh import have_tvtk
98-
>>> from nipype.algorithms.mesh import WarpPoints
99-
>>> if not have_tvtk:
100-
... wp = WarpPoints()
101-
>>> else:
102-
... wp = WarpPoints()
103-
... wp.inputs.points = 'surf1.vtk'
104-
... wp.inputs.warp = 'warpfield.nii'
105-
... res = wp.run() # doctest: +SKIP
106-
# Exceptions cannot be tested conditionally, so raise error
107-
# https://docs.python.org/2/library/doctest.html#id2
108-
... raise ImportError('This interface requires tvtk to run.')
109-
Traceback (most recent call last):
110-
...
111-
ImportError: This interface requires tvtk to run.
94+
Example::
11295
96+
from nipype.algorithms.mesh import WarpPoints
97+
wp = WarpPoints()
98+
wp.inputs.points = 'surf1.vtk'
99+
wp.inputs.warp = 'warpfield.nii'
100+
res = wp.run()
113101
114102
"""
115103
input_spec = WarpPointsInputSpec
@@ -225,24 +213,13 @@ class ComputeMeshWarp(TVTKBaseInterface):
225213
A point-to-point correspondence between surfaces is required
226214
227215
228-
Example
229-
-------
230-
231-
>>> from nipype.algorithms.mesh import have_tvtk
232-
>>> import nipype.algorithms.mesh as m
233-
>>> if not have_tvtk:
234-
... dist = m.ComputeMeshWarp()
235-
>>> else:
236-
... dist = m.ComputeMeshWarp()
237-
... dist.inputs.surface1 = 'surf1.vtk'
238-
... dist.inputs.surface2 = 'surf2.vtk'
239-
... res = dist.run() # doctest: +SKIP
240-
# Exceptions cannot be tested conditionally, so raise error
241-
# https://docs.python.org/2/library/doctest.html#id2
242-
... raise ImportError('This interface requires tvtk to run.')
243-
Traceback (most recent call last):
244-
...
245-
ImportError: This interface requires tvtk to run.
216+
Example::
217+
218+
import nipype.algorithms.mesh as m
219+
dist = m.ComputeMeshWarp()
220+
dist.inputs.surface1 = 'surf1.vtk'
221+
dist.inputs.surface2 = 'surf2.vtk'
222+
res = dist.run()
246223
247224
"""
248225

@@ -366,25 +343,14 @@ class MeshWarpMaths(TVTKBaseInterface):
366343
A point-to-point correspondence between surfaces is required
367344
368345
369-
Example
370-
-------
371-
372-
>>> from nipype.algorithms.mesh import have_tvtk
373-
>>> import nipype.algorithms.mesh as m
374-
>>> if not have_tvtk:
375-
... mmath = m.MeshWarpMaths()
376-
>>> else:
377-
... mmath = m.MeshWarpMaths()
378-
... mmath.inputs.in_surf = 'surf1.vtk'
379-
... mmath.inputs.operator = 'surf2.vtk'
380-
... mmath.inputs.operation = 'mul'
381-
... res = mmath.run() # doctest: +SKIP
382-
# Exceptions cannot be tested conditionally, so raise error
383-
# https://docs.python.org/2/library/doctest.html#id2
384-
... raise ImportError('This interface requires tvtk to run.')
385-
Traceback (most recent call last):
386-
...
387-
ImportError: This interface requires tvtk to run.
346+
Example::
347+
348+
import nipype.algorithms.mesh as m
349+
mmath = m.MeshWarpMaths()
350+
mmath.inputs.in_surf = 'surf1.vtk'
351+
mmath.inputs.operator = 'surf2.vtk'
352+
mmath.inputs.operation = 'mul'
353+
res = mmath.run()
388354
389355
"""
390356

nipype/algorithms/tests/test_mesh_ops.py

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,73 @@
66
from shutil import rmtree
77
from tempfile import mkdtemp
88

9-
from nipype.testing import (assert_equal, skipif,
9+
from nipype.testing import (assert_equal, assert_raises, skipif,
1010
assert_almost_equal, example_data)
1111

1212
import numpy as np
1313

1414
from nipype.algorithms import mesh as m
15+
from nipype.algorithms.mesh import have_vtk
1516

1617
import platform
1718

18-
notvtk = True
19-
if 'darwin' not in platform.system().lower():
20-
old_ets = os.getenv('ETS_TOOLKIT')
21-
os.environ['ETS_TOOLKIT'] = 'null'
22-
have_tvtk = False
23-
try:
24-
from tvtk.api import tvtk
25-
notvtk = False
26-
except ImportError:
27-
pass
28-
finally:
29-
if old_ets is not None:
30-
os.environ['ETS_TOOLKIT'] = old_ets
31-
else:
32-
del os.environ['ETS_TOOLKIT']
33-
34-
35-
@skipif(notvtk)
19+
3620
def test_ident_distances():
3721
tempdir = mkdtemp()
3822
curdir = os.getcwd()
3923
os.chdir(tempdir)
40-
in_surf = example_data('surf01.vtk')
41-
dist_ident = m.ComputeMeshWarp()
42-
dist_ident.inputs.surface1 = in_surf
43-
dist_ident.inputs.surface2 = in_surf
44-
dist_ident.inputs.out_file = os.path.join(tempdir, 'distance.npy')
45-
res = dist_ident.run()
46-
yield assert_equal, res.outputs.distance, 0.0
47-
48-
dist_ident.inputs.weighting = 'area'
49-
res = dist_ident.run()
50-
yield assert_equal, res.outputs.distance, 0.0
24+
25+
if not have_vtk:
26+
yield assert_raises, ImportError, m.ComputeMeshWarp
27+
else:
28+
in_surf = example_data('surf01.vtk')
29+
dist_ident = m.ComputeMeshWarp()
30+
dist_ident.inputs.surface1 = in_surf
31+
dist_ident.inputs.surface2 = in_surf
32+
dist_ident.inputs.out_file = os.path.join(tempdir, 'distance.npy')
33+
res = dist_ident.run()
34+
yield assert_equal, res.outputs.distance, 0.0
35+
36+
dist_ident.inputs.weighting = 'area'
37+
res = dist_ident.run()
38+
yield assert_equal, res.outputs.distance, 0.0
5139

5240
os.chdir(curdir)
5341
rmtree(tempdir)
5442

5543

56-
@skipif(notvtk)
5744
def test_trans_distances():
5845
tempdir = mkdtemp()
59-
in_surf = example_data('surf01.vtk')
60-
warped_surf = os.path.join(tempdir, 'warped.vtk')
61-
6246
curdir = os.getcwd()
6347
os.chdir(tempdir)
64-
inc = np.array([0.7, 0.3, -0.2])
65-
66-
r1 = tvtk.PolyDataReader(file_name=in_surf)
67-
vtk1 = r1.output
68-
r1.update()
69-
vtk1.points = np.array(vtk1.points) + inc
70-
71-
writer = tvtk.PolyDataWriter(file_name=warped_surf)
72-
writer.set_input_data(vtk1)
73-
writer.write()
74-
75-
dist = m.ComputeMeshWarp()
76-
dist.inputs.surface1 = in_surf
77-
dist.inputs.surface2 = warped_surf
78-
dist.inputs.out_file = os.path.join(tempdir, 'distance.npy')
79-
res = dist.run()
80-
yield assert_almost_equal, res.outputs.distance, np.linalg.norm(inc), 4
81-
dist.inputs.weighting = 'area'
82-
res = dist.run()
83-
yield assert_almost_equal, res.outputs.distance, np.linalg.norm(inc), 4
48+
49+
if not have_vtk:
50+
yield assert_raises, ImportError, m.ComputeMeshWarp
51+
else:
52+
from nipype.algorithms.mesh import tvtk
53+
in_surf = example_data('surf01.vtk')
54+
warped_surf = os.path.join(tempdir, 'warped.vtk')
55+
56+
inc = np.array([0.7, 0.3, -0.2])
57+
58+
r1 = tvtk.PolyDataReader(file_name=in_surf)
59+
vtk1 = r1.output
60+
r1.update()
61+
vtk1.points = np.array(vtk1.points) + inc
62+
63+
writer = tvtk.PolyDataWriter(file_name=warped_surf)
64+
writer.set_input_data(vtk1)
65+
writer.write()
66+
67+
dist = m.ComputeMeshWarp()
68+
dist.inputs.surface1 = in_surf
69+
dist.inputs.surface2 = warped_surf
70+
dist.inputs.out_file = os.path.join(tempdir, 'distance.npy')
71+
res = dist.run()
72+
yield assert_almost_equal, res.outputs.distance, np.linalg.norm(inc), 4
73+
dist.inputs.weighting = 'area'
74+
res = dist.run()
75+
yield assert_almost_equal, res.outputs.distance, np.linalg.norm(inc), 4
8476

8577
os.chdir(curdir)
8678
rmtree(tempdir)

0 commit comments

Comments
 (0)