Repository: scipy/scipy
File: scipy/stats/_stats_py.py (Python)

Code before:


F_onewayResult = namedtuple('F_onewayResult', ('statistic', 'pvalue'))


def _f_oneway_is_too_small(samples, kwargs=None, axis=-1):
    message = f"At least two samples are required; got {len(samples)}."
    if len(samples) < 2:
        raise TypeError(message)

    # Check this after forming alldata, so shape errors are detected
    # and reported before checking for 0 length inputs.
    if any(sample.shape[axis] == 0 for sample in samples):
        return True

    # Must have at least one group with length greater than 1.
    if all(sample.shape[axis] == 1 for sample in samples):
        msg = ('all input arrays have length 1.  f_oneway requires that at '
               'least one input has length greater than 1.')
        warnings.warn(SmallSampleWarning(msg), stacklevel=2)
        return True

    return False


@xp_capabilities(jax_jit=False)
@_axis_nan_policy_factory(
    F_onewayResult, n_samples=None, too_small=_f_oneway_is_too_small)
def f_oneway(*samples, axis=0, equal_var=True):
    """Perform one-way ANOVA.

    The one-way ANOVA tests the null hypothesis that two or more groups have
    the same population mean.  The test is applied to samples from two or
    more groups, possibly with differing sizes.

    Parameters
    ----------
    sample1, sample2, ... : array_like
        The sample measurements for each group.  There must be at least
        two arguments.  If the arrays are multidimensional, then all the
        dimensions of the array must be the same except for `axis`.
    axis : int, optional
        Axis of the input arrays along which the test is applied.
        Default is 0.
    equal_var: bool, optional
        If True (default), perform a standard one-way ANOVA test that
        assumes equal population variances [2]_.
        If False, perform Welch's ANOVA test, which does not assume
        equal population variances [4]_.

        .. versionadded:: 1.16.0

Review comment (bug):
To fix CI failure:
```suggestion
@xp_capabilities(jax_jit=False, cpu_only=True, exceptions=['cupy'])
```
`fdtrc` is not native to all backends.

I'll wait for other suggestions before committing.

Code after:
    return xp.mean(trimmed, axis=axis)


F_onewayResult = namedtuple('F_onewayResult', ('statistic', 'pvalue'))


def _f_oneway_is_too_small(samples, kwargs=None, axis=-1):
    message = f"At least two samples are required; got {len(samples)}."
    if len(samples) < 2:
        raise TypeError(message)

    # Check this after forming alldata, so shape errors are detected
    # and reported before checking for 0 length inputs.
    if any(sample.shape[axis] == 0 for sample in samples):
        return True

    # Must have at least one group with length greater than 1.
    if all(sample.shape[axis] == 1 for sample in samples):
        msg = ('all input arrays have length 1.  f_oneway requires that at '
               'least one input has length greater than 1.')
        warnings.warn(SmallSampleWarning(msg), stacklevel=2)
        return True

    return False


@xp_capabilities(jax_jit=False, cpu_only=True, exceptions=['cupy'])
@_axis_nan_policy_factory(
    F_onewayResult, n_samples=None, too_small=_f_oneway_is_too_small)
def f_oneway(*samples, axis=0, equal_var=True):
    """Perform one-way ANOVA.

    The one-way ANOVA tests the null hypothesis that two or more groups have
    the same population mean.  The test is applied to samples from two or
    more groups, possibly with differing sizes.

    Parameters
    ----------
    sample1, sample2, ... : array_like
        The sample measurements for each group.  There must be at least
        two arguments.  If the arrays are multidimensional, then all the
        dimensions of the array must be the same except for `axis`.
    axis : int, optional
        Axis of the input arrays along which the test is applied.
        Default is 0.
    equal_var: bool, optional
        If True (default), perform a standard one-way ANOVA test that
        assumes equal population variances [2]_.
        If False, perform Welch's ANOVA test, which does not assume
        equal population variances [4]_.