Repository: scipy/scipy
File: scipy/signal/tests/test_filter_design.py (Python)

Code before:
              25.7584699322366, -49.7319214483238,
              60.2667361289181, -46.9399590980486,
              22.9474798808461, -6.43799381299034,
              0.793651554625368]
        b2, a2 = map(xp.asarray, (b2, a2))
        xp_assert_close(b, b2, atol=1e-10, rtol=5e-5)
        xp_assert_close(a, a2, atol=1e-10, rtol=5e-5)

    def test_fs_validation(self):
        with pytest.raises(ValueError, match="Sampling.*single scalar"):
            gammatone(440, 'iir', fs=np.asarray([10, 20]))


@make_xp_test_case(findfreqs)
class TestFindFreqs:
    @pytest.mark.parametrize("kind", ["ba", "zp"])
    def test_findfreqs_log_spacing(self, xp, kind):
        N = 30
        w = findfreqs(xp.asarray([1.2]), xp.asarray([3.6, 9.8]), N=N, kind=kind)
        ratio = xp.diff(xp.log10(w))
        xp_assert_close(ratio, ratio[0]*xp.ones(N - 1, dtype=xp_default_dtype(xp)))

    def test_findfreqs_ba_zp_equiv(self, xp):
        num, den = [1.4, 2], [1, 3.8, 2]
        zeros = xp.asarray(np.roots(num), dtype=xp_default_dtype(xp))
        poles = xp.asarray(np.roots(den), dtype=xp_default_dtype(xp))

        ba = findfreqs(xp.asarray(num), xp.asarray(den), N=30, kind="ba")
        zp = findfreqs(zeros, poles, N=30, kind="zp")

        xp_assert_close(ba, zp)


Review comment (bug):
Sorry, the old way of doing things still has a lot of intertia. What I'm recommending is to not let 'SCIPY_DEFAULT_DTYPE` control input and expected output dtypes in tests at all. The idea shouldn't be to force testing for both 32 bit and 64 bit with the same test, but to catch bugs that can pop up with torch when functions use arrary creation functions without explicit dtypes, like I found here #23747. 

What I'm recommending now is to require explictly setting fixed dtypes, and deliberately testing on 32 bit if desired, not having it happen because of ‘SCIPY_DEFAULT_DTYPE`. 

Code after:
              25.7584699322366, -49.7319214483238,
              60.2667361289181, -46.9399590980486,
              22.9474798808461, -6.43799381299034,
              0.793651554625368]
        b2, a2 = map(xp.asarray, (b2, a2))
        xp_assert_close(b, b2, atol=1e-10, rtol=5e-5)
        xp_assert_close(a, a2, atol=1e-10, rtol=5e-5)

    def test_fs_validation(self):
        with pytest.raises(ValueError, match="Sampling.*single scalar"):
            gammatone(440, 'iir', fs=np.asarray([10, 20]))


@make_xp_test_case(findfreqs)
class TestFindFreqs:
    @pytest.mark.parametrize("kind", ["ba", "zp"])
    def test_findfreqs_log_spacing(self, xp, kind):
        N = 30
        w = findfreqs(xp.asarray([1.2], dtype=xp.float64),
                      xp.asarray([3.6, 9.8], dtype=xp.float64), N=N, kind=kind)
        ratio = xp.diff(xp.log10(w))
        xp_assert_close(ratio, ratio[0]*xp.ones(N - 1, dtype=xp.float64))

    def test_findfreqs_ba_zp_equiv(self, xp):
        num, den = [1.4, 2], [1, 3.8, 2]
        zeros = xp.asarray(np.roots(num))
        poles = xp.asarray(np.roots(den))

        ba = findfreqs(xp.asarray(num, dtype=xp.float64),
                       xp.asarray(den, dtype=xp.float64), N=30, kind="ba")
        zp = findfreqs(zeros, poles, N=30, kind="zp")

        xp_assert_close(ba, zp)