Repository: scipy/scipy
File: scipy/stats/tests/test_axis_nan_policy.py (Python)

Code before:
                    yield np.zeros(combo)

        # yield all possible combinations of small samples
        gens = [small_sample_generator(n_dims) for i in range(n_samples)]
        yield from product(*gens)

    n_dims = [1, 2, 3]
    for samples in small_data_generator(n_samples, n_dims):

        # this test is only for arrays of zero size
        if not any(sample.size == 0 for sample in samples):
            continue

        max_axis = max(sample.ndim for sample in samples)

        # need to test for all valid values of `axis` parameter, too
        for axis in range(-max_axis, max_axis):

            try:
                # After broadcasting, all arrays are the same shape, so
                # the shape of the output should be the same as a single-
                # sample statistic. Use np.mean as a reference.
                concat = stats._stats_py._broadcast_concatenate(samples, axis,
                                                                paired=paired)
                with np.testing.suppress_warnings() as sup:
                    sup.filter(RuntimeWarning, "Mean of empty slice.")
                    sup.filter(RuntimeWarning, "invalid value encountered")
                    expected = np.mean(concat, axis=axis) * np.nan
                    mask = np.isnan(expected)
                    expected = [np.asarray(expected.copy()) for i in range(n_outputs)]

                if hypotest in empty_special_case_funcs:
                    empty_val = hypotest(*([[]]*len(samples)), *args, **kwds)
                    empty_val = list(unpacker(empty_val))
                    for i in range(n_outputs):
                        expected[i][mask] = empty_val[i]

                if expected[0].size and hypotest not in too_small_special_case_funcs:
                    message = (too_small_1d_not_omit if max_axis == 1
                               else too_small_nd_not_omit)
                    with pytest.warns(SmallSampleWarning, match=message):
                        res = hypotest(*samples, *args, axis=axis, **kwds)
                else:
                    with np.testing.suppress_warnings() as sup:
                        # f_oneway special case
                        sup.filter(SmallSampleWarning, "all input arrays have length 1")
                        res = hypotest(*samples, *args, axis=axis, **kwds)
                res = unpacker(res)

                for i in range(n_outputs):
                    assert_equal(res[i], expected[i])

Review comment (suggestion):
I was wondering why this one was needed despite its absence in gh-23614, but Matt took care of some more cases in gh-23619, including this one.

I don't know why I ended up having to use a slightly different approach to the punctuation matching, but this is not really important, I'll just backport it since CI passing now, on the off chance we need `1.16.3` at some point.

Code after:
                    yield np.zeros(combo)

        # yield all possible combinations of small samples
        gens = [small_sample_generator(n_dims) for i in range(n_samples)]
        yield from product(*gens)

    n_dims = [1, 2, 3]
    for samples in small_data_generator(n_samples, n_dims):

        # this test is only for arrays of zero size
        if not any(sample.size == 0 for sample in samples):
            continue

        max_axis = max(sample.ndim for sample in samples)

        # need to test for all valid values of `axis` parameter, too
        for axis in range(-max_axis, max_axis):

            try:
                # After broadcasting, all arrays are the same shape, so
                # the shape of the output should be the same as a single-
                # sample statistic. Use np.mean as a reference.
                concat = stats._stats_py._broadcast_concatenate(samples, axis,
                                                                paired=paired)
                with np.testing.suppress_warnings() as sup:
                    sup.filter(RuntimeWarning, r"Mean of empty slice\.?")
                    sup.filter(RuntimeWarning, "invalid value encountered")
                    expected = np.mean(concat, axis=axis) * np.nan
                    mask = np.isnan(expected)
                    expected = [np.asarray(expected.copy()) for i in range(n_outputs)]

                if hypotest in empty_special_case_funcs:
                    empty_val = hypotest(*([[]]*len(samples)), *args, **kwds)
                    empty_val = list(unpacker(empty_val))
                    for i in range(n_outputs):
                        expected[i][mask] = empty_val[i]

                if expected[0].size and hypotest not in too_small_special_case_funcs:
                    message = (too_small_1d_not_omit if max_axis == 1
                               else too_small_nd_not_omit)
                    with pytest.warns(SmallSampleWarning, match=message):
                        res = hypotest(*samples, *args, axis=axis, **kwds)
                else:
                    with np.testing.suppress_warnings() as sup:
                        # f_oneway special case
                        sup.filter(SmallSampleWarning, "all input arrays have length 1")
                        res = hypotest(*samples, *args, axis=axis, **kwds)
                res = unpacker(res)

                for i in range(n_outputs):
                    assert_equal(res[i], expected[i])