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