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

Code before:
    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)
    mimage.resample(data, nonaffine_result, nonaffine_transform,
                        interpolation=interpolation)
    assert_allclose(nonaffine_result, expected, atol=5e-3)


def test_axesimage_get_shape():
    # generate dummy image to test get_shape method
    ax = plt.gca()
    im = AxesImage(ax)
    with pytest.raises(RuntimeError, match="You must first set the image array"):
        im.get_shape()
    z = np.arange(12, dtype=float).reshape((4, 3))

Review comment (suggestion):
```suggestion
    mimage.resample(data, affine_result, affine_transform, interpolation=interpolation)
```

Code after:
    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)
    mimage.resample(data, nonaffine_result, nonaffine_transform,
                    interpolation=interpolation)
    assert_allclose(nonaffine_result, expected, atol=5e-3)


def test_axesimage_get_shape():
    # generate dummy image to test get_shape method
    ax = plt.gca()
    im = AxesImage(ax)
    with pytest.raises(RuntimeError, match="You must first set the image array"):
        im.get_shape()
    z = np.arange(12, dtype=float).reshape((4, 3))
    im.set_data(z)