Coverage for nilearn/interfaces/fmriprep/tests/test_load_confounds_components.py: 0%

47 statements  

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

1import pandas as pd 

2import pytest 

3 

4from nilearn.interfaces.fmriprep import load_confounds 

5from nilearn.interfaces.fmriprep.load_confounds import _load_noise_component 

6from nilearn.interfaces.fmriprep.load_confounds_utils import ( 

7 load_confounds_json, 

8) 

9from nilearn.interfaces.fmriprep.tests._testing import create_tmp_filepath 

10 

11 

12@pytest.fixture 

13def expected_parameters(strategy_keywords): 

14 """Return expoected parameters for a given strategy.""" 

15 expectation = { 

16 "compcor": {"compcor": "anat_combined", "n_compcor": 6}, 

17 "ica_aroma": {"ica_aroma": "basic"}, 

18 } 

19 if strategy_keywords in expectation: 

20 return expectation[strategy_keywords] 

21 elif strategy_keywords != "high_pass": 

22 return {strategy_keywords: "full"} 

23 else: 

24 return False 

25 

26 

27@pytest.mark.parametrize("fmriprep_version", ["1.4.x", "21.x.x"]) 

28@pytest.mark.parametrize( 

29 "strategy_keywords", 

30 ["motion", "wm_csf", "global_signal", "compcor", "ica_aroma"], 

31) 

32def test_missing_keywords( 

33 tmp_path, strategy_keywords, expected_parameters, fmriprep_version 

34): 

35 """Check the strategy keywords are raising errors correctly in low \ 

36 and high level functions. 

37 """ 

38 img, bad_conf = create_tmp_filepath( 

39 tmp_path, 

40 copy_confounds=True, 

41 copy_json=True, 

42 fmriprep_version=fmriprep_version, 

43 ) 

44 legal_confounds = pd.read_csv(bad_conf, delimiter="\t", encoding="utf-8") 

45 meta_json = bad_conf.parent / bad_conf.name.replace("tsv", "json") 

46 meta_json = load_confounds_json(meta_json, flag_acompcor=True) 

47 

48 # remove confound strategy keywords in the test data 

49 remove_columns, missing = _load_noise_component( 

50 legal_confounds, 

51 component=strategy_keywords, 

52 missing={"confounds": [], "keywords": []}, 

53 meta_json=meta_json, 

54 **expected_parameters, 

55 ) 

56 

57 confounds_raw = legal_confounds.drop(columns=remove_columns) 

58 confounds_raw.to_csv(bad_conf, sep="\t", index=False) # save in tmp_path 

59 

60 # unit test of module `load_confounds_components` 

61 # and higher level wrapper `load_confounds` 

62 # The two should behave the same 

63 missing = {"confounds": [], "keywords": []} 

64 

65 confounds_unit_test, missing = _load_noise_component( 

66 confounds_raw, 

67 component=strategy_keywords, 

68 missing=missing, 

69 meta_json=meta_json, 

70 **expected_parameters, 

71 ) 

72 assert confounds_unit_test.empty is True 

73 assert missing["keywords"] == [strategy_keywords] 

74 

75 with pytest.raises(ValueError) as exc_info: 

76 load_confounds( 

77 img, strategy=[strategy_keywords], **expected_parameters 

78 ) 

79 assert strategy_keywords in exc_info.value.args[0] 

80 

81 

82@pytest.mark.parametrize("fmriprep_version", ["1.4.x", "21.x.x"]) 

83def test_missing_keywords_high_pass(tmp_path, fmriprep_version): 

84 """Check the strategy keywords are not raising errors with `high_pass`.""" 

85 strategy_keywords = "high_pass" 

86 img, bad_conf = create_tmp_filepath( 

87 tmp_path, 

88 copy_confounds=True, 

89 copy_json=True, 

90 fmriprep_version=fmriprep_version, 

91 ) 

92 legal_confounds = pd.read_csv(bad_conf, delimiter="\t", encoding="utf-8") 

93 meta_json = bad_conf.parent / bad_conf.name.replace("tsv", "json") 

94 meta_json = load_confounds_json(meta_json, flag_acompcor=True) 

95 

96 # remove confound strategy keywords in the test data 

97 remove_columns, missing = _load_noise_component( 

98 legal_confounds, 

99 component=strategy_keywords, 

100 missing={"confounds": [], "keywords": []}, 

101 ) 

102 confounds_raw = legal_confounds.drop(columns=remove_columns) 

103 confounds_raw.to_csv(bad_conf, sep="\t", index=False) # save in tmp_path 

104 

105 # unit test of module `load_confounds_components` 

106 # and higher level wrapper `load_confounds` 

107 # The two should behave the same 

108 missing = {"confounds": [], "keywords": []} 

109 

110 # when no cosine regressors are present in the confound file, 

111 # it will not raise error 

112 confounds_unit_test, missing = _load_noise_component( 

113 confounds_raw, component=strategy_keywords, missing=missing 

114 ) 

115 assert confounds_unit_test.empty is True 

116 assert len(missing["keywords"]) == 0 

117 

118 confounds_lc, _ = load_confounds(img, strategy=[strategy_keywords]) 

119 assert confounds_lc.empty is True