Skip to content

transformer

NToOneTransformer

Bases: BaseEstimator, TransformerMixin

Interface for N-to-one transformation functions.

Source code in eki_mmo_equations/transformer.py
class NToOneTransformer(BaseEstimator, TransformerMixin):
    """Interface for N-to-one transformation functions."""

    def __init__(self, ignore_input_indexes: Optional[list[int]] = None) -> None:
        """Interface for N-to-one transformation functions.

        Args:
            ignore_input_indexes (Optional[List[int]], optional): Indexes of the data in series to ignore when
                computing the repartition.

        Attributes:
            ignore_input_indexes (List[int]): Indexes of the data in series to ignore when
                computing the repartition.
        """
        self.ignore_input_indexes: list[int] = ignore_input_indexes if ignore_input_indexes is not None else []

    @property
    def is_inversable_derivable_transformer(self) -> bool:
        return False

    @property
    def is_observation_related_transformer(self) -> bool:
        return False

    @property
    def is_one_to_one_transformer(self) -> bool:
        return False

    @property
    def is_n_to_one_transformer(self) -> bool:
        return True

    @property
    def is_linear(self) -> bool:
        return False

    @property
    def is_identity(self) -> bool:
        return False

    @property
    def parameters(self) -> Dict[str, float]:
        raise NotImplementedError

    # ------- METHODS -------

    def fit(self, series: List[np.ndarray], y=None):
        self.check_params(series)

        return self

    def transform(self, series: List[np.ndarray], copy=False) -> np.ndarray:
        if copy:
            series = series.copy()

        return series

    def repartition(self, series: List[np.ndarray]) -> List[np.ndarray]:
        raise NotImplementedError

    def indexed_cumulative_sum(self, serie: np.ndarray) -> np.ndarray:
        return self.transform(serie, *self.parameters)

    # ------- TRANSFORMERS -------

    #    @staticmethod
    #    def _transformer(series: List[np.ndarray]) -> np.ndarray:
    #        raise NotImplementedError

    # ------- CHECKERS -------

    def check_params(self, series: List[np.ndarray]):
        """Check if parameters respect their application scope."""
        raise NotImplementedError

__init__(ignore_input_indexes=None)

Interface for N-to-one transformation functions.

Parameters:

Name Type Description Default
ignore_input_indexes Optional[List[int]]

Indexes of the data in series to ignore when computing the repartition.

None

Attributes:

Name Type Description
ignore_input_indexes List[int]

Indexes of the data in series to ignore when computing the repartition.

Source code in eki_mmo_equations/transformer.py
def __init__(self, ignore_input_indexes: Optional[list[int]] = None) -> None:
    """Interface for N-to-one transformation functions.

    Args:
        ignore_input_indexes (Optional[List[int]], optional): Indexes of the data in series to ignore when
            computing the repartition.

    Attributes:
        ignore_input_indexes (List[int]): Indexes of the data in series to ignore when
            computing the repartition.
    """
    self.ignore_input_indexes: list[int] = ignore_input_indexes if ignore_input_indexes is not None else []

check_params(series)

Check if parameters respect their application scope.

Source code in eki_mmo_equations/transformer.py
def check_params(self, series: List[np.ndarray]):
    """Check if parameters respect their application scope."""
    raise NotImplementedError

OneToOneInversableDerivableTransformer

Bases: OneToOneTransformer

Sub-interface for one-to-one transformation functions that can be inversed and derived.

Source code in eki_mmo_equations/transformer.py
class OneToOneInversableDerivableTransformer(OneToOneTransformer):
    """Sub-interface for one-to-one transformation functions that can be inversed and derived."""

    @property
    def is_inversable_derivable_transformer(self) -> bool:
        return True

    # ------- METHODS -------

    def inverse_transform(self, serie: np.ndarray, copy=False) -> np.ndarray:
        if copy:
            serie = serie.copy()

        return serie

    def derivative_transform(self, serie: np.ndarray, copy=False) -> np.ndarray:
        if copy:
            serie = serie.copy()

        return serie

OneToOneObservationRelatedTransformer

Bases: OneToOneTransformer

Source code in eki_mmo_equations/transformer.py
class OneToOneObservationRelatedTransformer(OneToOneTransformer):
    @property
    def is_observation_related_transformer(self) -> bool:
        return True

    @staticmethod
    def _transformer(*args: Any, **kwargs: Any) -> Any:
        """Transform the serie using the parameters."""
        raise NotImplementedError

    def transform(self, serie: np.ndarray, copy=False) -> np.ndarray:
        serie = super().transform(serie, copy)
        if serie.ndim == 2:
            # Apply the transformer to each row of the 2D array
            return np.array([self._transformer(serie[i, :], **self.parameters) for i in range(serie.shape[1])])
        else:
            return self._transformer(serie, **self.parameters)

OneToOneTransformer

Bases: BaseEstimator, TransformerMixin

Interface for one-to-one transformation functions.

Source code in eki_mmo_equations/transformer.py
class OneToOneTransformer(BaseEstimator, TransformerMixin):
    """Interface for one-to-one transformation functions."""

    @property
    def is_inversable_derivable_transformer(self) -> bool:
        return False

    @property
    def is_observation_related_transformer(self) -> bool:
        return False

    @property
    def is_n_to_one_transformer(self) -> bool:
        return False

    @property
    def is_one_to_one_transformer(self) -> bool:
        return True

    @property
    def is_linear(self) -> bool:
        return False

    @property
    def is_identity(self) -> bool:
        data = deepcopy(TEST_IDENTITY_DATA)
        transformed_data = self.fit_transform(data)
        return np.allclose(data, transformed_data)

    @property
    def parameters(self) -> Dict[str, float]:
        raise NotImplementedError

    # ------- METHODS -------

    def fit(self, serie: np.ndarray, y=None):

        self.check_params(serie)

        return self

    def transform(self, serie: np.ndarray, copy=False) -> np.ndarray:
        if copy:
            serie = serie.copy()

        return serie

    def repartition(self, serie: np.ndarray) -> np.ndarray:
        return np.array([1] * len(serie))

    def indexed_cumulative_sum(self, serie: np.ndarray) -> np.ndarray:
        return self.transform(serie, *self.parameters)

    # ------- TRANSFORMERS -------

    #    @staticmethod
    #    def _transformer(serie: np.ndarray) -> np.ndarray:
    #        raise NotImplementedError

    # ------- CHECKERS -------

    def check_params(self, serie: np.ndarray):
        """Check if parameters respect their application scope."""
        raise NotImplementedError

    def to_dict(self) -> Dict[str, Dict[str, Any]]:
        """
        Return transformation name with parameterss.

        Returns:
            Dict[str, Dict[str, Any]]: return transformation name with parameterss.
            Example : Lag(1).to_dict() returns {'lag':{'lag':1}}
        """
        return {underscore(self.__class__.__name__): self.__dict__}

check_params(serie)

Check if parameters respect their application scope.

Source code in eki_mmo_equations/transformer.py
def check_params(self, serie: np.ndarray):
    """Check if parameters respect their application scope."""
    raise NotImplementedError

to_dict()

Return transformation name with parameterss.

Returns:

Name Type Description
Dict[str, Dict[str, Any]]

Dict[str, Dict[str, Any]]: return transformation name with parameterss.

Example Dict[str, Dict[str, Any]]

Lag(1).to_dict() returns {'lag':{'lag':1}}

Source code in eki_mmo_equations/transformer.py
def to_dict(self) -> Dict[str, Dict[str, Any]]:
    """
    Return transformation name with parameterss.

    Returns:
        Dict[str, Dict[str, Any]]: return transformation name with parameterss.
        Example : Lag(1).to_dict() returns {'lag':{'lag':1}}
    """
    return {underscore(self.__class__.__name__): self.__dict__}

TimeRelatedTransformer

Interface for time related transformation functions.

Source code in eki_mmo_equations/transformer.py
class TimeRelatedTransformer:
    """Interface for time related transformation functions."""

    @property
    def is_time_related_transformer(self) -> bool:
        return True

    @property
    def parameters(self) -> Dict[str, float]:
        raise NotImplementedError

    # ------- METHODS -------

    def fit(self, dates: npt.NDArray[np.datetime64]):
        self.check_params()
        return self

    def transform(self, dates: npt.NDArray[np.datetime64]) -> npt.NDArray[np.float64]:
        pass

    # ------- CHECKERS -------

    def check_params(self):
        """Check if parameters respect their application scope."""
        raise NotImplementedError

check_params()

Check if parameters respect their application scope.

Source code in eki_mmo_equations/transformer.py
def check_params(self):
    """Check if parameters respect their application scope."""
    raise NotImplementedError