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

Code before:
        k = 6
        x[:, k] = np.nan
        y = np.delete(x, k, axis=1)
        corx, px = stats.spearmanr(x, nan_policy='omit')
        cory, py = stats.spearmanr(y)
        corx = np.delete(np.delete(corx, k, axis=1), k, axis=0)
        px = np.delete(np.delete(px, k, axis=1), k, axis=0)
        assert_allclose(corx, cory, atol=1e-14)
        assert_allclose(px, py, atol=1e-14)

    def test_nan_policy_bug_12411(self):
        m = 5
        n = 10
        x = self.rng.standard_normal((m, n))
        x[1, 0] = np.nan
        x[3, -1] = np.nan
        corr, pvalue = stats.spearmanr(x, axis=1, nan_policy="propagate")
        res = [[stats.spearmanr(x[i, :], x[j, :]).statistic for i in range(m)]
               for j in range(m)]
        assert_allclose(corr, res)

    @pytest.mark.parametrize("axis", [0, 1])
    def test_constant_input_gh22816(self, axis):
        X = np.zeros((10, 4))
        X[:,0] = np.arange(0, 10)
        X[:,1] = np.arange(0, 10)

        corr = np.array([[1., 1., np.nan, np.nan],
                         [1., 1., np.nan, np.nan],
                         [np.nan, np.nan, np.nan, np.nan],
                         [np.nan, np.nan, np.nan, np.nan]])
        if axis == 1:
            X = X.T
        with pytest.warns(stats._warnings_errors.ConstantInputWarning):
            res = stats.spearmanr(X, axis=axis)
        assert_allclose(res.statistic, corr, equal_nan=True)

    def test_sXX(self):
        y = stats.spearmanr(X,X)
        r = y[0]
        assert_approx_equal(r,1.0)

    def test_sXBIG(self):
        y = stats.spearmanr(X,BIG)
        r = y[0]
        assert_approx_equal(r,1.0)

    def test_sXLITTLE(self):
        y = stats.spearmanr(X,LITTLE)
        r = y[0]
        assert_approx_equal(r,1.0)

Review comment (suggestion):
```suggestion
        X[:, 2] = np.arange(0, 10)
        X[:, 3] = -np.arange(0, 10)
```

Code after:
        k = 6
        x[:, k] = np.nan
        y = np.delete(x, k, axis=1)
        corx, px = stats.spearmanr(x, nan_policy='omit')
        cory, py = stats.spearmanr(y)
        corx = np.delete(np.delete(corx, k, axis=1), k, axis=0)
        px = np.delete(np.delete(px, k, axis=1), k, axis=0)
        assert_allclose(corx, cory, atol=1e-14)
        assert_allclose(px, py, atol=1e-14)

    def test_nan_policy_bug_12411(self):
        m = 5
        n = 10
        x = self.rng.standard_normal((m, n))
        x[1, 0] = np.nan
        x[3, -1] = np.nan
        corr, pvalue = stats.spearmanr(x, axis=1, nan_policy="propagate")
        res = [[stats.spearmanr(x[i, :], x[j, :]).statistic for i in range(m)]
               for j in range(m)]
        assert_allclose(corr, res)

    @pytest.mark.parametrize("axis", [0, 1])
    def test_constant_input_gh22816(self, axis):
        X = np.zeros((10, 4))
        X[:, 2] = np.arange(0, 10)
        X[:, 3] = -np.arange(0, 10)

        corr = np.array([[np.nan, np.nan, np.nan, np.nan],
                         [np.nan, np.nan, np.nan, np.nan],
                         [np.nan, np.nan, 1., -1.],
                         [np.nan, np.nan, -1., 1.]])
        if axis == 1:
            X = X.T
        with pytest.warns(stats._warnings_errors.ConstantInputWarning):
            res = stats.spearmanr(X, axis=axis)
        assert_allclose(res.statistic, corr)
        assert_allclose(res.pvalue, 0*corr, atol=1e-50)

    def test_sXX(self):
        y = stats.spearmanr(X,X)
        r = y[0]
        assert_approx_equal(r,1.0)

    def test_sXBIG(self):
        y = stats.spearmanr(X,BIG)
        r = y[0]
        assert_approx_equal(r,1.0)

    def test_sXLITTLE(self):
        y = stats.spearmanr(X,LITTLE)
        r = y[0]