Coverage for nilearn/plotting/matrix/tests/test_utils.py: 0%
37 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-20 10:58 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-20 10:58 +0200
1import re
3import numpy as np
4import pytest
5from numpy.testing import assert_array_equal
7from nilearn.glm.first_level.design_matrix import (
8 make_first_level_design_matrix,
9)
10from nilearn.plotting.matrix._utils import (
11 VALID_REORDER_VALUES,
12 VALID_TRI_VALUES,
13 pad_contrast_matrix,
14 sanitize_labels,
15 sanitize_reorder,
16 sanitize_tri,
17)
20def test_pad_contrast_matrix():
21 """Test for contrasts padding before plotting.
23 See https://github.com/nilearn/nilearn/issues/4211
24 """
25 frame_times = np.linspace(0, 127 * 1.0, 128)
26 dmtx = make_first_level_design_matrix(
27 frame_times, drift_model="polynomial", drift_order=3
28 )
29 contrast = np.array([[1, -1]])
30 padded_contrast = pad_contrast_matrix(contrast, dmtx)
31 assert_array_equal(padded_contrast, np.array([[1, -1, 0, 0]]))
33 contrast = np.eye(3)
34 padded_contrast = pad_contrast_matrix(contrast, dmtx)
35 assert_array_equal(
36 padded_contrast,
37 np.array(
38 [
39 [1, 0, 0, 0],
40 [0, 1, 0, 0],
41 [0, 0, 1, 0],
42 ]
43 ),
44 )
47def test_sanitize_labels():
48 labs = ["foo", "bar"]
49 with pytest.raises(
50 ValueError,
51 match=re.escape(
52 "Length of labels (2) unequal to length of matrix (6)."
53 ),
54 ):
55 sanitize_labels((6, 6), labs)
56 for lab in [labs, np.array(labs)]:
57 assert sanitize_labels((2, 2), lab) == labs
60@pytest.mark.parametrize("reorder", VALID_REORDER_VALUES)
61def test_sanitize_reorder(reorder):
62 if reorder is not True:
63 assert sanitize_reorder(reorder) == reorder
64 else:
65 assert sanitize_reorder(reorder) == "average"
68@pytest.mark.parametrize("reorder", [None, "foo", 2])
69def test_sanitize_reorder_error(reorder):
70 with pytest.raises(
71 ValueError, match=("Parameter reorder needs to be one of")
72 ):
73 sanitize_reorder(reorder)
76@pytest.mark.parametrize("tri", VALID_TRI_VALUES)
77def test_sanitize_tri(tri):
78 sanitize_tri(tri)
81@pytest.mark.parametrize("tri", [None, "foo", 2])
82def test_sanitize_tri_error(tri):
83 with pytest.raises(
84 ValueError,
85 match=(
86 f"Parameter tri needs to be one of: {', '.join(VALID_TRI_VALUES)}"
87 ),
88 ):
89 sanitize_tri(tri)