Skip to content

Commit

Permalink
feat: when using from table to time series feature must be given (#572)
Browse files Browse the repository at this point in the history
Closes #571 

### Summary of Changes

TimeSeries no longer is subclass of TaggedTable only of Table
<!-- Please provide a summary of changes in this pull request, ensuring
all changes are explained. -->

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
Gerhardsa0 and megalinter-bot committed Mar 18, 2024
1 parent f6a3ca7 commit ca23f0f
Show file tree
Hide file tree
Showing 50 changed files with 486 additions and 515 deletions.
4 changes: 2 additions & 2 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

import sys
import functools
import io
import sys
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any, TypeVar
Expand Down Expand Up @@ -1774,7 +1774,7 @@ def time_columns(self, target_name: str, time_name: str, feature_names: list[str
"""
from ._time_series import TimeSeries

return TimeSeries._from_table_to_time_series(self, target_name, time_name, feature_names)
return TimeSeries._from_table(self, target_name, time_name, feature_names)

def transform_column(self, name: str, transformer: Callable[[Row], Any]) -> Table:
"""
Expand Down
359 changes: 196 additions & 163 deletions src/safeds/data/tabular/containers/_time_series.py

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions tests/helpers/_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def assert_that_time_series_are_equal(table1: TimeSeries, table2: TimeSeries) ->
The timeseries to compare the first timeseries to.
"""
assert table1.schema == table2.schema
assert table1.features == table2.features
assert table1.target == table2.target
assert table1._feature_names == table2._feature_names
assert table1.features == table2._features
assert table1.target == table2._target
assert table1.time == table2.time
assert table1 == table2

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
target_name="target",
time_name="time",
feature_names=["feature_1"],
feature_names=None,
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"target",
"time",
["feature_1"],
None,
),
),
],
Expand Down
100 changes: 100 additions & 0 deletions tests/safeds/data/tabular/containers/_time_series/test_eq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from typing import Any

import pytest
from safeds.data.tabular.containers import Row, Table, TaggedTable, TimeSeries


@pytest.mark.parametrize(
("table1", "table2", "expected"),
[
(
TimeSeries({"a": [], "b": [], "c": []}, "b", "c", ["a"]),
TimeSeries({"a": [], "b": [], "c": []}, "b", "c", ["a"]),
True,
),
(
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]),
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]),
True,
),
(
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [10, 11, 12]}, "b", "d", ["a"]),
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [10, 11, 12]}, "c", "d", ["a"]),
False,
),
(
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [10, 11, 12]}, "b", "c", ["a"]),
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "e": [10, 11, 12]}, "b", "c", ["a"]),
False,
),
(
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]),
TimeSeries({"a": [1, 1, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]),
False,
),
(
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]),
TimeSeries({"a": ["1", "2", "3"], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]),
False,
),
(
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [10, 11, 12]}, "b", "d", ["a"]),
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [10, 11, 12]}, "b", "d", ["c"]),
False,
),
(
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [10, 11, 12]}, "b", "d", ["a"]),
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [10, 11, 12]}, "b", "c", ["a"]),
False,
),
],
ids=[
"rowless table",
"equal tables",
"different target",
"different column names",
"different values",
"different types",
"different features",
"different time",
],
)
def test_should_return_whether_two_tagged_tables_are_equal(
table1: TimeSeries,
table2: TimeSeries,
expected: bool,
) -> None:
assert (table1.__eq__(table2)) == expected


@pytest.mark.parametrize(
"table1",
[TimeSeries({"a": [], "b": [], "c": []}, "b", "c", ["a"])],
ids=[
"any",
],
)
def test_should_return_true_if_objects_are_identical(table1: TimeSeries) -> None:
assert (table1.__eq__(table1)) is True


@pytest.mark.parametrize(
("table", "other"),
[
(TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]), None),
(TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]), Row()),
(TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]), Table()),
(
TimeSeries({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, "b", "c", ["a"]),
TaggedTable({"a": [1, 2, 3], "b": [4, 5, 6]}, "b", ["a"]),
),
],
ids=[
"TimeSeries vs. None",
"TimeSeries vs. Row",
"TimeSeries vs. Table",
"TimeSeries vs. TaggedTable",
],
)
def test_should_return_not_implemented_if_other_is_not_tagged_table(table: TimeSeries, other: Any) -> None:
assert (table.__eq__(other)) is NotImplemented
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
target_name="T",
time_name="time",
feature_names=["A", "B", "C"],
),
Table({"A": [1, 4], "B": [2, 5], "C": [3, 6]}),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,7 @@
"time",
["A", "B", "C"],
ValueError,
r"Column 'A' cannot be both feature and target.",
),
(
Table(
{
"time": [0, 1],
"A": [1, 4],
"B": [2, 5],
"C": [3, 6],
"T": [0, 1],
},
),
"A",
"time",
[],
ValueError,
r"At least one feature column must be specified.",
),
(
Table(
{
"time": [0, 1],
"A": [1, 4],
},
),
"A",
"time",
None,
ValueError,
r"At least one feature column must be specified.",
r"Column 'A' can not be target and feature column.",
),
(
Table(
Expand Down Expand Up @@ -120,8 +91,6 @@
"feature_does_not_exist",
"target_does_not_exist",
"target_and_feature_overlap",
"features_are_empty-explicitly",
"features_are_empty_implicitly",
"time_does_not_exist",
"time_is_also_feature",
],
Expand All @@ -135,7 +104,7 @@ def test_should_raise_error(
error_msg: str,
) -> None:
with pytest.raises(error, match=error_msg):
TimeSeries._from_table_to_time_series(
TimeSeries._from_table(
table,
target_name=target_name,
time_name=time_name,
Expand Down Expand Up @@ -186,7 +155,7 @@ def test_should_raise_error(
),
"T",
"time",
None,
["B"],
),
],
ids=["create_tagged_table", "tagged_table_not_all_columns_are_features", "tagged_table_with_feature_names_as_None"],
Expand All @@ -197,7 +166,7 @@ def test_should_create_a_tagged_table(
time_name: str,
feature_names: list[str] | None,
) -> None:
time_series = TimeSeries._from_table_to_time_series(
time_series = TimeSeries._from_table(
table,
target_name=target_name,
time_name=time_name,
Expand Down

0 comments on commit ca23f0f

Please sign in to comment.