|
1 | 1 | import matplotlib.pyplot as plt |
2 | 2 | import numpy as np |
| 3 | +import pandas as pd |
3 | 4 | import pytest |
4 | 5 | from sklearn.cluster import FeatureAgglomeration |
5 | | -from sklearn.linear_model import LassoCV |
| 6 | +from sklearn.linear_model import LassoCV, LinearRegression |
6 | 7 |
|
7 | | -from hidimstat import CluDL, DesparsifiedLasso |
| 8 | +from hidimstat import CFI, CluDL, DesparsifiedLasso |
8 | 9 | from hidimstat.base_variable_importance import BaseVariableImportance |
9 | 10 | from hidimstat.statistical_tools.multiple_testing import fdp_power |
10 | 11 | from hidimstat.statistical_tools.p_values import two_sided_pval_from_pval |
@@ -527,3 +528,26 @@ def test_clustered_fwer_selection(rng): |
527 | 528 | fwer=0.5, n_tests=n_features, two_tailed_test=True |
528 | 529 | ) |
529 | 530 | assert selection.shape[0] == n_features |
| 531 | + |
| 532 | + |
| 533 | +@pytest.mark.parametrize( |
| 534 | + "n_samples, n_features, support_size, rho, seed, value, signal_noise_ratio, rho_serial", |
| 535 | + [(100, 10, 5, 0.5, 0, 1.0, 64.0, 0.5)], |
| 536 | +) |
| 537 | +def test_feature_groups_order_preserved(data_generator): |
| 538 | + """Regression test to check that the order of the feature groups is |
| 539 | + preserved in the output of .importance() even when the keys are not in |
| 540 | + the order that would be returned by sorted(groups.keys()). |
| 541 | + """ |
| 542 | + X, y, important_features, non_important_features = data_generator |
| 543 | + X_df = pd.DataFrame(X) |
| 544 | + model = LinearRegression() |
| 545 | + model.fit(X_df, y) |
| 546 | + groups = { |
| 547 | + "non_important": non_important_features, |
| 548 | + "important": important_features, |
| 549 | + } |
| 550 | + |
| 551 | + cfi = CFI(estimator=model, features_groups=groups, random_state=0) |
| 552 | + importance = cfi.fit_importance(X_df, y) |
| 553 | + assert importance[0] < importance[1] |
0 commit comments