Skip to content

Commit 38b611b

Browse files
committed
TST: Make proj3d tests into real tests.
Bundling them up in the middle of the code ensures they're never run.
1 parent c93dff4 commit 38b611b

File tree

4 files changed

+117
-88
lines changed

4 files changed

+117
-88
lines changed

lib/mpl_toolkits/mplot3d/proj3d.py

-86
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import six
1010
from six.moves import zip
1111

12-
from matplotlib.collections import LineCollection
13-
from matplotlib.patches import Circle
1412
import numpy as np
1513
import numpy.linalg as linalg
1614

@@ -69,27 +67,6 @@ def line2d_seg_dist(p1, p2, p0):
6967

7068
return d
7169

72-
def test_lines_dists():
73-
import pylab
74-
ax = pylab.gca()
75-
76-
xs, ys = (0,30), (20,150)
77-
pylab.plot(xs, ys)
78-
points = list(zip(xs, ys))
79-
p0, p1 = points
80-
81-
xs, ys = (0,0,20,30), (100,150,30,200)
82-
pylab.scatter(xs, ys)
83-
84-
dist = line2d_seg_dist(p0, p1, (xs[0], ys[0]))
85-
dist = line2d_seg_dist(p0, p1, np.array((xs, ys)))
86-
for x, y, d in zip(xs, ys, dist):
87-
c = Circle((x, y), d, fill=0)
88-
ax.add_patch(c)
89-
90-
pylab.xlim(-200, 200)
91-
pylab.ylim(-200, 200)
92-
pylab.show()
9370

9471
def mod(v):
9572
"""3d vector length"""
@@ -105,12 +82,6 @@ def world_transformation(xmin, xmax,
10582
[0,0,1.0/dz,-zmin/dz],
10683
[0,0,0,1.0]])
10784

108-
def test_world():
109-
xmin, xmax = 100, 120
110-
ymin, ymax = -100, 100
111-
zmin, zmax = 0.1, 0.2
112-
M = world_transformation(xmin, xmax, ymin, ymax, zmin, zmax)
113-
print(M)
11485

11586
def view_transformation(E, R, V):
11687
n = (E - R)
@@ -218,53 +189,6 @@ def proj_trans_clip_points(points, M):
218189
xs, ys, zs = list(zip(*points))
219190
return proj_transform_clip(xs, ys, zs, M)
220191

221-
def test_proj_draw_axes(M, s=1):
222-
import pylab
223-
xs, ys, zs = [0, s, 0, 0], [0, 0, s, 0], [0, 0, 0, s]
224-
txs, tys, tzs = proj_transform(xs, ys, zs, M)
225-
o, ax, ay, az = (txs[0], tys[0]), (txs[1], tys[1]), \
226-
(txs[2], tys[2]), (txs[3], tys[3])
227-
lines = [(o, ax), (o, ay), (o, az)]
228-
229-
ax = pylab.gca()
230-
linec = LineCollection(lines)
231-
ax.add_collection(linec)
232-
for x, y, t in zip(txs, tys, ['o', 'x', 'y', 'z']):
233-
pylab.text(x, y, t)
234-
235-
def test_proj_make_M(E=None):
236-
# eye point
237-
E = E or np.array([1, -1, 2]) * 1000
238-
#E = np.array([20,10,20])
239-
R = np.array([1, 1, 1]) * 100
240-
V = np.array([0, 0, 1])
241-
viewM = view_transformation(E, R, V)
242-
perspM = persp_transformation(100, -100)
243-
M = np.dot(perspM, viewM)
244-
return M
245-
246-
def test_proj():
247-
import pylab
248-
M = test_proj_make_M()
249-
250-
ts = ['%d' % i for i in [0,1,2,3,0,4,5,6,7,4]]
251-
xs, ys, zs = [0,1,1,0,0, 0,1,1,0,0], [0,0,1,1,0, 0,0,1,1,0], \
252-
[0,0,0,0,0, 1,1,1,1,1]
253-
xs, ys, zs = [np.array(v)*300 for v in (xs, ys, zs)]
254-
#
255-
test_proj_draw_axes(M, s=400)
256-
txs, tys, tzs = proj_transform(xs, ys, zs, M)
257-
ixs, iys, izs = inv_transform(txs, tys, tzs, M)
258-
259-
pylab.scatter(txs, tys, c=tzs)
260-
pylab.plot(txs, tys, c='r')
261-
for x, y, t in zip(txs, tys, ts):
262-
pylab.text(x, y, t)
263-
264-
pylab.xlim(-0.2, 0.2)
265-
pylab.ylim(-0.2, 0.2)
266-
267-
pylab.show()
268192

269193
def rot_x(V, alpha):
270194
cosa, sina = np.cos(alpha), np.sin(alpha)
@@ -274,13 +198,3 @@ def rot_x(V, alpha):
274198
[0,0,0,0]])
275199

276200
return np.dot(M1, V)
277-
278-
def test_rot():
279-
V = [1,0,0,1]
280-
print(rot_x(V, np.pi/6))
281-
V = [0,1,0,1]
282-
print(rot_x(V, np.pi/6))
283-
284-
285-
if __name__ == "__main__":
286-
test_proj()

lib/mpl_toolkits/tests/test_mplot3d.py

+117-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import sys
22
import nose
33
from nose.tools import assert_raises
4-
from mpl_toolkits.mplot3d import Axes3D, axes3d
4+
from mpl_toolkits.mplot3d import Axes3D, axes3d, proj3d
55
from matplotlib import cm
66
from matplotlib.testing.decorators import image_comparison, cleanup
7+
from matplotlib.collections import LineCollection
8+
from matplotlib.patches import Circle
79
import matplotlib.pyplot as plt
810
import numpy as np
911

@@ -345,7 +347,120 @@ def test_plotsurface_1d_raises():
345347
fig = plt.figure(figsize=(14,6))
346348
ax = fig.add_subplot(1, 2, 1, projection='3d')
347349
assert_raises(ValueError, ax.plot_surface, X, Y, z)
348-
350+
351+
352+
def _test_proj_make_M():
353+
# eye point
354+
E = np.array([1000, -1000, 2000])
355+
R = np.array([100, 100, 100])
356+
V = np.array([0, 0, 1])
357+
viewM = proj3d.view_transformation(E, R, V)
358+
perspM = proj3d.persp_transformation(100, -100)
359+
M = np.dot(perspM, viewM)
360+
return M
361+
362+
363+
def test_proj_transform():
364+
M = _test_proj_make_M()
365+
366+
xs = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0]) * 300.0
367+
ys = np.array([0, 0, 1, 1, 0, 0, 0, 1, 1, 0]) * 300.0
368+
zs = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) * 300.0
369+
370+
txs, tys, tzs = proj3d.proj_transform(xs, ys, zs, M)
371+
ixs, iys, izs = proj3d.inv_transform(txs, tys, tzs, M)
372+
373+
np.testing.assert_almost_equal(ixs, xs)
374+
np.testing.assert_almost_equal(iys, ys)
375+
np.testing.assert_almost_equal(izs, zs)
376+
377+
378+
def _test_proj_draw_axes(M, s=1, *args, **kwargs):
379+
xs = [0, s, 0, 0]
380+
ys = [0, 0, s, 0]
381+
zs = [0, 0, 0, s]
382+
txs, tys, tzs = proj3d.proj_transform(xs, ys, zs, M)
383+
o, ax, ay, az = zip(txs, tys)
384+
lines = [(o, ax), (o, ay), (o, az)]
385+
386+
fig, ax = plt.subplots(*args, **kwargs)
387+
linec = LineCollection(lines)
388+
ax.add_collection(linec)
389+
for x, y, t in zip(txs, tys, ['o', 'x', 'y', 'z']):
390+
ax.text(x, y, t)
391+
392+
return fig, ax
393+
394+
395+
@image_comparison(baseline_images=['proj3d_axes_cube'], extensions=['png'],
396+
remove_text=True, style='default')
397+
def test_proj_axes_cube():
398+
M = _test_proj_make_M()
399+
400+
ts = '0 1 2 3 0 4 5 6 7 4'.split()
401+
xs = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0]) * 300.0
402+
ys = np.array([0, 0, 1, 1, 0, 0, 0, 1, 1, 0]) * 300.0
403+
zs = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) * 300.0
404+
405+
txs, tys, tzs = proj3d.proj_transform(xs, ys, zs, M)
406+
407+
fig, ax = _test_proj_draw_axes(M, s=400)
408+
409+
ax.scatter(txs, tys, c=tzs)
410+
ax.plot(txs, tys, c='r')
411+
for x, y, t in zip(txs, tys, ts):
412+
ax.text(x, y, t)
413+
414+
ax.set_xlim(-0.2, 0.2)
415+
ax.set_ylim(-0.2, 0.2)
416+
417+
418+
def test_rot():
419+
V = [1, 0, 0, 1]
420+
rotated_V = proj3d.rot_x(V, np.pi / 6)
421+
np.testing.assert_allclose(rotated_V, [1, 0, 0, 1])
422+
423+
V = [0, 1, 0, 1]
424+
rotated_V = proj3d.rot_x(V, np.pi / 6)
425+
np.testing.assert_allclose(rotated_V, [0, np.sqrt(3) / 2, 0.5, 1])
426+
427+
428+
def test_world():
429+
xmin, xmax = 100, 120
430+
ymin, ymax = -100, 100
431+
zmin, zmax = 0.1, 0.2
432+
M = proj3d.world_transformation(xmin, xmax, ymin, ymax, zmin, zmax)
433+
np.testing.assert_allclose(M,
434+
[[5e-2, 0, 0, -5],
435+
[0, 5e-3, 0, 5e-1],
436+
[0, 0, 1e1, -1],
437+
[0, 0, 0, 1]])
438+
439+
440+
@image_comparison(baseline_images=['proj3d_lines_dists'], extensions=['png'],
441+
remove_text=True, style='default')
442+
def test_lines_dists():
443+
fig, ax = plt.subplots(figsize=(4, 6), subplot_kw=dict(aspect='equal'))
444+
445+
xs = (0, 30)
446+
ys = (20, 150)
447+
ax.plot(xs, ys)
448+
p0, p1 = zip(xs, ys)
449+
450+
xs = (0, 0, 20, 30)
451+
ys = (100, 150, 30, 200)
452+
ax.scatter(xs, ys)
453+
454+
dist = proj3d.line2d_seg_dist(p0, p1, (xs[0], ys[0]))
455+
dist = proj3d.line2d_seg_dist(p0, p1, np.array((xs, ys)))
456+
for x, y, d in zip(xs, ys, dist):
457+
c = Circle((x, y), d, fill=0)
458+
ax.add_patch(c)
459+
460+
ax.set_xlim(-50, 150)
461+
ax.set_ylim(0, 300)
462+
463+
349464
if __name__ == '__main__':
350465
import nose
351466
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)