Repository: matplotlib/matplotlib
File: lib/matplotlib/tests/test_image.py (Python)

Code before:
    resample = functools.partial(mpl._image.resample, transform=Affine2D())
    with pytest.raises(TypeError, match="incompatible function arguments"):
        resample(np.zeros((9, 9)), None)
    with pytest.raises(ValueError, match="different dimensionalities"):
        resample(np.zeros((9, 9)), np.zeros((9, 9, 4)))
    with pytest.raises(ValueError, match="different dimensionalities"):
        resample(np.zeros((9, 9, 4)), np.zeros((9, 9)))
    with pytest.raises(ValueError, match="3D input array must be RGBA"):
        resample(np.zeros((9, 9, 3)), np.zeros((9, 9, 4)))
    with pytest.raises(ValueError, match="3D output array must be RGBA"):
        resample(np.zeros((9, 9, 4)), np.zeros((9, 9, 3)))
    with pytest.raises(ValueError, match="mismatched types"):
        resample(np.zeros((9, 9), np.uint8), np.zeros((9, 9)))
    with pytest.raises(ValueError, match="must be C-contiguous"):
        resample(np.zeros((9, 9)), np.zeros((9, 9)).T)

    out = np.zeros((9, 9))
    out.flags.writeable = False
    with pytest.raises(ValueError, match="Output array must be writeable"):
        resample(np.zeros((9, 9)), out)


@pytest.mark.parametrize("data, interpolation, expected",
    [(np.array([[0.1, 0.3, 0.2]]), mpl._image.NEAREST,
      np.array([[0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2]])),
     (np.array([[0.1, 0.3, 0.2]]), mpl._image.BILINEAR,
      np.array([[0.1, 0.1, 0.15078125, 0.21096191, 0.27033691,
                 0.28476562, 0.2546875, 0.22460938, 0.20002441, 0.20002441]])),
    ]
)
def test__resample_nonaffine(data, interpolation, expected):
    # Test that equivalent affine and nonaffine transforms resample the same

    # Create a simple affine transform for scaling the input array
    affine_transform = Affine2D().scale(sx=expected.shape[1] / data.shape[1], sy=1)

    affine_result = np.empty_like(expected)
    mpl._image.resample(data, affine_result, affine_transform,
                        interpolation=interpolation)
    assert_allclose(affine_result, expected)

    # Create a nonaffine version of the same transform
    # by compositing with a nonaffine identity transform
    class NonAffineIdentityTransform(Transform):
        input_dims = 2
        output_dims = 2

        def inverted(self):
           return self
    nonaffine_transform = NonAffineIdentityTransform() + affine_transform


Review comment (suggestion):
```suggestion
    [(np.array([[0.1, 0.3, 0.2]]), mimage.NEAREST,
      np.array([[0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2]])),
     (np.array([[0.1, 0.3, 0.2]]), mimage.BILINEAR,
```

These values are implemented in a private namespace, but are available in a public namespace (which is already imported directly)

This should also resolve the type hint problems

Code after:
    resample = functools.partial(mpl._image.resample, transform=Affine2D())
    with pytest.raises(TypeError, match="incompatible function arguments"):
        resample(np.zeros((9, 9)), None)
    with pytest.raises(ValueError, match="different dimensionalities"):
        resample(np.zeros((9, 9)), np.zeros((9, 9, 4)))
    with pytest.raises(ValueError, match="different dimensionalities"):
        resample(np.zeros((9, 9, 4)), np.zeros((9, 9)))
    with pytest.raises(ValueError, match="3D input array must be RGBA"):
        resample(np.zeros((9, 9, 3)), np.zeros((9, 9, 4)))
    with pytest.raises(ValueError, match="3D output array must be RGBA"):
        resample(np.zeros((9, 9, 4)), np.zeros((9, 9, 3)))
    with pytest.raises(ValueError, match="mismatched types"):
        resample(np.zeros((9, 9), np.uint8), np.zeros((9, 9)))
    with pytest.raises(ValueError, match="must be C-contiguous"):
        resample(np.zeros((9, 9)), np.zeros((9, 9)).T)

    out = np.zeros((9, 9))
    out.flags.writeable = False
    with pytest.raises(ValueError, match="Output array must be writeable"):
        resample(np.zeros((9, 9)), out)


@pytest.mark.parametrize("data, interpolation, expected",
    [(np.array([[0.1, 0.3, 0.2]]), mimage.NEAREST,
      np.array([[0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2]])),
     (np.array([[0.1, 0.3, 0.2]]), mimage.BILINEAR,
      np.array([[0.1, 0.1, 0.15078125, 0.21096191, 0.27033691,
                 0.28476562, 0.2546875, 0.22460938, 0.20002441, 0.20002441]])),
    ]
)
def test_resample_nonaffine(data, interpolation, expected):
    # Test that equivalent affine and nonaffine transforms resample the same

    # Create a simple affine transform for scaling the input array
    affine_transform = Affine2D().scale(sx=expected.shape[1] / data.shape[1], sy=1)

    affine_result = np.empty_like(expected)
    mimage.resample(data, affine_result, affine_transform, interpolation=interpolation)
    assert_allclose(affine_result, expected)

    # Create a nonaffine version of the same transform
    # by compositing with a nonaffine identity transform
    class NonAffineIdentityTransform(Transform):
        input_dims = 2
        output_dims = 2

        def inverted(self):
           return self
    nonaffine_transform = NonAffineIdentityTransform() + affine_transform

    nonaffine_result = np.empty_like(expected)