-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtest_texture_array.py
More file actions
209 lines (166 loc) · 5.54 KB
/
test_texture_array.py
File metadata and controls
209 lines (166 loc) · 5.54 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
import numpy as np
from numpy import testing as npt
import pytest
import fastplotlib as fpl
from fastplotlib.graphics.features import TextureArray
from .utils_textures import MAX_TEXTURE_SIZE, check_texture_array, check_image_graphic
def make_data(n_rows: int, n_cols: int) -> np.ndarray:
"""
Makes a 2D array where the amplitude of the sine wave
is increasing along the y-direction (along rows), and
the wavelength is increasing along the x-axis (columns)
"""
xs = np.linspace(0, 1_000, n_cols)
sine = np.sin(np.sqrt(xs))
return np.vstack([sine * i for i in range(n_rows)]).astype(np.float32)
def check_set_slice(data, ta, row_slice, col_slice):
ta[row_slice, col_slice] = 1
npt.assert_almost_equal(ta[row_slice, col_slice], 1)
# make sure other vals unchanged
npt.assert_almost_equal(ta[: row_slice.start], data[: row_slice.start])
npt.assert_almost_equal(ta[row_slice.stop :], data[row_slice.stop :])
npt.assert_almost_equal(ta[:, : col_slice.start], data[:, : col_slice.start])
npt.assert_almost_equal(ta[:, col_slice.stop :], data[:, col_slice.stop :])
def make_image_graphic(data) -> fpl.ImageGraphic:
fig = fpl.Figure()
return fig[0, 0].add_image(data)
@pytest.mark.parametrize("test_graphic", [False, True])
def test_small_texture(test_graphic):
# tests TextureArray with dims that requires only 1 texture
data = make_data(500, 500)
if test_graphic:
graphic = make_image_graphic(data)
ta = graphic.data
else:
ta = TextureArray(data)
check_texture_array(
data=data,
ta=ta,
buffer_size=1,
buffer_shape=(1, 1),
row_indices_size=1,
col_indices_size=1,
row_indices_values=np.array([0]),
col_indices_values=np.array([0]),
)
if test_graphic:
check_image_graphic(ta, graphic)
check_set_slice(data, ta, slice(50, 200), slice(200, 400))
@pytest.mark.parametrize("test_graphic", [False, True])
def test_texture_at_limit(test_graphic):
# tests TextureArray with data that is 1024 x 1024
data = make_data(MAX_TEXTURE_SIZE, MAX_TEXTURE_SIZE)
if test_graphic:
graphic = make_image_graphic(data)
ta = graphic.data
else:
ta = TextureArray(data)
check_texture_array(
data,
ta=ta,
buffer_size=1,
buffer_shape=(1, 1),
row_indices_size=1,
col_indices_size=1,
row_indices_values=np.array([0]),
col_indices_values=np.array([0]),
)
if test_graphic:
check_image_graphic(ta, graphic)
check_set_slice(data, ta, slice(500, 800), slice(200, 300))
@pytest.mark.parametrize("test_graphic", [False, True])
def test_wide(test_graphic):
data = make_data(1_200, 2_200)
if test_graphic:
graphic = make_image_graphic(data)
ta = graphic.data
else:
ta = TextureArray(data)
ta_shape = (2, 3)
check_texture_array(
data,
ta=ta,
buffer_size=np.prod(ta_shape),
buffer_shape=ta_shape,
row_indices_size=ta_shape[0],
col_indices_size=ta_shape[1],
row_indices_values=np.array(
[
i * MAX_TEXTURE_SIZE
for i in range(0, 1 + (data.shape[0] - 1) // MAX_TEXTURE_SIZE)
]
),
col_indices_values=np.array(
[
i * MAX_TEXTURE_SIZE
for i in range(0, 1 + (data.shape[1] - 1) // MAX_TEXTURE_SIZE)
]
),
)
if test_graphic:
check_image_graphic(ta, graphic)
check_set_slice(data, ta, slice(600, 1_100), slice(100, 2_100))
@pytest.mark.parametrize("test_graphic", [False, True])
def test_tall(test_graphic):
data = make_data(2_200, 1_200)
if test_graphic:
graphic = make_image_graphic(data)
ta = graphic.data
else:
ta = TextureArray(data)
ta_shape = (3, 2)
check_texture_array(
data,
ta=ta,
buffer_size=np.prod(ta_shape),
buffer_shape=ta_shape,
row_indices_size=ta_shape[0],
col_indices_size=ta_shape[1],
row_indices_values=np.array(
[
i * MAX_TEXTURE_SIZE
for i in range(0, 1 + (data.shape[0] - 1) // MAX_TEXTURE_SIZE)
]
),
col_indices_values=np.array(
[
i * MAX_TEXTURE_SIZE
for i in range(0, 1 + (data.shape[1] - 1) // MAX_TEXTURE_SIZE)
]
),
)
if test_graphic:
check_image_graphic(ta, graphic)
check_set_slice(data, ta, slice(100, 2_100), slice(600, 1_100))
@pytest.mark.parametrize("test_graphic", [False, True])
def test_square(test_graphic):
data = make_data(2_200, 2_200)
if test_graphic:
graphic = make_image_graphic(data)
ta = graphic.data
else:
ta = TextureArray(data)
ta_shape = (3, 3)
check_texture_array(
data,
ta=ta,
buffer_size=np.prod(ta_shape),
buffer_shape=ta_shape,
row_indices_size=ta_shape[0],
col_indices_size=ta_shape[1],
row_indices_values=np.array(
[
i * MAX_TEXTURE_SIZE
for i in range(0, 1 + (data.shape[0] - 1) // MAX_TEXTURE_SIZE)
]
),
col_indices_values=np.array(
[
i * MAX_TEXTURE_SIZE
for i in range(0, 1 + (data.shape[1] - 1) // MAX_TEXTURE_SIZE)
]
),
)
if test_graphic:
check_image_graphic(ta, graphic)
check_set_slice(data, ta, slice(100, 2_100), slice(100, 2_100))