PythonSTB commited on
Commit
1f926d8
·
verified ·
1 Parent(s): fb74ade

Upload pywavelets/Test_PyWavelets.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. pywavelets/Test_PyWavelets.py +165 -0
pywavelets/Test_PyWavelets.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """PyWavelets device test for Android Python STB.
2
+
3
+ Generated by RIMI.
4
+
5
+ Validates the pywavelets Android wheel (import name ``pywt``):
6
+ import, version reporting, Wavelet objects, dwt/idwt + wavedec/waverec
7
+ roundtrips, SWT roundtrip, 2D transforms, thresholding smoke, wavelist,
8
+ and bundled data loading.
9
+
10
+ Run on device (Scripts folder) AFTER installing:
11
+ 1. numpy 2.5.2 Android wheel (from its own package folder), then
12
+ 2. pywavelets-1.10.0-cp312-cp312-android_24_<arch>.whl (STANDALONE)
13
+
14
+ Exit-code contract: prints [PASS]/[FAIL] per test, a summary line, and
15
+ exits 0 only if every test passed (1 otherwise).
16
+ """
17
+
18
+ import sys
19
+ import traceback
20
+
21
+ PASS = 0
22
+ FAIL = 0
23
+ FAILURES = []
24
+
25
+
26
+ def run(name, fn):
27
+ global PASS, FAIL
28
+ try:
29
+ fn()
30
+ except Exception as e:
31
+ FAIL += 1
32
+ FAILURES.append(name)
33
+ print("[FAIL] %s -- %s: %s" % (name, type(e).__name__, e))
34
+ traceback.print_exc()
35
+ else:
36
+ PASS += 1
37
+ print("[PASS] %s" % name)
38
+
39
+
40
+ def test_import():
41
+ import pywt
42
+ import numpy
43
+ assert pywt is not None
44
+ assert numpy is not None
45
+ print(" pywt file:", pywt.__file__)
46
+ print(" numpy:", numpy.__version__)
47
+
48
+
49
+ def test_version():
50
+ import pywt
51
+ # NOTE (upstream quirk): the 1.10.0 sdist ships util/version_utils.py
52
+ # with MAJOR/MINOR/MICRO still at 1.8.0, so pywt.__version__ reports
53
+ # '1.8.0' even in the official PyPI 1.10.0 wheels. Our wheel is
54
+ # faithful to upstream here; the *wheel* version is 1.10.0
55
+ # (see pywavelets-1.10.0.dist-info). Only require a non-empty string.
56
+ assert isinstance(pywt.__version__, str) and len(pywt.__version__) > 0
57
+ print(" pywt.__version__ =", pywt.__version__)
58
+
59
+
60
+ def test_wavelet_object():
61
+ import pywt
62
+ w = pywt.Wavelet("db1")
63
+ assert w.name == "db1"
64
+ assert w.dec_len == 2 and w.rec_len == 2
65
+ assert "haar" in pywt.wavelist(kind="discrete") or "haar" in pywt.wavelist()
66
+
67
+
68
+ def test_wavelist():
69
+ import pywt
70
+ wl = pywt.wavelist()
71
+ for required in ("db1", "db2", "haar", "sym2"):
72
+ assert required in wl, "missing wavelet %s" % required
73
+ print(" %d wavelets listed" % len(wl))
74
+
75
+
76
+ def test_dwt_idwt_roundtrip():
77
+ import numpy as np
78
+ import pywt
79
+ x = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
80
+ cA, cD = pywt.dwt(x, "db1")
81
+ y = pywt.idwt(cA, cD, "db1")
82
+ assert np.allclose(x, y, atol=1e-12), "dwt/idwt roundtrip failed"
83
+
84
+
85
+ def test_wavedec_waverec_roundtrip():
86
+ import numpy as np
87
+ import pywt
88
+ rng = np.random.RandomState(42)
89
+ x = rng.randn(64)
90
+ coeffs = pywt.wavedec(x, "db2", level=2)
91
+ assert len(coeffs) == 3 # cA2, cD2, cD1
92
+ y = pywt.waverec(coeffs, "db2")
93
+ assert np.allclose(x, y, atol=1e-10), "wavedec/waverec roundtrip failed"
94
+
95
+
96
+ def test_swt_roundtrip():
97
+ import numpy as np
98
+ import pywt
99
+ x = np.arange(16, dtype=float)
100
+ coeffs = pywt.swt(x, "haar", level=1)
101
+ assert len(coeffs) == 1
102
+ cA, cD = coeffs[0]
103
+ assert cA.shape == x.shape and cD.shape == x.shape
104
+ y = pywt.iswt(coeffs, "haar")
105
+ assert np.allclose(x, y, atol=1e-12), "swt/iswt roundtrip failed"
106
+
107
+
108
+ def test_dwt2_idwt2_roundtrip():
109
+ import numpy as np
110
+ import pywt
111
+ x = np.arange(64, dtype=float).reshape(8, 8)
112
+ coeffs = pywt.dwt2(x, "haar")
113
+ cA, (cH, cV, cD) = coeffs
114
+ assert cA.shape == (4, 4)
115
+ y = pywt.idwt2(coeffs, "haar")
116
+ assert np.allclose(x, y, atol=1e-12), "dwt2/idwt2 roundtrip failed"
117
+
118
+
119
+ def test_threshold_smoke():
120
+ import numpy as np
121
+ import pywt
122
+ data = np.linspace(-2.0, 2.0, 32)
123
+ t = pywt.threshold(data, 1.0, mode="soft")
124
+ assert t.shape == data.shape
125
+ assert abs(t[16]) < abs(data[16])
126
+ t_hard = pywt.threshold(data, 1.0, mode="hard")
127
+ assert t_hard.shape == data.shape
128
+
129
+
130
+ def test_data_camera():
131
+ import pywt
132
+ arr = pywt.data.camera()
133
+ assert arr.ndim == 2 and arr.shape[0] > 0 and arr.shape[1] > 0
134
+ print(" camera shape:", arr.shape)
135
+
136
+
137
+ TESTS = [
138
+ ("import pywt + numpy", test_import),
139
+ ("version string present", test_version),
140
+ ("Wavelet object db1", test_wavelet_object),
141
+ ("wavelist contents", test_wavelist),
142
+ ("dwt/idwt roundtrip", test_dwt_idwt_roundtrip),
143
+ ("wavedec/waverec roundtrip", test_wavedec_waverec_roundtrip),
144
+ ("swt/iswt roundtrip", test_swt_roundtrip),
145
+ ("dwt2/idwt2 roundtrip", test_dwt2_idwt2_roundtrip),
146
+ ("threshold smoke", test_threshold_smoke),
147
+ ("data.camera load", test_data_camera),
148
+ ]
149
+
150
+
151
+ def main():
152
+ print("PyWavelets device test (pywt) - Generated by RIMI")
153
+ for name, fn in TESTS:
154
+ run(name, fn)
155
+ print("----------------------------------------")
156
+ print("RESULT: %d passed, %d failed" % (PASS, FAIL))
157
+ if FAIL:
158
+ print("FAILURES:", ", ".join(FAILURES))
159
+ return 1
160
+ print("ALL PASSED")
161
+ return 0
162
+
163
+
164
+ if __name__ == "__main__":
165
+ sys.exit(main())