Skip to content

SequentialTableTransformer

Bases: InvertibleTableTransformer

The SequentialTableTransformer transforms a table using multiple transformers in sequence.

Parameters:

Name Type Description Default
transformers list[TableTransformer]

The list of transformers used to transform the table. Used in the order as they are supplied in the list.

required

Methods:

Name Description
fit

Fits all the transformers in order.

fit_and_transform

Learn a transformation for a set of columns in a table and apply the learned transformation to the same table.

inverse_transform

Inversely transforms the table using all the transformers sequentially in inverse order.

transform

Transform the table using all the transformers sequentially.

Attributes:

Name Type Description
is_fitted bool

Whether the transformer is fitted.

Source code in src/safeds/data/tabular/transformation/_sequential_table_transformer.py
class SequentialTableTransformer(InvertibleTableTransformer):
    """
    The SequentialTableTransformer transforms a table using multiple transformers in sequence.

    Parameters
    ----------
    transformers:
        The list of transformers used to transform the table. Used in the order as they are supplied in the list.
    """

    def __init__(
        self,
        transformers: list[TableTransformer],
    ) -> None:
        super().__init__(None)

        # Check if transformers actually contains any transformers.
        if transformers is None or len(transformers) == 0:
            warn(
                "transformers should contain at least 1 transformer",
                UserWarning,
                stacklevel=2,
            )

        # Parameters
        self._transformers: list[TableTransformer] = transformers

        # Internal State
        self._is_fitted: bool = False

    def __hash__(self) -> int:
        return _structural_hash(
            super().__hash__(),
            self._transformers,
            self._is_fitted,
        )

    @property
    def is_fitted(self) -> bool:
        """Whether the transformer is fitted."""
        return self._is_fitted

    def fit(self, table: Table) -> SequentialTableTransformer:
        """
        Fits all the transformers in order.

        Parameters
        ----------
        table:
            The table used to fit the transformers.

        Returns
        -------
        fitted_transformer:
            The fitted transformer.

        Raises
        ------
        ValueError
            Raises a ValueError if the table has no rows.
        """
        if table.row_count == 0:
            raise ValueError("The SequentialTableTransformer cannot be fitted because the table contains 0 rows.")

        current_table: Table = table
        fitted_transformers: list[TableTransformer] = []

        for transformer in self._transformers:
            fitted_transformer = transformer.fit(current_table)
            fitted_transformers.append(fitted_transformer)
            current_table = fitted_transformer.transform(current_table)

        result: SequentialTableTransformer = SequentialTableTransformer(
            transformers=fitted_transformers,
        )

        result._is_fitted = True
        return result

    def transform(self, table: Table) -> Table:
        """
        Transform the table using all the transformers sequentially.

        Might change the order and type of columns based on the transformers used.

        Parameters
        ----------
        table:
            The table to be transformed.

        Returns
        -------
        transformed_table:
            The transformed table.

        Raises
        ------
        NotFittedError
            If the transformer has not been fitted yet.
        """
        if not self._is_fitted:
            raise NotFittedError(kind="transformer")

        current_table: Table = table
        for transformer in self._transformers:
            current_table = transformer.transform(current_table)

        return current_table

    def inverse_transform(self, transformed_table: Table) -> Table:
        """
        Inversely transforms the table using all the transformers sequentially in inverse order.

        Might change the order and type of columns base on the transformers used.

        Parameters
        ----------
        transformed_table:
            The table to be transformed back.

        Returns
        -------
        original_table:
            The original table.

        Raises
        ------
        NotFittedError
            If the transformer has not been fitted yet.
        NotInvertibleError
            If the transformer is not invertible.
        """
        if not self._is_fitted:
            raise NotFittedError(kind="transformer")

        # sequentially inverse-transform the table with all transformers, working from the back of the list forwards.
        current_table: Table = transformed_table
        for transformer in reversed(self._transformers):
            # check if transformer is invertible
            if not (isinstance(transformer, InvertibleTableTransformer)):
                raise NotInvertibleError(transformer)
            current_table = transformer.inverse_transform(current_table)

        return current_table

is_fitted

Whether the transformer is fitted.

fit

Fits all the transformers in order.

Parameters:

Name Type Description Default
table Table

The table used to fit the transformers.

required

Returns:

Name Type Description
fitted_transformer SequentialTableTransformer

The fitted transformer.

Raises:

Type Description
ValueError

Raises a ValueError if the table has no rows.

Source code in src/safeds/data/tabular/transformation/_sequential_table_transformer.py
def fit(self, table: Table) -> SequentialTableTransformer:
    """
    Fits all the transformers in order.

    Parameters
    ----------
    table:
        The table used to fit the transformers.

    Returns
    -------
    fitted_transformer:
        The fitted transformer.

    Raises
    ------
    ValueError
        Raises a ValueError if the table has no rows.
    """
    if table.row_count == 0:
        raise ValueError("The SequentialTableTransformer cannot be fitted because the table contains 0 rows.")

    current_table: Table = table
    fitted_transformers: list[TableTransformer] = []

    for transformer in self._transformers:
        fitted_transformer = transformer.fit(current_table)
        fitted_transformers.append(fitted_transformer)
        current_table = fitted_transformer.transform(current_table)

    result: SequentialTableTransformer = SequentialTableTransformer(
        transformers=fitted_transformers,
    )

    result._is_fitted = True
    return result

fit_and_transform

Learn a transformation for a set of columns in a table and apply the learned transformation to the same table.

Note: Neither this transformer nor the given table are modified.

Parameters:

Name Type Description Default
table Table

The table used to fit the transformer. The transformer is then applied to this table.

required

Returns:

Name Type Description
fitted_transformer Self

The fitted transformer.

transformed_table Table

The transformed table.

Source code in src/safeds/data/tabular/transformation/_table_transformer.py
def fit_and_transform(self, table: Table) -> tuple[Self, Table]:
    """
    Learn a transformation for a set of columns in a table and apply the learned transformation to the same table.

    **Note:** Neither this transformer nor the given table are modified.

    Parameters
    ----------
    table:
        The table used to fit the transformer. The transformer is then applied to this table.

    Returns
    -------
    fitted_transformer:
        The fitted transformer.
    transformed_table:
        The transformed table.
    """
    fitted_transformer = self.fit(table)
    transformed_table = fitted_transformer.transform(table)
    return fitted_transformer, transformed_table

inverse_transform

Inversely transforms the table using all the transformers sequentially in inverse order.

Might change the order and type of columns base on the transformers used.

Parameters:

Name Type Description Default
transformed_table Table

The table to be transformed back.

required

Returns:

Name Type Description
original_table Table

The original table.

Raises:

Type Description
NotFittedError

If the transformer has not been fitted yet.

NotInvertibleError

If the transformer is not invertible.

Source code in src/safeds/data/tabular/transformation/_sequential_table_transformer.py
def inverse_transform(self, transformed_table: Table) -> Table:
    """
    Inversely transforms the table using all the transformers sequentially in inverse order.

    Might change the order and type of columns base on the transformers used.

    Parameters
    ----------
    transformed_table:
        The table to be transformed back.

    Returns
    -------
    original_table:
        The original table.

    Raises
    ------
    NotFittedError
        If the transformer has not been fitted yet.
    NotInvertibleError
        If the transformer is not invertible.
    """
    if not self._is_fitted:
        raise NotFittedError(kind="transformer")

    # sequentially inverse-transform the table with all transformers, working from the back of the list forwards.
    current_table: Table = transformed_table
    for transformer in reversed(self._transformers):
        # check if transformer is invertible
        if not (isinstance(transformer, InvertibleTableTransformer)):
            raise NotInvertibleError(transformer)
        current_table = transformer.inverse_transform(current_table)

    return current_table

transform

Transform the table using all the transformers sequentially.

Might change the order and type of columns based on the transformers used.

Parameters:

Name Type Description Default
table Table

The table to be transformed.

required

Returns:

Name Type Description
transformed_table Table

The transformed table.

Raises:

Type Description
NotFittedError

If the transformer has not been fitted yet.

Source code in src/safeds/data/tabular/transformation/_sequential_table_transformer.py
def transform(self, table: Table) -> Table:
    """
    Transform the table using all the transformers sequentially.

    Might change the order and type of columns based on the transformers used.

    Parameters
    ----------
    table:
        The table to be transformed.

    Returns
    -------
    transformed_table:
        The transformed table.

    Raises
    ------
    NotFittedError
        If the transformer has not been fitted yet.
    """
    if not self._is_fitted:
        raise NotFittedError(kind="transformer")

    current_table: Table = table
    for transformer in self._transformers:
        current_table = transformer.transform(current_table)

    return current_table