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

Skip to content

Commit 101ffe8

Browse files
authored
Merge pull request #27657 from ksunden/np_20_tests
Fix Numpy 2.0 related test failures
2 parents b03407b + 6089283 commit 101ffe8

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

lib/matplotlib/pylab.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
bytes = __import__("builtins").bytes
6161
# We also don't want the numpy version of these functions
6262
abs = __import__("builtins").abs
63+
bool = __import__("builtins").bool
6364
max = __import__("builtins").max
6465
min = __import__("builtins").min
66+
pow = __import__("builtins").pow
6567
round = __import__("builtins").round

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5743,7 +5743,12 @@ def test_text_labelsize():
57435743
ax.tick_params(direction='out')
57445744

57455745

5746-
@image_comparison(['pie_default.png'])
5746+
# Note: The `pie` image tests were affected by Numpy 2.0 changing promotions
5747+
# (NEP 50). While the changes were only marginal, tolerances were introduced.
5748+
# These tolerances could likely go away when numpy 2.0 is the minimum supported
5749+
# numpy and the images are regenerated.
5750+
5751+
@image_comparison(['pie_default.png'], tol=0.01)
57475752
def test_pie_default():
57485753
# The slices will be ordered and plotted counter-clockwise.
57495754
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5756,7 +5761,7 @@ def test_pie_default():
57565761

57575762

57585763
@image_comparison(['pie_linewidth_0', 'pie_linewidth_0', 'pie_linewidth_0'],
5759-
extensions=['png'], style='mpl20')
5764+
extensions=['png'], style='mpl20', tol=0.01)
57605765
def test_pie_linewidth_0():
57615766
# The slices will be ordered and plotted counter-clockwise.
57625767
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5788,7 +5793,7 @@ def test_pie_linewidth_0():
57885793
plt.axis('equal')
57895794

57905795

5791-
@image_comparison(['pie_center_radius.png'], style='mpl20')
5796+
@image_comparison(['pie_center_radius.png'], style='mpl20', tol=0.005)
57925797
def test_pie_center_radius():
57935798
# The slices will be ordered and plotted counter-clockwise.
57945799
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5808,7 +5813,7 @@ def test_pie_center_radius():
58085813
plt.axis('equal')
58095814

58105815

5811-
@image_comparison(['pie_linewidth_2.png'], style='mpl20')
5816+
@image_comparison(['pie_linewidth_2.png'], style='mpl20', tol=0.01)
58125817
def test_pie_linewidth_2():
58135818
# The slices will be ordered and plotted counter-clockwise.
58145819
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5823,7 +5828,7 @@ def test_pie_linewidth_2():
58235828
plt.axis('equal')
58245829

58255830

5826-
@image_comparison(['pie_ccw_true.png'], style='mpl20')
5831+
@image_comparison(['pie_ccw_true.png'], style='mpl20', tol=0.01)
58275832
def test_pie_ccw_true():
58285833
# The slices will be ordered and plotted counter-clockwise.
58295834
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5838,7 +5843,7 @@ def test_pie_ccw_true():
58385843
plt.axis('equal')
58395844

58405845

5841-
@image_comparison(['pie_frame_grid.png'], style='mpl20')
5846+
@image_comparison(['pie_frame_grid.png'], style='mpl20', tol=0.002)
58425847
def test_pie_frame_grid():
58435848
# The slices will be ordered and plotted counter-clockwise.
58445849
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5865,7 +5870,7 @@ def test_pie_frame_grid():
58655870
plt.axis('equal')
58665871

58675872

5868-
@image_comparison(['pie_rotatelabels_true.png'], style='mpl20')
5873+
@image_comparison(['pie_rotatelabels_true.png'], style='mpl20', tol=0.009)
58695874
def test_pie_rotatelabels_true():
58705875
# The slices will be ordered and plotted counter-clockwise.
58715876
labels = 'Hogwarts', 'Frogs', 'Dogs', 'Logs'
@@ -5880,7 +5885,7 @@ def test_pie_rotatelabels_true():
58805885
plt.axis('equal')
58815886

58825887

5883-
@image_comparison(['pie_no_label.png'])
5888+
@image_comparison(['pie_no_label.png'], tol=0.01)
58845889
def test_pie_nolabel_but_legend():
58855890
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
58865891
sizes = [15, 30, 45, 10]
@@ -5894,7 +5899,7 @@ def test_pie_nolabel_but_legend():
58945899
plt.legend()
58955900

58965901

5897-
@image_comparison(['pie_shadow.png'], style='mpl20')
5902+
@image_comparison(['pie_shadow.png'], style='mpl20', tol=0.002)
58985903
def test_pie_shadow():
58995904
# Also acts as a test for the shade argument of Shadow
59005905
sizes = [15, 30, 45, 10]

lib/matplotlib/tests/test_colors.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,38 +1384,38 @@ def test_scalarmappable_to_rgba(bytes):
13841384
# uint8 RGBA
13851385
x = np.ones((2, 3, 4), dtype=np.uint8)
13861386
expected = x.copy() if bytes else x.astype(np.float32)/255
1387-
np.testing.assert_array_equal(sm.to_rgba(x, bytes=bytes), expected)
1387+
np.testing.assert_almost_equal(sm.to_rgba(x, bytes=bytes), expected)
13881388
# uint8 RGB
13891389
expected[..., 3] = alpha_1
1390-
np.testing.assert_array_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
1390+
np.testing.assert_almost_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
13911391
# uint8 masked RGBA
13921392
xm = np.ma.masked_array(x, mask=np.zeros_like(x))
13931393
xm.mask[0, 0, 0] = True
13941394
expected = x.copy() if bytes else x.astype(np.float32)/255
13951395
expected[0, 0, 3] = 0
1396-
np.testing.assert_array_equal(sm.to_rgba(xm, bytes=bytes), expected)
1396+
np.testing.assert_almost_equal(sm.to_rgba(xm, bytes=bytes), expected)
13971397
# uint8 masked RGB
13981398
expected[..., 3] = alpha_1
13991399
expected[0, 0, 3] = 0
1400-
np.testing.assert_array_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)
1400+
np.testing.assert_almost_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)
14011401

14021402
# float RGBA
14031403
x = np.ones((2, 3, 4), dtype=float) * 0.5
14041404
expected = (x * 255).astype(np.uint8) if bytes else x.copy()
1405-
np.testing.assert_array_equal(sm.to_rgba(x, bytes=bytes), expected)
1405+
np.testing.assert_almost_equal(sm.to_rgba(x, bytes=bytes), expected)
14061406
# float RGB
14071407
expected[..., 3] = alpha_1
1408-
np.testing.assert_array_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
1408+
np.testing.assert_almost_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
14091409
# float masked RGBA
14101410
xm = np.ma.masked_array(x, mask=np.zeros_like(x))
14111411
xm.mask[0, 0, 0] = True
14121412
expected = (x * 255).astype(np.uint8) if bytes else x.copy()
14131413
expected[0, 0, 3] = 0
1414-
np.testing.assert_array_equal(sm.to_rgba(xm, bytes=bytes), expected)
1414+
np.testing.assert_almost_equal(sm.to_rgba(xm, bytes=bytes), expected)
14151415
# float masked RGB
14161416
expected[..., 3] = alpha_1
14171417
expected[0, 0, 3] = 0
1418-
np.testing.assert_array_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)
1418+
np.testing.assert_almost_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)
14191419

14201420

14211421
def test_failed_conversions():

0 commit comments

Comments
 (0)