Skip to content

Commit 89f8532

Browse files
authored
Merge pull request scipy#22820 from ev-br/mv_test_order_filter
MAINT: signal: consolidate `order_filter` tests
2 parents cca7c85 + a62a1db commit 89f8532

File tree

2 files changed

+86
-84
lines changed

2 files changed

+86
-84
lines changed

scipy/signal/tests/test_filter_design.py

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
firwin, freqs_zpk, freqs, freqz, freqz_zpk,
2222
gammatone, group_delay, iircomb, iirdesign, iirfilter,
2323
iirnotch, iirpeak, lp2bp, lp2bs, lp2hp, lp2lp, normalize,
24-
medfilt, order_filter,
2524
sos2tf, sos2zpk, sosfreqz, freqz_sos, tf2sos, tf2zpk, zpk2sos,
2625
zpk2tf, bilinear_zpk, lp2lp_zpk, lp2hp_zpk, lp2bp_zpk,
2726
lp2bs_zpk)
2827
from scipy.signal._filter_design import (_cplxreal, _cplxpair, _norm_factor,
2928
_bessel_poly, _bessel_zeros)
3029

30+
3131
try:
3232
import mpmath
3333
except ImportError:
@@ -4493,86 +4493,3 @@ def test_iir_ba_output(self):
44934493
def test_fs_validation(self):
44944494
with pytest.raises(ValueError, match="Sampling.*single scalar"):
44954495
gammatone(440, 'iir', fs=np.array([10, 20]))
4496-
4497-
4498-
class TestOrderFilter:
4499-
def test_doc_example(self):
4500-
x = np.arange(25).reshape(5, 5)
4501-
domain = np.identity(3)
4502-
4503-
# minimum of elements 1,3,9 (zero-padded) on phone pad
4504-
# 7,5,3 on numpad
4505-
expected = np.array(
4506-
[[0., 0., 0., 0., 0.],
4507-
[0., 0., 1., 2., 0.],
4508-
[0., 5., 6., 7., 0.],
4509-
[0., 10., 11., 12., 0.],
4510-
[0., 0., 0., 0., 0.]],
4511-
)
4512-
xp_assert_close(order_filter(x, domain, 0), expected, check_dtype=False)
4513-
4514-
# maximum of elements 1,3,9 (zero-padded) on phone pad
4515-
# 7,5,3 on numpad
4516-
expected = np.array(
4517-
[[6., 7., 8., 9., 4.],
4518-
[11., 12., 13., 14., 9.],
4519-
[16., 17., 18., 19., 14.],
4520-
[21., 22., 23., 24., 19.],
4521-
[20., 21., 22., 23., 24.]],
4522-
)
4523-
xp_assert_close(order_filter(x, domain, 2), expected, check_dtype=False)
4524-
4525-
# and, just to complete the set, median of zero-padded elements
4526-
expected = np.array(
4527-
[[0, 1, 2, 3, 0],
4528-
[5, 6, 7, 8, 3],
4529-
[10, 11, 12, 13, 8],
4530-
[15, 16, 17, 18, 13],
4531-
[0, 15, 16, 17, 18]],
4532-
)
4533-
xp_assert_close(order_filter(x, domain, 1), expected)
4534-
4535-
def test_medfilt_order_filter(self):
4536-
x = np.arange(25).reshape(5, 5)
4537-
4538-
# median of zero-padded elements 1,5,9 on phone pad
4539-
# 7,5,3 on numpad
4540-
expected = np.array(
4541-
[[0, 1, 2, 3, 0],
4542-
[1, 6, 7, 8, 4],
4543-
[6, 11, 12, 13, 9],
4544-
[11, 16, 17, 18, 14],
4545-
[0, 16, 17, 18, 0]],
4546-
)
4547-
xp_assert_close(medfilt(x, 3), expected)
4548-
4549-
xp_assert_close(
4550-
order_filter(x, np.ones((3, 3)), 4),
4551-
expected
4552-
)
4553-
4554-
def test_order_filter_asymmetric(self):
4555-
x = np.arange(25).reshape(5, 5)
4556-
domain = np.array(
4557-
[[1, 1, 0],
4558-
[0, 1, 0],
4559-
[0, 0, 0]],
4560-
)
4561-
4562-
expected = np.array(
4563-
[[0, 0, 0, 0, 0],
4564-
[0, 0, 1, 2, 3],
4565-
[0, 5, 6, 7, 8],
4566-
[0, 10, 11, 12, 13],
4567-
[0, 15, 16, 17, 18]]
4568-
)
4569-
xp_assert_close(order_filter(x, domain, 0), expected)
4570-
4571-
expected = np.array(
4572-
[[0, 0, 0, 0, 0],
4573-
[0, 1, 2, 3, 4],
4574-
[5, 6, 7, 8, 9],
4575-
[10, 11, 12, 13, 14],
4576-
[15, 16, 17, 18, 19]]
4577-
)
4578-
xp_assert_close(order_filter(x, domain, 1), expected)

scipy/signal/tests/test_signaltools.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,91 @@ def test_basic(self, xp):
16381638
expect = xp.asarray([2, 3, 2])
16391639
xp_assert_equal(actual, expect)
16401640

1641+
def test_doc_example(self, xp):
1642+
x = xp.reshape(xp.arange(25, dtype=xp_default_dtype(xp)), (5, 5))
1643+
domain = xp.eye(3, dtype=xp_default_dtype(xp))
1644+
1645+
# minimum of elements 1,3,9 (zero-padded) on phone pad
1646+
# 7,5,3 on numpad
1647+
expected = xp.asarray(
1648+
[[0., 0., 0., 0., 0.],
1649+
[0., 0., 1., 2., 0.],
1650+
[0., 5., 6., 7., 0.],
1651+
[0., 10., 11., 12., 0.],
1652+
[0., 0., 0., 0., 0.]],
1653+
dtype=xp_default_dtype(xp)
1654+
)
1655+
xp_assert_close(signal.order_filter(x, domain, 0), expected)
1656+
1657+
# maximum of elements 1,3,9 (zero-padded) on phone pad
1658+
# 7,5,3 on numpad
1659+
expected = xp.asarray(
1660+
[[6., 7., 8., 9., 4.],
1661+
[11., 12., 13., 14., 9.],
1662+
[16., 17., 18., 19., 14.],
1663+
[21., 22., 23., 24., 19.],
1664+
[20., 21., 22., 23., 24.]],
1665+
)
1666+
xp_assert_close(signal.order_filter(x, domain, 2), expected)
1667+
1668+
# and, just to complete the set, median of zero-padded elements
1669+
expected = xp.asarray(
1670+
[[0, 1, 2, 3, 0],
1671+
[5, 6, 7, 8, 3],
1672+
[10, 11, 12, 13, 8],
1673+
[15, 16, 17, 18, 13],
1674+
[0, 15, 16, 17, 18]],
1675+
dtype=xp_default_dtype(xp)
1676+
)
1677+
xp_assert_close(signal.order_filter(x, domain, 1), expected)
1678+
1679+
@xfail_xp_backends('dask.array', reason='repeat requires an axis')
1680+
@xfail_xp_backends('torch', reason='array-api-compat#292')
1681+
def test_medfilt_order_filter(self, xp):
1682+
x = xp.reshape(xp.arange(25), (5, 5))
1683+
1684+
# median of zero-padded elements 1,5,9 on phone pad
1685+
# 7,5,3 on numpad
1686+
expected = xp.asarray(
1687+
[[0, 1, 2, 3, 0],
1688+
[1, 6, 7, 8, 4],
1689+
[6, 11, 12, 13, 9],
1690+
[11, 16, 17, 18, 14],
1691+
[0, 16, 17, 18, 0]],
1692+
)
1693+
xp_assert_close(signal.medfilt(x, 3), expected)
1694+
1695+
xp_assert_close(
1696+
signal.order_filter(x, xp.ones((3, 3)), 4),
1697+
expected
1698+
)
1699+
1700+
def test_order_filter_asymmetric(self, xp):
1701+
x = xp.reshape(xp.arange(25), (5, 5))
1702+
domain = xp.asarray(
1703+
[[1, 1, 0],
1704+
[0, 1, 0],
1705+
[0, 0, 0]],
1706+
)
1707+
1708+
expected = xp.asarray(
1709+
[[0, 0, 0, 0, 0],
1710+
[0, 0, 1, 2, 3],
1711+
[0, 5, 6, 7, 8],
1712+
[0, 10, 11, 12, 13],
1713+
[0, 15, 16, 17, 18]]
1714+
)
1715+
xp_assert_close(signal.order_filter(x, domain, 0), expected)
1716+
1717+
expected = xp.asarray(
1718+
[[0, 0, 0, 0, 0],
1719+
[0, 1, 2, 3, 4],
1720+
[5, 6, 7, 8, 9],
1721+
[10, 11, 12, 13, 14],
1722+
[15, 16, 17, 18, 19]]
1723+
)
1724+
xp_assert_close(signal.order_filter(x, domain, 1), expected)
1725+
16411726

16421727
@skip_xp_backends(cpu_only=True, exceptions=['cupy'])
16431728
class _TestLinearFilter:

0 commit comments

Comments
 (0)