Coverage for nilearn/plotting/tests/test_img_plotting/test_plot_markers.py: 0%

60 statements  

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

1"""Tests for :func:`nilearn.plotting.plot_markers`.""" 

2 

3# ruff: noqa: ARG001 

4 

5import matplotlib.pyplot as plt 

6import numpy as np 

7import pytest 

8 

9from nilearn.conftest import _rng 

10from nilearn.plotting import plot_markers 

11 

12 

13@pytest.fixture 

14def coords(): 

15 """Node coordinates for testing.""" 

16 return np.array( 

17 [[39, 6, -32], [29, 40, 1], [-20, -74, 35], [-29, -59, -37]] 

18 ) 

19 

20 

21@pytest.mark.parametrize( 

22 "node_values", 

23 [ 

24 [1, 2, 3, 4], 

25 np.array([1, 2, 3, 4]), 

26 np.array([1, 2, 3, 4])[:, np.newaxis], 

27 np.array([1, 2, 3, 4])[np.newaxis, :], 

28 (1, 1, 1, 1), 

29 ], 

30) 

31def test_plot_markers_node_values(matplotlib_pyplot, node_values, coords): 

32 """Smoke test for plot_markers with different node values.""" 

33 plot_markers(node_values, coords, display_mode="x") 

34 

35 

36@pytest.mark.parametrize( 

37 "node_size", [10, [10, 20, 30, 40], np.array([10, 20, 30, 40])] 

38) 

39def test_plot_markers_node_sizes(matplotlib_pyplot, node_size, coords): 

40 """Smoke test for plot_markers with different node sizes.""" 

41 plot_markers([1, 2, 3, 4], coords, node_size=node_size, display_mode="x") 

42 

43 

44@pytest.mark.parametrize( 

45 "node_size", [[10] * 4, [10, 20, 30, 40], np.array([10, 20, 30, 40])] 

46) 

47def test_plot_markers_node_sizes_lyrz_display( 

48 matplotlib_pyplot, node_size, coords 

49): 

50 """Tests for plot_markers and 'lyrz' display mode. 

51 

52 Tests that markers are plotted with the requested size 

53 with display_mode='lyrz'. (See issue #3012 and PR #3013). 

54 """ 

55 display = plot_markers( 

56 [1, 2, 3, 4], coords, display_mode="lyrz", node_size=node_size 

57 ) 

58 for d, axes in display.axes.items(): 

59 display_sizes = axes.ax.collections[0].get_sizes() 

60 if d == "l": 

61 expected_sizes = node_size[-2:] 

62 elif d == "r": 

63 expected_sizes = node_size[:-2] 

64 else: 

65 expected_sizes = node_size 

66 assert np.all(display_sizes == expected_sizes) 

67 

68 

69@pytest.mark.parametrize( 

70 "cmap,vmin,vmax", 

71 [ 

72 ("RdBu", 0, None), 

73 (plt.get_cmap("jet"), None, 5), 

74 ("viridis_r", 2, 3), 

75 ], 

76) 

77def test_plot_markers_cmap(matplotlib_pyplot, cmap, vmin, vmax, coords): 

78 """Smoke test for plot_markers with different cmaps.""" 

79 plot_markers( 

80 [1, 2, 3, 4], 

81 coords, 

82 node_cmap=cmap, 

83 node_vmin=vmin, 

84 node_vmax=vmax, 

85 display_mode="x", 

86 ) 

87 

88 

89@pytest.mark.parametrize("threshold", [-100, 2.5]) 

90def test_plot_markers_threshold(matplotlib_pyplot, threshold, coords): 

91 """Smoke test for plot_markers with different threshold values.""" 

92 plot_markers( 

93 [1, 2, 3, 4], coords, node_threshold=threshold, display_mode="x" 

94 ) 

95 

96 

97def test_plot_markers_tuple_node_coords(matplotlib_pyplot, coords): 

98 """Smoke test for plot_markers with node coordinates passed \ 

99 as a list of tuples. 

100 """ 

101 plot_markers( 

102 [1, 2, 3, 4], [tuple(coord) for coord in coords], display_mode="x" 

103 ) 

104 

105 

106def test_plot_markers_saving_to_file(matplotlib_pyplot, coords, tmp_path): 

107 """Smoke test for plot_markers and file saving.""" 

108 filename = tmp_path / "test.png" 

109 display = plot_markers( 

110 [1, 2, 3, 4], coords, output_file=filename, display_mode="x" 

111 ) 

112 

113 assert display is None 

114 assert filename.is_file() and filename.stat().st_size > 0 

115 

116 

117def test_plot_markers_node_kwargs(matplotlib_pyplot, coords): 

118 """Smoke test for plot_markers testing that node_kwargs is working \ 

119 and does not interfere with alpha. 

120 """ 

121 node_kwargs = {"marker": "s"} 

122 plot_markers( 

123 [1, 2, 3, 4], 

124 coords, 

125 alpha=0.1, 

126 node_kwargs=node_kwargs, 

127 display_mode="x", 

128 ) 

129 

130 

131@pytest.mark.parametrize( 

132 "matrix", 

133 [ 

134 [1, 2, 3, 4, 5], 

135 [1, 2, 3], 

136 _rng().random((4, 4)), 

137 ], 

138) 

139def test_plot_markers_dimension_mismatch(matplotlib_pyplot, matrix, coords): 

140 """Tests that an error is raised in plot_markers \ 

141 when the length of node_values mismatches with node_coords. 

142 """ 

143 with pytest.raises(ValueError, match="Dimension mismatch"): 

144 plot_markers(matrix, coords, display_mode="x") 

145 

146 

147@pytest.mark.parametrize("vmin,vmax", [(5, None), (None, 0)]) 

148def test_plot_markers_bound_error(matplotlib_pyplot, vmin, vmax, coords): 

149 """Tests that a ValueError is raised when vmin and vmax \ 

150 have inconsistent values. 

151 """ 

152 with pytest.raises(ValueError): 

153 plot_markers( 

154 [1, 2, 2, 4], 

155 coords, 

156 node_vmin=vmin, 

157 node_vmax=vmax, 

158 display_mode="x", 

159 ) 

160 

161 

162def test_plot_markers_node_values_errors(matplotlib_pyplot, coords): 

163 """Tests that a TypeError is raised when node_values is wrong type.""" 

164 with pytest.raises(TypeError): 

165 plot_markers(["1", "2", "3", "4"], coords, display_mode="x") 

166 

167 

168def test_plot_markers_threshold_errors(matplotlib_pyplot, coords): 

169 """Tests that a ValueError is raised when node_threshold is \ 

170 higher than the max node_value. 

171 """ 

172 with pytest.raises(ValueError, match="Provided 'node_threshold' value"): 

173 plot_markers([1, 2, 2, 4], coords, node_threshold=5, display_mode="x") 

174 

175 

176def test_plot_markers_single_node_value(matplotlib_pyplot): 

177 """Regression test for Issue #3253.""" 

178 plot_markers([1], [[1, 1, 1]]) 

179 

180 

181def test_plot_markers_radiological_view(matplotlib_pyplot): 

182 """Smoke test for radiological view.""" 

183 result = plot_markers([1], [[1, 1, 1]], radiological=True) 

184 assert result.axes.get("y").radiological is True