Skip to content

check_weighter_resolved_array_validation

yohou.testing.check_weighter_resolved_array_validation(weighter, key)

Check resolved-array validation rejects NaN/negative/inf/all-zero.

Drives _resolve_weighter_to_array (and _validate_weight_array) with weighters whose resolution yields each invalid case and asserts a ValueError is raised.

Parameters

Name Type Description Default
weighter BaseWeighter

Weighter instance (unused; the check is family-level).

required
key Series

Key series with which to resolve the stub weighters.

required

Raises

Type Description
AssertionError

If any invalid resolved array is accepted.

Source Code

Source code in src/yohou/testing/weighter.py
def check_weighter_resolved_array_validation(weighter, key: pl.Series) -> None:
    """Check resolved-array validation rejects NaN/negative/inf/all-zero.

    Drives ``_resolve_weighter_to_array`` (and ``_validate_weight_array``) with
    weighters whose resolution yields each invalid case and asserts a
    ``ValueError`` is raised.

    Parameters
    ----------
    weighter : BaseWeighter
        Weighter instance (unused; the check is family-level).
    key : pl.Series
        Key series with which to resolve the stub weighters.

    Raises
    ------
    AssertionError
        If any invalid resolved array is accepted.

    """
    n = len(key)

    class _BadWeighter(BaseWeighter):
        def __init__(self, values: np.ndarray) -> None:
            self._values = values

        def compute_weights(self, key: pl.Series, group_name: str | None = None) -> pl.Series:
            return pl.Series(self._values, dtype=pl.Float64).alias("weight")

    cases = {
        "NaN": np.array([np.nan] + [1.0] * (n - 1)),
        "negative": np.array([-1.0] + [1.0] * (n - 1)),
        "infinite": np.array([np.inf] + [1.0] * (n - 1)),
        "all-zero": np.zeros(n),
    }
    for label, values in cases.items():
        try:
            _resolve_weighter_to_array(_BadWeighter(values), key)
        except ValueError:
            continue
        raise AssertionError(f"resolved-array validation accepted {label} weights; expected ValueError")