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

65 statements  

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

1import json 

2 

3import numpy as np 

4import pytest 

5 

6from nilearn.plotting import html_connectome 

7from nilearn.plotting.js_plotting_utils import decode 

8from nilearn.plotting.tests.test_js_plotting_utils import check_html 

9 

10 

11def test_prepare_line(): 

12 e = np.asarray([0, 1, 2, 3], dtype=int) 

13 n = np.asarray([[0, 1], [0, 2], [2, 3], [8, 9]], dtype=int) 

14 pe, pn = html_connectome._prepare_line(e, n) 

15 assert (pn == [0, 1, 0, 0, 2, 0, 2, 3, 0, 8, 9, 0]).all() 

16 assert (pe == [0, 0, 0, 1, 1, 0, 2, 2, 0, 3, 3, 0]).all() 

17 

18 

19@pytest.mark.parametrize( 

20 "node_color,expected_marker_colors", 

21 [ 

22 ("cyan", ["#00ffff", "#00ffff", "#00ffff"]), 

23 ("auto", ["#440154", "#20908c", "#fde724"]), 

24 (["cyan", "red", "blue"], ["#00ffff", "#ff0000", "#0000ff"]), 

25 ], 

26) 

27def test_prepare_colors_for_markers(node_color, expected_marker_colors): 

28 number_of_nodes = 3 

29 marker_colors = html_connectome._prepare_colors_for_markers( 

30 node_color, 

31 number_of_nodes, 

32 ) 

33 

34 assert marker_colors == expected_marker_colors 

35 

36 

37def _make_connectome(): 

38 adj = np.diag([1.5, 0.3, 2.5], 2) 

39 adj += adj.T 

40 adj += np.eye(5) 

41 

42 coord = np.arange(5) 

43 coord = np.asarray([coord * 10, -coord, coord[::-1]]).T 

44 return adj, coord 

45 

46 

47def test_get_connectome(): 

48 adj, coord = _make_connectome() 

49 connectome = html_connectome._get_connectome(adj, coord) 

50 con_x = decode(connectome["_con_x"], "<f4") 

51 expected_x = np.asarray( 

52 [ 

53 0, 

54 0, 

55 0, 

56 0, 

57 20, 

58 0, 

59 10, 

60 10, 

61 0, 

62 10, 

63 30, 

64 0, 

65 20, 

66 0, 

67 0, 

68 20, 

69 20, 

70 0, 

71 20, 

72 40, 

73 0, 

74 30, 

75 10, 

76 0, 

77 30, 

78 30, 

79 0, 

80 40, 

81 20, 

82 0, 

83 40, 

84 40, 

85 0, 

86 ], 

87 dtype="<f4", 

88 ) 

89 assert (con_x == expected_x).all() 

90 assert {"_con_x", "_con_y", "_con_z", "_con_w"}.issubset(connectome.keys()) 

91 assert (connectome["line_cmin"], connectome["line_cmax"]) == (-2.5, 2.5) 

92 adj[adj == 0] = np.nan 

93 connectome = html_connectome._get_connectome(adj, coord) 

94 con_x = decode(connectome["_con_x"], "<f4") 

95 assert (con_x == expected_x).all() 

96 assert (connectome["line_cmin"], connectome["line_cmax"]) == (-2.5, 2.5) 

97 

98 

99def test_view_connectome(tmp_path): 

100 adj, coord = _make_connectome() 

101 html = html_connectome.view_connectome(adj, coord) 

102 check_html(tmp_path, html, False, "connectome-plot") 

103 html = html_connectome.view_connectome( 

104 adj, coord, "85.3%", title="SOME_TITLE" 

105 ) 

106 check_html(tmp_path, html, False, "connectome-plot", title="SOME_TITLE") 

107 assert "SOME_TITLE" in html.html 

108 html = html_connectome.view_connectome( 

109 adj, coord, "85.3%", linewidth=8.5, node_size=4.2 

110 ) 

111 check_html( 

112 tmp_path, html, False, "connectome-plot", title="Connectome plot" 

113 ) 

114 html = html_connectome.view_connectome( 

115 adj, coord, "85.3%", linewidth=8.5, node_size=np.arange(len(coord)) 

116 ) 

117 check_html(tmp_path, html, False, "connectome-plot") 

118 

119 

120def test_view_markers(tmp_path): 

121 coords = np.arange(12).reshape((4, 3)) 

122 colors = ["r", "g", "black", "white"] 

123 labels = ["red marker", "green marker", "black marker", "white marker"] 

124 html = html_connectome.view_markers(coords, colors) 

125 check_html(tmp_path, html, False, "connectome-plot") 

126 html = html_connectome.view_markers(coords) 

127 check_html(tmp_path, html, False, "connectome-plot") 

128 html = html_connectome.view_markers(coords, marker_size=15) 

129 check_html(tmp_path, html, False, "connectome-plot") 

130 html = html_connectome.view_markers( 

131 coords, marker_size=np.arange(len(coords)) 

132 ) 

133 check_html(tmp_path, html, False, "connectome-plot") 

134 html = html_connectome.view_markers( 

135 coords, marker_size=list(range(len(coords))) 

136 ) 

137 check_html(tmp_path, html, False, "connectome-plot") 

138 html = html_connectome.view_markers( 

139 coords, marker_size=5.0, marker_color=colors, marker_labels=labels 

140 ) 

141 labels_dict = {"marker_labels": labels} 

142 assert json.dumps(labels_dict)[1:-1] in html.html