/
test_scalable_textures.py
180 lines (140 loc) · 5 KB
/
test_scalable_textures.py
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
import numpy as np
import pytest
from vispy.testing import run_tests_if_main
from vispy.visuals._scalable_textures import (
get_default_clim_from_dtype,
get_default_clim_from_data,
CPUScaledTextureMixin,
GPUScaledTextureMixin,
)
class Stub:
_ndim = 2
def __init__(self, data, **kwargs):
pass
def set_data(self, data, *args, **kwargs):
self._data = data
class CPUScaledStub(CPUScaledTextureMixin, Stub):
pass
class GPUScaledStub(GPUScaledTextureMixin, Stub):
internalformat = "r32f"
def _get_texture_format_for_data(self, data, internalformat=None):
return None
def test_default_clim():
ref_data = np.array([10, 5, 15, 25, 15])
# f32
data = ref_data.astype(np.float32)
clim = get_default_clim_from_dtype(data.dtype)
assert clim == (0, 1)
clim = get_default_clim_from_data(data)
assert clim == (5, 25)
# i32
data = ref_data.astype(np.int32)
clim = get_default_clim_from_dtype(data.dtype)
assert clim == (-2**31, 2**31 - 1)
clim = get_default_clim_from_data(data)
assert clim == (5, 25)
# u8
data = ref_data.astype(np.uint8)
clim = get_default_clim_from_dtype(data.dtype)
assert clim == (0, 255)
clim = get_default_clim_from_data(data)
assert clim == (5, 25)
def test_default_clim_non_finite():
data = np.array([10, np.nan, 5, 15, 25, 15]).astype(np.float32)
clim = get_default_clim_from_dtype(data.dtype)
assert clim == (0, 1)
clim = get_default_clim_from_data(data)
assert clim == (5, 25)
data = np.array([10, np.inf, 5, 15, 25, 15]).astype(np.float32)
clim = get_default_clim_from_dtype(data.dtype)
assert clim == (0, 1)
clim = get_default_clim_from_data(data)
assert clim == (5, 25)
data = np.array([10, -np.inf, 5, 15, 25, 15]).astype(np.float32)
clim = get_default_clim_from_dtype(data.dtype)
assert clim == (0, 1)
clim = get_default_clim_from_data(data)
assert clim == (5, 25)
data = np.array([np.nan, np.nan, np.nan]).astype(np.float32)
clim = get_default_clim_from_dtype(data.dtype)
assert clim == (0, 1)
clim = get_default_clim_from_data(data)
assert clim == (0, 0)
data = np.array([np.nan, np.inf, -np.inf]).astype(np.float32)
clim = get_default_clim_from_dtype(data.dtype)
assert clim == (0, 1)
clim = get_default_clim_from_data(data)
assert clim == (0, 0)
def test_clim_handling_cpu():
ref_data = np.array([[10, 10, 5], [15, 25, 15]])
# f32 - auto clim
st = CPUScaledStub()
st.set_clim("auto")
st.scale_and_set_data(ref_data.astype(np.float32))
assert st.clim == (5, 25)
assert st.clim_normalized == (0, 1)
assert np.all(st._data == (ref_data - 5) / 20)
# Updating clim keeps data the same if not changing too much
st.set_clim((0, 20))
assert st.clim == (0, 20)
assert st.clim_normalized == (-0.25, 0.75)
assert np.all(st._data == (ref_data - 5) / 20)
# f32 - custom clim
st = CPUScaledStub()
st.set_clim((0, 20))
st.scale_and_set_data(ref_data.astype(np.float32))
assert st.clim == (0, 20)
assert st.clim_normalized == (0, 1)
assert np.all(st._data == ref_data / 20)
# f32 - flat clim
st = CPUScaledStub()
st.set_clim((10, 10))
st.scale_and_set_data(ref_data.astype(np.float32))
assert st.clim == (10, 10)
assert st.clim_normalized == (0, np.inf)
# assert np.min(st._data) == 0 - does not matter
# f32 - auto clim
st = CPUScaledStub()
st.set_clim("auto")
assert st.clim == "auto"
pytest.raises(RuntimeError, getattr, st, "clim_normalized")
st.scale_and_set_data(ref_data.astype(np.float32))
assert st.clim == (5, 25)
assert st.clim_normalized == (0, 1)
def test_clim_handling_gpu():
ref_data = np.array([[10, 10, 5], [15, 25, 15]])
# f32 - auto clim
st = GPUScaledStub()
st.set_clim("auto")
st.scale_and_set_data(ref_data.astype(np.float32))
assert st.clim == (5, 25)
assert st.clim_normalized == (5, 25)
assert np.all(st._data == ref_data)
# Updating clim keeps data the same if not changing too much
st.set_clim((0, 20))
assert st.clim == (0, 20)
assert st.clim_normalized == (0, 20)
assert np.all(st._data == ref_data)
# f32 - custom clim
st = GPUScaledStub()
st.set_clim((0, 20))
st.scale_and_set_data(ref_data.astype(np.float32))
assert st.clim == (0, 20)
assert st.clim_normalized == (0, 20)
assert np.all(st._data == ref_data)
# f32 - flat clim
st = GPUScaledStub()
st.set_clim((10, 10))
st.scale_and_set_data(ref_data.astype(np.float32))
assert st.clim == (10, 10)
assert st.clim_normalized == (10, np.inf)
# assert np.min(st._data) == 0 - does not matter
# f32 - auto clim
st = GPUScaledStub()
st.set_clim("auto")
assert st.clim == "auto"
pytest.raises(RuntimeError, getattr, st, "clim_normalized")
st.scale_and_set_data(ref_data.astype(np.float32))
assert st.clim == (5.0, 25.0)
assert st.clim_normalized == (5.0, 25.0)
run_tests_if_main()