Skip to content

Commit

Permalink
test: output of predict should include entire input
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Mar 29, 2023
1 parent 9e7b7fe commit 6c0de34
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 118 deletions.
6 changes: 6 additions & 0 deletions tests/safeds/ml/classification/test_ada_boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
prediction = fitted_classifier.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_include_complete_prediction_input(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_classifier = classifier.fit(valid_data)
prediction = fitted_classifier.predict(valid_data.features)
Expand Down
6 changes: 6 additions & 0 deletions tests/safeds/ml/classification/test_decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
prediction = fitted_classifier.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_include_complete_prediction_input(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_classifier = classifier.fit(valid_data)
prediction = fitted_classifier.predict(valid_data.features)
Expand Down
6 changes: 6 additions & 0 deletions tests/safeds/ml/classification/test_gradient_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
prediction = fitted_classifier.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_include_complete_prediction_input(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_classifier = classifier.fit(valid_data)
prediction = fitted_classifier.predict(valid_data.features)
Expand Down
7 changes: 6 additions & 1 deletion tests/safeds/ml/classification/test_k_nearest_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from safeds.data.tabular.containers import Table, TaggedTable, Column
from safeds.exceptions import LearningError, PredictionError
from safeds.ml.classification import Classifier, KNearestNeighbors
from tests.helpers import resolve_resource_path


@pytest.fixture()
Expand Down Expand Up @@ -51,6 +50,12 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
prediction = fitted_classifier.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_include_complete_prediction_input(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_classifier = classifier.fit(valid_data)
prediction = fitted_classifier.predict(valid_data.features)
Expand Down
6 changes: 6 additions & 0 deletions tests/safeds/ml/classification/test_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
prediction = fitted_classifier.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_include_complete_prediction_input(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_classifier = classifier.fit(valid_data)
prediction = fitted_classifier.predict(valid_data.features)
Expand Down
6 changes: 6 additions & 0 deletions tests/safeds/ml/classification/test_random_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
prediction = fitted_classifier.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_include_complete_prediction_input(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier: Classifier, valid_data: TaggedTable) -> None:
fitted_classifier = classifier.fit(valid_data)
prediction = fitted_classifier.predict(valid_data.features)
Expand Down
38 changes: 18 additions & 20 deletions tests/safeds/ml/regression/test_ada_boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,39 @@ def invalid_data() -> TaggedTable:


class TestFit:
def test_should_succeed_on_valid_data(self, regressor: Regressor, valid_data: TaggedTable) -> None:
regressor.fit(valid_data)
def test_should_succeed_on_valid_data(self, classifier, valid_data: TaggedTable) -> None:
classifier.fit(valid_data)
assert True # This asserts that the fit method succeeds

def test_should_raise_on_invalid_data(self, regressor: Regressor, invalid_data: TaggedTable) -> None:
def test_should_raise_on_invalid_data(self, classifier, invalid_data: TaggedTable) -> None:
with pytest.raises(LearningError):
regressor.fit(invalid_data)
classifier.fit(invalid_data)


class TestPredict:
def test_should_include_features_of_prediction_input(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_features_of_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_set_correct_target_name(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_complete_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.target.name == "target"

def test_should_include_other_columns_of_prediction_input(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
other_column_names = list(
set(valid_data.get_column_names()) - set(valid_data.features.get_column_names()) - {valid_data.target.name}
)
assert prediction.keep_only_columns(other_column_names) == valid_data.keep_only_columns(other_column_names)

def test_should_raise_when_not_fitted(self, regressor: Regressor, valid_data: TaggedTable) -> None:
def test_should_raise_when_not_fitted(self, classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
regressor.predict(valid_data.features)
classifier.predict(valid_data.features)

def test_should_raise_on_invalid_data(
self, regressor: Regressor, valid_data: TaggedTable, invalid_data: TaggedTable
self, classifier, valid_data: TaggedTable, invalid_data: TaggedTable
) -> None:
fitted_regressor = regressor.fit(valid_data)
fitted_regressor = classifier.fit(valid_data)
with pytest.raises(PredictionError):
fitted_regressor.predict(invalid_data.features)
30 changes: 18 additions & 12 deletions tests/safeds/ml/regression/test_decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,39 @@ def invalid_data() -> TaggedTable:


class TestFit:
def test_should_succeed_on_valid_data(self, regressor: Regressor, valid_data: TaggedTable) -> None:
regressor.fit(valid_data)
def test_should_succeed_on_valid_data(self, classifier, valid_data: TaggedTable) -> None:
classifier.fit(valid_data)
assert True # This asserts that the fit method succeeds

def test_should_raise_on_invalid_data(self, regressor: Regressor, invalid_data: TaggedTable) -> None:
def test_should_raise_on_invalid_data(self, classifier, invalid_data: TaggedTable) -> None:
with pytest.raises(LearningError):
regressor.fit(invalid_data)
classifier.fit(invalid_data)


class TestPredict:
def test_should_include_features_of_prediction_input(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_features_of_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_set_correct_target_name(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_complete_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, regressor: Regressor, valid_data: TaggedTable) -> None:
def test_should_raise_when_not_fitted(self, classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
regressor.predict(valid_data.features)
classifier.predict(valid_data.features)

def test_should_raise_on_invalid_data(
self, regressor: Regressor, valid_data: TaggedTable, invalid_data: TaggedTable
self, classifier, valid_data: TaggedTable, invalid_data: TaggedTable
) -> None:
fitted_regressor = regressor.fit(valid_data)
fitted_regressor = classifier.fit(valid_data)
with pytest.raises(PredictionError):
fitted_regressor.predict(invalid_data.features)
30 changes: 18 additions & 12 deletions tests/safeds/ml/regression/test_elastic_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,39 @@ def invalid_data() -> TaggedTable:


class TestFit:
def test_should_succeed_on_valid_data(self, regressor: Regressor, valid_data: TaggedTable) -> None:
regressor.fit(valid_data)
def test_should_succeed_on_valid_data(self, classifier, valid_data: TaggedTable) -> None:
classifier.fit(valid_data)
assert True # This asserts that the fit method succeeds

def test_should_raise_on_invalid_data(self, regressor: Regressor, invalid_data: TaggedTable) -> None:
def test_should_raise_on_invalid_data(self, classifier, invalid_data: TaggedTable) -> None:
with pytest.raises(LearningError):
regressor.fit(invalid_data)
classifier.fit(invalid_data)


class TestPredict:
def test_should_include_features_of_prediction_input(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_features_of_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_set_correct_target_name(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_complete_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, regressor: Regressor, valid_data: TaggedTable) -> None:
def test_should_raise_when_not_fitted(self, classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
regressor.predict(valid_data.features)
classifier.predict(valid_data.features)

def test_should_raise_on_invalid_data(
self, regressor: Regressor, valid_data: TaggedTable, invalid_data: TaggedTable
self, classifier, valid_data: TaggedTable, invalid_data: TaggedTable
) -> None:
fitted_regressor = regressor.fit(valid_data)
fitted_regressor = classifier.fit(valid_data)
with pytest.raises(PredictionError):
fitted_regressor.predict(invalid_data.features)
32 changes: 19 additions & 13 deletions tests/safeds/ml/regression/test_gradient_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@pytest.fixture()
def regressor() -> Regressor:
def classifier() -> Regressor:
return GradientBoosting()


Expand Down Expand Up @@ -35,33 +35,39 @@ def invalid_data() -> TaggedTable:


class TestFit:
def test_should_succeed_on_valid_data(self, regressor: Regressor, valid_data: TaggedTable) -> None:
regressor.fit(valid_data)
def test_should_succeed_on_valid_data(self, classifier, valid_data: TaggedTable) -> None:
classifier.fit(valid_data)
assert True # This asserts that the fit method succeeds

def test_should_raise_on_invalid_data(self, regressor: Regressor, invalid_data: TaggedTable) -> None:
def test_should_raise_on_invalid_data(self, classifier, invalid_data: TaggedTable) -> None:
with pytest.raises(LearningError):
regressor.fit(invalid_data)
classifier.fit(invalid_data)


class TestPredict:
def test_should_include_features_of_prediction_input(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_features_of_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_set_correct_target_name(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_complete_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, regressor: Regressor, valid_data: TaggedTable) -> None:
def test_should_raise_when_not_fitted(self, classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
regressor.predict(valid_data.features)
classifier.predict(valid_data.features)

def test_should_raise_on_invalid_data(
self, regressor: Regressor, valid_data: TaggedTable, invalid_data: TaggedTable
self, classifier, valid_data: TaggedTable, invalid_data: TaggedTable
) -> None:
fitted_regressor = regressor.fit(valid_data)
fitted_regressor = classifier.fit(valid_data)
with pytest.raises(PredictionError):
fitted_regressor.predict(invalid_data.features)
30 changes: 18 additions & 12 deletions tests/safeds/ml/regression/test_k_nearest_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,39 @@ def invalid_data() -> TaggedTable:


class TestFit:
def test_should_succeed_on_valid_data(self, regressor: Regressor, valid_data: TaggedTable) -> None:
regressor.fit(valid_data)
def test_should_succeed_on_valid_data(self, classifier, valid_data: TaggedTable) -> None:
classifier.fit(valid_data)
assert True # This asserts that the fit method succeeds

def test_should_raise_on_invalid_data(self, regressor: Regressor, invalid_data: TaggedTable) -> None:
def test_should_raise_on_invalid_data(self, classifier, invalid_data: TaggedTable) -> None:
with pytest.raises(LearningError):
regressor.fit(invalid_data)
classifier.fit(invalid_data)


class TestPredict:
def test_should_include_features_of_prediction_input(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_features_of_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.features == valid_data.features

def test_should_set_correct_target_name(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
def test_should_include_complete_prediction_input(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.drop_columns(["target"]))
target_name = prediction.target.name
assert prediction.drop_columns([target_name]) == valid_data.drop_columns([target_name])

def test_should_set_correct_target_name(self, classifier, valid_data: TaggedTable) -> None:
fitted_regressor = classifier.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, regressor: Regressor, valid_data: TaggedTable) -> None:
def test_should_raise_when_not_fitted(self, classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
regressor.predict(valid_data.features)
classifier.predict(valid_data.features)

def test_should_raise_on_invalid_data(
self, regressor: Regressor, valid_data: TaggedTable, invalid_data: TaggedTable
self, classifier, valid_data: TaggedTable, invalid_data: TaggedTable
) -> None:
fitted_regressor = regressor.fit(valid_data)
fitted_regressor = classifier.fit(valid_data)
with pytest.raises(PredictionError):
fitted_regressor.predict(invalid_data.features)
Loading

0 comments on commit 6c0de34

Please sign in to comment.