Coverage for nilearn/plotting/matrix/tests/test_matplotlib_backend.py: 0%

39 statements  

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

1import re 

2 

3import matplotlib.pyplot as plt 

4import numpy as np 

5import pytest 

6 

7from nilearn.plotting.matrix._matplotlib_backend import ( 

8 _sanitize_figure_and_axes, 

9 _sanitize_labels, 

10 _sanitize_reorder, 

11 _sanitize_tri, 

12) 

13from nilearn.plotting.matrix.tests.test_matrix_plotting import VALID_TRI_VALUES 

14 

15VALID_REORDER_VALUES = (True, False, "single", "complete", "average") 

16 

17############################################################################## 

18# Some smoke testing for graphics-related code 

19 

20 

21@pytest.mark.parametrize( 

22 "fig,axes", [("foo", "bar"), (1, 2), plt.subplots(1, 1, figsize=(7, 5))] 

23) 

24def test_sanitize_figure_and_axes_error(fig, axes): 

25 with pytest.raises( 

26 ValueError, 

27 match=("Parameters figure and axes cannot be specified together."), 

28 ): 

29 _sanitize_figure_and_axes(fig, axes) 

30 

31 

32@pytest.mark.parametrize( 

33 "fig,axes,expected", 

34 [ 

35 ((6, 4), None, True), 

36 (plt.figure(figsize=(3, 2)), None, True), 

37 (None, None, True), 

38 (None, plt.subplots(1, 1)[1], False), 

39 ], 

40) 

41def test_sanitize_figure_and_axes(fig, axes, expected): 

42 fig2, axes2, own_fig = _sanitize_figure_and_axes(fig, axes) 

43 assert isinstance(fig2, plt.Figure) 

44 assert isinstance(axes2, plt.Axes) 

45 assert own_fig == expected 

46 

47 

48def test_sanitize_labels(): 

49 labs = ["foo", "bar"] 

50 with pytest.raises( 

51 ValueError, 

52 match=re.escape( 

53 "Length of labels (2) unequal to length of matrix (6)." 

54 ), 

55 ): 

56 _sanitize_labels((6, 6), labs) 

57 for lab in [labs, np.array(labs)]: 

58 assert _sanitize_labels((2, 2), lab) == labs 

59 

60 

61@pytest.mark.parametrize("tri", VALID_TRI_VALUES) 

62def test_sanitize_tri(tri): 

63 _sanitize_tri(tri) 

64 

65 

66@pytest.mark.parametrize("tri", [None, "foo", 2]) 

67def test_sanitize_tri_error(tri): 

68 with pytest.raises( 

69 ValueError, 

70 match=( 

71 f"Parameter tri needs to be one of: {', '.join(VALID_TRI_VALUES)}" 

72 ), 

73 ): 

74 _sanitize_tri(tri) 

75 

76 

77@pytest.mark.parametrize("reorder", VALID_REORDER_VALUES) 

78def test_sanitize_reorder(reorder): 

79 if reorder is not True: 

80 assert _sanitize_reorder(reorder) == reorder 

81 else: 

82 assert _sanitize_reorder(reorder) == "average" 

83 

84 

85@pytest.mark.parametrize("reorder", [None, "foo", 2]) 

86def test_sanitize_reorder_error(reorder): 

87 with pytest.raises( 

88 ValueError, match=("Parameter reorder needs to be one of") 

89 ): 

90 _sanitize_reorder(reorder)