|
| 1 | + |
1 | 2 | from contextlib import contextmanager |
2 | 3 | import pytest |
3 | | - |
4 | 4 | import pandas as pd |
5 | 5 |
|
6 | 6 | from cobra.model_building.models import LogisticRegressionModel, LinearRegressionModel |
7 | 7 | from cobra.model_building.forward_selection import ForwardFeatureSelection |
8 | 8 |
|
9 | | - |
10 | 9 | @contextmanager |
11 | 10 | def does_not_raise(): |
12 | 11 | yield |
@@ -96,6 +95,18 @@ def mock_evaluate(self, X, y, split): # on AUC scale, but gives the same for RM |
96 | 95 |
|
97 | 96 | pd.testing.assert_frame_equal(actual, expected) |
98 | 97 |
|
| 98 | + @pytest.mark.parametrize("model_type", ["classification", "regression"]) |
| 99 | + def test_ffs_train_data_assertions(self, model_type): |
| 100 | + |
| 101 | + fw_selection = ForwardFeatureSelection(model_type=model_type) |
| 102 | + |
| 103 | + with pytest.raises(AssertionError): # no split column |
| 104 | + fw_selection.fit(pd.DataFrame(), "target", predictors=[""]) |
| 105 | + |
| 106 | + df = mock_data(add_split_col=True, model_type=model_type) |
| 107 | + with pytest.raises(AssertionError): # not at least train & selection sets |
| 108 | + fw_selection.fit(df[df["split"] == "train"], "target", predictors=[""]) |
| 109 | + |
99 | 110 | @pytest.mark.parametrize("model_type, max_predictors, expectation", |
100 | 111 | [("classification", 2, pytest.raises(ValueError)), |
101 | 112 | ("classification", 3, does_not_raise()), |
@@ -137,8 +148,9 @@ def mock_forward_selection(self, train_data, target_column_name, |
137 | 148 | mocker.patch("cobra.model_building.ForwardFeatureSelection._forward_selection", |
138 | 149 | mock_forward_selection) |
139 | 150 |
|
| 151 | + df = mock_data(add_split_col=True, model_type=model_type) |
140 | 152 | with expectation: |
141 | | - fw_selection.fit(pd.DataFrame(), "target", |
| 153 | + fw_selection.fit(df, "target", # data is ignored |
142 | 154 | predictors=predictors_list, |
143 | 155 | forced_predictors=forced_predictors_list, |
144 | 156 | excluded_predictors=[]) |
|
0 commit comments