Transform#

class openstef_core.mixins.Transform[source]#

Bases: Stateful, Generic

Abstract base class for data transformations.

This class provides the basic interface for transforms that can be fitted to data of type I and then applied to transform it to type O. It follows the scikit-learn pattern with separate fit and transform phases, and includes state management capabilities.

Type parameters:

I: The input data type. O: The output data type.

Subclasses must implement the is_fitted property, fit method, transform method, and the state management methods from Stateful.

Example

Implementing a simple scaling transform

>>> from openstef_core.datasets import TimeSeriesDataset
>>> class ScaleTransform(Transform[TimeSeriesDataset, TimeSeriesDataset]):
...     def __init__(self):
...         self.scale_factor = None
...
...     @property
...     def is_fitted(self) -> bool:
...         return self.scale_factor is not None
...
...     def fit(self, data: TimeSeriesDataset) -> None:
...         self.scale_factor = data.data.max().max()
...
...     def transform(self, data: TimeSeriesDataset) -> TimeSeriesDataset:
...         scaled_data = data.data / self.scale_factor
...         return TimeSeriesDataset(scaled_data, data.sample_interval)
abstract property is_fitted: bool#

Check if the transform has been fitted.

abstractmethod fit(data: I) None[source]#

Fit the transform to the input data.

This method should be called before applying the transform to the data. It allows the transform to learn any necessary parameters from the data.

Parameters:
  • data (TypeVar(I)) – The input data to fit the transform on.

  • data

Return type:

None

abstractmethod transform(data: I) O[source]#

Transform the input data.

This method should apply a transformation to the input data and return a new instance.

Parameters:
  • data (TypeVar(I)) – The input data to be transformed.

  • data

Returns:

A new instance of the transformed data.

Raises:

NotFittedError – If the transform has not been fitted yet.

Return type:

TypeVar(O)

fit_transform(data: I) O[source]#

Fit the transform to the data and then transform it.

This method combines fitting and transforming into a single step.

Parameters:
  • data (TypeVar(I)) – The input data to fit and transform.

  • data

Returns:

A new instance of the transformed data.

Return type:

TypeVar(O)