alirezaaminzadeh's picture
Publish Gradio console bundle
ab849c9 verified
Raw
History Blame Contribute Delete
5.05 kB
"""Plotly visualization helpers."""
from __future__ import annotations
from typing import Any
import plotly.graph_objects as go
def build_solver_comparison_chart(rows: list[dict[str, Any]], metric: str = "objective_value") -> go.Figure:
if not rows:
return go.Figure()
methods = [r.get("method_label", r.get("method_id", "")) for r in rows]
values = [r.get(metric, 0) for r in rows]
colors = ["#4f46e5" if r.get("winner") else "#94a3b8" for r in rows]
fig = go.Figure(go.Bar(x=methods, y=values, marker_color=colors))
fig.update_layout(
title=f"Method Comparison — {metric.replace('_', ' ').title()}",
xaxis_title="Method",
yaxis_title=metric.replace("_", " ").title(),
template="plotly_white",
height=400,
)
return fig
def build_gap_timeline(rows: list[dict[str, Any]]) -> go.Figure:
if not rows:
return go.Figure()
fig = go.Figure()
for r in rows:
fig.add_trace(go.Scatter(
x=[r.get("elapsed_time_sec", 0)],
y=[r.get("optimality_gap", 0)],
mode="markers+text",
name=r.get("method_label", ""),
text=[r.get("method_label", "")],
textposition="top center",
))
fig.update_layout(
title="Optimality Gap vs Solve Time",
xaxis_title="Elapsed Time (s)",
yaxis_title="Gap (%)",
template="plotly_white",
height=400,
)
return fig
def build_scalability_chart(rows: list[dict[str, Any]]) -> go.Figure:
if not rows:
return go.Figure()
sizes = sorted(set(r.get("size", "") for r in rows))
methods = sorted(set(r.get("method_id", "") for r in rows))
fig = go.Figure()
for mid in methods:
subset = [r for r in rows if r.get("method_id") == mid]
by_size = {r["size"]: r.get("elapsed_time_sec", 0) for r in subset}
fig.add_trace(go.Scatter(
x=sizes,
y=[by_size.get(s, 0) for s in sizes],
mode="lines+markers",
name=mid,
))
fig.update_layout(
title="Scalability — Solve Time by Instance Size",
xaxis_title="Size",
yaxis_title="Time (s)",
template="plotly_white",
height=400,
)
return fig
def build_category_radar(rows: list[dict[str, Any]]) -> go.Figure:
if not rows:
return go.Figure()
cats = sorted(set(r.get("method_category", "") for r in rows))
metrics = ["objective_value", "optimality_gap", "elapsed_time_sec"]
fig = go.Figure()
for cat in cats:
subset = [r for r in rows if r.get("method_category") == cat]
if not subset:
continue
vals = []
for m in metrics:
avg = sum(r.get(m, 0) for r in subset) / len(subset)
vals.append(avg)
fig.add_trace(go.Scatterpolar(
r=vals,
theta=[m.replace("_", " ").title() for m in metrics],
name=cat,
fill="toself",
))
fig.update_layout(
polar=dict(radialaxis=dict(visible=True)),
title="Method Category Profile",
template="plotly_white",
height=450,
)
return fig
def build_heatmap(comparisons: dict[str, Any], metric: str = "objective_value") -> go.Figure:
if not comparisons:
return go.Figure()
problems = list(comparisons.keys())
methods_set: set[str] = set()
for pt_data in comparisons.values():
for inst_data in pt_data.values():
methods_set.update(inst_data.get("results", {}).keys())
methods = sorted(methods_set)
z = []
for pt in problems:
row = []
for mid in methods:
vals = []
for inst_data in comparisons[pt].values():
mdata = inst_data.get("results", {}).get(mid, {})
if mdata:
vals.append(mdata.get(metric, 0))
row.append(sum(vals) / len(vals) if vals else 0)
z.append(row)
fig = go.Figure(go.Heatmap(z=z, x=methods, y=problems, colorscale="Viridis"))
fig.update_layout(
title=f"Benchmark Heatmap — {metric.replace('_', ' ').title()}",
template="plotly_white",
height=500,
)
return fig
def build_progress_chart(result_metrics: dict[str, Any]) -> go.Figure:
fig = go.Figure()
labels = ["Objective", "Best Bound", "Gap %", "Time (s)", "Iterations"]
values = [
result_metrics.get("objective_value", 0),
result_metrics.get("best_bound", 0),
result_metrics.get("optimality_gap", 0),
result_metrics.get("elapsed_time_sec", 0),
result_metrics.get("iterations", 0),
]
fig.add_trace(go.Bar(x=labels, y=values, marker_color=["#4f46e5", "#7c3aed", "#f59e0b", "#10b981", "#6366f1"]))
fig.update_layout(title="Solve Progress Snapshot", template="plotly_white", height=350)
return fig