Coverage for nilearn/_utils/tests/test_glm.py: 0%

21 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-20 10:58 +0200

1import numpy as np 

2import pandas as pd 

3import pytest 

4 

5from nilearn._utils.glm import check_and_load_tables, coerce_to_dict 

6 

7 

8def test_img_table_checks(): 

9 # check tables type and that can be loaded 

10 with pytest.raises( 

11 ValueError, match="Tables to load can only be TSV or CSV." 

12 ): 

13 check_and_load_tables([".csv", ".csv"], "") 

14 with pytest.raises( 

15 TypeError, 

16 match="can only be a pandas DataFrame, a Path object or a string", 

17 ): 

18 check_and_load_tables([[], pd.DataFrame()], "") 

19 with pytest.raises( 

20 ValueError, match="Tables to load can only be TSV or CSV." 

21 ): 

22 check_and_load_tables([".csv", pd.DataFrame()], "") 

23 

24 

25@pytest.mark.parametrize( 

26 "input, output", 

27 ( 

28 # None 

29 [None, None], 

30 # string 

31 ["StopSuccess - Go", {"StopSuccess - Go": "StopSuccess - Go"}], 

32 # list_of_strings, 

33 [ 

34 ["contrast_name_0", "contrast_name_1"], 

35 { 

36 "contrast_name_0": "contrast_name_0", 

37 "contrast_name_1": "contrast_name_1", 

38 }, 

39 ], 

40 # dict 

41 [ 

42 {"contrast_0": [0, 0, 1], "contrast_1": [0, 1, 1]}, 

43 {"contrast_0": [0, 0, 1], "contrast_1": [0, 1, 1]}, 

44 ], 

45 # list of lists 

46 [ 

47 [[0, 0, 1], [0, 1, 0]], 

48 {"[0, 0, 1]": [0, 0, 1], "[0, 1, 0]": [0, 1, 0]}, 

49 ], 

50 ), 

51) 

52def test_coerce_to_dict(input, output): 

53 """Check that proper dictionary of contrasts are generated.""" 

54 actual_output = coerce_to_dict(input) 

55 

56 assert actual_output == output 

57 

58 

59@pytest.mark.parametrize( 

60 "input, output", 

61 ( 

62 # list of ints 

63 [[1, 0, 1], {"[1, 0, 1]": [1, 0, 1]}], 

64 # array 

65 [np.array([1, 0, 1]), {"[1 0 1]": np.array([1, 0, 1])}], 

66 # list of arrays 

67 [ 

68 [np.array([0, 0, 1]), np.array([0, 1, 0])], 

69 { 

70 "[0 0 1]": np.array([0, 0, 1]), 

71 "[0 1 0]": np.array([0, 1, 0]), 

72 }, 

73 ], 

74 ), 

75) 

76def test_coerce_to_dict_with_arrays(input, output): 

77 """Check that proper dictionary of contrasts are generated from arrays.""" 

78 actual_output = coerce_to_dict(input) 

79 

80 assert actual_output.keys() == output.keys() 

81 for key in actual_output: 

82 assert np.array_equal(actual_output[key], output[key])