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

Code before:
        assert_approx_equal(stat_dist, 0.163, significant=1)
        assert_approx_equal(pvalue_dist, 0.001, significant=1)

    @pytest.mark.fail_slow(20)  # all other tests are XSLOW; we need at least one to run
    @pytest.mark.slow
    def test_pvalue_literature(self):
        # generate x and y
        x, y = self._simulations(samps=100, dims=1, sim_type="linear")

        # test stat and pvalue
        _, pvalue, _ = stats.multiscale_graphcorr(x, y, random_state=1)
        assert_allclose(pvalue, 1/1001)

    def test_zero_distance_matrix_index_error(self):
        # regression test for zero distance
        # matrix IndexError issue; see gh-19769
        n = 6
        x = np.arange(n).reshape(-1, 1)

        # valid distance matrix
        distx = cdist(x, x)

        # degenerate all-zero distance matrix
        disty = np.zeros((n, n))

        stats.multiscale_graphcorr(distx, disty, compute_distance=None)
    
    @pytest.mark.xslow
    def test_alias(self):
        # generate x and y
        x, y = self._simulations(samps=100, dims=1, sim_type="linear")

        res = stats.multiscale_graphcorr(x, y, random_state=1)
        assert_equal(res.stat, res.statistic)


Review comment (bug):
I think it is not too rare to write tests that basically check for the absence of an error, but I suppose it might be nice if we could assert something about the output here. From some local checking, most of the values in the returned `MGCResult` are zeros in this case--if that makes intuitive sense maybe an assertion could be made on one of them for a slightly more robust check on behavior as well as the absence of error.

Beyond that, I did check locally that the test fails before/pass after the patch. May be good for a `stats` regular to check it as well, especially to ensure that the output result for this case makes sense.

Code after:
        assert_approx_equal(stat_dist, 0.163, significant=1)
        assert_approx_equal(pvalue_dist, 0.001, significant=1)

    @pytest.mark.fail_slow(20)  # all other tests are XSLOW; we need at least one to run
    @pytest.mark.slow
    def test_pvalue_literature(self):
        # generate x and y
        x, y = self._simulations(samps=100, dims=1, sim_type="linear")

        # test stat and pvalue
        _, pvalue, _ = stats.multiscale_graphcorr(x, y, random_state=1)
        assert_allclose(pvalue, 1/1001)

    def test_zero_distance_matrix_index_error(self):
        # regression test for zero distance
        # matrix IndexError issue; see gh-19769
        n = 6
        x = np.arange(n).reshape(-1, 1)

        # valid distance matrix
        distx = cdist(x, x)

        # degenerate all-zero distance matrix
        disty = np.zeros((n, n))

        # test stat and pvalue
        res = stats.multiscale_graphcorr(distx, disty, compute_distance=None)
        assert_allclose(res.statistic, 0.0)
        assert_allclose(res.pvalue, 1.0)
    
    @pytest.mark.xslow
    def test_alias(self):
        # generate x and y
        x, y = self._simulations(samps=100, dims=1, sim_type="linear")

        res = stats.multiscale_graphcorr(x, y, random_state=1)
        assert_equal(res.stat, res.statistic)