Skip to content

Commit fca06da

Browse files
author
sborms
committed
unit testing ffs assertions
1 parent 3f06750 commit fca06da

10 files changed

Lines changed: 125 additions & 116 deletions

cobra/model_building/forward_selection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ def fit(self, train_data: pd.DataFrame, target_column_name: str,
158158
In case the number of forced predictors is larger than the maximum
159159
number of allowed predictors in the model.
160160
"""
161-
assert all(s in ["train", "selection"] for s in train_data["split"].unique()), \
161+
162+
assert "split" in train_data.columns, "The train_data input df does not include a split column."
163+
print(train_data["split"].unique())
164+
assert len(set(["train", "selection"]).difference(set(train_data["split"].unique()))) == 0, \
162165
"The train_data input df does not include a 'train' and 'selection' split."
163166

164167
# remove excluded predictors from predictor lists

tests/evaluation/test_evaluation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
12
import pytest
23
import pandas as pd
34
import numpy as np
5+
46
from cobra.evaluation import plot_incidence
57
from cobra.evaluation import ClassificationEvaluator, RegressionEvaluator
68

7-
89
def mock_data():
910
d = {'variable': ['education', 'education', 'education', 'education'],
1011
'label': ['1st-4th', '5th-6th', '7th-8th', '9th'],

tests/model_building/test_forward_selection.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
12
from contextlib import contextmanager
23
import pytest
3-
44
import pandas as pd
55

66
from cobra.model_building.models import LogisticRegressionModel, LinearRegressionModel
77
from cobra.model_building.forward_selection import ForwardFeatureSelection
88

9-
109
@contextmanager
1110
def does_not_raise():
1211
yield
@@ -96,6 +95,18 @@ def mock_evaluate(self, X, y, split): # on AUC scale, but gives the same for RM
9695

9796
pd.testing.assert_frame_equal(actual, expected)
9897

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+
99110
@pytest.mark.parametrize("model_type, max_predictors, expectation",
100111
[("classification", 2, pytest.raises(ValueError)),
101112
("classification", 3, does_not_raise()),
@@ -137,8 +148,9 @@ def mock_forward_selection(self, train_data, target_column_name,
137148
mocker.patch("cobra.model_building.ForwardFeatureSelection._forward_selection",
138149
mock_forward_selection)
139150

151+
df = mock_data(add_split_col=True, model_type=model_type)
140152
with expectation:
141-
fw_selection.fit(pd.DataFrame(), "target",
153+
fw_selection.fit(df, "target", # data is ignored
142154
predictors=predictors_list,
143155
forced_predictors=forced_predictors_list,
144156
excluded_predictors=[])

tests/model_building/test_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
12
import numpy as np
23
import pandas as pd
34

45
from cobra.model_building.models import LogisticRegressionModel, LinearRegressionModel
56

6-
77
def mock_data():
88
return pd.DataFrame({"var1_enc": [0.42] * 10,
99
"var2_enc": [0.94] * 10,

tests/model_building/test_univariate_selection.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import pytest
21

32
import pandas as pd
43

54
from cobra.model_building import univariate_selection
65

7-
86
def mock_data():
97
return pd.DataFrame({"var1_enc": [0.42] * 10,
108
"var2_enc": [0.94] * 10,

tests/preprocessing/test_categorical_data_processor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import pytest
21

2+
import pytest
33
import numpy as np
44
import pandas as pd
55

66
from cobra.preprocessing import CategoricalDataProcessor
77

8-
98
class TestCategoricalDataProcessor:
109

1110
def test_attributes_to_dict(self):

tests/preprocessing/test_kbins_discretizer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
12
from contextlib import contextmanager
23
import pytest
3-
44
import numpy as np
55
import pandas as pd
6-
import math
76

87
from cobra.preprocessing.kbins_discretizer import KBinsDiscretizer
98

10-
119
@contextmanager
1210
def does_not_raise():
1311
yield

tests/preprocessing/test_preprocessor.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
from contextlib import contextmanager
2-
import pytest
31

2+
from contextlib import contextmanager
43
from typing import Any
5-
4+
import pytest
65
import numpy as np
76
import pandas as pd
87

98
from cobra.preprocessing.preprocessor import PreProcessor
109

11-
1210
@contextmanager
1311
def does_not_raise():
1412
yield

tests/preprocessing/test_target_encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
12
import pytest
23
import pandas as pd
34
from sklearn.exceptions import NotFittedError
45

56
from cobra.preprocessing.target_encoder import TargetEncoder
67

7-
88
class TestTargetEncoder:
99

1010
def test_target_encoder_constructor_weight_value_error(self):

0 commit comments

Comments
 (0)