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

Code before:
        transform = getattr(stats, self.transform)
        normmax = getattr(stats, self.transform+"_normmax")
        special_transform = getattr(special, self.transform, _yeojohnson_transform)

        rng = np.random.default_rng(5908294582456923546)
        x = rng.random(10)
        x[3] = np.nan

        # lmb provided
        lmb = 1.0

        with pytest.raises(ValueError, match="The input contains nan values"):
            transform(x, lmb, nan_policy='raise')

        res = transform(x, lmb, nan_policy='propagate')
        np.testing.assert_allclose(res, special_transform(x, 1.))

        res = transform(x, lmb, nan_policy='omit')
        np.testing.assert_allclose(res, transform(x, lmb, nan_policy='propagate'))

        # lmb not provided
        with pytest.raises(ValueError, match="The input contains nan values"):
            transform(x, nan_policy='raise')

        res = transform(x, nan_policy='propagate')
        np.testing.assert_allclose(res[0], special_transform(x, np.nan))
        np.testing.assert_equal(res[1], np.nan)

        kwarg = {'method': 'mle'} if transform == stats.boxcox else {}
        res = transform(x, nan_policy='omit')
        lmb = normmax(x, nan_policy='omit', **kwarg)
        ref = transform(x, lmb)
        np.testing.assert_allclose(res[0], ref)
        np.testing.assert_allclose(res[1], lmb)


class TestBoxcox(TransformTest):
    transform = 'boxcox'

    def test_fixed_lmbda(self):
        x = _old_loggamma_rvs(5, size=50, random_state=12345) + 5
        xt = stats.boxcox(x, lmbda=1)
        assert_allclose(xt, x - 1)
        xt = stats.boxcox(x, lmbda=-1)
        assert_allclose(xt, 1 - 1/x)

        xt = stats.boxcox(x, lmbda=0)
        assert_allclose(xt, np.log(x))

        # Also test that array_like input works
        xt = stats.boxcox(list(x), lmbda=0)

Review comment (suggestion):
These should all be NaNs, so equality check is OK.
```suggestion
        np.testing.assert_equal(res[0], special_transform(x, np.nan))
```

Code after:
        transform = getattr(stats, self.transform)
        normmax = getattr(stats, f"{self.transform}_normmax")
        special_transform = getattr(special, self.transform, _yeojohnson_transform)

        rng = np.random.default_rng(5908294582456923546)
        x = rng.random(10)
        x[3] = np.nan

        # lmb provided
        lmb = 1.0

        with pytest.raises(ValueError, match="The input contains nan values"):
            transform(x, lmb, nan_policy='raise')

        res = transform(x, lmb, nan_policy='propagate')
        np.testing.assert_allclose(res, special_transform(x, 1.))

        res = transform(x, lmb, nan_policy='omit')
        np.testing.assert_allclose(res, transform(x, lmb, nan_policy='propagate'))

        # lmb not provided
        with pytest.raises(ValueError, match="The input contains nan values"):
            transform(x, nan_policy='raise')

        res = transform(x, nan_policy='propagate')
        np.testing.assert_equal(res[0], special_transform(x, np.nan))
        np.testing.assert_equal(res[1], np.nan)

        kwarg = {'method': 'mle'} if transform == stats.boxcox else {}
        res = transform(x, nan_policy='omit')
        lmb = normmax(x, nan_policy='omit', **kwarg)
        ref = transform(x, lmb)
        np.testing.assert_allclose(res[0], ref)
        np.testing.assert_allclose(res[1], lmb)


class TestBoxcox(TransformTest):
    transform = 'boxcox'

    def test_fixed_lmbda(self):
        x = _old_loggamma_rvs(5, size=50, random_state=12345) + 5
        xt = stats.boxcox(x, lmbda=1)
        assert_allclose(xt, x - 1)
        xt = stats.boxcox(x, lmbda=-1)
        assert_allclose(xt, 1 - 1/x)

        xt = stats.boxcox(x, lmbda=0)
        assert_allclose(xt, np.log(x))

        # Also test that array_like input works
        xt = stats.boxcox(list(x), lmbda=0)