Skip to content

Commit

Permalink
test: integrate data into test files
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Mar 29, 2023
1 parent c2edf88 commit 9e7b7fe
Show file tree
Hide file tree
Showing 35 changed files with 249 additions and 151 deletions.
3 changes: 0 additions & 3 deletions tests/resources/test_decision_tree.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_decision_tree_invalid.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_elastic_net_regression.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_elastic_net_regression_invalid.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_gradient_boosting_classification.csv

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_gradient_boosting_regression.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_gradient_boosting_regression_invalid.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_k_nearest_neighbors.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_k_nearest_neighbors_invalid.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_lasso_regression.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_lasso_regression_invalid.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_linear_regression.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_linear_regression_invalid.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_logistic_regression.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_logistic_regression_invalid.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_random_forest.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_random_forest_invalid.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_ridge_regression.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/test_ridge_regression_invalid.csv

This file was deleted.

1 change: 1 addition & 0 deletions tests/safeds/ml/classification/test_classifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import pandas as pd

from safeds.data.tabular.containers import Column, Table, TaggedTable
from safeds.ml.classification import Classifier

Expand Down
26 changes: 19 additions & 7 deletions tests/safeds/ml/classification/test_decision_tree.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from safeds.data.tabular.containers import Table, TaggedTable

from safeds.data.tabular.containers import Table, TaggedTable, Column
from safeds.exceptions import LearningError, PredictionError
from safeds.ml.classification import Classifier, DecisionTree
from tests.helpers import resolve_resource_path


@pytest.fixture()
Expand All @@ -12,14 +12,26 @@ def classifier() -> Classifier:

@pytest.fixture()
def valid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_decision_tree.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", [2, 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


@pytest.fixture()
def invalid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_decision_tree_invalid.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", ["a", 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


class TestFit:
Expand All @@ -41,7 +53,7 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
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)
assert prediction.target.name == "T"
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, classifier: Classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
Expand Down
26 changes: 19 additions & 7 deletions tests/safeds/ml/classification/test_gradient_boosting.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from safeds.data.tabular.containers import Table, TaggedTable

from safeds.data.tabular.containers import Table, TaggedTable, Column
from safeds.exceptions import LearningError, PredictionError
from safeds.ml.classification import Classifier, GradientBoosting
from tests.helpers import resolve_resource_path


@pytest.fixture()
Expand All @@ -12,14 +12,26 @@ def classifier() -> Classifier:

@pytest.fixture()
def valid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_gradient_boosting_classification.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", [2, 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


@pytest.fixture()
def invalid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_gradient_boosting_classification_invalid.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", ["a", 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


class TestFit:
Expand All @@ -41,7 +53,7 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
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)
assert prediction.target.name == "T"
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, classifier: Classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
Expand Down
25 changes: 19 additions & 6 deletions tests/safeds/ml/classification/test_k_nearest_neighbors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from safeds.data.tabular.containers import Table, TaggedTable

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
Expand All @@ -12,14 +13,26 @@ def classifier() -> Classifier:

@pytest.fixture()
def valid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_k_nearest_neighbors.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", [2, 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


@pytest.fixture()
def invalid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_k_nearest_neighbors_invalid.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", ["a", 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


class TestFit:
Expand All @@ -41,7 +54,7 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
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)
assert prediction.target.name == "T"
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, classifier: Classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
Expand Down
26 changes: 19 additions & 7 deletions tests/safeds/ml/classification/test_logistic_regression.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from safeds.data.tabular.containers import Table, TaggedTable

from safeds.data.tabular.containers import Table, TaggedTable, Column
from safeds.exceptions import LearningError, PredictionError
from safeds.ml.classification import Classifier, LogisticRegression
from tests.helpers import resolve_resource_path


@pytest.fixture()
Expand All @@ -12,14 +12,26 @@ def classifier() -> Classifier:

@pytest.fixture()
def valid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_logistic_regression.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", [2, 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


@pytest.fixture()
def invalid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_logistic_regression_invalid.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", ["a", 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


class TestFit:
Expand All @@ -41,7 +53,7 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
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)
assert prediction.target.name == "T"
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, classifier: Classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
Expand Down
26 changes: 19 additions & 7 deletions tests/safeds/ml/classification/test_random_forest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from safeds.data.tabular.containers import Table, TaggedTable

from safeds.data.tabular.containers import Table, TaggedTable, Column
from safeds.exceptions import LearningError, PredictionError
from safeds.ml.classification import Classifier, RandomForest
from tests.helpers import resolve_resource_path


@pytest.fixture()
Expand All @@ -12,14 +12,26 @@ def classifier() -> Classifier:

@pytest.fixture()
def valid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_random_forest.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", [2, 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


@pytest.fixture()
def invalid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_random_forest_invalid.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", ["a", 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


class TestFit:
Expand All @@ -41,7 +53,7 @@ def test_should_include_features_of_prediction_input(self, classifier: Classifie
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)
assert prediction.target.name == "T"
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, classifier: Classifier, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
Expand Down
26 changes: 19 additions & 7 deletions tests/safeds/ml/regression/test_decision_tree.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from safeds.data.tabular.containers import Table, TaggedTable

from safeds.data.tabular.containers import Table, TaggedTable, Column
from safeds.exceptions import LearningError, PredictionError
from safeds.ml.regression import DecisionTree, Regressor
from tests.helpers import resolve_resource_path


@pytest.fixture()
Expand All @@ -12,14 +12,26 @@ def regressor() -> Regressor:

@pytest.fixture()
def valid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_decision_tree.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", [2, 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


@pytest.fixture()
def invalid_data() -> TaggedTable:
table = Table.from_csv_file(resolve_resource_path("test_decision_tree_invalid.csv"))
return table.tag_columns(target_name="T")
return Table.from_columns(
[
Column("id", [1, 4]),
Column("feat1", ["a", 5]),
Column("feat2", [3, 6]),
Column("target", [0, 1]),
]
).tag_columns(target_name="target", feature_names=["feat1", "feat2"])


class TestFit:
Expand All @@ -41,7 +53,7 @@ def test_should_include_features_of_prediction_input(self, regressor: Regressor,
def test_should_set_correct_target_name(self, regressor: Regressor, valid_data: TaggedTable) -> None:
fitted_regressor = regressor.fit(valid_data)
prediction = fitted_regressor.predict(valid_data.features)
assert prediction.target.name == "T"
assert prediction.target.name == "target"

def test_should_raise_when_not_fitted(self, regressor: Regressor, valid_data: TaggedTable) -> None:
with pytest.raises(PredictionError):
Expand Down
Loading

0 comments on commit 9e7b7fe

Please sign in to comment.