|
| 1 | +import numpy as np |
| 2 | +from numpy import testing as npt |
| 3 | +import imageio.v3 as iio |
| 4 | + |
| 5 | +import fastplotlib as fpl |
| 6 | +from fastplotlib.utils import make_colors |
| 7 | + |
| 8 | +GRAY_IMAGE = iio.imread("imageio:camera.png") |
| 9 | +RGB_IMAGE = iio.imread("imageio:astronaut.png") |
| 10 | + |
| 11 | + |
| 12 | +COFFEE_IMAGE = iio.imread("imageio:coffee.png") |
| 13 | + |
| 14 | +# image cmap, vmin, vmax, interpolations |
| 15 | +# new screenshot tests too for these when in graphics |
| 16 | + |
| 17 | + |
| 18 | +def check_set_slice( |
| 19 | + data: np.ndarray, |
| 20 | + image_graphic: fpl.ImageGraphic, |
| 21 | + row_slice: slice, |
| 22 | + col_slice: slice, |
| 23 | +): |
| 24 | + image_graphic.data[row_slice, col_slice] = 1 |
| 25 | + data_values = image_graphic.data.value |
| 26 | + npt.assert_almost_equal(data_values[row_slice, col_slice], 1) |
| 27 | + |
| 28 | + # make sure other vals unchanged |
| 29 | + npt.assert_almost_equal(data_values[:row_slice.start], data[:row_slice.start]) |
| 30 | + npt.assert_almost_equal(data_values[row_slice.stop:], data[row_slice.stop:]) |
| 31 | + npt.assert_almost_equal(data_values[:, :col_slice.start], data[:, :col_slice.start]) |
| 32 | + npt.assert_almost_equal(data_values[:, col_slice.stop:], data[:, col_slice.stop:]) |
| 33 | + |
| 34 | + |
| 35 | +def test_gray(): |
| 36 | + fig = fpl.Figure() |
| 37 | + ig = fig[0, 0].add_image(GRAY_IMAGE) |
| 38 | + assert isinstance(ig, fpl.ImageGraphic) |
| 39 | + |
| 40 | + npt.assert_almost_equal(ig.data.value, GRAY_IMAGE) |
| 41 | + |
| 42 | + ig.cmap = "viridis" |
| 43 | + assert ig.cmap == "viridis" |
| 44 | + |
| 45 | + new_colors = make_colors(256, "viridis") |
| 46 | + for child in ig.world_object.children: |
| 47 | + npt.assert_almost_equal(child.material.map.data, new_colors) |
| 48 | + |
| 49 | + ig.cmap = "jet" |
| 50 | + assert ig.cmap == "jet" |
| 51 | + |
| 52 | + new_colors = make_colors(256, "jet") |
| 53 | + for child in ig.world_object.children: |
| 54 | + npt.assert_almost_equal(child.material.map.data, new_colors) |
| 55 | + |
| 56 | + assert ig.interpolation == "nearest" |
| 57 | + for child in ig.world_object.children: |
| 58 | + assert child.material.interpolation == "nearest" |
| 59 | + |
| 60 | + ig.interpolation = "linear" |
| 61 | + assert ig.interpolation == "linear" |
| 62 | + for child in ig.world_object.children: |
| 63 | + assert child.material.interpolation == "linear" |
| 64 | + |
| 65 | + assert ig.cmap_interpolation == "linear" |
| 66 | + for child in ig.world_object.children: |
| 67 | + assert child.material.map_interpolation == "linear" |
| 68 | + |
| 69 | + ig.cmap_interpolation = "nearest" |
| 70 | + assert ig.cmap_interpolation == "nearest" |
| 71 | + for child in ig.world_object.children: |
| 72 | + assert child.material.map_interpolation == "nearest" |
| 73 | + |
| 74 | + npt.assert_almost_equal(ig.vmin, GRAY_IMAGE.min()) |
| 75 | + npt.assert_almost_equal(ig.vmax, GRAY_IMAGE.max()) |
| 76 | + |
| 77 | + ig.vmin = 50 |
| 78 | + assert ig.vmin == 50 |
| 79 | + for child in ig.world_object.children: |
| 80 | + assert child.material.clim == (50, ig.vmax) |
| 81 | + |
| 82 | + ig.vmax = 100 |
| 83 | + assert ig.vmax == 100 |
| 84 | + for child in ig.world_object.children: |
| 85 | + assert child.material.clim == (ig.vmin, 100) |
| 86 | + |
| 87 | + check_set_slice(GRAY_IMAGE, ig, slice(100, 200), slice(200, 300)) |
| 88 | + |
| 89 | + |
| 90 | +def test_rgb(): |
| 91 | + fig = fpl.Figure() |
| 92 | + ig = fig[0, 0].add_image(RGB_IMAGE) |
| 93 | + assert isinstance(ig, fpl.ImageGraphic) |
| 94 | + |
| 95 | + npt.assert_almost_equal(ig.data.value, RGB_IMAGE) |
| 96 | + |
| 97 | + assert ig.interpolation == "nearest" |
| 98 | + for child in ig.world_object.children: |
| 99 | + assert child.material.interpolation == "nearest" |
| 100 | + |
| 101 | + ig.interpolation = "linear" |
| 102 | + assert ig.interpolation == "linear" |
| 103 | + for child in ig.world_object.children: |
| 104 | + assert child.material.interpolation == "linear" |
| 105 | + |
| 106 | + npt.assert_almost_equal(ig.vmin, RGB_IMAGE.min()) |
| 107 | + npt.assert_almost_equal(ig.vmax, RGB_IMAGE.max()) |
| 108 | + |
| 109 | + ig.vmin = 50 |
| 110 | + assert ig.vmin == 50 |
| 111 | + for child in ig.world_object.children: |
| 112 | + assert child.material.clim == (50, ig.vmax) |
| 113 | + |
| 114 | + ig.vmax = 100 |
| 115 | + assert ig.vmax == 100 |
| 116 | + for child in ig.world_object.children: |
| 117 | + assert child.material.clim == (ig.vmin, 100) |
| 118 | + |
| 119 | + check_set_slice(RGB_IMAGE, ig, slice(100, 200), slice(200, 300)) |
| 120 | + |
| 121 | + |
| 122 | +def test_rgba(): |
| 123 | + rgba = np.zeros(shape=(*COFFEE_IMAGE.shape[:2], 4), dtype=np.float32) |
| 124 | + |
| 125 | + fig = fpl.Figure() |
| 126 | + ig = fig[0, 0].add_image(rgba) |
| 127 | + assert isinstance(ig, fpl.ImageGraphic) |
| 128 | + |
| 129 | + npt.assert_almost_equal(ig.data.value, rgba) |
| 130 | + |
| 131 | + # fancy indexing |
| 132 | + # set the blue values of some pixels with an alpha > 1 |
| 133 | + ig.data[COFFEE_IMAGE[:, :, -1] > 200] = np.array([0.0, 0.0, 1.0, 0.6]).astype(np.float32) |
| 134 | + |
| 135 | + rgba[COFFEE_IMAGE[:, :, -1] > 200] = np.array([0.0, 0.0, 1.0, 0.6]).astype(np.float32) |
| 136 | + |
| 137 | + # check that fancy indexing works |
| 138 | + npt.assert_almost_equal(ig.data.value, rgba) |
0 commit comments