"""
A one-page segmentation report
==============================

A segmentation is only useful if it can be explained. The dashboard
combines the segmented series, the timeline of states, the cost curve of
the merge hierarchy and the contributions of the variables to every change
(shifts of the mean, the variance and the correlation). The segment table
summarizes every segment by its statistics and principal directions.
"""

import matplotlib.pyplot as plt

import mvtsa as mv
from mvtsa import segmentation as sg
from mvtsa.explain import boundary_contributions, describe_segments

X, cps, states = mv.datasets.make_regime_process(
    n_regimes=3, n_segments=6, length=150, random_state=4
)
names = ["temperature", "pressure", "flow", "level"]

# %%
# The dashboard
# -------------
st = sg.StateSegmenter(
    segmenter=sg.BottomUp("normal", n_segments=6, initial_size=15), n_states=3
)
seg = st.segment(X)
tree = sg.BottomUp("normal", initial_size=15).full_tree(X)
mv.viz.segmentation_dashboard(X, seg, names=names, truth=cps, tree=tree)
# %%
# Which variables changed?
# ------------------------
print(boundary_contributions(X, seg, kind="all", names=names).round(2))
print(describe_segments(X, seg, names=names).round(2).iloc[:, :8])
plt.show()
