From 6c30a83449ff9322c77a34869789245c80424445 Mon Sep 17 00:00:00 2001 From: Marc Hulcelle Date: Tue, 7 Jul 2026 11:07:58 +0200 Subject: [PATCH] [DOC] fixed inconsistent shape for importance or selection arrays --- src/hidimstat/base_perturbation.py | 26 +++++-------------- src/hidimstat/base_variable_importance.py | 6 ++--- .../conditional_feature_importance.py | 10 +++---- src/hidimstat/desparsified_lasso.py | 25 ++++++++++++------ src/hidimstat/ensemble_clustered_inference.py | 16 +++++++++--- src/hidimstat/knockoffs.py | 24 ++++++++--------- src/hidimstat/leave_one_covariate_in.py | 10 +++---- src/hidimstat/leave_one_covariate_out.py | 19 ++++++-------- .../permutation_feature_importance.py | 10 +++---- 9 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/hidimstat/base_perturbation.py b/src/hidimstat/base_perturbation.py index 90fb53e4e..ac6b4047c 100644 --- a/src/hidimstat/base_perturbation.py +++ b/src/hidimstat/base_perturbation.py @@ -179,27 +179,11 @@ def importance(self, X, y): The input samples to compute importance scores for. y : array-like of shape (n_samples,) - importances_ : ndarray of shape (n_groups,) - The importance scores for each group of covariates. - A higher score indicates greater importance of that group. - Returns ------- - importances_ : ndarray of shape (n_features,) - Importance scores for each feature. - - Attributes - ---------- - loss_reference_ : float - The loss of the model with the original (non-perturbed) data. - loss_ : dict - Dictionary with indices as keys and arrays of perturbed losses as values. - Contains the loss values for each permutation of each group. importances_ : ndarray of shape (n_groups,) - The calculated importance scores for each group. - pvalues_ : ndarray of shape (n_groups,) - P-values from one-sample t-test testing if importance scores are - significantly greater than 0. + The importance scores for each feature group. + A higher score indicates greater importance of that group. Notes ----- @@ -207,6 +191,8 @@ def importance(self, X, y): when that group is perturbed, compared to the reference loss. A higher importance score indicates that perturbing that group leads to worse model performance, suggesting those features are more important. + When no group has been specified, the importance is computed for each single + feature. """ self._check_fit() self._check_compatibility(X) @@ -347,7 +333,7 @@ class BasePerturbationCV(BaseVariableImportance): ---------- importance_estimators_ : list of BasePerturbation instances List of BasePerturbation instances for each fold. - importances_ : ndarray of shape (n_groups, n_splits) + importances_ : ndarray of shape (n_groups, n_folds) Importance scores for each fold and each group of covariates. pvalues_ : ndarray of shape (n_groups,) P-values for importance scores computed across folds. @@ -502,7 +488,7 @@ def importance(self, X, y): Returns ------- - importances_ : ndarray of shape (n_features, n_groups) + importances_ : ndarray of shape (n_groups, n_folds) The importance scores for each group of features. """ statistical_test = check_statistical_test( diff --git a/src/hidimstat/base_variable_importance.py b/src/hidimstat/base_variable_importance.py index ab5929f19..ab4adcd4e 100644 --- a/src/hidimstat/base_variable_importance.py +++ b/src/hidimstat/base_variable_importance.py @@ -295,7 +295,7 @@ def fdr_selection( Returns ------- - selected : ndarray of int + selected : ndarray of int of shape (n_features,) Integer array indicating the selected features. 1 indicates selected features with positive effects, -1 indicates selected features with negative effects, @@ -361,7 +361,7 @@ def fwer_selection( Returns ------- - selected : ndarray of int + selected : ndarray of int of shape (n_features,) Integer array indicating the selected features. 1 indicates selected features with positive effects, -1 indicates selected features with negative effects, @@ -446,7 +446,7 @@ def plot_importance( ] elif isinstance(feature_names, list): assert np.all(isinstance(name, str) for name in feature_names), ( - "The feature_names should be a list of the string" + "feature_names should be a list of strings" ) else: raise ValueError("feature_names should be a list") diff --git a/src/hidimstat/conditional_feature_importance.py b/src/hidimstat/conditional_feature_importance.py index b558b7853..c50a0dbd4 100644 --- a/src/hidimstat/conditional_feature_importance.py +++ b/src/hidimstat/conditional_feature_importance.py @@ -298,11 +298,11 @@ def cfi_importance( """ Returns ------- - selection : ndarray of shape (n_features,) - Boolean array indicating selected features (True = selected) - importances : ndarray of shape (n_features,) - Feature importance scores/test statistics. - pvalues : ndarray of shape (n_features,) + selection : ndarray of shape (n_groups,) + Boolean array indicating selected feature groups (True = selected) + importances : ndarray of shape (n_groups,) + Feature group importance scores/test statistics. + pvalues : ndarray of shape (n_groups,) P-values for importance scores. """, ) diff --git a/src/hidimstat/desparsified_lasso.py b/src/hidimstat/desparsified_lasso.py index 712126845..46546f267 100644 --- a/src/hidimstat/desparsified_lasso.py +++ b/src/hidimstat/desparsified_lasso.py @@ -90,12 +90,12 @@ class DesparsifiedLasso(BaseVariableImportance): Attributes ---------- - importances_ : ndarray of shape (n_features) - Debiased coefficient estimates. - pvalues_ : ndarray of shape (n_features) - Two-sided p-values. - pvalues_corr_ : ndarray of shape (n_features) - Multiple testing corrected p-values. + importances_ : ndarray of shape (n_features,) + Debiased coefficient estimates for each feature. + pvalues_ : ndarray of shape (n_features,) + Two-sided p-values for each feature. + pvalues_corr_ : ndarray of shape (n_features,) + Multiple testing corrected p-values for each feature. sigma_hat_ : float or ndarray of shape (n_task, n_task) Estimated noise level. precision_diagonal_ : ndarray of shape (n_features) @@ -317,7 +317,8 @@ def __sklearn_is_fitted__(self): ) def _initial_fit(self, estimator, X_, y_): - """Run initial fit of a sklearn estimator. + """ + Run initial fit of a sklearn estimator. Use during fit if an unfitted estimator was passed at instantiation. """ @@ -365,7 +366,7 @@ def importance(self, X=None, y=None): Returns ------- importances_ : ndarray of shape (n_features,) or (n_features, n_task) - Desparsified lasso coefficient estimates. + Desparsified lasso coefficient estimates for each feature. Notes ----- @@ -472,6 +473,14 @@ def fdr_selection( ): """ Overrides the signature to set two_tailed_test=True by default. + + Returns + ------- + selected : ndarray of int of shape (n_features,) + Integer array indicating the selected features. + 1 indicates selected features with positive effects, + -1 indicates selected features with negative effects, + 0 indicates non-selected features. """ return super().fdr_selection( fdr=fdr, diff --git a/src/hidimstat/ensemble_clustered_inference.py b/src/hidimstat/ensemble_clustered_inference.py index 4e3230c20..f28c6d766 100644 --- a/src/hidimstat/ensemble_clustered_inference.py +++ b/src/hidimstat/ensemble_clustered_inference.py @@ -127,6 +127,11 @@ def importance(self, X=None, y=None): Not used, present for API consistency by convention. y : Not used, present for API consistency by convention. + + Returns + ------- + importances_ : ndarray, shape (n_clusters,) or (n_clusters, n_tasks) + Estimated coefficients at cluster level. """ del y del X @@ -156,8 +161,8 @@ def fit_importance(self, X, y): Returns ------- - self : CluDL - Fitted estimator with computed importances. + importances_ : ndarray, shape (n_clusters,) or (n_clusters, n_tasks) + Estimated coefficients at cluster level. """ self.fit(X, y) self.importance(X, y) @@ -415,6 +420,11 @@ def importance(self, X=None, y=None): Not used, present for API consistency by convention. y : Not used, present for API consistency by convention. + + Returns + ------- + importances_ : ndarray, shape (n_features,) or (n_features, n_tasks) + Estimated coefficients at the feature level. """ del y del X @@ -460,7 +470,7 @@ def fit_importance(self, X, y): Returns ------- importances_ : ndarray, shape (n_features,) or (n_features, n_tasks) - Estimated coefficients at feature level. + Estimated coefficients at the feature level. """ self.fit(X, y) self.importance(X, y) diff --git a/src/hidimstat/knockoffs.py b/src/hidimstat/knockoffs.py index 04cdcb51c..ed3cb29d9 100644 --- a/src/hidimstat/knockoffs.py +++ b/src/hidimstat/knockoffs.py @@ -108,7 +108,7 @@ class ModelXKnockoff(BaseVariableImportance): Attributes ---------- importances_ : ndarray, shape (n_repeats, n_features) - Test statistics for each repeat. + Knockoff statistics :math:`W_j` for each original feature across repetitions. pvalues_ : ndarray, shape (n_repeats, n_features) Empirical p-values for each repeat. threshold_fdr_ : float @@ -225,9 +225,8 @@ def importance(self, X=None, y=None): Returns ------- - importances_ : ndarray of shape (n_features,) - Feature importance scores for each feature. - Higher absolute values indicate higher importance. + importances_ : ndarray of shape (n_repeats, n_features) + Knockoff statistics :math:`W_j` for each original feature across repetitions. Notes ----- @@ -269,9 +268,8 @@ def fit_importance(self, X, y): Returns ------- - importances_ : ndarray of shape (n_features,) - Feature importance scores (p-values) for each feature. - Lower values indicate higher importance. Values range from 0 to 1. + importances_ : ndarray of shape (n_repeats, n_features) + Knockoff statistics :math:`W_j` for each original feature across repetitions. Notes ----- @@ -325,7 +323,7 @@ def fdr_selection( Returns ------- - numpy.ndarray + selected: numpy.ndarray of shape (n_features,) Boolean array indicating selected features (True for selected, False for not selected) Raises @@ -449,7 +447,7 @@ def lasso_coefficient_difference_statistic(estimators, n_features): Returns ------- test_statistic : ndarray, shape (n_repeats, n_features) - Knockoff statistics :math:`W_j` for each original feature across repeats. The number + Knockoff statistics :math:`W_j` for each original feature across repetitions. The number of repeats corresponds to the length of the estimators list. """ test_statistic_list = [] @@ -515,12 +513,12 @@ def _empirical_knockoff_pval(test_score): Parameters ---------- - test_score : 1D ndarray, shape (n_features, ) + test_score : 1D ndarray, shape (n_features,) Vector of test statistics. Returns ------- - pvals : 1D ndarray, shape (n_features, ) + pvals : 1D ndarray, shape (n_features,) Vector of empirical p-values. """ pvals = [] @@ -547,7 +545,7 @@ def _empirical_knockoff_eval(test_score, ko_threshold): Parameters ---------- - test_score : 1D ndarray, shape (n_features, ) + test_score : 1D ndarray, shape (n_features,) Vector of test statistics. ko_threshold : float @@ -555,7 +553,7 @@ def _empirical_knockoff_eval(test_score, ko_threshold): Returns ------- - evals : 1D ndarray, shape (n_features, ) + evals : 1D ndarray, shape (n_features,) Vector of empirical e-values. """ evals = [] diff --git a/src/hidimstat/leave_one_covariate_in.py b/src/hidimstat/leave_one_covariate_in.py index 74781dcf8..dc36d73f3 100644 --- a/src/hidimstat/leave_one_covariate_in.py +++ b/src/hidimstat/leave_one_covariate_in.py @@ -280,11 +280,11 @@ def loci_importance( """ Returns ------- - selection : ndarray of shape (n_features,) - Boolean array indicating selected features (True = selected) - importances : ndarray of shape (n_features,) - Feature importance scores/test statistics. - pvalues : ndarray of shape (n_features,) + selection : ndarray of shape (n_groups,) + Boolean array indicating selected feature groups (True = selected). + importances : ndarray of shape (n_groups,) + Feature group importance scores/test statistics. + pvalues : ndarray of shape (n_groups,) P-values computed for the marginal importance. """, ) diff --git a/src/hidimstat/leave_one_covariate_out.py b/src/hidimstat/leave_one_covariate_out.py index 88b7df11a..f2f72a7a4 100644 --- a/src/hidimstat/leave_one_covariate_out.py +++ b/src/hidimstat/leave_one_covariate_out.py @@ -115,15 +115,12 @@ def importance(self, X, y): The input samples to compute importance scores for. y : array-like of shape (n_samples,) + Returns + ------- importances_ : ndarray of shape (n_groups,) The importance scores for each group of covariates. A higher score indicates greater importance of that group. - Returns - ------- - importances_ : ndarray of shape (n_features,) - Importance scores for each feature. - Attributes ---------- loss_reference_ : float @@ -275,12 +272,12 @@ def loco_importance( """ Returns ------- - selection : ndarray of shape (n_features,) - Boolean array indicating selected features (True = selected) - importances : ndarray of shape (n_features,) - Feature importance scores/test statistics. - pvalues : ndarray of shape (n_features,) - None because there is no p-value for this method + selection : ndarray of shape (n_groups,) + Boolean array indicating selected feature groups (True = selected). + importances : ndarray of shape (n_groups,) + Feature group importance scores/test statistics. + pvalues : ndarray of shape (n_groups,) + None because there is no p-value for this method. """, ) diff --git a/src/hidimstat/permutation_feature_importance.py b/src/hidimstat/permutation_feature_importance.py index 5a99880e0..238a486f2 100644 --- a/src/hidimstat/permutation_feature_importance.py +++ b/src/hidimstat/permutation_feature_importance.py @@ -130,11 +130,11 @@ def pfi_importance( """ Returns ------- - selection : ndarray of shape (n_features,) - Boolean array indicating selected features (True = selected) - importances : ndarray of shape (n_features,) - Feature importance scores/test statistics. - pvalues : ndarray of shape (n_features,) + selection : ndarray of shape (n_groups,) + Boolean array indicating selected feature groups (True = selected) + importances : ndarray of shape (n_groups,) + Feature group importance scores/test statistics. + pvalues : ndarray of shape (n_groups,) P-values for importance scores. """, )