Skip to content

check_transformers_unfitted_stateless

yohou.testing.transformer.check_transformers_unfitted_stateless(transformer, X)

Check stateless transformers work without fitting.

For transformers with observation_horizon = 0, transform should work on unfitted instances (similar to sklearn's FunctionTransformer).

Parameters

Name Type Description Default
transformer BaseTransformer

Unfitted transformer with observation_horizon = 0

required
X DataFrame

Test data

required

Raises

Type Description
AssertionError

If stateless transformer requires fitting

Source Code

Show/Hide source
def check_transformers_unfitted_stateless(transformer, X: pl.DataFrame) -> None:
    """Check stateless transformers work without fitting.

    For transformers with observation_horizon = 0, transform should
    work on unfitted instances (similar to sklearn's FunctionTransformer).

    Parameters
    ----------
    transformer : BaseTransformer
        Unfitted transformer with observation_horizon = 0
    X : pl.DataFrame
        Test data

    Raises
    ------
    AssertionError
        If stateless transformer requires fitting

    """
    transformer_clone = clone(transformer)

    # Check if it's actually stateless
    try:
        horizon = transformer_clone.observation_horizon
        if horizon != 0:
            # Not stateless, skip this check
            return
    except NotFittedError:
        # Can't determine, skip
        return

    # Should work without fit() for stateless transformers
    try:
        X_trans = transformer_clone.transform(X)
        assert X_trans.shape[0] == X.shape[0], (
            f"Stateless transformer changed n_samples: {X.shape[0]} -> {X_trans.shape[0]}"
        )
    except NotFittedError as e:
        raise AssertionError(
            f"{transformer.__class__.__name__} claims to be stateless "
            f"(observation_horizon=0) but raises NotFittedError. "
            f"Stateless transformers should work without fitting."
        ) from e