"""
Representations: PAA, PLA, SAX, episodes
========================================

Four ways to summarize a noisy signal: piecewise aggregate and piecewise
linear approximations, the symbolic SAX word and the qualitative episodes
of trend analysis (Cheung and Stephanopoulos, 1990; Balaskó et al., 2007).
"""

import matplotlib.pyplot as plt
import numpy as np

from mvtsa import representation as rp
from mvtsa import viz

rng = np.random.default_rng(1)
t = np.linspace(0, 4 * np.pi, 400)
x = np.sin(t) + 0.4 * np.sin(3.1 * t) + 0.05 * rng.normal(size=t.size)

# %%
# Piecewise linear approximation
# ------------------------------
p = rp.pla(x, n_segments=12)
print("slope angles (degrees):", np.degrees(p.angles[:, 0]).round(0))
viz.plot_pla(x, p)

# %%
# SAX
# ---
print("SAX word:", "".join(rp.sax(x, n_segments=16, alphabet_size=5)))
viz.plot_sax(x, 16, 5)

# %%
# Qualitative episodes
# --------------------
eps = rp.triangular_episodes(x, sigma=6)
print("episodes:", rp.episode_string(eps))
viz.plot_episodes(x, eps, smooth=6)

# %%
# Parameter study: the smoothing of the episodes
# ----------------------------------------------
for s in (2, 6, 15, 30):
    print(
        f"sigma={s:2d}:", rp.episode_string(rp.triangular_episodes(x, sigma=s))
    )
plt.show()
