r"""
HAT-VIS case study: developing an autonomous electric car
=========================================================

The house of quality of the HAT-VIS case study relates eight customer
requirements (CR) to eleven engineering specifications (ES). As in the HAT-VIS
paper, the requirements are the vertices, the specifications the
hyperedges, and the incidence values are relationship scores multiplied by
the importance ratings. Line widths show the hyperedge centrality, vertex
sizes the vertex centrality (here: the linear node–edge eigenvector
centralities of Tudisco and Higham 2021).
"""

# %%
import matplotlib.pyplot as plt

import hatvis as hv
from hatvis.plotting import draw_hatvis

qfd = hv.datasets.load_electric_car_qfd()
H = qfd.hypergraph
# [snippet:centrality]
x, y = hv.node_edge_eigenvector(H, "linear")
print("vertex centrality:\n", x.sort_values(ascending=False).round(4).head(4).to_string())
print("hyperedge centrality:\n", y.sort_values(ascending=False).round(4).head(4).to_string())
# [/snippet:centrality]
print({k: qfd.names[k] for k in ("CR1", "CR4", "ES5", "ES6", "ES10", "ES11")})

# %%
# [snippet:views]
fig, axes = plt.subplots(1, 3, figsize=(18, 5.6))
draw_hatvis(H, ax=axes[0], width_by=y, size_by=x, title="requirements (vertices) and specifications")
draw_hatvis(H, ax=axes[1], ego="CR4", width_by=y, size_by=x, title="ego view of CR4 (obstacle avoidance)")
draw_hatvis(H.dual(), ax=axes[2], title="dual: specifications as vertices")
# [/snippet:views]
plt.tight_layout()
D = H.dual()
print("ES10 and ES11 have identical profiles:", (D.to_dataframe().loc["ES10"] == D.to_dataframe().loc["ES11"]).all())
