From e43f631dd8642a38a9df65d55eca5719c789f075 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Tue, 30 Apr 2024 23:20:43 +0200 Subject: [PATCH] feat: `is_fitted` is now always a property --- .../tabular/transformation/_discretizer.py | 14 ++++---------- .../data/tabular/transformation/_imputer.py | 14 ++++---------- .../tabular/transformation/_label_encoder.py | 14 ++++---------- .../tabular/transformation/_one_hot_encoder.py | 12 +++--------- .../tabular/transformation/_range_scaler.py | 14 ++++---------- .../tabular/transformation/_standard_scaler.py | 14 ++++---------- .../transformation/_table_transformer.py | 18 ++++++------------ .../ml/classical/classification/_ada_boost.py | 10 ++-------- .../ml/classical/classification/_classifier.py | 12 +++--------- .../classical/classification/_decision_tree.py | 10 ++-------- .../classification/_gradient_boosting.py | 10 ++-------- .../classification/_k_nearest_neighbors.py | 10 ++-------- .../classification/_logistic_regression.py | 10 ++-------- .../classical/classification/_random_forest.py | 10 ++-------- .../classification/_support_vector_machine.py | 10 ++-------- .../ml/classical/regression/_ada_boost.py | 10 ++-------- src/safeds/ml/classical/regression/_arima.py | 16 +++++----------- .../ml/classical/regression/_decision_tree.py | 10 ++-------- .../regression/_elastic_net_regression.py | 10 ++-------- .../classical/regression/_gradient_boosting.py | 10 ++-------- .../regression/_k_nearest_neighbors.py | 10 ++-------- .../classical/regression/_lasso_regression.py | 10 ++-------- .../classical/regression/_linear_regression.py | 10 ++-------- .../ml/classical/regression/_random_forest.py | 10 ++-------- .../ml/classical/regression/_regressor.py | 12 +++--------- .../classical/regression/_ridge_regression.py | 10 ++-------- .../regression/_support_vector_machine.py | 10 ++-------- src/safeds/ml/nn/_model.py | 18 ++---------------- .../tabular/transformation/test_discretizer.py | 4 ++-- .../tabular/transformation/test_imputer.py | 4 ++-- .../transformation/test_label_encoder.py | 4 ++-- .../transformation/test_one_hot_encoder.py | 4 ++-- .../transformation/test_range_scaler.py | 4 ++-- .../transformation/test_standard_scaler.py | 4 ++-- .../classification/test_classifier.py | 7 ++++--- .../classical/regression/test_arima_model.py | 6 +++--- .../ml/classical/regression/test_regressor.py | 7 ++++--- 37 files changed, 99 insertions(+), 273 deletions(-) diff --git a/src/safeds/data/tabular/transformation/_discretizer.py b/src/safeds/data/tabular/transformation/_discretizer.py index 3845549f2..83f9d70ad 100644 --- a/src/safeds/data/tabular/transformation/_discretizer.py +++ b/src/safeds/data/tabular/transformation/_discretizer.py @@ -149,15 +149,9 @@ def transform(self, table: Table) -> Table: data[self._column_names] = self._wrapped_transformer.transform(data[self._column_names]) return Table._from_pandas_dataframe(data) + @property def is_fitted(self) -> bool: - """ - Check if the transformer is fitted. - - Returns - ------- - is_fitted : - Whether the transformer is fitted. - """ + """Whether the transformer is fitted.""" return self._wrapped_transformer is not None def get_names_of_added_columns(self) -> list[str]: @@ -174,7 +168,7 @@ def get_names_of_added_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] @@ -211,6 +205,6 @@ def get_names_of_removed_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] diff --git a/src/safeds/data/tabular/transformation/_imputer.py b/src/safeds/data/tabular/transformation/_imputer.py index 17103e4de..a1756abe0 100644 --- a/src/safeds/data/tabular/transformation/_imputer.py +++ b/src/safeds/data/tabular/transformation/_imputer.py @@ -261,15 +261,9 @@ def transform(self, table: Table) -> Table: ) return Table._from_pandas_dataframe(data, table.schema) + @property def is_fitted(self) -> bool: - """ - Check if the transformer is fitted. - - Returns - ------- - is_fitted : bool - Whether the transformer is fitted. - """ + """Whether the transformer is fitted.""" return self._wrapped_transformer is not None def get_names_of_added_columns(self) -> list[str]: @@ -286,7 +280,7 @@ def get_names_of_added_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] @@ -323,6 +317,6 @@ def get_names_of_removed_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] diff --git a/src/safeds/data/tabular/transformation/_label_encoder.py b/src/safeds/data/tabular/transformation/_label_encoder.py index 31b32b72f..cec06f736 100644 --- a/src/safeds/data/tabular/transformation/_label_encoder.py +++ b/src/safeds/data/tabular/transformation/_label_encoder.py @@ -177,15 +177,9 @@ def inverse_transform(self, transformed_table: Table) -> Table: data[self._column_names] = self._wrapped_transformer.inverse_transform(data[self._column_names]) return Table._from_pandas_dataframe(data) + @property def is_fitted(self) -> bool: - """ - Check if the transformer is fitted. - - Returns - ------- - is_fitted : bool - Whether the transformer is fitted. - """ + """Whether the transformer is fitted.""" return self._wrapped_transformer is not None def get_names_of_added_columns(self) -> list[str]: @@ -202,7 +196,7 @@ def get_names_of_added_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] @@ -239,6 +233,6 @@ def get_names_of_removed_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] diff --git a/src/safeds/data/tabular/transformation/_one_hot_encoder.py b/src/safeds/data/tabular/transformation/_one_hot_encoder.py index 503d0817a..aca63d82b 100644 --- a/src/safeds/data/tabular/transformation/_one_hot_encoder.py +++ b/src/safeds/data/tabular/transformation/_one_hot_encoder.py @@ -339,15 +339,9 @@ def inverse_transform(self, transformed_table: Table) -> Table: return table.sort_columns(lambda col1, col2: column_names.index(col1.name) - column_names.index(col2.name)) + @property def is_fitted(self) -> bool: - """ - Check if the transformer is fitted. - - Returns - ------- - is_fitted : bool - Whether the transformer is fitted. - """ + """Whether the transformer is fitted.""" return ( self._column_names is not None and self._value_to_column is not None @@ -387,7 +381,7 @@ def get_names_of_changed_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] diff --git a/src/safeds/data/tabular/transformation/_range_scaler.py b/src/safeds/data/tabular/transformation/_range_scaler.py index 0260d537d..cd2b87b0d 100644 --- a/src/safeds/data/tabular/transformation/_range_scaler.py +++ b/src/safeds/data/tabular/transformation/_range_scaler.py @@ -223,15 +223,9 @@ def inverse_transform(self, transformed_table: Table) -> Table: data[self._column_names] = self._wrapped_transformer.inverse_transform(data[self._column_names]) return Table._from_pandas_dataframe(data) + @property def is_fitted(self) -> bool: - """ - Check if the transformer is fitted. - - Returns - ------- - is_fitted : bool - Whether the transformer is fitted. - """ + """Whether the transformer is fitted.""" return self._wrapped_transformer is not None def get_names_of_added_columns(self) -> list[str]: @@ -248,7 +242,7 @@ def get_names_of_added_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] @@ -285,6 +279,6 @@ def get_names_of_removed_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] diff --git a/src/safeds/data/tabular/transformation/_standard_scaler.py b/src/safeds/data/tabular/transformation/_standard_scaler.py index b74db5261..bc7a2aba9 100644 --- a/src/safeds/data/tabular/transformation/_standard_scaler.py +++ b/src/safeds/data/tabular/transformation/_standard_scaler.py @@ -205,15 +205,9 @@ def inverse_transform(self, transformed_table: Table) -> Table: data[self._column_names] = self._wrapped_transformer.inverse_transform(data[self._column_names]) return Table._from_pandas_dataframe(data) + @property def is_fitted(self) -> bool: - """ - Check if the transformer is fitted. - - Returns - ------- - is_fitted : bool - Whether the transformer is fitted. - """ + """Whether the transformer is fitted.""" return self._wrapped_transformer is not None def get_names_of_added_columns(self) -> list[str]: @@ -230,7 +224,7 @@ def get_names_of_added_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] @@ -267,6 +261,6 @@ def get_names_of_removed_columns(self) -> list[str]: TransformerNotFittedError If the transformer has not been fitted yet. """ - if not self.is_fitted(): + if not self.is_fitted: raise TransformerNotFittedError return [] diff --git a/src/safeds/data/tabular/transformation/_table_transformer.py b/src/safeds/data/tabular/transformation/_table_transformer.py index 2e933fd0a..74fb14be1 100644 --- a/src/safeds/data/tabular/transformation/_table_transformer.py +++ b/src/safeds/data/tabular/transformation/_table_transformer.py @@ -21,10 +21,10 @@ def __hash__(self) -> int: hash: The hash value. """ - added = self.get_names_of_added_columns() if self.is_fitted() else [] - changed = self.get_names_of_changed_columns() if self.is_fitted() else [] - removed = self.get_names_of_removed_columns() if self.is_fitted() else [] - return _structural_hash(self.__class__.__qualname__, self.is_fitted(), added, changed, removed) + added = self.get_names_of_added_columns() if self.is_fitted else [] + changed = self.get_names_of_changed_columns() if self.is_fitted else [] + removed = self.get_names_of_removed_columns() if self.is_fitted else [] + return _structural_hash(self.__class__.__qualname__, self.is_fitted, added, changed, removed) @abstractmethod def fit(self, table: Table, column_names: list[str] | None) -> TableTransformer: @@ -117,16 +117,10 @@ def get_names_of_removed_columns(self) -> list[str]: If the transformer has not been fitted yet. """ + @property @abstractmethod def is_fitted(self) -> bool: - """ - Check if the transformer is fitted. - - Returns - ------- - is_fitted : bool - Whether the transformer is fitted. - """ + """Whether the transformer is fitted.""" def fit_and_transform(self, table: Table, column_names: list[str] | None = None) -> Table: """ diff --git a/src/safeds/ml/classical/classification/_ada_boost.py b/src/safeds/ml/classical/classification/_ada_boost.py index 11a3969e8..2a661f711 100644 --- a/src/safeds/ml/classical/classification/_ada_boost.py +++ b/src/safeds/ml/classical/classification/_ada_boost.py @@ -184,15 +184,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_classifier, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the classifier is fitted. - """ + """Whether the classifier is fitted.""" return self._wrapped_classifier is not None def _get_sklearn_classifier(self) -> ClassifierMixin: diff --git a/src/safeds/ml/classical/classification/_classifier.py b/src/safeds/ml/classical/classification/_classifier.py index 9d761520f..0ee8cc062 100644 --- a/src/safeds/ml/classical/classification/_classifier.py +++ b/src/safeds/ml/classical/classification/_classifier.py @@ -25,7 +25,7 @@ def __hash__(self) -> int: hash: The hash value. """ - return _structural_hash(self.__class__.__qualname__, self.is_fitted()) + return _structural_hash(self.__class__.__qualname__, self.is_fitted) @abstractmethod def fit(self, training_set: TaggedTable) -> Classifier: @@ -77,16 +77,10 @@ def predict(self, dataset: Table) -> TaggedTable: If predicting with the given dataset failed. """ + @property @abstractmethod def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the classifier is fitted. - """ + """Whether the classifier is fitted.""" @abstractmethod def _get_sklearn_classifier(self) -> ClassifierMixin: diff --git a/src/safeds/ml/classical/classification/_decision_tree.py b/src/safeds/ml/classical/classification/_decision_tree.py index 214dc83f4..8afd667f6 100644 --- a/src/safeds/ml/classical/classification/_decision_tree.py +++ b/src/safeds/ml/classical/classification/_decision_tree.py @@ -98,15 +98,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_classifier, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the classifier is fitted. - """ + """Whether the classifier is fitted.""" return self._wrapped_classifier is not None def _get_sklearn_classifier(self) -> ClassifierMixin: diff --git a/src/safeds/ml/classical/classification/_gradient_boosting.py b/src/safeds/ml/classical/classification/_gradient_boosting.py index ba42bfb9f..a5780b6be 100644 --- a/src/safeds/ml/classical/classification/_gradient_boosting.py +++ b/src/safeds/ml/classical/classification/_gradient_boosting.py @@ -155,15 +155,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_classifier, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the classifier is fitted. - """ + """Whether the classifier is fitted.""" return self._wrapped_classifier is not None def _get_sklearn_classifier(self) -> ClassifierMixin: diff --git a/src/safeds/ml/classical/classification/_k_nearest_neighbors.py b/src/safeds/ml/classical/classification/_k_nearest_neighbors.py index eb882d51a..339844b9b 100644 --- a/src/safeds/ml/classical/classification/_k_nearest_neighbors.py +++ b/src/safeds/ml/classical/classification/_k_nearest_neighbors.py @@ -147,15 +147,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_classifier, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the classifier is fitted. - """ + """Whether the classifier is fitted.""" return self._wrapped_classifier is not None def _get_sklearn_classifier(self) -> ClassifierMixin: diff --git a/src/safeds/ml/classical/classification/_logistic_regression.py b/src/safeds/ml/classical/classification/_logistic_regression.py index f0dffb783..6e82c1bea 100644 --- a/src/safeds/ml/classical/classification/_logistic_regression.py +++ b/src/safeds/ml/classical/classification/_logistic_regression.py @@ -98,15 +98,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_classifier, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the classifier is fitted. - """ + """Whether the classifier is fitted.""" return self._wrapped_classifier is not None def _get_sklearn_classifier(self) -> ClassifierMixin: diff --git a/src/safeds/ml/classical/classification/_random_forest.py b/src/safeds/ml/classical/classification/_random_forest.py index 133c056c8..1f6409341 100644 --- a/src/safeds/ml/classical/classification/_random_forest.py +++ b/src/safeds/ml/classical/classification/_random_forest.py @@ -134,15 +134,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_classifier, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the classifier is fitted. - """ + """Whether the classifier is fitted.""" return self._wrapped_classifier is not None def _get_sklearn_classifier(self) -> ClassifierMixin: diff --git a/src/safeds/ml/classical/classification/_support_vector_machine.py b/src/safeds/ml/classical/classification/_support_vector_machine.py index 0c83de328..07760f2b2 100644 --- a/src/safeds/ml/classical/classification/_support_vector_machine.py +++ b/src/safeds/ml/classical/classification/_support_vector_machine.py @@ -305,15 +305,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_classifier, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the classifier is fitted. - """ + """Whether the classifier is fitted.""" return self._wrapped_classifier is not None def _get_sklearn_classifier(self) -> ClassifierMixin: diff --git a/src/safeds/ml/classical/regression/_ada_boost.py b/src/safeds/ml/classical/regression/_ada_boost.py index 9c5f23e7a..14b46cc61 100644 --- a/src/safeds/ml/classical/regression/_ada_boost.py +++ b/src/safeds/ml/classical/regression/_ada_boost.py @@ -184,15 +184,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_arima.py b/src/safeds/ml/classical/regression/_arima.py index b70b4489d..f35e066fc 100644 --- a/src/safeds/ml/classical/regression/_arima.py +++ b/src/safeds/ml/classical/regression/_arima.py @@ -31,7 +31,7 @@ def __hash__(self) -> int: hash: The hash value. """ - return _structural_hash(self.__class__.__qualname__, self.is_fitted(), self._order) + return _structural_hash(self.__class__.__qualname__, self.is_fitted, self._order) def __init__(self) -> None: # Internal state @@ -137,7 +137,7 @@ def predict(self, time_series: TimeSeries) -> TimeSeries: result_table = time_series._as_table() result_table = result_table.remove_columns([time_series.target.name]) # Validation - if not self.is_fitted() or self._arima is None: + if not self.is_fitted or self._arima is None: raise ModelNotFittedError # forecast @@ -178,7 +178,7 @@ def plot_predictions(self, test_series: TimeSeries) -> Image: """ import matplotlib.pyplot as plt - if not self.is_fitted() or self._arima is None: + if not self.is_fitted or self._arima is None: raise ModelNotFittedError test_data = test_series.target._data.to_numpy() n_steps = len(test_data) @@ -198,13 +198,7 @@ def plot_predictions(self, test_series: TimeSeries) -> Image: buffer.seek(0) return Image.from_bytes(buffer.read()) + @property def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted: - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._fitted diff --git a/src/safeds/ml/classical/regression/_decision_tree.py b/src/safeds/ml/classical/regression/_decision_tree.py index 891639d3f..9fe3353e8 100644 --- a/src/safeds/ml/classical/regression/_decision_tree.py +++ b/src/safeds/ml/classical/regression/_decision_tree.py @@ -98,15 +98,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_elastic_net_regression.py b/src/safeds/ml/classical/regression/_elastic_net_regression.py index d5d2dc3e5..5447188e2 100644 --- a/src/safeds/ml/classical/regression/_elastic_net_regression.py +++ b/src/safeds/ml/classical/regression/_elastic_net_regression.py @@ -185,15 +185,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_gradient_boosting.py b/src/safeds/ml/classical/regression/_gradient_boosting.py index 6437f6410..383c12ffb 100644 --- a/src/safeds/ml/classical/regression/_gradient_boosting.py +++ b/src/safeds/ml/classical/regression/_gradient_boosting.py @@ -155,15 +155,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_k_nearest_neighbors.py b/src/safeds/ml/classical/regression/_k_nearest_neighbors.py index 84d145334..0c8ce5d0b 100644 --- a/src/safeds/ml/classical/regression/_k_nearest_neighbors.py +++ b/src/safeds/ml/classical/regression/_k_nearest_neighbors.py @@ -148,15 +148,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_lasso_regression.py b/src/safeds/ml/classical/regression/_lasso_regression.py index 6e2ac4620..685ea83a8 100644 --- a/src/safeds/ml/classical/regression/_lasso_regression.py +++ b/src/safeds/ml/classical/regression/_lasso_regression.py @@ -139,15 +139,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_linear_regression.py b/src/safeds/ml/classical/regression/_linear_regression.py index d88c1bdc0..d70074dcb 100644 --- a/src/safeds/ml/classical/regression/_linear_regression.py +++ b/src/safeds/ml/classical/regression/_linear_regression.py @@ -98,15 +98,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_random_forest.py b/src/safeds/ml/classical/regression/_random_forest.py index c97db625e..c1216dc59 100644 --- a/src/safeds/ml/classical/regression/_random_forest.py +++ b/src/safeds/ml/classical/regression/_random_forest.py @@ -129,15 +129,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_regressor.py b/src/safeds/ml/classical/regression/_regressor.py index 91c768f2c..9797ebcb4 100644 --- a/src/safeds/ml/classical/regression/_regressor.py +++ b/src/safeds/ml/classical/regression/_regressor.py @@ -23,7 +23,7 @@ def __hash__(self) -> int: hash: The hash value. """ - return _structural_hash(self.__class__.__qualname__, self.is_fitted()) + return _structural_hash(self.__class__.__qualname__, self.is_fitted) @abstractmethod def fit(self, training_set: TaggedTable) -> Regressor: @@ -75,16 +75,10 @@ def predict(self, dataset: Table) -> TaggedTable: If predicting with the given dataset failed. """ + @property @abstractmethod def is_fitted(self) -> bool: - """ - Check if the classifier is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" @abstractmethod def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_ridge_regression.py b/src/safeds/ml/classical/regression/_ridge_regression.py index 1d6de0772..25a517e81 100644 --- a/src/safeds/ml/classical/regression/_ridge_regression.py +++ b/src/safeds/ml/classical/regression/_ridge_regression.py @@ -140,15 +140,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/classical/regression/_support_vector_machine.py b/src/safeds/ml/classical/regression/_support_vector_machine.py index 2366745cc..6918452e3 100644 --- a/src/safeds/ml/classical/regression/_support_vector_machine.py +++ b/src/safeds/ml/classical/regression/_support_vector_machine.py @@ -305,15 +305,9 @@ def predict(self, dataset: Table) -> TaggedTable: """ return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name) + @property def is_fitted(self) -> bool: - """ - Check if the regressor is fitted. - - Returns - ------- - is_fitted : bool - Whether the regressor is fitted. - """ + """Whether the regressor is fitted.""" return self._wrapped_regressor is not None def _get_sklearn_regressor(self) -> RegressorMixin: diff --git a/src/safeds/ml/nn/_model.py b/src/safeds/ml/nn/_model.py index 0f862e4b6..42a22e945 100644 --- a/src/safeds/ml/nn/_model.py +++ b/src/safeds/ml/nn/_model.py @@ -169,14 +169,7 @@ def predict(self, test_data: IPT) -> OT: @property def is_fitted(self) -> bool: - """ - Check if the model is fitted. - - Returns - ------- - is_fitted - Whether the model is fitted. - """ + """Whether the model is fitted.""" return self._is_fitted @@ -334,14 +327,7 @@ def predict(self, test_data: IPT) -> OT: @property def is_fitted(self) -> bool: - """ - Check if the model is fitted. - - Returns - ------- - is_fitted : - Whether the model is fitted. - """ + """Whether the model is fitted.""" return self._is_fitted diff --git a/tests/safeds/data/tabular/transformation/test_discretizer.py b/tests/safeds/data/tabular/transformation/test_discretizer.py index dc9ea7d13..c3c009340 100644 --- a/tests/safeds/data/tabular/transformation/test_discretizer.py +++ b/tests/safeds/data/tabular/transformation/test_discretizer.py @@ -148,7 +148,7 @@ def test_should_raise_if_not_fitted(self) -> None: class TestIsFitted: def test_should_return_false_before_fitting(self) -> None: transformer = Discretizer() - assert not transformer.is_fitted() + assert not transformer.is_fitted def test_should_return_true_after_fitting(self) -> None: table = Table( @@ -159,7 +159,7 @@ def test_should_return_true_after_fitting(self) -> None: transformer = Discretizer() fitted_transformer = transformer.fit(table, None) - assert fitted_transformer.is_fitted() + assert fitted_transformer.is_fitted class TestFitAndTransform: diff --git a/tests/safeds/data/tabular/transformation/test_imputer.py b/tests/safeds/data/tabular/transformation/test_imputer.py index 15e71a29d..1d986192b 100644 --- a/tests/safeds/data/tabular/transformation/test_imputer.py +++ b/tests/safeds/data/tabular/transformation/test_imputer.py @@ -167,7 +167,7 @@ class TestIsFitted: @pytest.mark.parametrize("strategy", strategies(), ids=lambda x: x.__class__.__name__) def test_should_return_false_before_fitting(self, strategy: ImputerStrategy) -> None: transformer = Imputer(strategy) - assert not transformer.is_fitted() + assert not transformer.is_fitted @pytest.mark.parametrize("strategy", strategies(), ids=lambda x: x.__class__.__name__) def test_should_return_true_after_fitting(self, strategy: ImputerStrategy) -> None: @@ -179,7 +179,7 @@ def test_should_return_true_after_fitting(self, strategy: ImputerStrategy) -> No transformer = Imputer(strategy) fitted_transformer = transformer.fit(table, None) - assert fitted_transformer.is_fitted() + assert fitted_transformer.is_fitted class TestFitAndTransform: diff --git a/tests/safeds/data/tabular/transformation/test_label_encoder.py b/tests/safeds/data/tabular/transformation/test_label_encoder.py index 57614335d..453fad0f7 100644 --- a/tests/safeds/data/tabular/transformation/test_label_encoder.py +++ b/tests/safeds/data/tabular/transformation/test_label_encoder.py @@ -83,7 +83,7 @@ def test_should_raise_if_table_contains_no_rows(self) -> None: class TestIsFitted: def test_should_return_false_before_fitting(self) -> None: transformer = LabelEncoder() - assert not transformer.is_fitted() + assert not transformer.is_fitted def test_should_return_true_after_fitting(self) -> None: table = Table( @@ -94,7 +94,7 @@ def test_should_return_true_after_fitting(self) -> None: transformer = LabelEncoder() fitted_transformer = transformer.fit(table, None) - assert fitted_transformer.is_fitted() + assert fitted_transformer.is_fitted class TestFitAndTransform: 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 810f9bd24..c35337065 100644 --- a/tests/safeds/data/tabular/transformation/test_one_hot_encoder.py +++ b/tests/safeds/data/tabular/transformation/test_one_hot_encoder.py @@ -120,7 +120,7 @@ def test_should_raise_value_not_present_when_fitted(self) -> None: class TestIsFitted: def test_should_return_false_before_fitting(self) -> None: transformer = OneHotEncoder() - assert not transformer.is_fitted() + assert not transformer.is_fitted @pytest.mark.parametrize( "table", @@ -150,7 +150,7 @@ def test_should_return_true_after_fitting(self, table: Table) -> None: category=UserWarning, ) fitted_transformer = transformer.fit(table, None) - assert fitted_transformer.is_fitted() + assert fitted_transformer.is_fitted class TestFitAndTransform: diff --git a/tests/safeds/data/tabular/transformation/test_range_scaler.py b/tests/safeds/data/tabular/transformation/test_range_scaler.py index 5d8e1dde6..a150b4da8 100644 --- a/tests/safeds/data/tabular/transformation/test_range_scaler.py +++ b/tests/safeds/data/tabular/transformation/test_range_scaler.py @@ -95,7 +95,7 @@ def test_should_raise_if_table_contains_no_rows(self) -> None: class TestIsFitted: def test_should_return_false_before_fitting(self) -> None: transformer = RangeScaler() - assert not transformer.is_fitted() + assert not transformer.is_fitted def test_should_return_true_after_fitting(self) -> None: table = Table( @@ -106,7 +106,7 @@ def test_should_return_true_after_fitting(self) -> None: transformer = RangeScaler() fitted_transformer = transformer.fit(table, None) - assert fitted_transformer.is_fitted() + assert fitted_transformer.is_fitted class TestFitAndTransform: diff --git a/tests/safeds/data/tabular/transformation/test_standard_scaler.py b/tests/safeds/data/tabular/transformation/test_standard_scaler.py index e11069d5b..24ea8b54f 100644 --- a/tests/safeds/data/tabular/transformation/test_standard_scaler.py +++ b/tests/safeds/data/tabular/transformation/test_standard_scaler.py @@ -97,7 +97,7 @@ def test_should_raise_if_table_contains_no_rows(self) -> None: class TestIsFitted: def test_should_return_false_before_fitting(self) -> None: transformer = StandardScaler() - assert not transformer.is_fitted() + assert not transformer.is_fitted def test_should_return_true_after_fitting(self) -> None: table = Table( @@ -108,7 +108,7 @@ def test_should_return_true_after_fitting(self) -> None: transformer = StandardScaler() fitted_transformer = transformer.fit(table, None) - assert fitted_transformer.is_fitted() + assert fitted_transformer.is_fitted class TestFitAndTransformOnMultipleTables: diff --git a/tests/safeds/ml/classical/classification/test_classifier.py b/tests/safeds/ml/classical/classification/test_classifier.py index 704565bb7..9af1201b9 100644 --- a/tests/safeds/ml/classical/classification/test_classifier.py +++ b/tests/safeds/ml/classical/classification/test_classifier.py @@ -73,7 +73,7 @@ def test_should_succeed_on_valid_data(self, classifier: Classifier, valid_data: def test_should_not_change_input_classifier(self, classifier: Classifier, valid_data: TaggedTable) -> None: classifier.fit(valid_data) - assert not classifier.is_fitted() + assert not classifier.is_fitted def test_should_not_change_input_table(self, classifier: Classifier, request: FixtureRequest) -> None: valid_data = request.getfixturevalue("valid_data") @@ -254,11 +254,11 @@ def test_should_raise_on_invalid_data( @pytest.mark.parametrize("classifier", classifiers(), ids=lambda x: x.__class__.__name__) class TestIsFitted: def test_should_return_false_before_fitting(self, classifier: Classifier) -> None: - assert not classifier.is_fitted() + assert not classifier.is_fitted def test_should_return_true_after_fitting(self, classifier: Classifier, valid_data: TaggedTable) -> None: fitted_classifier = classifier.fit(valid_data) - assert fitted_classifier.is_fitted() + assert fitted_classifier.is_fitted class TestHash: @@ -333,6 +333,7 @@ def predict(self, dataset: Table) -> TaggedTable: return dataset.tag_columns(target_name="predicted") + @property def is_fitted(self) -> bool: return True diff --git a/tests/safeds/ml/classical/regression/test_arima_model.py b/tests/safeds/ml/classical/regression/test_arima_model.py index b8ca54693..5d697accd 100644 --- a/tests/safeds/ml/classical/regression/test_arima_model.py +++ b/tests/safeds/ml/classical/regression/test_arima_model.py @@ -62,7 +62,7 @@ def test_should_not_change_input_regressor() -> None: valid_data = create_test_data() model = ArimaModelRegressor() model.fit(valid_data) - assert not model.is_fitted() + assert not model.is_fitted def test_should_not_change_input_table() -> None: @@ -184,13 +184,13 @@ def test_should_raise_if_not_fitted() -> None: def test_if_fitted_not_fitted() -> None: model = ArimaModelRegressor() - assert not model.is_fitted() + assert not model.is_fitted def test_if_fitted_fitted() -> None: model = ArimaModelRegressor() model = model.fit(create_test_data()) - assert model.is_fitted() + assert model.is_fitted def test_should_raise_if_horizon_too_small_plot() -> None: diff --git a/tests/safeds/ml/classical/regression/test_regressor.py b/tests/safeds/ml/classical/regression/test_regressor.py index 152aa8c0d..36ad9f9cb 100644 --- a/tests/safeds/ml/classical/regression/test_regressor.py +++ b/tests/safeds/ml/classical/regression/test_regressor.py @@ -84,7 +84,7 @@ def test_should_succeed_on_valid_data(self, regressor: Regressor, valid_data: Ta def test_should_not_change_input_regressor(self, regressor: Regressor, valid_data: TaggedTable) -> None: regressor.fit(valid_data) - assert not regressor.is_fitted() + assert not regressor.is_fitted def test_should_not_change_input_table(self, regressor: Regressor, request: FixtureRequest) -> None: valid_data = request.getfixturevalue("valid_data") @@ -265,11 +265,11 @@ def test_should_raise_on_invalid_data( @pytest.mark.parametrize("regressor", regressors(), ids=lambda x: x.__class__.__name__) class TestIsFitted: def test_should_return_false_before_fitting(self, regressor: Regressor) -> None: - assert not regressor.is_fitted() + assert not regressor.is_fitted def test_should_return_true_after_fitting(self, regressor: Regressor, valid_data: TaggedTable) -> None: fitted_regressor = regressor.fit(valid_data) - assert fitted_regressor.is_fitted() + assert fitted_regressor.is_fitted class TestHash: @@ -340,6 +340,7 @@ def predict(self, dataset: Table) -> TaggedTable: return dataset.tag_columns(target_name="predicted") + @property def is_fitted(self) -> bool: return True