r"""
Comparing two versions of a production system
=============================================

The suggestions of the HyperGodot case study — drop the redundant final
inspection (E7) and automate the manual assembly (E5) — define a redesigned
system. The difference hypergraph marks the hyperedges kept, removed and
added; group statistics compare human and machine work before and after.
"""

# %%
import matplotlib.pyplot as plt
import pandas as pd

import hatvis as hv
from hatvis.plotting import draw_hypergraph

before = hv.datasets.load_manufacturing_cps()
# [snippet:redesign]
after = before.remove_edges(["E7", "E5"]).add_edges({"E5r": ["R3", "C2", "C3", "C4", "FP"]}, weights=[18])
after.edge_data.loc["E5r", ["task", "group"]] = ["Robotic assembly", "M"]
diff = hv.temporal.difference(before, after, labels=("before", "after"))
stats = pd.concat({"before": hv.temporal.group_statistics(before, weight_name="minutes"),
                   "after": hv.temporal.group_statistics(after, weight_name="minutes")})
# [/snippet:redesign]
print(diff.edge_data["status"].value_counts())
print(stats[["hyperedges", "total minutes", "mean size"]])
# [snippet:distance]
dist = {kind: hv.similarity.hypergraph_distance(before, after, kind) for kind in ("jaccard", "hamming", "spectral", "degree")}
# [/snippet:distance]
print({k: round(v, 3) for k, v in dist.items()})

# %%
fig, ax = plt.subplots(figsize=(8.5, 6.5))
draw_hypergraph(diff, layout="force", group="status", edge_color="status", ax=ax, random_state=3,
                edge_labels=True, title="difference: kept (solid), removed and added (dashed, dotted)")
plt.tight_layout()
