Exemple #1
0
def test_mpl_cmap():
    cm = pytest.importorskip("matplotlib.cm")

    # 2D uint8
    image = Image()
    data = np.random.randint(0, 255, (60, 60)).astype("uint8")
    image.set_data(data)
    rendered = image.image_rgba
    assert isinstance(rendered, np.ndarray)
    # without a colormap, the output data should be nearly identical
    assert data.size - np.count_nonzero(rendered[...,
                                                 0] == data) < data.size * 0.01
    assert isinstance(rendered, np.ndarray)
    assert rendered.shape == (60, 60, 4)

    image.set_cmap("magma")
    rendered2 = image.image_rgba
    assert isinstance(rendered2, np.ndarray)
    # the colormap has been applied
    assert data.size - np.count_nonzero(rendered2[..., 0] == data) > 3000

    # can also use an mpl colormap instance
    image.set_cmap(cm.get_cmap("viridis"))
    rendered3 = image.image_rgba
    assert isinstance(rendered3, np.ndarray)
    # the colormap has been applied
    assert not np.allclose(rendered2, rendered3)
Exemple #2
0
def test_empty_image():
    image = Image()
    assert image.image_data is None
    assert image.image_rgba is None
    assert image.get_clim() == (None, None)

    with pytest.raises(RuntimeError):
        image.set_clim(0, 100)
    with pytest.raises(RuntimeError):
        image.set_clim(0, 100)
    with pytest.raises(RuntimeError):
        image.set_cmap("turbo")
    with pytest.raises(RuntimeError):
        image.set_norm(1)
Exemple #3
0
def test_internal_cmap():
    cmap = _mpl_image.Colormap([[0, 0, 0, 1], [1, 0, 0, 1]])
    image = Image()
    data = np.random.randint(0, 255, (60, 60)).astype("uint8")
    image.set_data(data, cmap=cmap)
    rendered = image.image_rgba
    assert isinstance(rendered, np.ndarray)
    assert rendered.shape == (60, 60, 4)

    image.set_cmap(
        _mpl_image.Colormap([[0, 0, 0, 1], [1, 0, 0, 1]],
                            interpolation="nearest"))
    rendered2 = image.image_rgba
    assert isinstance(rendered2, np.ndarray)
    assert not np.allclose(rendered, rendered2)