Skip to content

Commit abf8b87

Browse files
committed
Added array comparison tests for shaded relief
1 parent 4afed00 commit abf8b87

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

lib/matplotlib/tests/test_colors.py

+99
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,105 @@ def test_light_source_topo_surface():
254254
ax.imshow(rgb)
255255
ax.set(xticks=[], yticks=[])
256256

257+
def test_light_source_shading_default():
258+
"""Array comparison test for the default "hsv" blend mode. Ensure the
259+
default result doesn't change without warning."""
260+
y, x = np.mgrid[-1.2:1.2:8j, -1.2:1.2:8j]
261+
z = 10 * np.cos(x**2 + y**2)
262+
263+
cmap = plt.cm.copper
264+
ls = mcolors.LightSource(315, 45)
265+
rgb = ls.shade(z, cmap)
266+
267+
r = np.array([[[ 0.87, 0.85, 0.9 , 0.9 , 0.82, 0.62, 0.34, 0. ],
268+
[ 0.85, 0.94, 0.99, 1. , 1. , 0.96, 0.62, 0.17],
269+
[ 0.9 , 0.99, 1. , 1. , 1. , 1. , 0.71, 0.33],
270+
[ 0.9 , 1. , 1. , 1. , 1. , 0.98, 0.51, 0.29],
271+
[ 0.82, 1. , 1. , 1. , 1. , 0.64, 0.25, 0.13],
272+
[ 0.62, 0.96, 1. , 0.98, 0.64, 0.22, 0.06, 0.03],
273+
[ 0.34, 0.62, 0.71, 0.51, 0.25, 0.06, 0. , 0.01],
274+
[ 0. , 0.17, 0.33, 0.29, 0.13, 0.03, 0.01, 0. ]],
275+
276+
[[ 0.87, 0.79, 0.83, 0.8 , 0.66, 0.44, 0.23, 0. ],
277+
[ 0.79, 0.88, 0.93, 0.92, 0.83, 0.66, 0.38, 0.1 ],
278+
[ 0.83, 0.93, 0.99, 1. , 0.92, 0.75, 0.4 , 0.18],
279+
[ 0.8 , 0.92, 1. , 0.99, 0.93, 0.75, 0.28, 0.14],
280+
[ 0.66, 0.83, 0.92, 0.93, 0.87, 0.44, 0.12, 0.06],
281+
[ 0.44, 0.66, 0.75, 0.75, 0.44, 0.12, 0.03, 0.01],
282+
[ 0.23, 0.38, 0.4 , 0.28, 0.12, 0.03, 0. , 0. ],
283+
[ 0. , 0.1 , 0.18, 0.14, 0.06, 0.01, 0. , 0. ]],
284+
285+
[[ 0.87, 0.75, 0.78, 0.73, 0.55, 0.33, 0.16, 0. ],
286+
[ 0.75, 0.85, 0.9 , 0.86, 0.71, 0.48, 0.23, 0.05],
287+
[ 0.78, 0.9 , 0.98, 1. , 0.82, 0.51, 0.21, 0.08],
288+
[ 0.73, 0.86, 1. , 0.97, 0.84, 0.47, 0.11, 0.05],
289+
[ 0.55, 0.71, 0.82, 0.84, 0.71, 0.2 , 0.03, 0.01],
290+
[ 0.33, 0.48, 0.51, 0.47, 0.2 , 0.02, 0. , 0. ],
291+
[ 0.16, 0.23, 0.21, 0.11, 0.03, 0. , 0. , 0. ],
292+
[ 0. , 0.05, 0.08, 0.05, 0.01, 0. , 0. , 0. ]],
293+
294+
[[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
295+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
296+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
297+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
298+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
299+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
300+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
301+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ]]])
302+
# Result stored transposed and rounded for for more compact display...
303+
assert_array_almost_equal(rgb, r.T, decimal=2)
304+
305+
306+
def test_light_source_masked_shading():
307+
"""Array comparison test for a surface with a masked portion. Ensures that
308+
we don't wind up with "fringes" of odd colors around masked regions."""
309+
y, x = np.mgrid[-1.2:1.2:8j, -1.2:1.2:8j]
310+
z = 10 * np.cos(x**2 + y**2)
311+
312+
z = np.ma.masked_greater(z, 9.9)
313+
314+
cmap = plt.cm.copper
315+
ls = mcolors.LightSource(315, 45)
316+
rgb = ls.shade(z, cmap)
317+
318+
r = np.array([[[ 1. , 0.95, 0.96, 0.94, 0.86, 0.67, 0.4 , 0.03],
319+
[ 0.95, 0.99, 1. , 1. , 1. , 0.98, 0.67, 0.19],
320+
[ 0.96, 1. , 1. , 1. , 1. , 1. , 0.78, 0.36],
321+
[ 0.94, 1. , 1. , 0. , 0. , 1. , 0.55, 0.32],
322+
[ 0.86, 1. , 1. , 0. , 0. , 1. , 0.27, 0.14],
323+
[ 0.67, 0.98, 1. , 1. , 1. , 1. , 0.07, 0.03],
324+
[ 0.4 , 0.67, 0.78, 0.55, 0.27, 0.07, 0. , 0.01],
325+
[ 0.03, 0.19, 0.36, 0.32, 0.14, 0.03, 0.01, 0. ]],
326+
327+
[[ 1. , 0.93, 0.93, 0.88, 0.72, 0.5 , 0.28, 0.03],
328+
[ 0.93, 0.97, 0.99, 0.96, 0.87, 0.7 , 0.42, 0.11],
329+
[ 0.93, 0.99, 0.74, 0.78, 0.78, 0.74, 0.45, 0.2 ],
330+
[ 0.88, 0.96, 0.78, 0. , 0. , 0.78, 0.32, 0.16],
331+
[ 0.72, 0.87, 0.78, 0. , 0. , 0.78, 0.14, 0.06],
332+
[ 0.5 , 0.7 , 0.74, 0.78, 0.78, 0.74, 0.03, 0.01],
333+
[ 0.28, 0.42, 0.45, 0.32, 0.14, 0.03, 0. , 0. ],
334+
[ 0.03, 0.11, 0.2 , 0.16, 0.06, 0.01, 0. , 0. ]],
335+
336+
[[ 1. , 0.91, 0.91, 0.84, 0.64, 0.39, 0.21, 0.03],
337+
[ 0.91, 0.96, 0.98, 0.93, 0.77, 0.53, 0.27, 0.06],
338+
[ 0.91, 0.98, 0.47, 0.5 , 0.5 , 0.47, 0.25, 0.1 ],
339+
[ 0.84, 0.93, 0.5 , 0. , 0. , 0.5 , 0.13, 0.06],
340+
[ 0.64, 0.77, 0.5 , 0. , 0. , 0.5 , 0.03, 0.01],
341+
[ 0.39, 0.53, 0.47, 0.5 , 0.5 , 0.47, 0. , 0. ],
342+
[ 0.21, 0.27, 0.25, 0.13, 0.03, 0. , 0. , 0. ],
343+
[ 0.03, 0.06, 0.1 , 0.06, 0.01, 0. , 0. , 0. ]],
344+
345+
[[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
346+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
347+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
348+
[ 1. , 1. , 1. , 0. , 0. , 1. , 1. , 1. ],
349+
[ 1. , 1. , 1. , 0. , 0. , 1. , 1. , 1. ],
350+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
351+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
352+
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ]]])
353+
# Result stored transposed and rounded for for more compact display...
354+
assert_array_almost_equal(rgb, r.T, decimal=2)
355+
257356

258357
def test_light_source_hillshading():
259358
"""Compare the current hillshading method against one that should be

0 commit comments

Comments
 (0)