"""
The :mod:`chemotools.smooth._savitzky_golay_filter` module implements the Savitzky-Golay Filter (SGF) transformation.
"""
# Authors: Pau Cabaneros
# License: MIT
from typing import Literal
import numpy as np
from scipy.signal import savgol_filter
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
from sklearn.utils.validation import check_is_fitted, validate_data
[docs]
class SavitzkyGolayFilter(TransformerMixin, OneToOneFeatureMixin, BaseEstimator):
"""
A transformer that calculates the Savitzky-Golay filter of the input data.
Parameters
----------
window_size : int, optional, default=3
The size of the window to use for the Savitzky-Golay filter. Must be odd. Default
is 3.
polynomial_order : int, optional, default=1
The order of the polynomial to use for the Savitzky-Golay filter. Must be less
than window_size. Default is 1.
mode : str, optional, default="nearest"
The mode to use for the Savitzky-Golay filter. Can be "nearest", "constant",
"reflect", "wrap", "mirror" or "interp". Default is "nearest".
Attributes
----------
n_features_in_ : int
The number of features in the training data.
Examples
--------
>>> from chemotools.datasets import load_fermentation_train
>>> from chemotools.smooth import SavitzkyGolayFilter
>>> # Load sample data
>>> X, _ = load_fermentation_train()
>>> # Initialize SavitzkyGolayFilter
>>> sgf = SavitzkyGolayFilter()
SavitzkyGolayFilter()
>>> # Fit and transform the data
>>> X_smoothed = sgf.fit_transform(X)
"""
def __init__(
self,
window_size: int = 3,
polynomial_order: int = 1,
mode: Literal["mirror", "constant", "nearest", "wrap", "interp"] = "nearest",
) -> None:
self.window_size = window_size
self.polynomial_order = polynomial_order
self.mode = mode
[docs]
def fit(self, X: np.ndarray, y=None) -> "SavitzkyGolayFilter":
"""
Fit the transformer to the input data.
Parameters
----------
X : np.ndarray of shape (n_samples, n_features)
The input data to fit the transformer to.
y : None
Ignored to align with API.
Returns
-------
self : SavitzkyGolayFilter
The fitted transformer.
"""
# Check that X is a 2D array and has only finite values
X = validate_data(
self, X, y="no_validation", ensure_2d=True, reset=True, dtype=np.float64
)
return self
def _calculate_smoothing(self, x) -> np.ndarray:
return savgol_filter(
x,
self.window_size,
self.polynomial_order,
deriv=0,
axis=0,
mode=self.mode,
)