From fec1f5f6b912a7ba8724586c77b8c439b480f8c9 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 21 Apr 2023 13:16:05 +0200 Subject: [PATCH 1/7] Added Table.transform_table Method which returns the transformed Table --- src/safeds/data/tabular/containers/_table.py | 27 ++++++++++++- .../containers/_table/test_transform_table.py | 38 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/safeds/data/tabular/containers/_table/test_transform_table.py diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index 13d212016..7fe441762 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -31,11 +31,11 @@ from ._column import Column from ._row import Row + if TYPE_CHECKING: from collections.abc import Callable, Iterable - from ._tagged_table import TaggedTable - + from ..transformation import TableTransformer # noinspection PyProtectedMember class Table: @@ -991,6 +991,29 @@ def transform_column(self, name: str, transformer: Callable[[Row], Any]) -> Tabl return self.replace_column(name, result) raise UnknownColumnNameError([name]) + + def transform_table(self, transformer: TableTransformer) -> Table: + """ + Apply a learned transformation onto this table. + + Parameters + ---------- + transformer : TableTransformer + The transformer which transforms the given table + + Returns + ------- + transformed_table : Table + The transformed table. + + Raises + ------ + TransformerNotFittedError + If the transformer has not been fitted yet. + """ + return transformer.transform(self) + + # ------------------------------------------------------------------------------------------------------------------ # Plotting # ------------------------------------------------------------------------------------------------------------------ diff --git a/tests/safeds/data/tabular/containers/_table/test_transform_table.py b/tests/safeds/data/tabular/containers/_table/test_transform_table.py new file mode 100644 index 000000000..a4cf3a678 --- /dev/null +++ b/tests/safeds/data/tabular/containers/_table/test_transform_table.py @@ -0,0 +1,38 @@ +import pytest + +from safeds.data.tabular.containers import Table +from safeds.data.tabular.exceptions import UnknownColumnNameError, TransformerNotFittedError +from safeds.data.tabular.transformation import OneHotEncoder + + +class TestTransform: + def test_should_raise_if_column_not_found(self) -> None: + table_to_fit = Table.from_dict( + { + "col1": ["a", "b", "c"], + }, + ) + + transformer = OneHotEncoder().fit(table_to_fit, None) + + table_to_transform = Table.from_dict( + { + "col2": ["a", "b", "c"], + }, + ) + + with pytest.raises(UnknownColumnNameError): + table_to_transform.transform_table(transformer) + + def test_should_raise_if_not_fitted(self) -> None: + table = Table.from_dict( + { + "col1": ["a", "b", "c"], + }, + ) + + transformer = OneHotEncoder() + + with pytest.raises(TransformerNotFittedError): + table.transform_table(transformer) + From cb4987dabc395a761ae28b50b58d204b6b83ccfc Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Fri, 21 Apr 2023 11:22:40 +0000 Subject: [PATCH 2/7] style: apply automated linter fixes --- src/safeds/data/tabular/containers/_table.py | 36 +++++++++---------- .../containers/_table/test_transform_table.py | 4 +-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index 7fe441762..fa65d0d6a 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -31,11 +31,13 @@ from ._column import Column from ._row import Row - if TYPE_CHECKING: from collections.abc import Callable, Iterable + + from safeds.data.tabular.transformation import TableTransformer + from ._tagged_table import TaggedTable - from ..transformation import TableTransformer + # noinspection PyProtectedMember class Table: @@ -991,29 +993,27 @@ def transform_column(self, name: str, transformer: Callable[[Row], Any]) -> Tabl return self.replace_column(name, result) raise UnknownColumnNameError([name]) - def transform_table(self, transformer: TableTransformer) -> Table: """ - Apply a learned transformation onto this table. + Apply a learned transformation onto this table. - Parameters - ---------- - transformer : TableTransformer - The transformer which transforms the given table + Parameters + ---------- + transformer : TableTransformer + The transformer which transforms the given table - Returns - ------- - transformed_table : Table - The transformed table. + Returns + ------- + transformed_table : Table + The transformed table. - Raises - ------ - TransformerNotFittedError - If the transformer has not been fitted yet. - """ + Raises + ------ + TransformerNotFittedError + If the transformer has not been fitted yet. + """ return transformer.transform(self) - # ------------------------------------------------------------------------------------------------------------------ # Plotting # ------------------------------------------------------------------------------------------------------------------ diff --git a/tests/safeds/data/tabular/containers/_table/test_transform_table.py b/tests/safeds/data/tabular/containers/_table/test_transform_table.py index a4cf3a678..5e9af92f9 100644 --- a/tests/safeds/data/tabular/containers/_table/test_transform_table.py +++ b/tests/safeds/data/tabular/containers/_table/test_transform_table.py @@ -1,7 +1,6 @@ import pytest - from safeds.data.tabular.containers import Table -from safeds.data.tabular.exceptions import UnknownColumnNameError, TransformerNotFittedError +from safeds.data.tabular.exceptions import TransformerNotFittedError, UnknownColumnNameError from safeds.data.tabular.transformation import OneHotEncoder @@ -35,4 +34,3 @@ def test_should_raise_if_not_fitted(self) -> None: with pytest.raises(TransformerNotFittedError): table.transform_table(transformer) - From ae26b44d7e35b5a124f3b7c2ee7d69b296f1eae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Gr=C3=A9us?= Date: Fri, 21 Apr 2023 15:57:13 +0200 Subject: [PATCH 3/7] docs: Added example for `Table.transform_table()` test: Added tests to test if 'Table.transform_table()' runs correctly Co-authored-by: sibre28 <86068340+sibre28@users.noreply.github.com> --- src/safeds/data/tabular/containers/_table.py | 13 +++ .../containers/_table/test_transform_table.py | 87 ++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index fa65d0d6a..f80803126 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -1011,6 +1011,19 @@ def transform_table(self, transformer: TableTransformer) -> Table: ------ TransformerNotFittedError If the transformer has not been fitted yet. + + Examples + -------- + >>> from safeds.data.tabular.transformation import OneHotEncoder + >>> from safeds.data.tabular.containers import Table + >>> transformer = OneHotEncoder() + >>> table = Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}) + >>> transformer = transformer.fit(table, None) + >>> table.transform_table(transformer) + col1_1 col1_2 col2_1 col2_2 col2_4 + 0 1.0 0.0 1.0 0.0 0.0 + 1 0.0 1.0 0.0 1.0 0.0 + 2 1.0 0.0 0.0 0.0 1.0 """ return transformer.transform(self) diff --git a/tests/safeds/data/tabular/containers/_table/test_transform_table.py b/tests/safeds/data/tabular/containers/_table/test_transform_table.py index 5e9af92f9..adbab7c29 100644 --- a/tests/safeds/data/tabular/containers/_table/test_transform_table.py +++ b/tests/safeds/data/tabular/containers/_table/test_transform_table.py @@ -1,6 +1,7 @@ import pytest from safeds.data.tabular.containers import Table -from safeds.data.tabular.exceptions import TransformerNotFittedError, UnknownColumnNameError +from safeds.data.tabular.exceptions import TransformerNotFittedError, \ + UnknownColumnNameError from safeds.data.tabular.transformation import OneHotEncoder @@ -34,3 +35,87 @@ def test_should_raise_if_not_fitted(self) -> None: with pytest.raises(TransformerNotFittedError): table.transform_table(transformer) + + @pytest.mark.parametrize( + ("table", "column_names", "expected"), + [ + ( + Table.from_dict( + { + "col1": ["a", "b", "b", "c"], + }, + ), + None, + Table.from_dict( + { + "col1_a": [1.0, 0.0, 0.0, 0.0], + "col1_b": [0.0, 1.0, 1.0, 0.0], + "col1_c": [0.0, 0.0, 0.0, 1.0], + }, + ), + ), + ( + Table.from_dict( + { + "col1": ["a", "b", "b", "c"], + "col2": ["a", "b", "b", "c"], + }, + ), + ["col1"], + Table.from_dict( + { + "col1_a": [1.0, 0.0, 0.0, 0.0], + "col1_b": [0.0, 1.0, 1.0, 0.0], + "col1_c": [0.0, 0.0, 0.0, 1.0], + "col2": ["a", "b", "b", "c"], + }, + ), + ), + ( + Table.from_dict( + { + "col1": ["a", "b", "b", "c"], + "col2": ["a", "b", "b", "c"], + }, + ), + ["col1", "col2"], + Table.from_dict( + { + "col1_a": [1.0, 0.0, 0.0, 0.0], + "col1_b": [0.0, 1.0, 1.0, 0.0], + "col1_c": [0.0, 0.0, 0.0, 1.0], + "col2_a": [1.0, 0.0, 0.0, 0.0], + "col2_b": [0.0, 1.0, 1.0, 0.0], + "col2_c": [0.0, 0.0, 0.0, 1.0], + }, + ), + ), + ], + ids=["all columns", "one column", "multiple columns"], + ) + def test_should_return_transformed_table( + self, + table: Table, + column_names: list[str] | None, + expected: Table, + ) -> None: + transformer = OneHotEncoder().fit(table, column_names) + assert table.transform_table(transformer) == expected + + def test_should_not_change_original_table(self) -> None: + table = Table.from_dict( + { + "col1": ["a", "b", "c"], + }, + ) + + transformer = OneHotEncoder().fit(table, None) + table.transform_table(transformer) + + expected = Table.from_dict( + { + "col1": ["a", "b", "c"], + }, + ) + + assert table == expected From 29f2af2ab95d0fe6331c8be27ebbca4877ce9c19 Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Fri, 21 Apr 2023 13:58:50 +0000 Subject: [PATCH 4/7] style: apply automated linter fixes --- .../data/tabular/containers/_table/test_transform_table.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/safeds/data/tabular/containers/_table/test_transform_table.py b/tests/safeds/data/tabular/containers/_table/test_transform_table.py index adbab7c29..200740047 100644 --- a/tests/safeds/data/tabular/containers/_table/test_transform_table.py +++ b/tests/safeds/data/tabular/containers/_table/test_transform_table.py @@ -1,7 +1,6 @@ import pytest from safeds.data.tabular.containers import Table -from safeds.data.tabular.exceptions import TransformerNotFittedError, \ - UnknownColumnNameError +from safeds.data.tabular.exceptions import TransformerNotFittedError, UnknownColumnNameError from safeds.data.tabular.transformation import OneHotEncoder From 6ced056df1331916b497d9bf45fc50116dbec186 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Fri, 21 Apr 2023 19:58:32 +0200 Subject: [PATCH 5/7] test: order tests like source --- .../_table/test_inverse_transform_table.py | 2 +- .../containers/_table/test_transform_table.py | 60 +++++++++---------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/tests/safeds/data/tabular/containers/_table/test_inverse_transform_table.py b/tests/safeds/data/tabular/containers/_table/test_inverse_transform_table.py index 84ee05527..8d49a0474 100644 --- a/tests/safeds/data/tabular/containers/_table/test_inverse_transform_table.py +++ b/tests/safeds/data/tabular/containers/_table/test_inverse_transform_table.py @@ -4,7 +4,7 @@ from safeds.data.tabular.transformation import OneHotEncoder -class TestInverseTransformTableOnOneHotEncoder: +class TestInverseTransformTable: @pytest.mark.parametrize( ("table_to_fit", "column_names", "table_to_transform"), [ diff --git a/tests/safeds/data/tabular/containers/_table/test_transform_table.py b/tests/safeds/data/tabular/containers/_table/test_transform_table.py index 200740047..59ffe90d3 100644 --- a/tests/safeds/data/tabular/containers/_table/test_transform_table.py +++ b/tests/safeds/data/tabular/containers/_table/test_transform_table.py @@ -5,36 +5,6 @@ class TestTransform: - def test_should_raise_if_column_not_found(self) -> None: - table_to_fit = Table.from_dict( - { - "col1": ["a", "b", "c"], - }, - ) - - transformer = OneHotEncoder().fit(table_to_fit, None) - - table_to_transform = Table.from_dict( - { - "col2": ["a", "b", "c"], - }, - ) - - with pytest.raises(UnknownColumnNameError): - table_to_transform.transform_table(transformer) - - def test_should_raise_if_not_fitted(self) -> None: - table = Table.from_dict( - { - "col1": ["a", "b", "c"], - }, - ) - - transformer = OneHotEncoder() - - with pytest.raises(TransformerNotFittedError): - table.transform_table(transformer) - @pytest.mark.parametrize( ("table", "column_names", "expected"), [ @@ -118,3 +88,33 @@ def test_should_not_change_original_table(self) -> None: ) assert table == expected + + def test_should_raise_if_column_not_found(self) -> None: + table_to_fit = Table.from_dict( + { + "col1": ["a", "b", "c"], + }, + ) + + transformer = OneHotEncoder().fit(table_to_fit, None) + + table_to_transform = Table.from_dict( + { + "col2": ["a", "b", "c"], + }, + ) + + with pytest.raises(UnknownColumnNameError): + table_to_transform.transform_table(transformer) + + def test_should_raise_if_not_fitted(self) -> None: + table = Table.from_dict( + { + "col1": ["a", "b", "c"], + }, + ) + + transformer = OneHotEncoder() + + with pytest.raises(TransformerNotFittedError): + table.transform_table(transformer) From 093d914b71d2442885d2585f043fd72ee8a93903 Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Fri, 21 Apr 2023 18:01:27 +0000 Subject: [PATCH 6/7] style: apply automated linter fixes --- src/safeds/data/tabular/containers/_table.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index bad7793c3..04715109d 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -34,8 +34,7 @@ if TYPE_CHECKING: from collections.abc import Callable, Iterable - from safeds.data.tabular.transformation import TableTransformer - from safeds.data.tabular.transformation import InvertibleTableTransformer + from safeds.data.tabular.transformation import InvertibleTableTransformer, TableTransformer from ._tagged_table import TaggedTable From ae16e2d4ee0772304f771942e3cf17e13a5547ae Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Fri, 21 Apr 2023 20:03:41 +0200 Subject: [PATCH 7/7] docs: add missing dots --- src/safeds/data/tabular/containers/_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index 04715109d..cf3a4693a 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -1000,7 +1000,7 @@ def transform_table(self, transformer: TableTransformer) -> Table: Parameters ---------- transformer : TableTransformer - The transformer which transforms the given table + The transformer which transforms the given table. Returns ------- @@ -1039,7 +1039,7 @@ def inverse_transform_table(self, transformer: InvertibleTableTransformer) -> Ta Returns ------- table : Table - The original table + The original table. Raises ------