"""
Transition graphs, matrices and MatrixWave
==========================================

The directly-follows graph of process mining (van der Aalst, 2016) counts
consecutive pairs: a first-order summary that is easy to read but forgets the
history. The same information as a row-normalized transition matrix, and as a
MatrixWave-style sequence of step-by-step matrices (Zhao et al., 2015).
"""

import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.viz import plot_matrixwave, plot_transition_graph, plot_transition_matrix

db = sv.datasets.make_customer_journeys(1000, random_state=0)
plot_transition_graph(db, top_edges=24)
G = sv.networks.transition_graph(db)         # networkx.DiGraph with counts and probabilities
print(G["product"]["cart"])
# %%
# Transition matrix
# -----------------
plot_transition_matrix(db)

# %%
# MatrixWave: one matrix per step
# -------------------------------
plot_matrixwave(db, steps=3, top=6)

# %%
# Parameter study: the frequency filter
# -------------------------------------
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
for ax, k in zip(axes, (10, 40)):
    plot_transition_graph(db, ax=ax, top_edges=k, labels=None, title=f"{k} strongest transitions")
plt.show()
