-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathtest_image_graphic.py
More file actions
213 lines (160 loc) · 6.59 KB
/
test_image_graphic.py
File metadata and controls
213 lines (160 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import numpy as np
from numpy import testing as npt
import imageio.v3 as iio
import fastplotlib as fpl
from fastplotlib.graphics._features import FeatureEvent
from fastplotlib.utils import make_colors
GRAY_IMAGE = iio.imread("imageio:camera.png")
RGB_IMAGE = iio.imread("imageio:astronaut.png")
COFFEE_IMAGE = iio.imread("imageio:coffee.png")
# image cmap, vmin, vmax, interpolations
# new screenshot tests too for these when in graphics
EVENT_RETURN_VALUE: FeatureEvent = None
def event_handler(ev):
global EVENT_RETURN_VALUE
EVENT_RETURN_VALUE = ev
def check_event(graphic, feature, value):
global EVENT_RETURN_VALUE
assert isinstance(EVENT_RETURN_VALUE, FeatureEvent)
assert EVENT_RETURN_VALUE.type == feature
assert EVENT_RETURN_VALUE.graphic == graphic
assert EVENT_RETURN_VALUE.target == graphic.world_object
if isinstance(EVENT_RETURN_VALUE.info["value"], float):
# floating point error
npt.assert_almost_equal(EVENT_RETURN_VALUE.info["value"], value)
else:
assert EVENT_RETURN_VALUE.info["value"] == value
def check_set_slice(
data: np.ndarray,
image_graphic: fpl.ImageGraphic,
row_slice: slice,
col_slice: slice,
):
image_graphic.data[row_slice, col_slice] = 1
data_values = image_graphic.data.value
npt.assert_almost_equal(data_values[row_slice, col_slice], 1)
# make sure other vals unchanged
npt.assert_almost_equal(data_values[: row_slice.start], data[: row_slice.start])
npt.assert_almost_equal(data_values[row_slice.stop :], data[row_slice.stop :])
npt.assert_almost_equal(
data_values[:, : col_slice.start], data[:, : col_slice.start]
)
npt.assert_almost_equal(data_values[:, col_slice.stop :], data[:, col_slice.stop :])
global EVENT_RETURN_VALUE
assert isinstance(EVENT_RETURN_VALUE, FeatureEvent)
assert EVENT_RETURN_VALUE.type == "data"
assert EVENT_RETURN_VALUE.graphic == image_graphic
assert EVENT_RETURN_VALUE.target == image_graphic.world_object
assert EVENT_RETURN_VALUE.info["key"] == (row_slice, col_slice)
npt.assert_almost_equal(EVENT_RETURN_VALUE.info["value"], 1)
def test_gray():
fig = fpl.Figure()
ig = fig[0, 0].add_image(GRAY_IMAGE)
assert isinstance(ig, fpl.ImageGraphic)
ig.add_event_handler(
event_handler,
"data",
"cmap",
"vmin",
"vmax",
"interpolation",
"cmap_interpolation",
)
# make sure entire data is the same
npt.assert_almost_equal(ig.data.value, GRAY_IMAGE)
# since this entire image is under the wgpu max texture limit,
# the entire image should be in the single Texture buffer
npt.assert_almost_equal(ig.data.buffer[0, 0].data, GRAY_IMAGE)
ig.cmap = "viridis"
assert ig.cmap == "viridis"
check_event(graphic=ig, feature="cmap", value="viridis")
new_colors = make_colors(256, "viridis")
for child in ig.world_object.children:
npt.assert_almost_equal(child.material.map.texture.data, new_colors)
ig.cmap = "jet"
assert ig.cmap == "jet"
new_colors = make_colors(256, "jet")
for child in ig.world_object.children:
npt.assert_almost_equal(child.material.map.texture.data, new_colors)
assert ig.interpolation == "nearest"
for child in ig.world_object.children:
assert child.material.interpolation == "nearest"
ig.interpolation = "linear"
assert ig.interpolation == "linear"
for child in ig.world_object.children:
assert child.material.interpolation == "linear"
check_event(graphic=ig, feature="interpolation", value="linear")
assert ig.cmap_interpolation == "linear"
for child in ig.world_object.children:
assert child.material.map.min_filter == "linear"
assert child.material.map.mag_filter == "linear"
ig.cmap_interpolation = "nearest"
assert ig.cmap_interpolation == "nearest"
for child in ig.world_object.children:
assert child.material.map.min_filter == "nearest"
assert child.material.map.mag_filter == "nearest"
check_event(graphic=ig, feature="cmap_interpolation", value="nearest")
npt.assert_almost_equal(ig.vmin, GRAY_IMAGE.min())
npt.assert_almost_equal(ig.vmax, GRAY_IMAGE.max())
ig.vmin = 50
assert ig.vmin == 50
for child in ig.world_object.children:
assert child.material.clim == (50, ig.vmax)
check_event(graphic=ig, feature="vmin", value=50)
ig.vmax = 100
assert ig.vmax == 100
for child in ig.world_object.children:
assert child.material.clim == (ig.vmin, 100)
check_event(graphic=ig, feature="vmax", value=100)
# test reset
ig.reset_vmin_vmax()
npt.assert_almost_equal(ig.vmin, GRAY_IMAGE.min())
npt.assert_almost_equal(ig.vmax, GRAY_IMAGE.max())
check_set_slice(GRAY_IMAGE, ig, slice(100, 200), slice(200, 300))
# test setting all values
ig.data = 1
npt.assert_almost_equal(ig.data.value, 1)
def test_rgb():
fig = fpl.Figure()
ig = fig[0, 0].add_image(RGB_IMAGE)
assert isinstance(ig, fpl.ImageGraphic)
ig.add_event_handler(event_handler, "data")
npt.assert_almost_equal(ig.data.value, RGB_IMAGE)
assert ig.interpolation == "nearest"
for child in ig.world_object.children:
assert child.material.interpolation == "nearest"
ig.interpolation = "linear"
assert ig.interpolation == "linear"
for child in ig.world_object.children:
assert child.material.interpolation == "linear"
npt.assert_almost_equal(ig.vmin, RGB_IMAGE.min())
npt.assert_almost_equal(ig.vmax, RGB_IMAGE.max())
ig.vmin = 50
assert ig.vmin == 50
for child in ig.world_object.children:
assert child.material.clim == (50, ig.vmax)
ig.vmax = 100
assert ig.vmax == 100
for child in ig.world_object.children:
assert child.material.clim == (ig.vmin, 100)
# test reset
ig.reset_vmin_vmax()
npt.assert_almost_equal(ig.vmin, RGB_IMAGE.min())
npt.assert_almost_equal(ig.vmax, RGB_IMAGE.max())
check_set_slice(RGB_IMAGE, ig, slice(100, 200), slice(200, 300))
def test_rgba():
rgba = np.zeros(shape=(*COFFEE_IMAGE.shape[:2], 4), dtype=np.float32)
fig = fpl.Figure()
ig = fig[0, 0].add_image(rgba)
assert isinstance(ig, fpl.ImageGraphic)
npt.assert_almost_equal(ig.data.value, rgba)
# fancy indexing
# set the blue values of some pixels with an alpha > 1
ig.data[COFFEE_IMAGE[:, :, -1] > 200] = np.array([0.0, 0.0, 1.0, 0.6]).astype(
np.float32
)
rgba[COFFEE_IMAGE[:, :, -1] > 200] = np.array([0.0, 0.0, 1.0, 0.6]).astype(
np.float32
)
# check that fancy indexing works
npt.assert_almost_equal(ig.data.value, rgba)