From 2856296fb7228e8d4adebceb86e22ecaeb609ad9 Mon Sep 17 00:00:00 2001 From: Alex Senger <91055000+alex-senger@users.noreply.github.com> Date: Fri, 21 Apr 2023 12:21:05 +0200 Subject: [PATCH] feat: Set `column_names` in `fit` methods of table transformers to be required (#225) Closes #179. ### Summary of Changes Every `fit` method of `TableTransformer`s now requires `column_names` to be explicitly set. --- src/safeds/data/tabular/transformation/_imputer.py | 2 +- .../data/tabular/transformation/_label_encoder.py | 2 +- .../data/tabular/transformation/_one_hot_encoder.py | 2 +- .../data/tabular/transformation/_table_transformer.py | 2 +- .../safeds/data/tabular/transformation/test_imputer.py | 6 +++--- .../data/tabular/transformation/test_label_encoder.py | 10 +++++----- .../tabular/transformation/test_one_hot_encoder.py | 8 ++++---- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/safeds/data/tabular/transformation/_imputer.py b/src/safeds/data/tabular/transformation/_imputer.py index 6ecd3adbb..6a890f97a 100644 --- a/src/safeds/data/tabular/transformation/_imputer.py +++ b/src/safeds/data/tabular/transformation/_imputer.py @@ -90,7 +90,7 @@ def __init__(self, strategy: ImputerStrategy): self._column_names: list[str] | None = None # noinspection PyProtectedMember - def fit(self, table: Table, column_names: list[str] | None = None) -> Imputer: + def fit(self, table: Table, column_names: list[str] | None) -> Imputer: """ Learn a transformation for a set of columns in a table. diff --git a/src/safeds/data/tabular/transformation/_label_encoder.py b/src/safeds/data/tabular/transformation/_label_encoder.py index ad5b95e13..e2004c15f 100644 --- a/src/safeds/data/tabular/transformation/_label_encoder.py +++ b/src/safeds/data/tabular/transformation/_label_encoder.py @@ -27,7 +27,7 @@ def __init__(self) -> None: self._wrapped_transformer: sk_OrdinalEncoder | None = None self._column_names: list[str] | None = None - def fit(self, table: Table, column_names: list[str] | None = None) -> LabelEncoder: + def fit(self, table: Table, column_names: list[str] | None) -> LabelEncoder: """ Learn a transformation for a set of columns in a table. diff --git a/src/safeds/data/tabular/transformation/_one_hot_encoder.py b/src/safeds/data/tabular/transformation/_one_hot_encoder.py index da268f48a..69c9f0d4e 100644 --- a/src/safeds/data/tabular/transformation/_one_hot_encoder.py +++ b/src/safeds/data/tabular/transformation/_one_hot_encoder.py @@ -18,7 +18,7 @@ def __init__(self) -> None: self._column_names: dict[str, list[str]] | None = None # noinspection PyProtectedMember - def fit(self, table: Table, column_names: list[str] | None = None) -> OneHotEncoder: + def fit(self, table: Table, column_names: list[str] | None) -> OneHotEncoder: """ Learn a transformation for a set of columns in a table. diff --git a/src/safeds/data/tabular/transformation/_table_transformer.py b/src/safeds/data/tabular/transformation/_table_transformer.py index dac2f5072..ba431d0bc 100644 --- a/src/safeds/data/tabular/transformation/_table_transformer.py +++ b/src/safeds/data/tabular/transformation/_table_transformer.py @@ -11,7 +11,7 @@ class TableTransformer(ABC): """Learn a transformation for a set of columns in a `Table` and transform another `Table` with the same columns.""" @abstractmethod - def fit(self, table: Table, column_names: list[str] | None = None) -> TableTransformer: + def fit(self, table: Table, column_names: list[str] | None) -> TableTransformer: """ Learn a transformation for a set of columns in a table. diff --git a/tests/safeds/data/tabular/transformation/test_imputer.py b/tests/safeds/data/tabular/transformation/test_imputer.py index 8930e313b..6fad18153 100644 --- a/tests/safeds/data/tabular/transformation/test_imputer.py +++ b/tests/safeds/data/tabular/transformation/test_imputer.py @@ -24,7 +24,7 @@ def test_should_not_change_original_transformer(self) -> None: ) transformer = Imputer(Imputer.Strategy.Constant(0)) - transformer.fit(table) + transformer.fit(table, None) assert transformer._wrapped_transformer is None assert transformer._column_names is None @@ -38,7 +38,7 @@ def test_should_raise_if_column_not_found(self) -> None: }, ) - transformer = Imputer(Imputer.Strategy.Constant(0)).fit(table_to_fit) + transformer = Imputer(Imputer.Strategy.Constant(0)).fit(table_to_fit, None) table_to_transform = Table.from_dict( { @@ -75,7 +75,7 @@ def test_should_return_true_after_fitting(self) -> None: ) transformer = Imputer(Imputer.Strategy.Mean()) - fitted_transformer = transformer.fit(table) + fitted_transformer = transformer.fit(table, None) assert fitted_transformer.is_fitted() diff --git a/tests/safeds/data/tabular/transformation/test_label_encoder.py b/tests/safeds/data/tabular/transformation/test_label_encoder.py index a731d7793..055ef621e 100644 --- a/tests/safeds/data/tabular/transformation/test_label_encoder.py +++ b/tests/safeds/data/tabular/transformation/test_label_encoder.py @@ -23,7 +23,7 @@ def test_should_not_change_original_transformer(self) -> None: ) transformer = LabelEncoder() - transformer.fit(table) + transformer.fit(table, None) assert transformer._wrapped_transformer is None assert transformer._column_names is None @@ -37,7 +37,7 @@ def test_should_raise_if_column_not_found(self) -> None: }, ) - transformer = LabelEncoder().fit(table_to_fit) + transformer = LabelEncoder().fit(table_to_fit, None) table_to_transform = Table.from_dict( { @@ -74,7 +74,7 @@ def test_should_return_true_after_fitting(self) -> None: ) transformer = LabelEncoder() - fitted_transformer = transformer.fit(table) + fitted_transformer = transformer.fit(table, None) assert fitted_transformer.is_fitted() @@ -150,7 +150,7 @@ class TestInverseTransform: ], ) def test_should_return_original_table(self, table: Table) -> None: - transformer = LabelEncoder().fit(table) + transformer = LabelEncoder().fit(table, None) assert transformer.inverse_transform(transformer.transform(table)) == table @@ -161,7 +161,7 @@ def test_should_not_change_transformed_table(self) -> None: }, ) - transformer = LabelEncoder().fit(table) + transformer = LabelEncoder().fit(table, None) transformed_table = transformer.transform(table) transformer.inverse_transform(transformed_table) diff --git a/tests/safeds/data/tabular/transformation/test_one_hot_encoder.py b/tests/safeds/data/tabular/transformation/test_one_hot_encoder.py index 95cfddec2..426a20d6e 100644 --- a/tests/safeds/data/tabular/transformation/test_one_hot_encoder.py +++ b/tests/safeds/data/tabular/transformation/test_one_hot_encoder.py @@ -23,7 +23,7 @@ def test_should_not_change_original_transformer(self) -> None: ) transformer = OneHotEncoder() - transformer.fit(table) + transformer.fit(table, None) assert transformer._wrapped_transformer is None assert transformer._column_names is None @@ -37,7 +37,7 @@ def test_should_raise_if_column_not_found(self) -> None: }, ) - transformer = OneHotEncoder().fit(table_to_fit) + transformer = OneHotEncoder().fit(table_to_fit, None) table_to_transform = Table.from_dict( { @@ -74,7 +74,7 @@ def test_should_return_true_after_fitting(self) -> None: ) transformer = OneHotEncoder() - fitted_transformer = transformer.fit(table) + fitted_transformer = transformer.fit(table, None) assert fitted_transformer.is_fitted() @@ -247,7 +247,7 @@ def test_should_not_change_transformed_table(self) -> None: }, ) - transformer = OneHotEncoder().fit(table) + transformer = OneHotEncoder().fit(table, None) transformed_table = transformer.transform(table) transformer.inverse_transform(transformed_table)