-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_colorizer.py
375 lines (311 loc) · 15.6 KB
/
test_colorizer.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""Tests for the colorizer module."""
import json
import unittest
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import pytest
import geoengine as ge
from geoengine import colorizer
class ColorizerTests(unittest.TestCase):
"""Colorizer test runner."""
def setUp(self) -> None:
"""Set up the geo engine session."""
ge.reset(logout=False)
def test_gray_linear(self):
"""Test the basic black to white cmap colorizer."""
expected = {
"type": "linearGradient",
"breakpoints": [
{"value": 0.0, "color": [0, 0, 0, 255]},
{"value": 63.75, "color": [64, 64, 64, 255]},
{"value": 127.5, "color": [128, 128, 128, 255]},
{"value": 191.25, "color": [192, 192, 192, 255]},
{"value": 255.0, "color": [255, 255, 255, 255]}
],
"noDataColor": [0, 0, 0, 0],
"overColor": [0, 0, 0, 0],
"underColor": [0, 0, 0, 0]
}
geo_colorizer = colorizer.Colorizer.linear_with_mpl_cmap(color_map="gray", min_max=(0.0, 255.0), n_steps=5)
gray = geo_colorizer.to_api_dict().to_dict()
assert gray == expected
def test_gray_logarithmic(self):
"""Test the basic black to white cmap colorizer."""
expected = {
"type": "logarithmicGradient",
"breakpoints": [
{"value": 1.0, "color": [0, 0, 0, 255]},
{"value": 10.0, "color": [64, 64, 64, 255]},
{"value": 100.0, "color": [128, 128, 128, 255]},
{"value": 1000.0, "color": [192, 192, 192, 255]},
{"value": 10000.0, "color": [255, 255, 255, 255]}
],
"noDataColor": [0, 0, 0, 0],
"overColor": [0, 0, 0, 0],
"underColor": [0, 0, 0, 0]
}
geo_colorizer = colorizer.Colorizer.logarithmic_with_mpl_cmap(
color_map="gray", min_max=(1.0, 10000.0), n_steps=5)
gray = geo_colorizer.to_api_dict().to_dict()
assert gray == expected
def test_gray_palette_with_explicit_mapping(self):
"""Test the basic black to white cmap colorizer."""
expected = {
"type": "palette",
"colors": {
"1.0": [0, 0, 0, 255],
"2.0": [128, 128, 128, 255],
"3.0": [255, 255, 255, 255]
},
"noDataColor": [0, 0, 0, 0],
"defaultColor": [0, 0, 0, 0]
}
geo_colorizer = colorizer.Colorizer.palette(
color_mapping={
1.0: [0, 0, 0, 255],
2.0: [128, 128, 128, 255],
3.0: [255, 255, 255, 255],
})
gray = geo_colorizer.to_api_dict().to_dict()
assert gray == expected
def test_gray_palette_with_mpl_cmap(self):
"""Test the basic black to white cmap colorizer.
Checks, if cmap or name of cmap can be given as a parameter."""
expected = {
"type": "palette",
"colors": {
"1.0": [0, 0, 0, 255],
"2.0": [128, 128, 128, 255],
"3.0": [255, 255, 255, 255]
},
"noDataColor": [0, 0, 0, 0],
"defaultColor": [0, 0, 0, 0]
}
# verify color map object variant
cmap_obj = plt.colormaps["gray"]
geo_colorizer = colorizer.Colorizer.palette_with_colormap(
values=[1.0, 2.0, 3.0], color_map=cmap_obj)
gray_obj = geo_colorizer.to_api_dict().to_dict()
assert gray_obj == expected
# verify color map name variant
cmap_name = "gray"
geo_colorizer = colorizer.Colorizer.palette_with_colormap(
values=[1.0, 2.0, 3.0], color_map=cmap_name)
gray_name = geo_colorizer.to_api_dict().to_dict()
assert gray_name == expected
def test_colormap_not_available(self):
"""Test that an error is raised when a colormap is not available."""
with self.assertRaises(ValueError) as ctx:
colorizer.Colorizer.linear_with_mpl_cmap(color_map="some_map", min_max=(0.0, 255.0))
result = str(ctx.exception)
expected_end = "supported values are 'Accent', 'Accent_r', "\
"'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', "\
"'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', "\
"'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', "\
"'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', "\
"'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', "\
"'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', "\
"'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', "\
"'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', "\
"'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', "\
"'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', "\
"'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', "\
"'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', "\
"'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', "\
"'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', "\
"'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', "\
"'jet', 'jet_r', 'magma', " "'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', "\
"'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', "\
"'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', "\
"'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', "\
"'terrain_r', 'turbo', " "'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', "\
"'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r'"
assert result.endswith(expected_end)
def test_defaults(self):
"""Tests the manipulation of the default values."""
expected = {
"type": "linearGradient",
"breakpoints": [
{"value": 0.0, "color": [68, 1, 84, 255]},
{"value": 255.0, "color": [253, 231, 36, 255]}
],
"noDataColor": [100, 100, 100, 100],
"overColor": [100, 100, 100, 100],
"underColor": [100, 100, 100, 100]
}
geo_colorizer = colorizer.Colorizer.linear_with_mpl_cmap(
color_map="viridis",
min_max=(0.0, 255.0),
n_steps=2,
no_data_color=(100, 100, 100, 100),
over_color=(100, 100, 100, 100),
under_color=(100, 100, 100, 100)
)
viridis = geo_colorizer.to_api_dict().to_dict()
assert viridis == expected
def test_set_steps(self):
"""Tests the setting of the number of steps."""
geo_colorizer = colorizer.Colorizer.linear_with_mpl_cmap(color_map="viridis", min_max=(0.0, 255.0), n_steps=2)
viridis = geo_colorizer.to_api_dict().to_dict()
expected = {
"type": "linearGradient",
"breakpoints": [
{"value": 0.0, "color": [68, 1, 84, 255]},
{"value": 255.0, "color": [253, 231, 36, 255]}
],
"noDataColor": [0, 0, 0, 0],
"overColor": [0, 0, 0, 0],
"underColor": [0, 0, 0, 0]
}
assert viridis == expected
geo_colorizer = colorizer.Colorizer.linear_with_mpl_cmap(color_map="viridis", min_max=(0.0, 255.0), n_steps=3)
viridis = geo_colorizer.to_api_dict().to_dict()
expected = {
"type": "linearGradient", "breakpoints": [
{"value": 0.0, "color": [68, 1, 84, 255]},
{"value": 127.5, "color": [32, 144, 140, 255]},
{"value": 255.0, "color": [253, 231, 36, 255]}
],
"noDataColor": [0, 0, 0, 0],
"overColor": [0, 0, 0, 0],
"underColor": [0, 0, 0, 0]
}
assert viridis == expected
def test_set_minmax(self):
"""Tests the setting of the min and max values."""
geo_colorizer = colorizer.Colorizer.linear_with_mpl_cmap(color_map="viridis", min_max=(-10.0, 10.0), n_steps=3)
expected = {
"type": "linearGradient",
"breakpoints": [
{"value": -10.0, "color": [68, 1, 84, 255]},
{"value": 0.0, "color": [32, 144, 140, 255]},
{"value": 10.0, "color": [253, 231, 36, 255]}
],
"noDataColor": [0, 0, 0, 0],
"overColor": [0, 0, 0, 0],
"underColor": [0, 0, 0, 0]
}
viridis = geo_colorizer.to_api_dict().to_dict()
assert viridis == expected
def test_set_minmax_log_wrong_values(self):
"""Tests the setting of wrong values for the min and max of a logarithmic gradient."""
with self.assertRaises(ValueError) as ctx:
colorizer.Colorizer.logarithmic_with_mpl_cmap(color_map="viridis", min_max=(-10.0, 10.0), n_steps=3)
self.assertEqual(str(ctx.exception), "min_max[0] must be greater than 0 for a logarithmic gradient, got -10.0.")
def test_minmax_wrong_order(self):
"""Tests if an error is raised, when the setting of the min and max values is wrong."""
wrong_min = 10.0
wrong_max = -10.0
with self.assertRaises(ValueError) as ctx:
colorizer.Colorizer.linear_with_mpl_cmap(color_map="viridis", min_max=(wrong_min, wrong_max), n_steps=3)
self.assertEqual(str(ctx.exception), "min_max[1] must be greater than min_max[0],"
f" got {wrong_max} and {wrong_min}.")
def test_wrong_color_specification(self):
"""Tests if an error is raised, when the color specification is wrong."""
# no data color
wrong_colors = [(-1, 0, 0, 0), (0, 0, 0, -1), (256, 0, 0, 0), (0, 256, 0, 0), (-1, 258, 0, 0)]
for wrong_color_code in wrong_colors:
with self.assertRaises(ValueError) as ctx:
colorizer.Colorizer.linear_with_mpl_cmap(
color_map="viridis",
min_max=(0.0, 255.0),
n_steps=3,
no_data_color=wrong_color_code
)
self.assertEqual(str(ctx.exception), 'noDataColor must be a RGBA color specification, '
f'got {wrong_color_code} instead.')
# over color
with self.assertRaises(ValueError) as ctx:
colorizer.Colorizer.linear_with_mpl_cmap(
color_map="viridis",
min_max=(0.0, 255.0),
n_steps=3,
over_color=wrong_color_code
)
self.assertEqual(str(ctx.exception), "overColor must be a RGBA color specification, "
f"got {wrong_color_code} instead.")
# under color
with self.assertRaises(ValueError) as ctx:
colorizer.Colorizer.linear_with_mpl_cmap(
color_map="viridis",
min_max=(0.0, 255.0),
n_steps=3,
under_color=wrong_color_code
)
self.assertEqual(str(ctx.exception), "underColor must be a RGBA color specification, "
f"got {wrong_color_code} instead.")
def test_custom_map(self):
"""Tests the setting of a custom map."""
expected = {
"type": "linearGradient",
"breakpoints": [
{"value": 0.0, "color": [255, 140, 0, 255]},
{"value": 127.5, "color": [124, 252, 0, 255]},
{"value": 255.0, "color": [32, 178, 170, 255]}
],
"noDataColor": [0, 0, 0, 0],
"overColor": [0, 0, 0, 0],
"underColor": [0, 0, 0, 0]
}
custom_map = ListedColormap(["darkorange", "gold", "lawngreen", "lightseagreen"])
geo_colorizer = colorizer.Colorizer.linear_with_mpl_cmap(color_map=custom_map, min_max=(0.0, 255.0), n_steps=3)
custom = geo_colorizer.to_api_dict().to_dict()
assert custom == expected
def test_custom_map_with_options(self):
"""Tests the setting of a custom map with options."""
expected = {
"type": "linearGradient",
"breakpoints": [
{"value": 40.0, "color": [255, 140, 0, 255]},
{"value": 220.0, "color": [124, 252, 0, 255]},
{"value": 400.0, "color": [32, 178, 170, 255]}
],
"noDataColor": [100, 100, 100, 100],
"overColor": [100, 100, 100, 100],
"underColor": [100, 100, 100, 100]
}
custom_map = ListedColormap(["darkorange", "gold", "lawngreen", "lightseagreen"])
geo_colorizer = colorizer.Colorizer.linear_with_mpl_cmap(
color_map=custom_map,
min_max=(40.0, 400.0),
n_steps=3,
no_data_color=(100, 100, 100, 100),
over_color=(100, 100, 100, 100),
under_color=(100, 100, 100, 100)
)
custom = geo_colorizer.to_api_dict().to_dict()
assert custom == expected
def test_to_json(self):
"""Tests the to_json method."""
expected = '{"overColor": [0, 0, 0, 0], "underColor": [0, 0, 0, 0],'\
' "breakpoints": [{"color": [68, 1, 84, 255], "value": 0.0'\
'}, {"color": [253, 231, 36, 255], "value": 255.0}], "noDataColor": [0, 0, 0, 0],'\
' "type": "linearGradient"}'
geo_colorizer = colorizer.Colorizer.linear_with_mpl_cmap(color_map="viridis", min_max=(0.0, 255.0), n_steps=2)
jsonstr = geo_colorizer.to_api_dict().to_json()
self.assertDictEqual(json.loads(jsonstr), json.loads(expected))
def test_palette_with_too_small_colormap(self):
"""Tests, if the warning is emitted if an unappropriate color map is chosen."""
with pytest.warns(UserWarning, match="Warning!\nYour colormap does not have enough colors "
"to display all unique values of the palette!"
"\nNumber of values given: 6 vs. Number of available colors: 4"):
custom_map = ListedColormap(["darkorange", "gold", "lawngreen", "lightseagreen"])
colorizer.Colorizer.palette_with_colormap(
values=[1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
color_map=custom_map,
no_data_color=(0, 0, 0, 0),
default_color=(0, 0, 0, 0)
)
def test_palette_with_too_small_colormap_from_mpl(self):
"""Tests, if the warning is emittd if an unappropriate color map is chosen."""
with pytest.warns(UserWarning, match="Warning!\nYour colormap does not have enough colors "
"to display all unique values of the palette!"
"\nNumber of values given: 10 vs. Number of available colors: 8"):
colorizer.Colorizer.palette_with_colormap(
values=[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
color_map="Accent", # holds 8 colors
no_data_color=(0, 0, 0, 0),
default_color=(0, 0, 0, 0)
)
if __name__ == '__main__':
unittest.main()