"""
Comparing two groups: contrast sunburst
=======================================

Mobile and desktop visitors walk through the same shop differently. The contrast
sunburst colors every prefix by the log-ratio of its relative frequency in the
two groups (blue: more frequent on desktop, vermilion: on mobile). The contrast
table of the frequent sequences adds Fisher's exact test (Dong & Li, 1999,
emerging patterns).
"""

import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.viz import plot_sunburst

db = sv.datasets.make_customer_journeys(1500, random_state=2)
plot_sunburst(db, max_depth=4, color_by="contrast", groups="device", positive="desktop",
              title="desktop (blue) versus mobile (vermilion)")
ps = sv.mine(db, 0.03, max_gap=1, max_length=5)
table = sv.measures.contrast(ps, "device", positive="desktop")
print(table.head(8)[["pattern", "supp[desktop]", "supp[mobile]", "log2_ratio", "p_value"]])
# %%
# Conversion: converted versus lost visitors
# ------------------------------------------
plot_sunburst(db, max_depth=3, color_by="contrast", groups=db.attributes["converted"].map({True: "converted", False: "lost"}),
              positive="converted", title="converted (blue) versus lost (vermilion) visitors")
plt.show()
