# -*- coding: utf-8 -*- """Unit tests: SAX transformation primitives.""" import numpy as np import pytest from src.sax import transform as sx def test_znorm_zero_std(): a = sx.z_normalize([5.0, 5.0, 5.0]) assert np.allclose(a, 0.0) def test_znorm_standard(): a = sx.z_normalize([1.0, 2.0, 3.0, 4.0]) assert abs(np.mean(a)) < 1e-9 assert abs(np.std(a) - 1.0) < 1e-9 def test_paa_reduces_length(): res = sx.to_paa(np.arange(10, dtype=float), 4) assert res.shape[0] == 4 def test_paa_preserves_mean(): xs = np.linspace(0, 9, 10) assert abs(np.mean(sx.to_paa(xs, 2)) - np.mean(xs)) < 1e-9 def test_sax_encode_length_and_alphabet(): word = sx.sax_encode(np.random.default_rng(0).uniform(-1, 1, 24), 8, 5) assert len(word) == 8 assert all(ch in "abcde" for ch in word) def test_sax_alphabet_error(): with pytest.raises(ValueError): sx.sax_encode(np.arange(24, dtype=float), 8, 2) # 2 not supported def test_mindist_same_word_zero(): w = "baccd" assert sx.mindist_mid(w, w, 5) == 0.0 def test_mindist_symmetric(): a, b = "aabbc", "ccbbd" assert sx.mindist_mid(a, b, 5) == sx.mindist_mid(b, a, 5) def test_mindist_monotonic_symbol_distance(): d1 = sx.mindist_mid("a", "b", 5) d2 = sx.mindist_mid("a", "e", 5) assert d2 > d1