Coverage for nilearn/plotting/tests/test_cm.py: 0%

37 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-16 12:32 +0200

1# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- 

2# vi: set ft=python sts=4 ts=4 sw=4 et: 

3"""Smoke testing the cm module.""" 

4 

5import matplotlib.pyplot as plt 

6import numpy as np 

7import pytest 

8 

9from nilearn.plotting.cm import dim_cmap, mix_colormaps, replace_inside 

10 

11 

12def test_dim_cmap(): 

13 # This is only a smoke test 

14 dim_cmap(plt.cm.jet) 

15 

16 

17def test_replace_inside(): 

18 # This is only a smoke test 

19 replace_inside(plt.cm.jet, plt.cm.hsv, 0.2, 0.8) 

20 # We also test with gnuplot, which is defined using function 

21 if hasattr(plt.cm, "gnuplot"): 

22 # gnuplot is only in recent version of MPL 

23 replace_inside(plt.cm.gnuplot, plt.cm.gnuplot2, 0.2, 0.8) 

24 

25 

26def test_cm_preload(): 

27 plt.imshow([list(range(10))], cmap="cold_hot") 

28 

29 

30def test_mix_colormaps(rng): 

31 n = 100 

32 

33 # Mixin map's shape should be equal to that of 

34 # the foreground and background maps 

35 foreground_map = rng.random((n, 4)) 

36 background_map = rng.random((n, 4)) 

37 mix_map = mix_colormaps(foreground_map, background_map) 

38 assert mix_map.shape == (n, 4) 

39 # Transparency of mixin map should be higher 

40 # than that of both the background and the foreground maps 

41 assert np.all(mix_map[:, 3] >= foreground_map[:, 3]) 

42 assert np.all(mix_map[:, 3] >= background_map[:, 3]) 

43 

44 # If foreground and background maps' shapes are different, 

45 # an Exception should be raised 

46 background_map = rng.random((n - 1, 4)) 

47 with pytest.raises(ValueError): 

48 mix_colormaps(foreground_map, background_map) 

49 

50 # If foreground map is transparent, 

51 # mixin should be equal to background map 

52 foreground_map = rng.random((n, 4)) 

53 background_map = rng.random((n, 4)) 

54 foreground_map[:, 3] = 0 

55 mix_map = mix_colormaps(foreground_map, background_map) 

56 assert np.allclose(mix_map, background_map) 

57 

58 # If background map is transparent, 

59 # mixin should be equal to foreground map 

60 foreground_map = rng.random((n, 4)) 

61 background_map = rng.random((n, 4)) 

62 background_map[:, 3] = 0 

63 mix_map = mix_colormaps(foreground_map, background_map) 

64 assert np.allclose(mix_map, foreground_map) 

65 

66 # If foreground and background maps are equal, 

67 # RBG values of the mixin map should be equal 

68 # to that of the foreground and background maps 

69 foreground_map = rng.random((n, 4)) 

70 background_map = foreground_map 

71 mix_map = mix_colormaps(foreground_map, background_map) 

72 assert np.allclose(mix_map[:, :3], foreground_map[:, :3])