File size: 10,559 Bytes
2d64baa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {MODEL_TYPES, SCORE_AXES, axisLabel, modelType, selectPlotPoints, xScale, fraction, validateLocalSummary} from "./plot-data.mjs";

const $ = id => document.getElementById(id);
const NS = "http://www.w3.org/2000/svg";
const number = value => value >= 0.001 && value < 100000
  ? Number(value.toPrecision(4)).toLocaleString("en-US") : value.toExponential(1);
function svgNode(tag, attributes = {}, text = null) {
  const node = document.createElementNS(NS, tag);
  for (const [key, value] of Object.entries(attributes)) node.setAttribute(key, value);
  if (text !== null) node.textContent = text;
  return node;
}
function add(parent, tag, attrs = {}, text = null) {
  const child = svgNode(tag, attrs, text); parent.append(child); return child;
}
export function setupPlotter(board) {
  let local = [], currentSVG = null, notice = "";
  for (const axis of SCORE_AXES) {
    for (const id of ["plot-x", "plot-y"]) {
      const option = document.createElement("option"); option.value = axis.key; option.textContent = axis.label; $(id).append(option);
    }
  }
  $("plot-y").value = "macro.rms.f1";
  for (const domain of Object.keys(board.domain_tables)) {
    const option = document.createElement("option"); option.value = domain; option.textContent = domain.replaceAll("_", " "); $("plot-domain").append(option);
  }
  function detail(point, xAxis, yAxis) {
    const root = $("plot-detail"); root.replaceChildren();
    const heading = document.createElement("h3"); heading.textContent = point.record.submission.method_name;
    const type = document.createElement("p"); type.textContent = MODEL_TYPES[point.type].label + " · " +
      (point.record._local ? "Local preview · unreviewed" : "Reviewed submission");
    const values = document.createElement("dl");
    for (const [label, value] of [[axisLabel(xAxis), xAxis === "parameters" ? number(point.x) : point.x.toFixed(2)],
                                  [axisLabel(yAxis), point.y.toFixed(2)],
                                  ["Evaluation mode", "Physical table · image-only"]]) {
      const term = document.createElement("dt"), definition = document.createElement("dd");
      term.textContent = label; definition.textContent = value; values.append(term, definition);
    }
    const revision = document.createElement("p"); revision.className = "mono"; revision.textContent = point.record.submission.model_revision || "";
    root.append(heading, type, values, revision);
  }
  function render() {
    const xAxis = $("plot-x").value, yAxis = $("plot-y").value, domain = $("plot-domain").value;
    const sizeAxis = xAxis === "parameters";
    $("plot-log").disabled = !sizeAxis;
    $("plot-log").closest("label").classList.toggle("muted-option", !sizeAxis);
    const records = [...board.results, ...local];
    const {points, candidates, omitted} = selectPlotPoints(records, {x: xAxis, y: yAxis, domain, type: $("plot-type").value});
    const scale = xScale(points, xAxis, sizeAxis && $("plot-log").checked);
    const W = 920, H = 460, L = 76, R = 27, T = 28, B = 65, iw = W-L-R, ih = H-T-B;
    const X = value => L + fraction(value, scale) * iw, Y = value => T + (1-value/100) * ih;
    const svg = svgNode("svg", {viewBox: "0 0 920 460", role: "img", "aria-labelledby": "scatter-title scatter-description",
      xmlns: NS, style: "background:#fffefa;font-family:Arial,sans-serif"});
    add(svg, "title", {id: "scatter-title"}, axisLabel(xAxis) + " versus " + axisLabel(yAxis));
    add(svg, "desc", {id: "scatter-description"}, points.length + " model points; color indicates model type. " +
      "All reconstruction scores range from 0 to 100. Local previews use outlined points.");
    for (const value of [0,20,40,60,80,100]) {
      const y = Y(value);
      add(svg,"line",{x1:L,x2:W-R,y1:y,y2:y,stroke:"#e0e5d9","stroke-width":1});
      add(svg,"text",{x:L-12,y:y+4,"text-anchor":"end",fill:"#6b796c","font-size":12},String(value));
    }
    for (const value of scale.ticks) {
      const x = X(value);
      add(svg,"line",{x1:x,x2:x,y1:T,y2:H-B,stroke:"#e8ebdf","stroke-width":1});
      add(svg,"text",{x,y:H-B+23,"text-anchor":"middle",fill:"#6b796c","font-size":12},number(value));
    }
    add(svg,"line",{x1:L,x2:L,y1:T,y2:H-B,stroke:"#82917e","stroke-width":1});
    add(svg,"line",{x1:L,x2:W-R,y1:H-B,y2:H-B,stroke:"#82917e","stroke-width":1});
    add(svg,"text",{x:L+iw/2,y:H-13,"text-anchor":"middle",fill:"#3d5847","font-size":13},
      axisLabel(xAxis) + (scale.log ? " · log scale" : ""));
    add(svg,"text",{transform:"translate(20 "+(T+ih/2)+") rotate(-90)","text-anchor":"middle",fill:"#3d5847","font-size":13},axisLabel(yAxis));
    add(svg,"text",{x:W-R,y:T-10,"text-anchor":"end",fill:"#7a8775","font-size":11},
      sizeAxis ? "Smaller ← · higher score ↑" : "Higher score → ↑");
    const occupied = [], layer = add(svg,"g");
    for (const point of points) {
      const x = X(point.x), y = Y(point.y), color = MODEL_TYPES[point.type].color;
      const circle = add(layer,"circle",{cx:x,cy:y,r:7,fill:point.record._local?"#fffefa":color,stroke:color,
        "stroke-width":point.record._local?3:1.5,tabindex:0,role:"button",
        "aria-label":point.record.submission.method_name + ", " + MODEL_TYPES[point.type].label +
          ", " + axisLabel(xAxis) + " " + number(point.x) + ", " + axisLabel(yAxis) + " " + point.y.toFixed(2) +
          (point.record._local ? ", local unreviewed preview" : ", reviewed"),class:"plot-point"});
      add(circle,"title",{},point.record.submission.method_name + " · " + MODEL_TYPES[point.type].label +
        "\n" + axisLabel(xAxis) + ": " + number(point.x) + "\n" + axisLabel(yAxis) + ": " + point.y.toFixed(2));
      const show = () => {detail(point,xAxis,yAxis);};
      circle.addEventListener("pointerenter",show); circle.addEventListener("focus",show); circle.addEventListener("click",show);
      circle.addEventListener("keydown",event=>{if(event.key==="Enter"||event.key===" "){event.preventDefault();show();}});
      const label = point.record.submission.method_name;
      const display = label.length > 28 ? label.slice(0,27)+"…" : label;
      const width = display.length * 6.3;
      const anchor = x+12+width < W-R ? "start" : "end";
      const start = anchor==="start"?x+12:x-12-width;
      const box = {left:start,right:start+width,top:y-10,bottom:y+7};
      const touchesPoint = points.some(other=>other!==point && X(other.x)>box.left-7 && X(other.x)<box.right+7 &&
        Y(other.y)>box.top-7 && Y(other.y)<box.bottom+7);
      if (box.left>L && box.right<W-R && !touchesPoint &&
          !occupied.some(old=>box.left<old.right && box.right>old.left && box.top<old.bottom && box.bottom>old.top)) {
        add(svg,"text",{x:anchor==="start"?x+12:x-12,y:y+4,"text-anchor":anchor,fill:"#3d5847","font-size":12,"pointer-events":"none"},display);
        occupied.push(box);
      }
    }
    $("plot-chart").replaceChildren(svg); currentSVG = svg;
    $("plot-count").textContent = points.length + " plotted · " + board.results.length + " reviewed · " + local.length + " local";
    $("plot-message").textContent = [notice, omitted ? omitted + " of " + candidates +
      " matching submissions omitted: missing " + (sizeAxis ? "parameter count or selected score." : "selected score.") : ""].filter(Boolean).join(" ");
    const empty = $("plot-empty"); empty.hidden = points.length > 0;
    if (!points.length) {
      empty.querySelector("h3").textContent = records.length ? "No points for this view." : "Ready to compare the first models.";
      empty.querySelector("p").textContent = sizeAxis && records.length
        ? "Choose a score for the horizontal axis to include models without a declared parameter count, or change the filters."
        : records.length ? "Try another domain or model type." : "Reviewed submissions will appear here. You can also preview your saved evaluation summaries below.";
    }
    $("download-plot").disabled = !points.length; $("clear-local").hidden = !local.length;
    $("plot-detail").replaceChildren();
    const heading = document.createElement("h3"), text = document.createElement("p");
    heading.textContent = "Select a point"; text.textContent = "Hover, tap, or focus a model to see its type, exact scores, and evaluation mode.";
    $("plot-detail").append(heading,text);
  }
  for (const id of ["plot-x","plot-y","plot-domain","plot-type","plot-log"]) $(id).addEventListener("change",render);
  $("plot-files").addEventListener("change", async event => {
    if (event.target.files.length > 50) {notice = "Load at most 50 summaries at a time."; render(); return;}
    let loaded = 0; const errors = [];
    for (const file of event.target.files) {
      try {
        if (file.size > 5*1024*1024) throw new Error("Summary file exceeds 5 MB.");
        const summary = validateLocalSummary(JSON.parse(await file.text()),board);
        local = local.filter(item=>item.submission.submission_id!==summary.submission.submission_id);
        local.push(summary); loaded++;
      } catch(error) {errors.push(file.name+": "+error.message);}
    }
    notice = [loaded ? loaded+" local preview"+(loaded===1?"":"s")+" loaded; rankings are unchanged." : "",...errors].join(" ");
    event.target.value = ""; render();
  });
  $("clear-local").addEventListener("click",()=>{local=[];notice="Local previews cleared.";render();});
  $("download-plot").addEventListener("click",()=>{
    if (!currentSVG) return;
    const clone = currentSVG.cloneNode(true);
    clone.setAttribute("viewBox","0 0 920 490");
    clone.querySelectorAll("[tabindex]").forEach(point=>{point.removeAttribute("tabindex");point.removeAttribute("role");});
    const subtitle = svgNode("text",{x:76,y:17,fill:"#6b796c","font-size":10},$("plot-count").textContent+
      " · "+($("plot-domain").value==="all"?"All domains":$("plot-domain").value.replaceAll("_"," "))+
      (local.length ? " · local previews are unreviewed" : ""));
    clone.append(subtitle);
    const key = svgNode("text",{x:76,y:482,fill:"#6b796c","font-size":10},
      "VLM: blue · Specialist: green · Classical: orange · Hybrid: purple · Unspecified: gray · Outlined: local");
    clone.append(key);
    const blob = new Blob([new XMLSerializer().serializeToString(clone)],{type:"image/svg+xml;charset=utf-8"});
    const url = URL.createObjectURL(blob), link = document.createElement("a");
    link.href=url; link.download="scientific-table-reconstruction-comparison.svg"; link.click();
    setTimeout(()=>URL.revokeObjectURL(url),1000);
  });
  render();
}