Skip to content

Commit 398be5f

Browse files
committed
Add test to check for consistency in scaling images to axes edges
1 parent bebb263 commit 398be5f

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

lib/matplotlib/tests/test_image.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,17 @@ def test__resample_valid_output():
16411641
resample(np.zeros((9, 9)), out)
16421642

16431643

1644+
@pytest.fixture
1645+
def nonaffine_identity():
1646+
class NonAffineIdentityTransform(Transform):
1647+
input_dims = 2
1648+
output_dims = 2
1649+
1650+
def inverted(self):
1651+
return self
1652+
return NonAffineIdentityTransform()
1653+
1654+
16441655
@pytest.mark.parametrize("data, interpolation, expected",
16451656
[(np.array([[0.1, 0.3, 0.2]]), mimage.NEAREST,
16461657
np.array([[0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2]])),
@@ -1649,7 +1660,7 @@ def test__resample_valid_output():
16491660
0.28476562, 0.2546875, 0.22460938, 0.20002441, 0.20002441]])),
16501661
]
16511662
)
1652-
def test_resample_nonaffine(data, interpolation, expected):
1663+
def test_resample_nonaffine(data, interpolation, expected, nonaffine_identity):
16531664
# Test that equivalent affine and nonaffine transforms resample the same
16541665

16551666
# Create a simple affine transform for scaling the input array
@@ -1661,20 +1672,36 @@ def test_resample_nonaffine(data, interpolation, expected):
16611672

16621673
# Create a nonaffine version of the same transform
16631674
# by compositing with a nonaffine identity transform
1664-
class NonAffineIdentityTransform(Transform):
1665-
input_dims = 2
1666-
output_dims = 2
1667-
1668-
def inverted(self):
1669-
return self
1670-
nonaffine_transform = NonAffineIdentityTransform() + affine_transform
1675+
nonaffine_transform = nonaffine_identity + affine_transform
16711676

16721677
nonaffine_result = np.empty_like(expected)
16731678
mimage.resample(data, nonaffine_result, nonaffine_transform,
16741679
interpolation=interpolation)
16751680
assert_allclose(nonaffine_result, expected, atol=5e-3)
16761681

16771682

1683+
@check_figures_equal()
1684+
def test_nonaffine_scaling_to_axes_edges(fig_test, fig_ref, nonaffine_identity):
1685+
# Test that plotting an image with equivalent affine and nonaffine
1686+
# transforms is scaled the same to the axes edges
1687+
data = np.arange(16).reshape((4, 4)) % 3
1688+
1689+
# Specifically choose an axes bbox that has a fractional pixel
1690+
1691+
fig_test.set_size_inches(5, 5)
1692+
fig_test.set_dpi(100)
1693+
ax = fig_test.subplots()
1694+
ax.set_position([0.2, 0.2, 300.5 / 500, 300.5 / 500])
1695+
ax.imshow(data, interpolation='nearest',
1696+
transform=nonaffine_identity + ax.transData)
1697+
1698+
fig_ref.set_size_inches(5, 5)
1699+
fig_ref.set_dpi(100)
1700+
ax = fig_ref.subplots()
1701+
ax.set_position([0.2, 0.2, 300.5 / 500, 300.5 / 500])
1702+
ax.imshow(data, interpolation='nearest')
1703+
1704+
16781705
def test_axesimage_get_shape():
16791706
# generate dummy image to test get_shape method
16801707
ax = plt.gca()

0 commit comments

Comments
 (0)