Coverage for nilearn/mass_univariate/tests/_testing.py: 0%

45 statements  

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

1"""Utility functions for the mass_univariate module's test suite.""" 

2 

3import numpy as np 

4 

5 

6def get_tvalue_with_alternative_library(tested_vars, target_vars, covars=None): 

7 """Compute t values with linalg or statsmodels. 

8 

9 Massively univariate linear model (= each target is considered 

10 independently). 

11 

12 Parameters 

13 ---------- 

14 tested_vars : array-like, shape=(n_samples, n_regressors) 

15 Tested variates, the associated coefficient of which are to be tested 

16 independently with a t-test, resulting in as many t-values. 

17 

18 target_vars : array-like, shape=(n_samples, n_descriptors) 

19 Target variates, to be approximated with a linear combination of 

20 the tested variates and the confounding variates. 

21 

22 covars : array-like, shape=(n_samples, n_confounds) 

23 Confounding variates, to be fitted but not to be tested 

24 

25 Returns 

26 ------- 

27 t-values: np.ndarray, shape=(n_regressors, n_descriptors) 

28 

29 """ 

30 # set up design 

31 n_samples, n_regressors = tested_vars.shape 

32 n_descriptors = target_vars.shape[1] 

33 if covars is not None: 

34 n_covars = covars.shape[1] 

35 design_matrix = np.hstack((tested_vars, covars)) 

36 else: 

37 n_covars = 0 

38 design_matrix = tested_vars 

39 mask_covars = np.ones(n_regressors + n_covars, dtype=bool) 

40 mask_covars[:n_regressors] = False 

41 test_matrix = np.array([[1.0] + [0.0] * n_covars]) 

42 

43 # t-values computation 

44 try: # try with statsmodels if available (more concise) 

45 from statsmodels.regression.linear_model import OLS 

46 

47 t_values = np.empty((n_descriptors, n_regressors)) 

48 for i in range(n_descriptors): 

49 current_target = target_vars[:, i].reshape((-1, 1)) 

50 for j in range(n_regressors): 

51 current_tested_mask = mask_covars.copy() 

52 current_tested_mask[j] = True 

53 current_design_matrix = design_matrix[:, current_tested_mask] 

54 ols_fit = OLS(current_target, current_design_matrix).fit() 

55 t_values[i, j] = np.squeeze(ols_fit.t_test(test_matrix).tvalue) 

56 

57 except ImportError: # use linalg if statsmodels is not available 

58 from numpy import linalg 

59 

60 lost_dof = n_covars + 1 # fit all tested variates independently 

61 t_values = np.empty((n_descriptors, n_regressors)) 

62 for i in range(n_regressors): 

63 current_tested_mask = mask_covars.copy() 

64 current_tested_mask[i] = True 

65 current_design_matrix = design_matrix[:, current_tested_mask] 

66 invcov = linalg.pinv(current_design_matrix) 

67 normalized_cov = np.dot(invcov, invcov.T) 

68 t_val_denom_aux = np.diag( 

69 np.dot(test_matrix, np.dot(normalized_cov, test_matrix.T)) 

70 ) 

71 t_val_denom_aux = t_val_denom_aux.reshape((-1, 1)) 

72 

73 for j in range(n_descriptors): 

74 current_target = target_vars[:, j].reshape((-1, 1)) 

75 res_lstsq = linalg.lstsq( 

76 current_design_matrix, current_target, rcond=-1 

77 ) 

78 residuals = current_target - np.dot( 

79 current_design_matrix, res_lstsq[0] 

80 ) 

81 t_val_num = np.dot(test_matrix, res_lstsq[0]) 

82 t_val_denom = np.sqrt( 

83 np.sum(residuals**2, 0) 

84 / float(n_samples - lost_dof) 

85 * t_val_denom_aux 

86 ) 

87 t_values[j, i] = np.squeeze(t_val_num / t_val_denom) 

88 

89 t_values = t_values.T 

90 assert t_values.shape == (n_regressors, n_descriptors) 

91 return t_values