Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12990b9

Browse files
committed
Use NumPy testing asserts more consistently.
1 parent 98c1e57 commit 12990b9

File tree

1 file changed

+46
-53
lines changed

1 file changed

+46
-53
lines changed

lib/matplotlib/tests/test_transforms.py

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
import unittest
88

9-
import numpy.testing as np_test
10-
from numpy.testing import assert_almost_equal, assert_array_equal
11-
from numpy.testing import assert_array_almost_equal
9+
import numpy as np
10+
from numpy.testing import (assert_allclose, assert_almost_equal,
11+
assert_array_equal, assert_array_almost_equal)
1212
import pytest
13+
1314
from matplotlib.transforms import (Affine2D, BlendedGenericTransform, Bbox,
1415
TransformedPath, TransformedPatchPath)
1516
from matplotlib.path import Path
1617
from matplotlib.scale import LogScale
1718
from matplotlib.testing.decorators import cleanup, image_comparison
18-
import numpy as np
1919

2020
import matplotlib.transforms as mtrans
2121
import matplotlib.pyplot as plt
@@ -76,8 +76,8 @@ def _as_mpl_transform(self, axes):
7676
ax.set_xlim(0, 100)
7777
ax.set_ylim(0, 100)
7878
# assert that the top transform of the line is the scale transform.
79-
np.testing.assert_allclose(line.get_transform()._a.get_matrix(),
80-
mtrans.Affine2D().scale(10).get_matrix())
79+
assert_allclose(line.get_transform()._a.get_matrix(),
80+
mtrans.Affine2D().scale(10).get_matrix())
8181

8282

8383
@image_comparison(baseline_images=['pre_transform_data'],
@@ -208,7 +208,7 @@ def test_clipping_of_log():
208208
simplify=False)
209209

210210
tpoints, tcodes = list(zip(*result))
211-
assert np.allclose(tcodes, [M, L, L, L, C])
211+
assert_allclose(tcodes, [M, L, L, L, C])
212212

213213

214214
class NonAffineForTest(mtrans.Transform):
@@ -344,26 +344,26 @@ def test_affine_simplification(self):
344344
dtype=np.float64)
345345

346346
# check we have the expected results from doing the affine part only
347-
np_test.assert_array_almost_equal(na_pts, na_expected)
347+
assert_array_almost_equal(na_pts, na_expected)
348348
# check we have the expected results from a full transformation
349-
np_test.assert_array_almost_equal(all_pts, all_expected)
349+
assert_array_almost_equal(all_pts, all_expected)
350350
# check we have the expected results from doing the transformation in
351351
# two steps
352-
np_test.assert_array_almost_equal(self.stack1.transform_affine(na_pts),
353-
all_expected)
352+
assert_array_almost_equal(self.stack1.transform_affine(na_pts),
353+
all_expected)
354354
# check that getting the affine transformation first, then fully
355355
# transforming using that yields the same result as before.
356-
np_test.assert_array_almost_equal(
357-
self.stack1.get_affine().transform(na_pts), all_expected)
356+
assert_array_almost_equal(self.stack1.get_affine().transform(na_pts),
357+
all_expected)
358358

359359
# check that the affine part of stack1 & stack2 are equivalent
360360
# (i.e. the optimization is working)
361361
expected_result = (self.ta2 + self.ta3).get_matrix()
362362
result = self.stack1.get_affine().get_matrix()
363-
np_test.assert_array_equal(expected_result, result)
363+
assert_array_equal(expected_result, result)
364364

365365
result = self.stack2.get_affine().get_matrix()
366-
np_test.assert_array_equal(expected_result, result)
366+
assert_array_equal(expected_result, result)
367367

368368

369369
class TestTransformPlotInterface(unittest.TestCase):
@@ -374,35 +374,35 @@ def test_line_extent_axes_coords(self):
374374
# a simple line in axes coordinates
375375
ax = plt.axes()
376376
ax.plot([0.1, 1.2, 0.8], [0.9, 0.5, 0.8], transform=ax.transAxes)
377-
np.testing.assert_array_equal(ax.dataLim.get_points(),
378-
np.array([[np.inf, np.inf],
379-
[-np.inf, -np.inf]]))
377+
assert_array_equal(ax.dataLim.get_points(),
378+
np.array([[np.inf, np.inf],
379+
[-np.inf, -np.inf]]))
380380

381381
def test_line_extent_data_coords(self):
382382
# a simple line in data coordinates
383383
ax = plt.axes()
384384
ax.plot([0.1, 1.2, 0.8], [0.9, 0.5, 0.8], transform=ax.transData)
385-
np.testing.assert_array_equal(ax.dataLim.get_points(),
386-
np.array([[0.1, 0.5], [1.2, 0.9]]))
385+
assert_array_equal(ax.dataLim.get_points(),
386+
np.array([[0.1, 0.5], [1.2, 0.9]]))
387387

388388
def test_line_extent_compound_coords1(self):
389389
# a simple line in data coordinates in the y component, and in axes
390390
# coordinates in the x
391391
ax = plt.axes()
392392
trans = mtrans.blended_transform_factory(ax.transAxes, ax.transData)
393393
ax.plot([0.1, 1.2, 0.8], [35, -5, 18], transform=trans)
394-
np.testing.assert_array_equal(ax.dataLim.get_points(),
395-
np.array([[np.inf, -5.],
396-
[-np.inf, 35.]]))
394+
assert_array_equal(ax.dataLim.get_points(),
395+
np.array([[np.inf, -5.],
396+
[-np.inf, 35.]]))
397397
plt.close()
398398

399399
def test_line_extent_predata_transform_coords(self):
400400
# a simple line in (offset + data) coordinates
401401
ax = plt.axes()
402402
trans = mtrans.Affine2D().scale(10) + ax.transData
403403
ax.plot([0.1, 1.2, 0.8], [35, -5, 18], transform=trans)
404-
np.testing.assert_array_equal(ax.dataLim.get_points(),
405-
np.array([[1., -50.], [12., 350.]]))
404+
assert_array_equal(ax.dataLim.get_points(),
405+
np.array([[1., -50.], [12., 350.]]))
406406
plt.close()
407407

408408
def test_line_extent_compound_coords2(self):
@@ -412,27 +412,24 @@ def test_line_extent_compound_coords2(self):
412412
trans = mtrans.blended_transform_factory(
413413
ax.transAxes, mtrans.Affine2D().scale(10) + ax.transData)
414414
ax.plot([0.1, 1.2, 0.8], [35, -5, 18], transform=trans)
415-
np.testing.assert_array_equal(
416-
ax.dataLim.get_points(),
417-
np.array([[np.inf, -50.], [-np.inf, 350.]]))
415+
assert_array_equal(ax.dataLim.get_points(),
416+
np.array([[np.inf, -50.], [-np.inf, 350.]]))
418417
plt.close()
419418

420419
def test_line_extents_affine(self):
421420
ax = plt.axes()
422421
offset = mtrans.Affine2D().translate(10, 10)
423422
plt.plot(list(xrange(10)), transform=offset + ax.transData)
424-
expeted_data_lim = np.array([[0., 0.], [9., 9.]]) + 10
425-
np.testing.assert_array_almost_equal(ax.dataLim.get_points(),
426-
expeted_data_lim)
423+
expected_data_lim = np.array([[0., 0.], [9., 9.]]) + 10
424+
assert_array_almost_equal(ax.dataLim.get_points(), expected_data_lim)
427425

428426
def test_line_extents_non_affine(self):
429427
ax = plt.axes()
430428
offset = mtrans.Affine2D().translate(10, 10)
431429
na_offset = NonAffineForTest(mtrans.Affine2D().translate(10, 10))
432430
plt.plot(list(xrange(10)), transform=offset + na_offset + ax.transData)
433-
expeted_data_lim = np.array([[0., 0.], [9., 9.]]) + 20
434-
np.testing.assert_array_almost_equal(ax.dataLim.get_points(),
435-
expeted_data_lim)
431+
expected_data_lim = np.array([[0., 0.], [9., 9.]]) + 20
432+
assert_array_almost_equal(ax.dataLim.get_points(), expected_data_lim)
436433

437434
def test_pathc_extents_non_affine(self):
438435
ax = plt.axes()
@@ -442,19 +439,17 @@ def test_pathc_extents_non_affine(self):
442439
patch = mpatches.PathPatch(pth,
443440
transform=offset + na_offset + ax.transData)
444441
ax.add_patch(patch)
445-
expeted_data_lim = np.array([[0., 0.], [10., 10.]]) + 20
446-
np.testing.assert_array_almost_equal(ax.dataLim.get_points(),
447-
expeted_data_lim)
442+
expected_data_lim = np.array([[0., 0.], [10., 10.]]) + 20
443+
assert_array_almost_equal(ax.dataLim.get_points(), expected_data_lim)
448444

449445
def test_pathc_extents_affine(self):
450446
ax = plt.axes()
451447
offset = mtrans.Affine2D().translate(10, 10)
452448
pth = mpath.Path(np.array([[0, 0], [0, 10], [10, 10], [10, 0]]))
453449
patch = mpatches.PathPatch(pth, transform=offset + ax.transData)
454450
ax.add_patch(patch)
455-
expeted_data_lim = np.array([[0., 0.], [10., 10.]]) + 10
456-
np.testing.assert_array_almost_equal(ax.dataLim.get_points(),
457-
expeted_data_lim)
451+
expected_data_lim = np.array([[0., 0.], [10., 10.]]) + 10
452+
assert_array_almost_equal(ax.dataLim.get_points(), expected_data_lim)
458453

459454
def test_line_extents_for_non_affine_transData(self):
460455
ax = plt.axes(projection='polar')
@@ -465,9 +460,8 @@ def test_line_extents_for_non_affine_transData(self):
465460
# the data lim of a polar plot is stored in coordinates
466461
# before a transData transformation, hence the data limits
467462
# are not what is being shown on the actual plot.
468-
expeted_data_lim = np.array([[0., 0.], [9., 9.]]) + [0, 10]
469-
np.testing.assert_array_almost_equal(ax.dataLim.get_points(),
470-
expeted_data_lim)
463+
expected_data_lim = np.array([[0., 0.], [9., 9.]]) + [0, 10]
464+
assert_array_almost_equal(ax.dataLim.get_points(), expected_data_lim)
471465

472466

473467
def assert_bbox_eq(bbox1, bbox2):
@@ -585,19 +579,20 @@ def test_transformed_path():
585579

586580
trans = mtrans.Affine2D()
587581
trans_path = TransformedPath(path, trans)
588-
assert np.allclose(trans_path.get_fully_transformed_path().vertices,
589-
points)
582+
assert_allclose(trans_path.get_fully_transformed_path().vertices, points)
590583

591584
# Changing the transform should change the result.
592585
r2 = 1 / np.sqrt(2)
593586
trans.rotate(np.pi / 4)
594-
assert np.allclose(trans_path.get_fully_transformed_path().vertices,
595-
[(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)])
587+
assert_allclose(trans_path.get_fully_transformed_path().vertices,
588+
[(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)],
589+
atol=1e-15)
596590

597591
# Changing the path does not change the result (it's cached).
598592
path.points = [(0, 0)] * 4
599-
assert np.allclose(trans_path.get_fully_transformed_path().vertices,
600-
[(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)])
593+
assert_allclose(trans_path.get_fully_transformed_path().vertices,
594+
[(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)],
595+
atol=1e-15)
601596

602597

603598
def test_transformed_patch_path():
@@ -609,11 +604,9 @@ def test_transformed_patch_path():
609604

610605
# Changing the transform should change the result.
611606
trans.scale(2)
612-
assert np.allclose(tpatch.get_fully_transformed_path().vertices,
613-
points * 2)
607+
assert_allclose(tpatch.get_fully_transformed_path().vertices, points * 2)
614608

615609
# Changing the path should change the result (and cancel out the scaling
616610
# from the transform).
617611
patch.set_radius(0.5)
618-
assert np.allclose(tpatch.get_fully_transformed_path().vertices,
619-
points)
612+
assert_allclose(tpatch.get_fully_transformed_path().vertices, points)

0 commit comments

Comments
 (0)