-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add SklearnTransformAdapter (wrap any scikit-learn transformer) #1017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Valyrian-Code
wants to merge
3
commits into
OpenSTEF:main
Choose a base branch
from
Valyrian-Code:feat/sklearn-transform-adapter-683
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/openstef-models/src/openstef_models/transforms/general/sklearn_adapter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # SPDX-FileCopyrightText: 2025 Contributors to the OpenSTEF project <openstef@lfenergy.org> | ||
| # | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| """Adapter that exposes any scikit-learn transformer as a TimeSeriesTransform.""" | ||
|
|
||
| import importlib | ||
| from typing import Any, override | ||
|
|
||
| import pandas as pd | ||
| from pydantic import Field, PrivateAttr | ||
|
|
||
| from openstef_core.base_model import BaseConfig | ||
| from openstef_core.datasets import TimeSeriesDataset | ||
| from openstef_core.exceptions import NotFittedError | ||
| from openstef_core.transforms import TimeSeriesTransform | ||
| from openstef_models.utils.feature_selection import FeatureSelection | ||
|
|
||
|
|
||
| class SklearnTransformAdapter(BaseConfig, TimeSeriesTransform): | ||
| """Adapt any scikit-learn transformer to the OpenSTEF ``TimeSeriesTransform`` interface. | ||
|
|
||
| The transformer is specified by its import path and constructor parameters rather than | ||
| as an object, so the configuration stays serializable (save/load-able). It is fitted on | ||
| the selected feature columns; its output then replaces those columns while the remaining | ||
| columns pass through unchanged. Output column names come from the transformer's | ||
| ``get_feature_names_out()``, so shape-changing transforms (e.g. PCA, one-hot encoders) | ||
| are handled the same way as shape-preserving ones (e.g. scalers). | ||
|
|
||
| ``features_added()`` is populated after ``fit()`` (the output names of some transformers | ||
| are only known once fitted). | ||
|
|
||
| Example: | ||
| >>> import pandas as pd | ||
| >>> from datetime import timedelta | ||
| >>> from openstef_core.datasets import TimeSeriesDataset | ||
| >>> from openstef_models.transforms.general import SklearnTransformAdapter | ||
| >>> | ||
| >>> data = pd.DataFrame( | ||
| ... {"load": [100.0, 200.0, 300.0]}, | ||
| ... index=pd.date_range("2025-01-01", periods=3, freq="h"), | ||
| ... ) | ||
| >>> dataset = TimeSeriesDataset(data, timedelta(hours=1)) | ||
| >>> adapter = SklearnTransformAdapter(transformer_class="sklearn.preprocessing.StandardScaler") | ||
| >>> adapter.fit(dataset) | ||
| >>> transformed = adapter.transform(dataset) | ||
| >>> abs(float(transformed.data["load"].mean().round(6))) | ||
| 0.0 | ||
| >>> adapter.features_added() | ||
| [] | ||
| """ | ||
|
|
||
| transformer_class: str = Field( | ||
| description="Import path of the scikit-learn transformer, e.g. 'sklearn.preprocessing.StandardScaler'.", | ||
| ) | ||
| transformer_params: dict[str, Any] = Field( | ||
| default_factory=dict, | ||
| description="Keyword arguments passed to the transformer's constructor.", | ||
| ) | ||
| selection: FeatureSelection = Field( | ||
| default=FeatureSelection.ALL, | ||
| description="Features the transformer is applied to.", | ||
| ) | ||
|
|
||
| _transformer: Any = PrivateAttr() | ||
| _is_fitted: bool = PrivateAttr(default=False) | ||
| _added_features: list[str] = PrivateAttr(default_factory=list) | ||
|
|
||
| @property | ||
| @override | ||
| def is_fitted(self) -> bool: | ||
| return self._is_fitted | ||
|
|
||
| @override | ||
| def model_post_init(self, context: Any) -> None: | ||
| module_path, _, class_name = self.transformer_class.rpartition(".") | ||
| if not module_path: | ||
| msg = f"transformer_class must be a fully qualified import path, got {self.transformer_class!r}." | ||
| raise ValueError(msg) | ||
| transformer_cls = getattr(importlib.import_module(module_path), class_name) | ||
| self._transformer = transformer_cls(**self.transformer_params) | ||
| if not hasattr(self._transformer, "get_feature_names_out"): | ||
| msg = f"{self.transformer_class} does not implement get_feature_names_out and cannot be adapted." | ||
| raise TypeError(msg) | ||
|
|
||
| @override | ||
| def fit(self, data: TimeSeriesDataset) -> None: | ||
| features = self.selection.resolve(data.feature_names) | ||
| self._transformer.fit(data.data[features]) | ||
| output_names = list(self._transformer.get_feature_names_out(features)) | ||
| self._added_features = [name for name in output_names if name not in data.feature_names] | ||
| self._is_fitted = True | ||
|
|
||
| @override | ||
| def transform(self, data: TimeSeriesDataset) -> TimeSeriesDataset: | ||
| if not self._is_fitted: | ||
| raise NotFittedError(self.__class__.__name__) | ||
|
|
||
| features = self.selection.resolve(data.feature_names) | ||
| output_names = list(self._transformer.get_feature_names_out(features)) | ||
|
Valyrian-Code marked this conversation as resolved.
Outdated
|
||
| transformed = pd.DataFrame( | ||
| self._transformer.transform(data.data[features]), | ||
| index=data.data.index, | ||
| columns=output_names, | ||
| ) | ||
|
|
||
| # Replace the transformed inputs with the transformer's output, keep the rest. | ||
| passthrough = [column for column in data.data.columns if column not in features] | ||
| result = pd.concat([data.data[passthrough], transformed], axis=1) | ||
|
|
||
| return TimeSeriesDataset(data=result, sample_interval=data.sample_interval) | ||
|
|
||
| @override | ||
| def features_added(self) -> list[str]: | ||
| return self._added_features | ||
|
|
||
|
|
||
| __all__ = ["SklearnTransformAdapter"] | ||
97 changes: 97 additions & 0 deletions
97
packages/openstef-models/tests/unit/transforms/general/test_sklearn_adapter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # SPDX-FileCopyrightText: 2025 Contributors to the OpenSTEF project <openstef@lfenergy.org> | ||
| # | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| from datetime import timedelta | ||
|
|
||
| import pandas as pd | ||
| import pytest | ||
|
|
||
| from openstef_core.datasets import TimeSeriesDataset | ||
| from openstef_core.exceptions import NotFittedError | ||
| from openstef_models.transforms.general import SklearnTransformAdapter | ||
| from openstef_models.utils.feature_selection import Include | ||
|
|
||
|
|
||
| def _dataset() -> TimeSeriesDataset: | ||
| data = pd.DataFrame( | ||
| {"load": [100.0, 200.0, 300.0], "temperature": [20.0, 25.0, 30.0]}, | ||
| index=pd.date_range("2025-01-01", periods=3, freq="h"), | ||
| ) | ||
| return TimeSeriesDataset(data, timedelta(hours=1)) | ||
|
|
||
|
|
||
| def test_shape_preserving_transformer_scales_in_place(): | ||
| """A scaler keeps the same columns and reports no added features.""" | ||
| dataset = _dataset() | ||
| adapter = SklearnTransformAdapter(transformer_class="sklearn.preprocessing.StandardScaler") | ||
|
|
||
| adapter.fit(dataset) | ||
| result = adapter.transform(dataset) | ||
|
|
||
| assert set(result.data.columns) == {"load", "temperature"} | ||
| assert result.data["load"].mean() == pytest.approx(0.0, abs=1e-9) | ||
| assert result.data["load"].std(ddof=0) == pytest.approx(1.0) | ||
| assert adapter.features_added() == [] | ||
|
|
||
|
|
||
| def test_shape_changing_transformer_replaces_features_with_outputs(): | ||
| """PCA replaces the input features with its components (the added features).""" | ||
| adapter = SklearnTransformAdapter( | ||
| transformer_class="sklearn.decomposition.PCA", | ||
| transformer_params={"n_components": 2, "random_state": 0}, | ||
| ) | ||
| dataset = _dataset() | ||
|
|
||
| adapter.fit(dataset) | ||
| result = adapter.transform(dataset) | ||
|
|
||
| added = adapter.features_added() | ||
| assert len(added) == 2 | ||
| # the two input features are gone, replaced by the two components | ||
| assert set(result.data.columns) == set(added) | ||
| assert "load" not in result.data.columns | ||
|
|
||
|
|
||
| def test_unselected_features_pass_through_unchanged(): | ||
| """Only the selected features are transformed; the rest pass through untouched.""" | ||
| dataset = _dataset() | ||
| adapter = SklearnTransformAdapter( | ||
| transformer_class="sklearn.preprocessing.StandardScaler", | ||
| selection=Include("load"), | ||
| ) | ||
|
|
||
| adapter.fit(dataset) | ||
| result = adapter.transform(dataset) | ||
|
|
||
| assert result.data["temperature"].tolist() == [20.0, 25.0, 30.0] | ||
| assert result.data["load"].mean() == pytest.approx(0.0, abs=1e-9) | ||
|
|
||
|
|
||
| def test_transform_before_fit_raises(): | ||
| """transform() before fit() raises NotFittedError.""" | ||
| adapter = SklearnTransformAdapter(transformer_class="sklearn.preprocessing.StandardScaler") | ||
|
|
||
| with pytest.raises(NotFittedError): | ||
| adapter.transform(_dataset()) | ||
|
|
||
|
|
||
| def test_config_round_trips_and_rebuilds_transformer(): | ||
| """The config is serializable and reconstructs an equivalent transformer.""" | ||
| adapter = SklearnTransformAdapter( | ||
| transformer_class="sklearn.decomposition.PCA", | ||
| transformer_params={"n_components": 3}, | ||
| ) | ||
|
|
||
| restored = SklearnTransformAdapter.model_validate(adapter.model_dump()) | ||
|
|
||
| assert restored.transformer_class == "sklearn.decomposition.PCA" | ||
| assert restored.transformer_params == {"n_components": 3} | ||
| assert type(restored._transformer).__name__ == "PCA" | ||
| assert restored._transformer.n_components == 3 | ||
|
|
||
|
|
||
| def test_invalid_transformer_class_raises(): | ||
| """An unimportable transformer_class fails fast at construction.""" | ||
| with pytest.raises(ModuleNotFoundError): | ||
| SklearnTransformAdapter(transformer_class="not_a_real_module.Nope") | ||
|
Valyrian-Code marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.