Spaces:
Running
Running
File size: 77,771 Bytes
2cd8242 d53a457 2cd8242 62d1926 2cd8242 0131ebf 2cd8242 62d1926 2cd8242 ea6f247 62d1926 2cd8242 0472130 2cd8242 0472130 a0623b0 0472130 2cd8242 dc0f28c 2cd8242 dc0f28c 2cd8242 dc0f28c 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 dc0f28c 2cd8242 1113165 2cd8242 dc0f28c 2cd8242 0131ebf 2cd8242 1113165 2cd8242 dc0f28c 2cd8242 0131ebf bc0d548 2cd8242 1113165 2cd8242 62d1926 dc0f28c 1113165 2cd8242 dc0f28c d53a457 2cd8242 dc0f28c 2cd8242 62d1926 0131ebf 2cd8242 dc0f28c 2cd8242 d53a457 2cd8242 d53a457 2cd8242 dc0f28c 1113165 dc0f28c 2cd8242 d53a457 dc0f28c 2cd8242 dc0f28c 0131ebf dc0f28c 1113165 dc0f28c d53a457 0131ebf 2cd8242 dc0f28c 2cd8242 dc0f28c 2cd8242 dc0f28c 2cd8242 dc0f28c 2cd8242 dc0f28c 0131ebf dc0f28c 2cd8242 0472130 62d1926 2cd8242 0472130 a0623b0 2cd8242 62d1926 0472130 2cd8242 62d1926 2cd8242 0472130 62d1926 0472130 62d1926 2cd8242 62d1926 2cd8242 0472130 2cd8242 0472130 a0623b0 0472130 a0623b0 2cd8242 0472130 2cd8242 0472130 2cd8242 a0623b0 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 ea6f247 2cd8242 0472130 2cd8242 0472130 2cd8242 62d1926 2cd8242 0472130 2cd8242 ea6f247 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 62d1926 2cd8242 f0ce8b7 2cd8242 f0ce8b7 2cd8242 f0ce8b7 62d1926 f0ce8b7 ea6f247 62d1926 f0ce8b7 0472130 ea6f247 f0ce8b7 0472130 f0ce8b7 0472130 f0ce8b7 bc0d548 f0ce8b7 bc0d548 0472130 bc0d548 0472130 f0ce8b7 0472130 62d1926 ea6f247 0472130 ea6f247 0472130 ea6f247 0472130 ea6f247 0472130 ea6f247 0472130 ea6f247 0472130 ea6f247 0472130 ea6f247 0472130 ea6f247 0472130 ea6f247 0472130 ea6f247 62d1926 ea6f247 2cd8242 ea6f247 2cd8242 ea6f247 2cd8242 ea6f247 2cd8242 ea6f247 2cd8242 ea6f247 2cd8242 ea6f247 2cd8242 ea6f247 2cd8242 | 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | // open-predico dashboard. Static: draws the JSON that build.py writes to data/. No framework.
"use strict";
const DATA = "data/";
const STEP = 900;
const MON = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" ");
const WEEKDAYS = "Mon Tue Wed Thu Fri Sat Sun".split(" ");
const LABEL = { energy: "Energy", price: "Price", wind: "Wind", solar: "Solar", solar_be: "Solar" };
const cache = {};
const json = p =>
(cache[p] ??= fetch(DATA + p).then(r => {
if (!r.ok) throw new Error(`${p}: HTTP ${r.status}`);
return r.json();
}));
const css = n => getComputedStyle(document.documentElement).getPropertyValue(n).trim();
const pad = v => String(v).padStart(2, "0");
const esc = s => String(s ?? "").replace(/[&<>"]/g, c => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
const minus = s => String(s).replace(/^-/, "−");
const f1 = v => (v == null || Number.isNaN(v) ? "–" : minus(v.toFixed(1)));
const f3 = v => (v == null || Number.isNaN(v) ? "–" : v.toFixed(3));
const pct = v => (v == null || Number.isNaN(v) ? "–" : `${Math.round(v * 100)}%`);
const mean = a => { let s = 0, n = 0; for (const v of a) if (v != null && !Number.isNaN(v)) { s += v; n++; } return n ? s / n : null; };
const fmtUTC = s => { const d = new Date(s * 1000); return `${d.getUTCDate()} ${MON[d.getUTCMonth()]} ${d.getUTCFullYear()}, ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())} UTC`; };
const fmtDay = key => { const [y, m, d] = key.split("-"); return `${+d} ${MON[+m - 1]} ${y}`; };
// Madrid calendar for a UTC second, cached per hour (offsets are whole hours). Berlin shares
// Madrid's offsets, so it serves the German markets too.
const MAD = new Intl.DateTimeFormat("en-CA", { timeZone: "Europe/Madrid", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", hourCycle: "h23", weekday: "short" });
const hourCache = new Map();
function madrid(sec) {
const h = Math.floor(sec / 3600);
let v = hourCache.get(h);
if (!v) {
const p = Object.fromEntries(MAD.formatToParts(new Date(h * 3600e3)).map(x => [x.type, x.value]));
v = { day: `${p.year}-${p.month}-${p.day}`, month: `${p.year}-${p.month}`, hour: +p.hour, wd: WEEKDAYS.indexOf(p.weekday) };
hourCache.set(h, v);
}
return v;
}
// --- Charts: a registry so resizes and theme switches redraw every chart --------------------
const charts = new Set();
function mount(el, build, height) {
const h = () => (typeof height === "function" ? height(el.clientWidth) : height);
const c = { el, build: () => build(el.clientWidth || 600, h()), inst: null, h };
c.inst = c.build();
charts.add(c);
new ResizeObserver(() => {
if (!document.body.contains(el)) { c.inst?.destroy(); charts.delete(c); return; }
if (el.clientWidth && c.inst) c.inst.setSize({ width: el.clientWidth, height: c.h() });
}).observe(el);
return c;
}
function redrawAll() {
for (const c of charts) {
if (!document.body.contains(c.el)) { charts.delete(c); continue; }
const x = c.inst?.scales?.x;
const keep = x && { min: x.min, max: x.max };
c.inst?.destroy();
c.inst = c.build();
if (keep && c.keepX) c.inst.setScale("x", keep);
}
}
matchMedia("(prefers-color-scheme: dark)").addEventListener("change", redrawAll);
new MutationObserver(redrawAll).observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
function axisBase() {
return {
stroke: css("--ink-soft"), font: `11px ${css("--mono")}`, labelFont: `12px ${css("--sans")}`,
grid: { stroke: css("--grid"), width: 1 }, ticks: { stroke: css("--grid"), width: 1, size: 4 },
};
}
function timeValues(u, splits, ax, space, incr) {
return splits.map(s => {
const d = new Date(s * 1000), date = `${pad(d.getUTCDate())}-${pad(d.getUTCMonth() + 1)}`;
if (incr >= 86400 * 25) return `${MON[d.getUTCMonth()]} ${d.getUTCFullYear()}`;
if (incr >= 86400) return date;
const hm = `${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}`;
return d.getUTCHours() === 0 && d.getUTCMinutes() === 0 ? `${hm}\n${date}` : hm;
});
}
const yValues = (u, s) => s.map(v => minus(Math.abs(v) >= 1000 ? Math.round(v).toLocaleString("en-GB") : +v.toFixed(2)));
function axes(yLabel, xValues = timeValues, xOpts = {}) {
const a = axisBase();
return [{ ...a, values: xValues, space: 64, size: 44, ...xOpts }, { ...a, label: yLabel, labelSize: yLabel ? 20 : 0, size: 58, values: yValues }];
}
const tzUTC = ts => uPlot.tzDate(new Date(ts * 1000), "Etc/UTC");
function tooltip(rows) {
let tt;
return {
hooks: {
init: u => { tt = document.createElement("div"); tt.className = "tt"; tt.hidden = true; u.over.appendChild(tt); },
setCursor: u => {
const { left, top, idx } = u.cursor;
const html = idx == null || left < 0 ? null : rows(u, idx);
if (!html) { tt.hidden = true; return; }
tt.innerHTML = html;
tt.hidden = false;
const w = tt.offsetWidth, h = tt.offsetHeight, W = u.over.clientWidth, H = u.over.clientHeight;
const x = left + 16 + w > W ? Math.max(0, left - 16 - w) : left + 16;
tt.style.transform = `translate(${x}px, ${Math.min(Math.max(0, top - h / 2), Math.max(0, H - h))}px)`;
},
},
};
}
const ttHTML = (title, rows) =>
`<div class="t">${esc(title)}</div>` +
rows.map(([c, k, v, dash]) => `<div class="r"><i style="background:${c};${dash ? "height:0;border-top:3px dotted " + c + ";background:none" : ""}"></i><span>${esc(k)}</span><span>${v}</span></div>`).join("");
// Default tooltip: every visible series with a `tt` label.
const seriesRows = fmtX => (u, idx) => {
const rows = [];
u.series.forEach((s, i) => { if (i && s.show && s.tt) { const v = u.data[i][idx]; if (v != null) rows.push([s._color, s.tt, f1(v)]); } });
return rows.length ? ttHTML(fmtX(u.data[0][idx]), rows) : null;
};
function line(label, color, opts = {}) {
return { label, tt: label, stroke: color, _color: color, width: 2, points: { show: false }, ...opts };
}
// Shades [start, end) spans (seconds) behind the series: the playground days.
function shade(spans) {
return {
hooks: {
drawAxes: u => {
const { left, top, width, height } = u.bbox;
u.ctx.save(); u.ctx.fillStyle = css("--pg");
for (const [a, b] of spans) {
const x0 = Math.max(u.valToPos(a, "x", true), left), x1 = Math.min(u.valToPos(b, "x", true), left + width);
if (x1 > x0) u.ctx.fillRect(x0, top, x1 - x0, height);
}
u.ctx.restore();
},
},
};
}
function nowMarker(nowSec) {
return {
hooks: {
drawAxes: u => {
const { left, top, width, height } = u.bbox, x = Math.max(u.valToPos(nowSec, "x", true), left);
if (x >= left + width) return;
u.ctx.save(); u.ctx.fillStyle = css("--future"); u.ctx.fillRect(x, top, left + width - x, height); u.ctx.restore();
},
draw: u => {
const { left, top, width, height } = u.bbox, x = u.valToPos(nowSec, "x", true), dpr = devicePixelRatio || 1;
if (x < left || x > left + width) return;
const ctx = u.ctx; ctx.save();
ctx.strokeStyle = css("--ink-soft"); ctx.setLineDash([4 * dpr, 3 * dpr]); ctx.lineWidth = dpr;
ctx.beginPath(); ctx.moveTo(x, top); ctx.lineTo(x, top + height); ctx.stroke();
const d = new Date(nowSec * 1000);
ctx.fillStyle = css("--ink-soft"); ctx.font = `${10 * dpr}px ${css("--mono")}`;
ctx.fillText(`NOW ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())} UTC`, x + 6 * dpr, top + 12 * dpr);
ctx.restore();
},
},
};
}
const chartHeight = w => Math.max(280, Math.min(440, w * 0.45));
// UI bits
function seg(id, options, value) {
return `<div class="seg" role="group" id="${id}">${options.map(([v, l]) => `<button data-v="${v}" aria-pressed="${v === value}">${esc(l)}</button>`).join("")}</div>`;
}
function onSeg(root, id, fn) {
const g = root.querySelector(`#${id}`);
g?.addEventListener("click", e => {
const b = e.target.closest("button"); if (!b) return;
g.querySelectorAll("button").forEach(x => x.setAttribute("aria-pressed", String(x === b)));
fn(b.dataset.v);
});
}
// The selected country and variable (COUNTRIES, below), merged: id = its data key (build.py
// MARKETS), name = "Spain mFRR energy", resources = [its Predico resource].
const mk = () => {
const c = COUNTRIES.find(x => x.id === sel.c), v = c.vars.find(x => x.id === sel.v);
return { ...c, ...v, c: c.id, v: v.id, id: v.data ?? c.data, name: `${c.name} ${v.name}`, resources: [v.resource] };
};
const shortsOf = (m, meta) => m.resources.map(r => meta.short[r]);
const targetSeg = (id, shorts, value) => (shorts.length > 1 ? seg(id, shorts.map(t => [t, LABEL[t]]), value) : "");
const kpi = (k, v, d = "", cls = "") => `<div class="kpi"><span class="k">${esc(k)}</span><span class="v">${v}</span><span class="d ${cls}">${d}</span></div>`;
const relClass = r => (r == null || Math.abs(r - 1) < 5e-4 ? "" : r < 1 ? "good" : "bad");
const relText = r => (r == null ? "no baseline" : `${f3(r)} × baseline`);
// Colours per model family, dash per variant: the same config always looks the same.
const FAMILY = [["slot_climatology", "--ink-soft"], ["t0", "--accent-ink"], ["chronos", "#3b6fd1"], ["timesfm", "#8a63d2"], ["timestral", "#d08a1e"], ["timeh2o", "#1f9a8a"], ["ciproto", "#7a8f2a"]];
const DASHES = [[], [7, 4], [2, 3], [10, 3, 2, 3], [4, 4], [1, 2]];
let CONFIGS = [];
function configStyle(name) {
const fam = FAMILY.find(([p]) => name.startsWith(p));
const color = fam ? (fam[1].startsWith("--") ? css(fam[1]) : fam[1]) : "#c2456b";
const sibs = CONFIGS.filter(c => (fam ? c.startsWith(fam[0]) : !FAMILY.some(([p]) => c.startsWith(p))));
return { color, dash: DASHES[Math.max(0, sibs.indexOf(name)) % DASHES.length] };
}
// A line swatch with the config's colour and dash, as in the charts.
const swatch = name => { const st = configStyle(name); return `<svg width="22" height="6" aria-hidden="true" style="flex:none"><line x1="0" x2="22" y1="3" y2="3" stroke="${st.color}" stroke-width="2.5" stroke-dasharray="${st.dash.join(" ")}"/></svg>`; };
// Config names as readable parts: t0gamma_cov_ecmwfd2_knmi_ctx90 -> t0-gamma, covariates, 90 d,
// IFS + ICON-D2 + KNMI. `wx` = the market's weather models (weather.json), what a plain `cov`
// config reads. Unknown parts land in `notes` as they are.
const MODEL_NAMES = [["slot_climatology", "Climatology"], ["t0gamma", "t0-gamma"], ["chronos2", "Chronos-2"], ["timesfm3", "TimesFM 3"], ["timestral5m", "Timestral 5M"], ["timeh2o", "TimeH2O (H2O.ai)"], ["ciproto", "CI Proto (Continuous Intelligence)"], ["elia", "Elia (TSO)"], ["entsoe", "TSO (ENTSO-E)"]];
const WX = { ecmwf: "IFS", icond2: "ICON-D2", knmi: "KNMI", aifs: "AIFS" };
const wxShort = name => name.replace("ECMWF ", "").replace("KNMI HARMONIE-AROME", "KNMI");
function parseConfig(name, wx = []) {
const known = MODEL_NAMES.find(([p]) => name === p || name.startsWith(p + "_"));
const parts = name.slice(known ? known[0].length : name.indexOf("_") + 1 || name.length).split("_").filter(Boolean);
const r = { model: known ? known[1] : name.split("_")[0], cov: null, ctx: null, weather: null, notes: [] };
let explicit = null;
for (const p of parts) {
let x;
if (p === "cov" || p === "uni") r.cov = p === "cov";
else if ((x = p.match(/^ctx(\d+)$/) ?? p.match(/^(\d+)d$/))) r.ctx = +x[1];
else if (p === "ecmwfd2") (explicit ??= []).push("IFS", "ICON-D2");
else if (WX[p]) (explicit ??= []).push(WX[p]);
// WeatherNext 3 (BigQuery, not in weather.json): `wn3` = the market's models + WN3
else if (p === "wn3") (explicit ??= wx.map(wxShort)).push("WN3");
else if (p === "wn3only") explicit = ["WN3"];
// the noX ablations predate KNMI and AIFS: IFS + ICON-D2 minus one
else if (p === "noecmwf") explicit = ["ICON-D2"];
else if (p === "noicond2") explicit = ["IFS"];
else if (p === "cap") r.notes.push("+ PV capacity");
else if ((x = p.match(/^o(\d\d)(\d\d)$/))) r.notes.push(`origin ${x[1]}:${x[2]}`);
else if ((x = p.match(/^no(\d+)$/))) r.notes.push(`without ESIOS ${x[1]}`);
else if ((x = p.match(/^dayahead(\d+)h$/))) r.notes.push(`issued ${x[1]}:00 D-1`);
else if (p === "a69") r.notes.push("day-ahead (A69)");
else r.notes.push(p);
}
if (r.cov) r.weather = explicit ?? wx.map(wxShort);
return r;
}
const cfgLabel = (name, wx) => {
const p = parseConfig(name, wx);
return [p.model, p.cov == null ? "" : p.cov ? "cov" : "uni", p.ctx == null ? "" : `${p.ctx} d`, p.weather?.join(" + ") ?? "", ...p.notes].filter(Boolean).join(" · ");
};
// --- Daily scores (same formulas as src/openpredico/evaluation/metrics.py) ------------------
function dailyScores(x, q10, q50, q90, obs) {
const days = new Map();
for (let i = 0; i < x.length; i++) {
const o = obs[i], m = q50[i];
if (o == null || m == null) continue;
const key = madrid(x[i]).day;
let d = days.get(key);
if (!d) days.set(key, (d = { key, t: x[i], n: 0, se: 0, ae: 0, wk: 0, cov: 0 }));
d.n++; d.se += (o - m) ** 2; d.ae += Math.abs(o - m);
d.wk += q90[i] - q10[i] + 10 * Math.max(0, q10[i] - o) + 10 * Math.max(0, o - q90[i]);
if (o >= q10[i] && o <= q90[i]) d.cov++;
}
return [...days.values()].map(d => ({ key: d.key, t: d.t, n: d.n, rmse: Math.sqrt(d.se / d.n), mae: d.ae / d.n, winkler: d.wk / d.n, coverage: d.cov / d.n }));
}
// --- Live --------------------------------------------------------------------------------
// q*: our continuous forecast as it stood at the gate. s*: what Predico registered as our session
// submission (the values it scores). b*: slot climatology from the same run's inputs.
async function renderLive(root) {
const [live, meta] = await Promise.all([json("live.json"), json("meta.json")]);
const m = mk(), shorts = shortsOf(m, meta);
const head = `<div class="head"><div><h1>Live forecasts</h1><p class="lede">What we sent to Predico for ${esc(m.name)}, published only after each gate (${m.gateLocal}), next to the measured values from ${m.source}.</p></div>`;
if (!live.available || !shorts.some(s => live.targets[s])) {
root.innerHTML = `${head}</div><div class="card"><p class="empty">No live forecasts are published yet. The first ones appear here half an hour after the next gate.</p></div>`;
return;
}
let target = shorts[0];
root.innerHTML = `${head}<div class="controls">${targetSeg("lv-t", shorts, target)}</div></div><div id="lv-body" style="display:grid;gap:18px"></div>`;
onSeg(root, "lv-t", v => { target = v; draw(); });
draw();
function draw() {
const T = live.targets[target], unit = meta.units[target];
const n = T.q50.length, x = Array.from({ length: n }, (_, i) => T.start + i * STEP);
const nul = () => new Array(n).fill(null);
const [s10, s50, s90] = ["s10", "s50", "s90"].map(k => T[k] ?? nul());
// Scored = Predico's registered values where it has them, else our continuous forecast.
const pick = (s, q) => s.map((v, i) => v ?? q[i]);
const e10 = pick(s10, T.q10), e50 = pick(s50, T.q50), e90 = pick(s90, T.q90);
const model = dailyScores(x, e10, e50, e90, T.obs);
const base = new Map(dailyScores(x, T.b10, T.b50, T.b90, T.obs).map(d => [d.key, d]));
// Day record: registered by Predico, model forecast only (not submitted), or none.
const dayInfo = new Map();
for (let i = 0; i < n; i++) {
const key = madrid(x[i]).day;
let d = dayInfo.get(key);
if (!d) dayInfo.set(key, (d = { key, i0: i, i1: i, rows: 0, sub: 0 }));
d.i1 = i;
if (e50[i] != null) d.rows++;
if (s50[i] != null) d.sub++;
}
const record = [...dayInfo.values()];
const counts = { sub: 0, cont: 0, miss: 0 };
for (const d of record) { d.state = d.rows === 0 ? "miss" : d.sub ? "sub" : "cont"; d.pg = T.env_days?.[d.key] === "playground"; }
const pgDays = record.filter(d => d.pg), PG = "* playground: qualifying runs on the Predico playground, never scored by Predico";
record.forEach(d => counts[d.state]++);
const STATE = { sub: "submitted", cont: "not submitted", miss: "no forecast" };
const envNote = live.demo
? `<div class="banner"><b>Demo data.</b> No live forecasts are published yet, so this shows the last 21 backtest days of <span class="mono">${esc(T.model)}</span> in the live format.</div>`
: T.env === "playground"
? `<div class="banner"><b>Qualifying on the Predico playground.</b> Production submissions open after five good days; scores below are playground runs.</div>`
: pgDays.length
? `<div class="banner">Days marked <b>*</b> (shaded in the chart) were qualifying runs on the Predico playground, before production; Predico never scored them.</div>`
: "";
const t50 = T.t50 ?? nul(), hasTso = t50.some(v => v != null);
const body = root.querySelector("#lv-body");
body.innerHTML = `${envNote}
<div class="card">
<div class="card-head"><div><h2>Forecast vs measured</h2><p class="sub">Dotted: our model's forecast as of the gate. Solid underneath: what Predico registered; days without it were not submitted. ${unit} · times in UTC</p></div>
<div class="controls">${seg("lv-r", [["7", "7 days"], ["30", "30 days"], ["all", "All"]], "7")}</div></div>
<div class="chart" id="lv-chart"></div>
<div class="foot"><div class="legend" id="lv-legend">
<button data-s="6" aria-pressed="true"><span class="sw" style="border-color:var(--obs)"></span>Observed (${m.source})</button>
<button data-s="3" aria-pressed="true"><span class="sw thick" style="border-color:var(--sub)"></span>Submitted forecast</button>
<button data-s="4" aria-pressed="true"><span class="sw dot" style="border-color:var(--cont)"></span>Model forecast</button>
<button data-s="1,2" aria-pressed="true"><span class="sw box"></span>Interval (q10–q90)</button>
<button data-s="5" aria-pressed="false"><span class="sw dash" style="border-color:var(--base)"></span>Climatology baseline</button>
${hasTso ? `<button data-s="7" aria-pressed="true" title="The TSO's own day-ahead forecast, published after the gate"><span class="sw dash" style="border-color:var(--tso)"></span>${m.source} forecast (TSO)</button>` : ""}
</div><span class="hint">drag to zoom · double-click to reset · scores follow the view</span></div>
</div>
<div class="card"><div class="card-head"><div><h2>Scores against climatology</h2><p class="sub" id="lv-span"></p></div></div>
<div class="tablewrap" id="lv-sum"></div>
<p class="sub">Climatology is the slot climatology of the last 28 days, built from the same run's inputs. Ratio = our score ÷ climatology on the days both have; below 1 beats it. Coverage target is 80%. Lower is better for every other metric.</p>
</div>
<div class="card"><div class="card-head"><div><h2>Predico's official scores</h2><p class="sub">How Predico scored our submissions and where we ranked among all forecasters on that challenge. Only our own results are shown.</p></div></div>
<div id="lv-off"></div></div>
<div class="grid2">
<div class="card"><div class="card-head"><div><h2>Submission record</h2><p class="sub">One square per target day (${m.city}). Click a day to show it in the chart.</p></div></div>
<div class="days" id="lv-days">${record.map(d => `<button class="day ${d.state}${d.pg ? " pg" : ""}" data-k="${d.key}" title="${fmtDay(d.key)}: ${STATE[d.state]}${d.pg ? " (playground)" : ""}">${+d.key.slice(8)}${d.pg ? "*" : ""}</button>`).join("")}</div>
<div class="legend"><span class="chip static"><b class="good">■</b> submitted · ${counts.sub}</span><span class="chip static">⋯ not submitted · ${counts.cont}</span><span class="chip static">▨ missed · ${counts.miss}</span>${pgDays.length ? `<span class="chip static" title="${PG}">* playground · ${pgDays.length}</span>` : ""}</div>
</div>
<div class="card"><div class="card-head"><div><h2>Model and inputs</h2><p class="sub">Latest run ${esc(T.last_run.slice(0, 16).replace("T", " "))} UTC · ${T.runs} run${T.runs === 1 ? "" : "s"} published</p></div><span class="pill ${T.env === "production" ? "" : "warn"}">${esc(T.env)}</span></div>
<p><span class="mono">${esc(T.model)}</span>: t0-gamma (The Forecasting Company, Apache-2.0), zero-shot, 90 days of context, with covariates. D+2 to D+10 fall back to slot climatology. <a href="#backtest">Backtest results</a></p>
<div class="chips">${(T.features.length ? T.features : ["(feature list arrives with the first published run)"]).map(f => `<span class="chip static">${esc(f)}</span>`).join("")}</div>
</div>
</div>
<div class="card"><div class="card-head"><div><h2>Daily scores</h2><p class="sub">Every target day, newest first. Click a row to show it in the chart.</p></div></div>
<div class="tablewrap" style="max-height:520px" id="lv-daily"></div></div>`;
const data = [x, e10, e90, s50, T.q50, T.b50, T.obs, t50];
const span = x[n - 1] - x[0];
let range = "7";
const setRange = r => {
range = r;
const max = x[n - 1] + STEP, min = r === "all" ? x[0] : Math.max(x[0], max - +r * 86400);
main.inst.setScale("x", { min, max });
};
const shown = [true, true, true, true, true, false, true, hasTso];
const main = mount(root.querySelector("#lv-chart"), (w, h) => new uPlot({
width: w, height: h, tzDate: tzUTC, legend: { show: false },
scales: { x: { time: true } },
cursor: { drag: { x: true, y: false }, points: { size: 7, width: 2, fill: css("--surface") }, bind: { dblclick: () => () => { setRange(range); return null; } } },
series: [{},
{ stroke: css("--accent"), width: 1, dash: [2, 3], points: { show: false } },
{ stroke: css("--accent"), width: 1, dash: [2, 3], points: { show: false } },
line("Submitted", css("--sub"), { width: 6 }),
line("Model", css("--cont"), { width: 2, dash: [2, 4], cap: "round" }),
line("Climatology", css("--base"), { width: 1.5, dash: [6, 4] }),
line("Observed", css("--obs"), { width: 2 }),
line("TSO forecast", css("--tso"), { width: 1.5, dash: [8, 3, 2, 3] }),
].map((s, i) => (i ? { ...s, show: shown[i] } : s)),
bands: [{ series: [2, 1], fill: css("--band") }],
axes: axes(`${LABEL[target]} (${unit})`),
hooks: { setScale: [(u, k) => k === "x" && summary(u.scales.x.min, u.scales.x.max)] },
plugins: [shade(pgDays.map(d => [x[d.i0], x[d.i1] + STEP])), nowMarker(Date.now() / 1000), tooltip((u, i) => {
const rows = [], on = k => u.series[k].show;
if (on(6) && T.obs[i] != null) rows.push([css("--obs"), "Observed", `${f1(T.obs[i])} ${unit}`]);
if (on(3) && s50[i] != null) rows.push([css("--sub"), "Submitted", `${f1(s50[i])} ${unit}`]);
if (on(4) && T.q50[i] != null) rows.push([css("--cont"), "Model", `${f1(T.q50[i])} ${unit}`, true]);
if (on(1) && e10[i] != null) rows.push([css("--accent"), "q10–q90", `${f1(e10[i])} … ${f1(e90[i])}`]);
if (on(5) && T.b50[i] != null) rows.push([css("--base"), "Climatology", `${f1(T.b50[i])} ${unit}`]);
if (on(7) && t50[i] != null) rows.push([css("--tso"), `${m.source} forecast`, `${f1(t50[i])} ${unit}`]);
return rows.length ? ttHTML(fmtUTC(x[i]) + (dayInfo.get(madrid(x[i]).day)?.pg ? " · playground" : ""), rows) : null;
})],
}, data, root.querySelector("#lv-chart")), chartHeight);
main.keepX = true;
setRange(span < 7 * 86400 ? "all" : "7");
onSeg(root, "lv-r", setRange);
root.querySelector("#lv-legend").addEventListener("click", e => {
const b = e.target.closest("button"); if (!b) return;
const on = b.getAttribute("aria-pressed") !== "true";
b.setAttribute("aria-pressed", String(on));
b.dataset.s.split(",").forEach(i => { shown[+i] = on; main.inst.setSeries(+i, { show: on }); });
});
const zoomDay = k => { const d = dayInfo.get(k); main.inst.setScale("x", { min: x[d.i0], max: x[d.i1] + STEP }); };
root.querySelector("#lv-days").addEventListener("click", e => { const b = e.target.closest("button"); if (b) zoomDay(b.dataset.k); });
// Summary table: the days in the chart's view; ratios only on days with a baseline.
const METRICS = [["rmse", "RMSE (q50)", f1, unit], ["mae", "MAE (q50)", f1, unit], ["winkler", "Winkler (q10–q90)", f1, unit], ["coverage", "Coverage (q10–q90)", pct, "target 80%"]];
const covClass = c => (c == null ? "" : Math.abs(c - 0.8) <= 0.05 ? "good" : "warn");
function summary(lo, hi) {
const inView = model.filter(d => d.t >= lo - 3600 && d.t < hi), both = inView.filter(d => base.has(d.key));
root.querySelector("#lv-span").textContent = inView.length
? `${inView.length} scored day${inView.length === 1 ? "" : "s"} in view: ${fmtDay(inView[0].key)} – ${fmtDay(inView[inView.length - 1].key)}`
: "No measured days in view yet.";
root.querySelector("#lv-sum").innerHTML = `<table><thead><tr><th>Metric</th><th class="n">Our forecast</th><th class="n">Climatology</th><th class="n">Ratio</th><th>Unit</th></tr></thead><tbody>${METRICS.map(([m, label, f, u]) => {
const ours = mean(inView.map(d => d[m])), clim = mean(both.map(d => base.get(d.key)[m]));
const r = m === "coverage" || !both.length ? null : mean(both.map(d => d[m])) / clim;
return `<tr><td>${label}</td><td class="n ${m === "coverage" ? covClass(ours) : ""}"><b>${f(ours)}</b></td><td class="n muted">${f(clim)}</td><td class="n ${relClass(r)}">${r == null ? "–" : f3(r)}</td><td class="muted">${u}</td></tr>`;
}).join("")}</tbody></table>`;
}
summary(main.inst.scales.x.min, main.inst.scales.x.max);
// Official scores: one column per variable and metric, value with our rank among N.
const off = T.official ?? [], offEl = root.querySelector("#lv-off");
if (!off.length) offEl.innerHTML = `<p class="empty">No official scores yet. Predico scores a day once its measurements are in, usually a day or two after delivery.</p>`;
else {
const cols = [...new Set(off.map(r => `${r.variable} ${r.metric}`))].sort();
const byDay = new Map();
for (const r of off) (byDay.get(r.day) ?? byDay.set(r.day, {}).get(r.day))[`${r.variable} ${r.metric}`] = r;
const days = [...byDay.keys()].sort().reverse();
const rankCell = r => (r ? `<td class="n ${r.rank <= 3 ? `m${r.rank}` : ""}">${f1(r.value)} <span class="muted">·</span> ${r.rank <= 3 ? `<span class="medal">${r.rank}</span>` : r.rank}<span class="muted">/${r.participants}</span></td>` : `<td class="n muted">–</td>`);
const latest = byDay.get(days[0]);
offEl.innerHTML = `<div class="kpis">${cols.map(c => { const r = latest[c]; return kpi(`${c} · ${fmtDay(days[0])}`, r ? `${r.rank}<span class="muted" style="font-size:14px"> / ${r.participants}</span>` : "–", r ? `score ${f1(r.value)}` : ""); }).join("")}</div>
<div class="tablewrap" style="max-height:420px"><table><thead><tr><th>Day</th>${cols.map(c => `<th class="n">${esc(c)} <span class="muted">· rank</span></th>`).join("")}</tr></thead><tbody>${days.map(d => `<tr><td class="mono">${fmtDay(d)}</td>${cols.map(c => rankCell(byDay.get(d)[c])).join("")}</tr>`).join("")}</tbody></table></div>`;
}
// Daily table, newest first; days without measurements still show their status.
const scoredDay = new Map(model.map(d => [d.key, d]));
const cell = (d, b, m, f) => {
const v = d?.[m], c = b?.[m], r = m === "coverage" || v == null || c == null ? null : v / c;
return `<td class="n ${m === "coverage" ? covClass(v) : ""}">${f(v)}</td><td class="n muted">${f(c)}</td>${m === "coverage" ? "" : `<td class="n ${relClass(r)}">${r == null ? "–" : f3(r)}</td>`}`;
};
root.querySelector("#lv-daily").innerHTML = `<table><thead>
<tr><th rowspan="2">Day</th><th rowspan="2">Status</th><th class="n" colspan="3">RMSE (${unit})</th><th class="n" colspan="3">MAE (${unit})</th><th class="n" colspan="3">Winkler (${unit})</th><th class="n" colspan="2">Coverage</th></tr>
<tr>${["ours", "clim.", "ratio", "ours", "clim.", "ratio", "ours", "clim.", "ratio", "ours", "clim."].map(h => `<th class="n">${h}</th>`).join("")}</tr></thead><tbody>${[...record].reverse().map(r => {
const d = scoredDay.get(r.key), b = base.get(r.key);
return `<tr class="click${r.pg ? " pg" : ""}" data-k="${r.key}"><td class="mono">${fmtDay(r.key)}${r.pg ? `<span title="${PG}">*</span>` : ""}</td><td><span class="pill ${r.state === "sub" ? "" : r.state === "cont" ? "warn" : "muted"}">${STATE[r.state]}${r.pg ? " · playground" : ""}</span></td>${d ? cell(d, b, "rmse", f1) + cell(d, b, "mae", f1) + cell(d, b, "winkler", f1) + cell(d, b, "coverage", pct) : `<td class="muted" colspan="11">${r.state === "miss" ? "no forecast" : "awaiting measurements"}</td>`}</tr>`;
}).join("")}</tbody></table>`;
root.querySelector("#lv-daily").addEventListener("click", e => { const tr = e.target.closest("tr[data-k]"); if (tr) zoomDay(tr.dataset.k); });
}
}
// --- Backtest ----------------------------------------------------------------------------
function leaderboard(days, baseline) {
const rows = {};
for (const [t, d] of Object.entries(days)) {
for (const [c, s] of Object.entries(d.configs)) {
const r = (rows[c] ??= { config: c });
for (const m of ["rmse", "winkler"]) {
const a = s[m], b = d.configs[baseline][m];
let sa = 0, sb = 0, nb = 0;
for (let i = 0; i < a.length; i++) if (a[i] != null && b[i] != null) { sa += a[i]; sb += b[i]; nb++; }
r[`${t} ${m}`] = mean(a);
r[`${t} ${m} rel`] = nb ? sa / sb : null;
}
r[`${t} coverage`] = mean(s.coverage);
r[`${t} days`] = s.rmse.filter(v => v != null).length;
}
}
const out = Object.values(rows);
for (const r of out) {
const rel = Object.keys(r).filter(k => k.endsWith(" rel")).map(k => r[k]);
r.score = rel.every(v => v) ? Math.exp(mean(rel.map(Math.log))) : null;
}
out.sort((a, b) => (a.score ?? 9) - (b.score ?? 9));
out.forEach((r, i) => (r.rank = r.score == null ? null : i + 1));
return out;
}
async function renderBacktest(root) {
const [allDays, meta, prof, wxAll] = await Promise.all([json("days.json"), json("meta.json"), json("profiles.json"), json("weather.json").catch(() => ({}))]);
const m = mk(), wx = (wxAll[m.id]?.models ?? []).map(r => r.name), targets = shortsOf(m, meta).filter(t => allDays[t]);
if (!targets.length) {
root.innerHTML = `<div class="head"><div><h1>Backtest</h1></div></div><div class="card"><p class="empty">No backtest of ${esc(m.name)} is published yet.</p></div>`;
return;
}
const days = Object.fromEntries(targets.map(t => [t, allDays[t]]));
CONFIGS = meta.configs.map(c => c.name).filter(c => targets.some(t => days[t].configs[c]));
const type = Object.fromEntries(meta.configs.map(c => [c.name, c.type]));
const info = Object.fromEntries(CONFIGS.map(c => [c, parseConfig(c, wx)]));
const label = c => cfgLabel(c, wx);
const lb = leaderboard(days, meta.baseline), t0 = targets[0], live = meta.live_config[m.id];
const top = lb.filter(r => r.config !== live && r.config !== meta.baseline).slice(0, 2).map(r => r.config);
let sel = [live, meta.baseline, ...top].filter(c => CONFIGS.includes(c)), types = new Set(), absolute = false, sortK = "rank", sortDir = 1;
root.innerHTML = `
<div class="head"><div><h1>Backtest</h1><p class="lede">Test year 1 Nov 2025 – 31 Oct 2026. Each forecast is made at 14:00 ${m.city} on D-1 from data published before then, and scored per local day as Predico does. <b>Lower is better.</b> ${m.missingTruth}</p></div></div>
<div class="card"><div class="card-head"><div><h2>Leaderboard</h2><p class="sub">Scores relative to <span class="mono">${esc(meta.baseline)}</span> (= 1.000) on the days both are scored. Highlighted: the live config. Hover a model for its config name; click a column to sort.</p></div>
<div class="controls"><div class="chips" id="bt-types">${["baseline", "TSO forecast", "foundation model", "ML"].filter(t => CONFIGS.some(c => type[c] === t)).map(t => `<button class="chip" data-v="${t}" aria-pressed="false">${t}</button>`).join("")}</div>
<label class="tog"><input type="checkbox" id="bt-abs"> absolute scores</label></div></div>
<div class="tablewrap" id="bt-lb"></div>
<details><summary class="sub">How the scores work</summary><ul class="sub">
<li><b>RMSE</b> (Predico's q50 score): √(mean (obs − q50)²) over a local day.</li>
<li><b>Winkler</b> (Predico's q10/q90 score, α = 0.2): mean of (q90 − q10) + 10·max(0, q10 − obs) + 10·max(0, obs − q90). Rewards narrow intervals, penalises misses.</li>
<li><b>Coverage</b>: share of quarter-hours inside [q10, q90]; a calibrated model gets 80%.</li>
<li><b>Score</b>: geometric mean of the ${targets.length > 1 ? "four normalised values (RMSE and Winkler, both targets)" : "two normalised values (RMSE and Winkler)"}; rank sorts by it.</li></ul></details>
</div>
<div class="card"><div class="card-head"><div><h2>Configs in the charts below</h2><p class="sub">Same colour = same model family; dash = variant.</p></div></div><div class="chips" id="bt-sel"></div></div>
<div class="card"><div class="card-head"><div><h2>Day viewer</h2><p class="sub">The configs selected above, line styles as there. Times in UTC.</p></div>
<div class="controls">${targetSeg("dv-t", targets, t0)}<input type="date" id="dv-d" value="${days[t0].days.at(-1)}" min="${days[t0].days[0]}" max="${days[t0].days.at(-1)}"></div></div>
<div class="chart" id="dv-chart"></div>
<div class="legend sub"><span>lines: q50 of each config</span><span class="legend-i"><span class="sw box"></span>q10–q90 (first selected)</span><span class="legend-i"><span class="sw" style="border-color:var(--obs)"></span>actual</span></div>
<div class="legend sub" id="dv-s"></div></div>
<div class="card"><div class="card-head"><div><h2>By month</h2></div><div class="controls">${targetSeg("mo-t", targets, t0)}${seg("mo-m", [["rmse", "RMSE"], ["winkler", "Winkler"]], "rmse")}</div></div><div class="chart" id="mo-chart"></div></div>
<div class="card"><div class="card-head"><div><h2>Absolute error of q50 by hour and weekday</h2><p class="sub">${m.city} time.</p></div><div class="controls">${targetSeg("pr-t", targets, t0)}</div></div>
<div class="grid2"><div class="chart" id="pr-h"></div><div class="chart" id="pr-w"></div></div></div>
<div class="card"><div class="card-head"><div><h2>Win rate</h2><p class="sub">Cell (row A, column B): share of days scored for both where A has the lower score. Read across a row.</p></div>
<div class="controls">${targetSeg("wr-t", targets, t0)}${seg("wr-m", [["rmse", "RMSE"], ["winkler", "Winkler"]], "rmse")}</div></div>
<div class="tablewrap" id="wr"></div></div>`;
const lbEl = root.querySelector("#bt-lb");
// Sortable columns: [key, header, value for sorting, cell html, numeric].
const yesNo = v => (v == null ? "–" : v ? "yes" : "no");
function drawLb() {
const rows = lb.filter(r => !types.size || types.has(type[r.config]));
const cols = targets.flatMap(t => [[`${t} rmse rel`, `${LABEL[t]} RMSE`, f3], [`${t} winkler rel`, `${LABEL[t]} Winkler`, f3], [`${t} coverage`, `${LABEL[t]} cov.`, pct],
...(absolute ? [[`${t} rmse`, `${LABEL[t]} RMSE abs`, f1], [`${t} winkler`, `${LABEL[t]} Winkler abs`, f1], [`${t} days`, "days", String]] : [])]);
const notes = rows.some(r => info[r.config].notes.length), weather = rows.some(r => info[r.config].weather?.length);
const COLS = [
["rank", "Rank", r => r.rank, r => (r.rank <= 3 ? `<span class="medal" title="${["Gold", "Silver", "Bronze"][r.rank - 1]}">${r.rank}</span>` : r.rank ?? "–"), true],
["model", "Model", r => info[r.config].model, r => `<span title="${esc(r.config)}">${esc(info[r.config].model)}</span>${r.config === live ? ' <span class="pill">live</span>' : ""}`],
["cov", "Covariates", r => yesNo(info[r.config].cov), r => yesNo(info[r.config].cov)],
["ctx", "Context", r => info[r.config].ctx, r => (info[r.config].ctx == null ? "–" : `${info[r.config].ctx} d`), true],
...(weather ? [["wx", "Weather", r => info[r.config].weather?.join(", ") ?? "", r => esc(info[r.config].weather?.join(", ") || "–")]] : []),
...(notes ? [["notes", "Notes", r => info[r.config].notes.join(", "), r => esc(info[r.config].notes.join(", "))]] : []),
["type", "Type", r => type[r.config], r => esc(type[r.config])],
["score", "Score", r => r.score, r => `<b>${f3(r.score)}</b>`, true],
...cols.map(([k, h, f]) => [k, h, r => r[k], r => f(r[k]), true]),
];
const col = COLS.find(c => c[0] === sortK) ?? COLS[0];
const val = r => col[2](r);
const sorted = [...rows].sort((a, b) => {
const x = val(a), y = val(b);
if (x == null || x === "") return 1; if (y == null || y === "") return -1; // empty last
return (col[4] ? x - y : String(x).localeCompare(String(y))) * sortDir;
});
const cls = k => (k === "score" || k.endsWith("rel") ? "rel" : "");
lbEl.innerHTML = `<table><thead><tr>${COLS.map(([k, h, , , num]) => `<th class="${num ? "n" : ""}" data-k="${k}" style="cursor:pointer" aria-sort="${k === sortK ? (sortDir > 0 ? "ascending" : "descending") : "none"}">${h}${k === sortK ? (sortDir > 0 ? " ↑" : " ↓") : ""}</th>`).join("")}</tr></thead><tbody>${sorted.map(r =>
`<tr class="${r.config === live ? "live" : ""} ${r.rank <= 3 ? `m${r.rank}` : ""}">${COLS.map(([k, , v, html, num]) => `<td class="${num ? "n" : ""} ${cls(k) ? relClass(v(r)) : ["cov", "ctx", "type", "wx", "notes"].includes(k) ? "muted" : ""}">${html(r)}</td>`).join("")}</tr>`).join("")}</tbody></table>`;
}
lbEl.addEventListener("click", e => {
const th = e.target.closest("th[data-k]"); if (!th) return;
sortDir = th.dataset.k === sortK ? -sortDir : 1; sortK = th.dataset.k;
drawLb();
});
drawLb();
root.querySelector("#bt-types").addEventListener("click", e => {
const b = e.target.closest("button"); if (!b) return;
types.has(b.dataset.v) ? types.delete(b.dataset.v) : types.add(b.dataset.v);
b.setAttribute("aria-pressed", String(types.has(b.dataset.v)));
drawLb();
});
root.querySelector("#bt-abs").addEventListener("change", e => { absolute = e.target.checked; drawLb(); });
const selEl = root.querySelector("#bt-sel");
const drawSel = () => (selEl.innerHTML = lb.map(r => `<button class="chip legend-i" data-v="${r.config}" title="${esc(r.config)}" aria-pressed="${sel.includes(r.config)}">${swatch(r.config)} ${esc(label(r.config))}</button>`).join(""));
drawSel();
selEl.addEventListener("click", e => {
const b = e.target.closest("button"); if (!b) return;
sel = sel.includes(b.dataset.v) ? sel.filter(c => c !== b.dataset.v) : [...sel, b.dataset.v];
b.setAttribute("aria-pressed", String(sel.includes(b.dataset.v)));
redrawSel();
});
const cfgSeries = c => { const st = configStyle(c); return line(label(c), st.color, { dash: st.dash, width: c === live ? 2.5 : 1.75 }); };
// By month
let moT = t0, moM = "rmse";
const months = t => [...new Set(days[t].days.map(d => d.slice(0, 7)))];
const mo = mount(root.querySelector("#mo-chart"), (w, h) => {
const ms = months(moT), x = ms.map((m, i) => i);
const ys = sel.map(c => ms.map(m => mean(days[moT].days.map((d, i) => (d.startsWith(m) ? days[moT].configs[c]?.[moM][i] : null)))));
return new uPlot({ width: w, height: h, legend: { show: false }, scales: { x: { time: false } },
cursor: { points: { size: 6, width: 2, fill: css("--surface") } },
series: [{}, ...sel.map(c => ({ ...cfgSeries(c), points: { show: true, size: 5 } }))],
axes: axes(`${moM === "rmse" ? "RMSE" : "Winkler"} (${meta.units[moT]})`, (u, s) => s.map(i => (Number.isInteger(i) && ms[i] ? `${MON[+ms[i].slice(5) - 1]}\n${ms[i].slice(0, 4)}` : "")), { space: 40 }),
plugins: [tooltip(seriesRows(i => { const m = ms[i]; return `${MON[+m.slice(5) - 1]} ${m.slice(0, 4)}`; }))],
}, [x, ...ys], root.querySelector("#mo-chart"));
}, 300);
onSeg(root, "mo-t", v => { moT = v; rebuild(mo); });
onSeg(root, "mo-m", v => { moM = v; rebuild(mo); });
// Profiles
let prT = t0;
const prof1 = (el, key, labels) => mount(el, (w, h) => {
const x = labels.map((_, i) => i);
return new uPlot({ width: w, height: h, legend: { show: false }, scales: { x: { time: false } }, cursor: { points: { size: 6, width: 2, fill: css("--surface") } },
series: [{}, ...sel.map(cfgSeries)],
axes: axes(`MAE (${meta.units[prT]})`, (u, s) => s.map(i => labels[i] ?? ""), { space: 34 }),
plugins: [tooltip(seriesRows(i => labels[i]))],
}, [x, ...sel.map(c => prof[prT][c]?.[key] ?? x.map(() => null))], el);
}, 260);
const ph = prof1(root.querySelector("#pr-h"), "hour", Array.from({ length: 24 }, (_, h) => `${pad(h)}:00`));
const pw = prof1(root.querySelector("#pr-w"), "weekday", WEEKDAYS);
onSeg(root, "pr-t", v => { prT = v; rebuild(ph); rebuild(pw); });
// Day viewer
let dvT = t0;
const target = await json(`target/${mk().id}.json`);
const off = (meta.start - target.start) / STEP;
const x = Array.from({ length: meta.n }, (_, i) => meta.start + i * STEP);
const dayIdx = new Map();
x.forEach((s, i) => { const k = madrid(s).day; const r = dayIdx.get(k); r ? (r[1] = i) : dayIdx.set(k, [i, i]); });
const dv = { el: root.querySelector("#dv-chart") };
async function drawDay() {
const key = root.querySelector("#dv-d").value, r = dayIdx.get(key);
if (!r) return;
const steps = await Promise.all(sel.map(c => json(`steps/${dvT}/${c}.json`)));
const [i0, i1] = r, sl = a => a.slice(i0, i1 + 1);
const xs = sl(x), obs = target[dvT].slice(off + i0, off + i1 + 1);
const k = days[dvT].days.indexOf(key);
root.querySelector("#dv-s").innerHTML = k < 0 ? "" : `<span>This day:</span>${sel.map(c => `<span class="legend-i" title="${esc(label(c))}">${swatch(c)} RMSE ${f1(days[dvT].configs[c].rmse[k])} · Winkler ${f1(days[dvT].configs[c].winkler[k])}</span>`).join("")}`;
const first = steps[0];
const build = (w, h) => new uPlot({
width: w, height: h, tzDate: tzUTC, legend: { show: false }, scales: { x: { time: true } },
cursor: { points: { size: 6, width: 2, fill: css("--surface") } },
series: [{}, { stroke: "transparent", points: { show: false } }, { stroke: "transparent", points: { show: false } }, ...sel.map(cfgSeries), line("Actual", css("--obs"), { width: 2.25 })],
bands: first ? [{ series: [2, 1], fill: css("--band") }] : [],
axes: axes(`${LABEL[dvT]} (${meta.units[dvT]})`),
plugins: [tooltip(seriesRows(fmtUTC))],
}, [xs, first ? sl(first.q10) : xs.map(() => null), first ? sl(first.q90) : xs.map(() => null), ...steps.map(s => sl(s.q50)), obs], dv.el);
if (dv.c) { dv.c.build = () => build(dv.el.clientWidth, chartHeight(dv.el.clientWidth)); rebuild(dv.c); } else dv.c = mount(dv.el, build, chartHeight);
}
onSeg(root, "dv-t", v => { dvT = v; drawDay(); });
root.querySelector("#dv-d").addEventListener("change", drawDay);
await drawDay();
function redrawSel() { rebuild(mo); rebuild(ph); rebuild(pw); drawDay(); drawWr(); }
// Win rate
let wrT = t0, wrM = "rmse";
function drawWr() {
const order = lb.map(r => r.config).filter(c => sel.includes(c));
const d = days[wrT].configs;
const cell = (a, b) => { let w = 0, n = 0; const A = d[a][wrM], B = d[b][wrM]; for (let i = 0; i < A.length; i++) if (A[i] != null && B[i] != null) { n++; if (A[i] < B[i]) w++; } return n ? w / n : null; };
root.querySelector("#wr").innerHTML = order.length < 2 ? `<p class="empty">Select at least two configs above.</p>` :
`<table><thead><tr><th>A \\ B</th>${order.map(c => `<th class="n" title="${esc(c)}" style="white-space:normal;min-width:8em">${esc(label(c))}</th>`).join("")}</tr></thead><tbody>${order.map(a => `<tr><td title="${esc(a)}" style="white-space:nowrap">${esc(label(a))}</td>${order.map(b => {
if (a === b) return `<td></td>`;
const v = cell(a, b), g = v == null ? 0 : (v - 0.5) * 2;
const bg = g >= 0 ? `color-mix(in srgb, var(--accent) ${Math.round(g * 70)}%, transparent)` : `color-mix(in srgb, var(--bad) ${Math.round(-g * 55)}%, transparent)`;
return `<td class="n" style="background:${bg}">${pct(v)}</td>`;
}).join("")}</tr>`).join("")}</tbody></table>`;
}
drawWr();
onSeg(root, "wr-t", v => { wrT = v; drawWr(); });
onSeg(root, "wr-m", v => { wrM = v; drawWr(); });
}
function rebuild(c) { c.inst?.destroy(); c.inst = c.build(); }
// --- Target data -------------------------------------------------------------------------
async function renderTarget(root) {
const [target, meta] = await Promise.all([json(`target/${mk().id}.json`), json("meta.json")]);
const mkt = mk(), shorts = shortsOf(mkt, meta), scatter = "price" in target;
const n = target[shorts[0]].length, x = Array.from({ length: n }, (_, i) => target.start + i * STEP);
const cal = x.map(madrid);
let t = shorts[0];
root.innerHTML = `
<div class="head"><div><h1>Target data</h1><p class="lede">${mkt.about(fmtDay(cal[0].day), fmtDay(cal[n - 1].day))}</p></div>
<div class="controls">${targetSeg("tg-t", shorts, t)}</div></div>
<div class="kpis" id="tg-k"></div>
<div class="card"><div class="card-head"><div><h2>Time series</h2><p class="sub">Every quarter-hour of the history. Times in UTC.</p></div><div class="controls">${seg("tg-r", [["14", "14 days"], ["90", "90 days"], ["all", "All"]], "90")}</div></div>
<div class="chart" id="tg-ts"></div><div class="foot"><span></span><span class="hint">drag to zoom · double-click to reset</span></div></div>
<div class="card"><div class="card-head"><div><h2>Typical patterns</h2><p class="sub">Mean (line) and q10–q90 (band) over the whole history, ${mkt.city} time.</p></div></div>
<div class="grid3"><div class="chart" id="tg-p1"></div><div class="chart" id="tg-p2"></div><div class="chart" id="tg-p3"></div></div></div>
<div class="grid2">
<div class="card"><div class="card-head"><div><h2>Distribution</h2><p class="sub">Clipped to the 0.5–99.5% range for display.</p></div></div><div class="chart" id="tg-h"></div></div>
${scatter ? `<div class="card"><div class="card-head"><div><h2>Price vs energy</h2><p class="sub">A random sample of 5,000 quarter-hours.</p></div></div><canvas class="scatter" id="tg-sc" role="img" aria-label="Scatter of mFRR price against net mFRR energy"></canvas></div>` : ""}
</div>
<div class="card"><div class="card-head"><div><h2>Per day</h2><p class="sub" id="tg-ms"></p></div><div class="controls">${seg("tg-mk", [["miss", "Missing"], ["zero", "Exact zeros"]], "miss")}</div></div>
<div style="overflow-x:auto"><div class="heat" id="tg-heat"></div></div></div>`;
let range = "90", kind = "miss";
const ts = mount(root.querySelector("#tg-ts"), (w, h) => new uPlot({
width: w, height: h, tzDate: tzUTC, legend: { show: false }, scales: { x: { time: true } },
cursor: { drag: { x: true, y: false }, points: { size: 6, width: 2, fill: css("--surface") }, bind: { dblclick: () => () => { setR(range); return null; } } },
series: [{}, line(LABEL[t], css("--accent-ink"), { width: 1.25 })],
axes: axes(`${LABEL[t]} (${meta.units[t]})`),
plugins: [tooltip(seriesRows(fmtUTC))],
}, [x, target[t]], root.querySelector("#tg-ts")), chartHeight);
ts.keepX = true;
const setR = r => { range = r; ts.inst.setScale("x", { min: r === "all" ? x[0] : x[n - 1] - +r * 86400, max: x[n - 1] }); };
onSeg(root, "tg-r", setR);
function quant(a, q) { if (!a.length) return null; const s = Float64Array.from(a).sort(); const p = (s.length - 1) * q, lo = Math.floor(p); return s[lo] + (s[Math.min(lo + 1, s.length - 1)] - s[lo]) * (p - lo); }
function profile(keyFn, keys) {
const g = new Map(keys.map(k => [k, []]));
target[t].forEach((v, i) => { if (v != null) g.get(keyFn(i))?.push(v); });
return { mean: keys.map(k => mean(g.get(k))), q10: keys.map(k => quant(g.get(k), 0.1)), q90: keys.map(k => quant(g.get(k), 0.9)) };
}
const months = [...new Set(cal.map(c => c.month))];
const monthIdx = new Map(months.map((m, i) => [m, i]));
const monthName = i => `${MON[+months[i].slice(5) - 1]} ${months[i].slice(0, 4)}`;
const PROFILES = [
["tg-p1", "Quarter-hour of day", i => cal[i].hour * 4 + Math.floor((x[i] % 3600) / 900), Array.from({ length: 96 }, (_, i) => i), i => `${pad(i / 4)}:00`, i => `${pad(Math.floor(i / 4))}:${pad((i % 4) * 15)}`, 16],
["tg-p2", "Weekday", i => cal[i].wd, [0, 1, 2, 3, 4, 5, 6], i => WEEKDAYS[i], i => WEEKDAYS[i], 1],
["tg-p3", "Month", i => monthIdx.get(cal[i].month), months.map((_, i) => i), i => `${MON[+months[i].slice(5) - 1]}\n${months[i].slice(2, 4)}`, monthName, 3],
];
const profCharts = PROFILES.map(([id, title, keyFn, keys, tick, ttl, step]) => mount(root.querySelector(`#${id}`), (w, h) => {
const p = profile(keyFn, keys);
return new uPlot({ width: w, height: h, title, legend: { show: false }, scales: { x: { time: false } }, cursor: { points: { size: 5, width: 2, fill: css("--surface") } },
series: [{}, { stroke: css("--accent"), width: 1, points: { show: false } }, { stroke: css("--accent"), width: 1, points: { show: false } }, line("Mean", css("--accent-ink"), { width: 2 })],
bands: [{ series: [2, 1], fill: css("--band") }],
axes: axes("", (u, s) => s.map(i => (Number.isInteger(i) && i in keys ? tick(i) : "")), { space: 20, incrs: [step] }),
plugins: [tooltip((u, i) => ttHTML(ttl(i), [[css("--accent-ink"), "Mean", f1(p.mean[i])], [css("--accent"), "q10–q90", `${f1(p.q10[i])} … ${f1(p.q90[i])}`]]))],
}, [keys, p.q10, p.q90, p.mean], root.querySelector(`#${id}`));
}, 230));
function stats() {
const v = target[t].filter(a => a != null), s = Float64Array.from(v).sort(), m = mean(v);
const sd = Math.sqrt(mean(v.map(a => (a - m) ** 2)));
root.querySelector("#tg-k").innerHTML = kpi("Mean", f1(m), meta.units[t]) + kpi("Std", f1(sd)) +
kpi("Exact zeros", pct(v.filter(a => a === 0).length / v.length)) + kpi("Negative", pct(v.filter(a => a < 0).length / v.length)) +
kpi("Min / max", `${minus(Math.round(s[0]))} / ${Math.round(s[s.length - 1])}`);
return s;
}
const hist = mount(root.querySelector("#tg-h"), (w, h) => {
const s = stats(), lo = quant(s, 0.005), hi = quant(s, 0.995), bins = 80, width = (hi - lo) / bins;
const counts = new Array(bins).fill(0);
for (const v of s) counts[Math.min(bins - 1, Math.max(0, Math.floor((Math.min(Math.max(v, lo), hi) - lo) / width)))]++;
const centers = counts.map((_, i) => lo + (i + 0.5) * width);
return new uPlot({ width: w, height: h, legend: { show: false }, scales: { x: { time: false } }, cursor: { points: { show: false } },
series: [{}, { label: "Quarter-hours", stroke: css("--accent-ink"), fill: css("--band"), paths: uPlot.paths.bars({ size: [0.92, 100] }), points: { show: false } }],
axes: axes("Quarter-hours", (u, sp) => sp.map(v => minus(Math.round(v))), { space: 50 }),
plugins: [tooltip((u, i) => ttHTML(`${f1(centers[i] - width / 2)} … ${f1(centers[i] + width / 2)}`, [[css("--accent-ink"), "Quarter-hours", counts[i].toLocaleString("en-GB")]]))],
}, [centers, counts], root.querySelector("#tg-h"));
}, 280);
// Scatter on a plain canvas (5,000 points; static). Spain only: price vs energy.
const scEl = root.querySelector("#tg-sc");
const pairs = []; if (scatter) for (let i = 0; i < n; i++) if (target.energy[i] != null && target.price[i] != null) pairs.push([target.energy[i], target.price[i]]);
let seed = 1; const rnd = () => ((seed = (seed * 16807) % 2147483647) / 2147483647);
const sample = Array.from({ length: Math.min(5000, pairs.length) }, () => pairs[Math.floor(rnd() * pairs.length)]);
const drawScatter = w => {
const dpr = devicePixelRatio || 1, h = 360; scEl.width = w * dpr; scEl.height = h * dpr;
const ctx = scEl.getContext("2d"); ctx.scale(dpr, dpr); ctx.clearRect(0, 0, w, h);
const xs = sample.map(p => p[0]), ys = sample.map(p => p[1]);
const qx = [quant(xs, 0.002), quant(xs, 0.998)], qy = [quant(ys, 0.002), quant(ys, 0.998)];
const L = 58, B = 40, X = v => L + ((v - qx[0]) / (qx[1] - qx[0])) * (w - L - 12), Y = v => h - B - ((v - qy[0]) / (qy[1] - qy[0])) * (h - B - 10);
ctx.font = `11px ${css("--mono")}`; ctx.fillStyle = css("--ink-soft"); ctx.strokeStyle = css("--grid");
for (let k = 0; k <= 4; k++) {
const vx = qx[0] + ((qx[1] - qx[0]) * k) / 4, vy = qy[0] + ((qy[1] - qy[0]) * k) / 4;
ctx.beginPath(); ctx.moveTo(X(vx), 10); ctx.lineTo(X(vx), h - B); ctx.stroke(); ctx.fillText(minus(Math.round(vx)), X(vx) - 14, h - B + 16);
ctx.beginPath(); ctx.moveTo(L, Y(vy)); ctx.lineTo(w - 12, Y(vy)); ctx.stroke(); ctx.fillText(minus(Math.round(vy)), 8, Y(vy) + 4);
}
ctx.font = `12px ${css("--sans")}`;
ctx.fillText("Net mFRR energy (MWh)", L, h - 6);
ctx.fillStyle = css("--accent-ink"); ctx.globalAlpha = 0.28;
for (const [a, b] of sample) if (a >= qx[0] && a <= qx[1] && b >= qy[0] && b <= qy[1]) ctx.fillRect(X(a) - 1.2, Y(b) - 1.2, 2.4, 2.4);
ctx.globalAlpha = 1; ctx.fillStyle = css("--ink-soft"); ctx.fillText("Price (EUR/MWh) ↑", L + 6, 22);
};
if (scatter) mount(scEl, w => { drawScatter(w); return { destroy() {}, setSize: ({ width }) => drawScatter(width) }; }, 360);
function heat() {
const per = new Map();
target[t].forEach((v, i) => { const k = cal[i].day; per.set(k, (per.get(k) ?? 0) + (kind === "miss" ? v == null : v === 0)); });
const max = Math.max(1, ...per.values()), total = [...per.values()].reduce((a, b) => a + b, 0);
root.querySelector("#tg-ms").textContent = `${kind === "miss" ? "Missing" : "Exactly zero"} quarter-hours per day: ${total.toLocaleString("en-GB")} of ${n.toLocaleString("en-GB")} (${pct(total / n)}).${t === "energy" && kind === "miss" ? " Energy gaps were filled with 0 upstream: use Exact zeros to see them." : ""}`;
let html = `<span class="lab"></span>${Array.from({ length: 31 }, (_, d) => `<span class="lab" style="text-align:center">${d % 5 === 0 ? d + 1 : ""}</span>`).join("")}`;
for (const m of months) {
html += `<span class="lab">${MON[+m.slice(5) - 1]} ${m.slice(2, 4)}</span>`;
for (let d = 1; d <= 31; d++) {
const k = `${m}-${pad(d)}`, v = per.get(k);
html += v == null ? `<div></div>` : `<div title="${fmtDay(k)}: ${v}" style="background:${v ? `color-mix(in srgb, var(--warn) ${Math.round(15 + (85 * v) / max)}%, var(--grid))` : "var(--grid)"}"></div>`;
}
}
root.querySelector("#tg-heat").innerHTML = html;
}
heat();
setR(range);
onSeg(root, "tg-mk", v => { kind = v; heat(); });
onSeg(root, "tg-t", v => {
t = v; heat();
rebuild(ts); setR(range);
profCharts.forEach(rebuild); rebuild(hist);
});
}
// --- Input features ----------------------------------------------------------------------
async function renderInputs(root) {
const cat = await json("catalog.json"), used = await liveFeatures(mk());
const groups = new Set(cat.flatMap(r => (r.group ? r.group.split(", ") : [])));
const cats = [...new Set(cat.map(r => r.category))].map(c => ({ c, n: cat.filter(r => r.category === c).length, used: cat.filter(r => r.category === c && r.used).length })).sort((a, b) => b.used - a.used || b.n - a.n);
root.innerHTML = `
<div class="head"><div><h1>Input features</h1><p class="lede">Every series we could feed a model. ESIOS serves <b>indicators</b> (time series), <b>offer indicators</b> (bid curves) and <b>archives</b> (bulk files). Calendar columns are computed. <b>Used</b> marks series in a feature group of <span class="mono">configs/groups.toml</span>; availability <b>future</b> = known for the target day before the gate, <b>past</b> = only known up to the forecast origin. Categories come from keyword rules on the English name, so a few rows may sit in the wrong bucket.</p></div></div>
${used.size ? `<div class="card"><div class="card-head"><div><h2>Used by the live model</h2><p class="sub">The ${used.size} input columns of the latest live run (besides the target's own history).</p></div></div><div class="chips">${[...used].sort().map(f => `<span class="chip static mono">${esc(f)}</span>`).join("")}</div></div>` : ""}
<div class="kpis">${kpi("Series", cat.length.toLocaleString("en-GB"))}${kpi("Used", String(cat.filter(r => r.used).length))}${kpi("Feature groups", String(groups.size))}</div>
<div class="card"><div class="card-head"><div><h2>Catalog</h2><p class="sub" id="in-n"></p></div>
<div class="controls"><select id="in-c" aria-label="Category"><option value="">All categories</option>${cats.map(c => `<option value="${esc(c.c)}">${esc(c.c)} (${c.used}/${c.n})</option>`).join("")}</select>
<label class="tog"><input type="checkbox" id="in-u"> used only</label>
<input type="search" id="in-q" placeholder="Search: mFRR, wind, 600…" aria-label="Search name, description or id"></div></div>
<div class="tablewrap" style="max-height:640px" id="in-t"></div>
<button class="chip" id="in-more" hidden>Show all</button></div>`;
let limit = 200;
const draw = () => {
const c = root.querySelector("#in-c").value, u = root.querySelector("#in-u").checked, q = root.querySelector("#in-q").value.trim().toLowerCase();
const rows = cat.filter(r => (!c || r.category === c) && (!u || r.used) && (!q || [r.name, r.name_es, r.description].some(s => (s ?? "").toLowerCase().includes(q)) || String(r.id) === q));
root.querySelector("#in-n").textContent = `${rows.length.toLocaleString("en-GB")} of ${cat.length.toLocaleString("en-GB")} series`;
root.querySelector("#in-more").hidden = rows.length <= limit;
root.querySelector("#in-t").innerHTML = `<table><thead><tr><th>Used</th><th>Category</th><th>Source</th><th class="n">ID</th><th>Name</th><th>Group</th><th>Availability</th><th>Columns</th><th>Description</th></tr></thead><tbody>${rows.slice(0, limit).map(r =>
`<tr><td>${r.used ? '<span class="pill">used</span>' : ""}</td><td class="muted">${esc(r.category)}${r.subcategory ? `<br><small>${esc(r.subcategory)}</small>` : ""}</td><td class="mono">${esc(r.source)}</td><td class="n">${esc(r.id)}</td><td>${esc(r.name)}${r.area ? `<br><small class="muted">${esc(r.area)}</small>` : ""}</td><td class="mono">${esc(r.group)}</td><td>${esc(r.availability)}</td><td class="mono">${esc(r.column)}</td><td class="desc">${esc(r.description)}</td></tr>`).join("")}</tbody></table>`;
};
["in-c", "in-u"].forEach(id => root.querySelector(`#${id}`).addEventListener("change", draw));
root.querySelector("#in-q").addEventListener("input", draw);
root.querySelector("#in-more").addEventListener("click", () => { limit = Infinity; draw(); });
draw();
}
// Feature columns the latest live run of each of the market's targets used (live.json).
async function liveFeatures(m) {
const [live, meta] = await Promise.all([json("live.json").catch(() => null), json("meta.json")]), out = new Set();
for (const t of shortsOf(m, meta)) for (const f of live?.targets?.[t]?.features ?? []) out.add(f);
return out;
}
// Outside Spain: the market's feature columns from configs/groups.toml (build.py writes them),
// and a map of the weather points with their weights where the market reads Open-Meteo.
async function renderFeatures(root) {
const m = mk(), rows = (await json("features.json"))[m.id], used = await liveFeatures(m);
const geo = (await json("geo.json").catch(() => ({})))[m.id];
const wx = (await json("weather.json").catch(() => ({})))[m.id];
root.innerHTML = `
<div class="head"><div><h1>Input features</h1><p class="lede">The columns the ${esc(m.name)} models see, one line each in <span class="mono">configs/groups.toml</span>. Availability <b>future</b> = known for the target day before the gate, <b>past</b> = only known up to the forecast origin. <b>Live</b> marks the columns the latest live run used.</p></div></div>
${geo ? `<div class="card"><div class="card-head"><div><h2>Weather sampling</h2><p class="sub">${geo.sets.map(s => `${s.points.length} points (${s.weighted ? "weighted by PV capacity; dot size = weight" : "plain mean"}): ${s.runs.join(" and ")} run of D-1`).join(" · ")}. Outline: ${esc(geo.label)}.</p></div></div>
<div class="map" id="ft-map" role="img" aria-label="Map of the weather points"></div>
<p class="sub">Boundaries: ${esc(geo.credit)}.</p></div>` : ""}
${wx ? weatherTimeline(wx, m) : ""}
<div class="card"><div class="card-head"><div><h2>Feature columns</h2><p class="sub">${rows.length} columns · ${rows.filter(r => used.has(r.column)).length} in the latest live run</p></div></div>
<div class="tablewrap"><table><thead><tr><th>Live</th><th>Group</th><th>Column</th><th>Source</th><th>Availability</th><th>Description</th></tr></thead><tbody>${rows.map(r =>
`<tr><td>${used.has(r.column) ? '<span class="pill">live</span>' : ""}</td><td class="mono">${esc(r.group)}</td><td class="mono">${esc(r.column)}</td><td class="mono">${esc(r.source)}</td><td>${esc(r.availability)}</td><td class="desc">${esc(r.description)}</td></tr>`).join("")}</tbody></table></div></div>`;
// In the chart registry, so a theme switch redraws it in the new colours.
if (geo && window.L) mount(root.querySelector("#ft-map"), () => drawMap(root.querySelector("#ft-map"), geo, CITIES[m.c] ?? []), 440);
}
// A few major cities as labels on the plain map.
const CITIES = {
be: [["Brussels", 50.85, 4.35], ["Antwerp", 51.22, 4.40], ["Ghent", 51.05, 3.72], ["Liège", 50.63, 5.57], ["Charleroi", 50.41, 4.44]],
de: [["Berlin", 52.52, 13.40], ["Hamburg", 53.55, 9.99], ["Leipzig", 51.34, 12.37], ["Dresden", 51.05, 13.74], ["Rostock", 54.09, 12.10]],
};
function drawMap(el, geo, cities) {
const map = L.map(el, { scrollWheelZoom: false, attributionControl: false });
const regions = L.geoJSON(geo.regions, {
style: { color: css("--accent-ink"), weight: 1.5, fillColor: css("--accent"), fillOpacity: 0.12 },
onEachFeature: (f, layer) => layer.bindTooltip(esc(f.properties.name)),
});
const bounds = regions.getBounds();
for (const s of geo.sets) for (const [lat, lon] of s.points) bounds.extend([lat, lon]);
map.fitBounds(bounds.pad(0.05)); // view first: vector layers added to a map without one throw
// No tiles: the country on the grey background, the regions we use, a few cities.
if (geo.country) L.geoJSON(geo.country, { interactive: false, style: { color: css("--ink-soft"), weight: 1.5, fillColor: css("--surface"), fillOpacity: 1 } }).addTo(map);
regions.addTo(map);
for (const [name, lat, lon] of cities)
L.marker([lat, lon], { icon: L.divIcon({ className: "city", html: `<i></i>${esc(name)}`, iconSize: null, iconAnchor: [3, 8] }), interactive: false, keyboard: false }).addTo(map);
for (const s of geo.sets) for (const [lat, lon, w] of s.points) {
const r = s.weighted ? 5 + Math.sqrt(w) * 30 : 7;
L.circleMarker([lat, lon], { radius: r, color: css("--surface"), weight: 2, fillColor: css("--tso"), fillOpacity: 0.9 })
.bindTooltip(`${lat.toFixed(2)}, ${lon.toFixed(2)}${s.weighted ? ` · weight ${(w * 100).toFixed(1)}%` : ""}<br>${s.runs.join(", ")}`)
.addTo(map);
}
return { destroy: () => map.remove(), setSize: () => map.invalidateSize() };
}
// --- Weather runs timeline (Input features, targets that read Open-Meteo) -------------------
// One row per weather model: each archived run as a bar from init to when it is on the API
// (openmeteo.LATENCY), the run we read highlighted; origin, submit and gate as vertical lines.
// Intraday: the target day, no chosen run yet, a gate every hour. The axis is in the market's
// local time, like the origin and gate; runs keep their UTC names in the hover text and table.
function weatherTimeline(w, m) {
const intraday = !!m.intraday, H = 3600, OTHER = "color-mix(in srgb, var(--ink-soft) 40%, transparent)";
const t0 = intraday ? w.day_start : Math.min(...w.models.map(r => r.used)) - 6 * H;
const t1 = intraday ? w.day_end : w.gate + 2 * H;
const L = 150, R = 20, W = 900, rowH = 30, top = 22, n = w.models.length, plotH = n * rowH, Hh = top + plotH + 30;
const X = s => L + ((s - t0) / (t1 - t0)) * (W - L - R);
const hm = s => { const d = new Date(s * 1000); return `${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}`; };
const local = new Intl.DateTimeFormat("en-GB", { timeZone: m.tz, hour: "2-digit", minute: "2-digit", hourCycle: "h23" });
const loc = s => local.format(new Date(s * 1000));
const date = new Intl.DateTimeFormat("en-GB", { timeZone: m.tz, day: "numeric", month: "short" });
const dur = s => `${Math.floor(s / H)}h${pad(Math.round((s % H) / 60))}`;
// ponytail: one UTC offset for the whole window (no DST switch inside it).
const utc = new Intl.DateTimeFormat("en-GB", { timeZone: m.tz, timeZoneName: "shortOffset" }).formatToParts(new Date(t0 * 1000)).find(p => p.type === "timeZoneName").value.replace("GMT", "UTC");
const off = (+utc.slice(3) || 0) * H, city = m.city, zone = `${city} time (${utc})`;
let svg = `<text x="${L - 10}" y="${top - 8}" text-anchor="end" style="fill:var(--ink-soft);font-family:var(--sans)">${esc(zone)}</text>`;
for (let s = Math.ceil((t0 + off) / (3 * H)) * 3 * H - off; s <= t1; s += 3 * H) { // grid every 3 local hours
svg += `<line x1="${X(s)}" x2="${X(s)}" y1="${top}" y2="${top + plotH}" style="stroke:var(--grid)"/>`;
svg += `<text x="${X(s)}" y="${top + plotH + 14}" text-anchor="middle" style="fill:var(--ink-soft)">${loc(s)}</text>`;
if (loc(s) === "00:00") svg += `<text x="${X(s)}" y="${top + plotH + 27}" text-anchor="middle" style="fill:var(--ink-soft)">${date.format(new Date(s * 1000))}</text>`;
}
w.models.forEach((r, i) => {
const y = top + i * rowH + rowH / 2;
svg += `<text x="${L - 10}" y="${y + 4}" text-anchor="end" style="fill:var(--ink);font-family:var(--sans)">${esc(r.name)}</text>`;
const inits = [];
for (let s = Math.floor((t0 - r.latency) / r.cycle) * r.cycle; s < t1; s += r.cycle) inits.push(s);
inits.sort((p, q) => (p === r.used) - (q === r.used)); // the run we read drawn last, on top
// A run slower than the cycle (IFS: out 6h40 after init, every 6 h) overlaps the next one:
// alternate lanes so each bar keeps its own end.
const lanes = Math.ceil(r.latency / r.cycle);
for (const s of inits) {
const a = Math.max(s, t0), b = Math.min(s + r.latency, t1), used = !intraday && s === r.used;
if (b <= a) continue;
const yl = y + (lanes > 1 ? ((Math.round(s / r.cycle) % lanes) - (lanes - 1) / 2) * 9 : 0);
const title = `${r.name} ${hm(s)} UTC run (${loc(s)} ${city}): on Open-Meteo ~${loc(s + r.latency)} ${city} (${hm(s + r.latency)} UTC), ${dur(r.latency)} after init${used ? "; the run we read" : ""}`;
svg += `<g><title>${esc(title)}</title><rect x="${X(a)}" y="${yl - (used ? 4 : 2.5)}" width="${Math.max(2, X(b) - X(a))}" height="${used ? 8 : 5}" rx="${used ? 4 : 2.5}" style="fill:${used ? "var(--accent-ink)" : OTHER}"/>`;
if (s + r.latency <= t1) svg += `<circle cx="${X(s + r.latency)}" cy="${yl}" r="${used ? 5 : 3.5}" style="fill:${used ? "var(--accent-ink)" : "var(--ink-soft)"};stroke:var(--surface);stroke-width:1.5"/>`;
svg += `<rect x="${X(a)}" y="${yl - 5}" width="${Math.max(8, X(b) - X(a))}" height="10" fill="transparent"/></g>`;
}
});
const vline = (s, color, dash, label) =>
`<g><title>${esc(label)}</title><line x1="${X(s)}" x2="${X(s)}" y1="${top - 6}" y2="${top + plotH}" style="stroke:${color};stroke-width:2${dash ? `;stroke-dasharray:${dash}` : ""}"/></g>`;
if (intraday) for (let s = Math.ceil(t0 / H) * H; s < t1; s += H) svg += vline(s, "var(--ink-soft)", "2 3", `Hourly intraday gate ${loc(s)} ${m.city}`);
else {
svg += vline(w.origin, "var(--base)", "6 4", `Origin ${loc(w.origin)} ${m.city} (${hm(w.origin)} UTC)`);
svg += vline(w.submit, "var(--cont)", "2 3", `Live run ${loc(w.submit)} ${m.city} (${hm(w.submit)} UTC)`);
svg += vline(w.gate, "var(--ink)", "", `Predico gate ${loc(w.gate)} ${m.city} (${hm(w.gate)} UTC)`);
}
const legend = intraday
? `<span class="legend-i"><span class="sw" style="border-color:${OTHER};border-top-width:6px"></span>run: init → on Open-Meteo</span><span class="legend-i"><span class="sw dot" style="border-color:var(--ink-soft)"></span>hourly intraday gates</span>`
: `<span class="legend-i"><span class="sw thick" style="border-color:var(--accent-ink)"></span>run we read</span><span class="legend-i"><span class="sw" style="border-color:${OTHER};border-top-width:6px"></span>other runs: init → on Open-Meteo</span><span class="legend-i"><span class="sw dash" style="border-color:var(--base)"></span>origin ${loc(w.origin)} ${m.city}</span><span class="legend-i"><span class="sw dot" style="border-color:var(--cont)"></span>live run ${loc(w.submit)}</span><span class="legend-i"><span class="sw" style="border-color:var(--ink)"></span>Predico gate ${loc(w.gate)} ${m.city}</span>`;
const rows = w.models.map(r => {
const runs = Array.from({ length: 24 / (r.cycle / H) }, (_, k) => pad((k * r.cycle) / H)).join(", ");
return `<tr><td>${esc(r.name)}</td><td class="mono">${runs}</td><td class="n">${dur(r.latency)}</td>${intraday ? `<td class="muted">not chosen yet</td>` : `<td class="mono">${hm(r.used)} UTC, ${fmtDay(new Date(r.used * 1000).toISOString().slice(0, 10))}</td><td class="n">${loc(r.used + r.latency)}</td><td class="n">${dur(w.origin - r.used - r.latency)}</td>`}</tr>`;
}).join("");
return `<div class="card"><div class="card-head"><div><h2>Weather runs and gates</h2><p class="sub">${intraday
? `Today (${fmtDay(w.day)}), chart in ${esc(zone)}. Each archived run from init to when Open-Meteo serves it; Predico's intraday sessions close every hour. Which run the intraday forecasts read is not decided yet.`
: `For tomorrow's session (D-1 = ${fmtDay(w.day)}), chart in ${esc(zone)}; runs keep their UTC names. Each archived run from init to when Open-Meteo serves it (typical delays, measured Nov 2026); the run we read is one that is out well before the ${loc(w.origin)} ${m.city} origin, the same in the backtest and live. Hover a bar for its times.`}</p></div></div>
<div class="chart" style="overflow-x:auto"><svg viewBox="0 0 ${W} ${Hh}" width="100%" role="img" aria-label="Timeline of weather model runs, their availability and the gate; the table below lists the same" style="font:11px var(--mono);display:block;min-width:640px">${svg}</svg></div>
<div class="legend sub">${legend}</div>
<div class="tablewrap"><table><thead><tr><th>Model</th><th>Runs (UTC)</th><th class="n">Out after</th><th>Run we read</th>${intraday ? "" : `<th class="n">Out at (${esc(city)})</th><th class="n">Margin to origin</th>`}</tr></thead><tbody>${rows}</tbody></table></div></div>`;
}
// --- Countries and variables (Predico resources) -------------------------------------------
// data: build.py MARKETS key (target/<data>.json, features.json, geo.json, weather.json).
// city/tz/gateLocal/source/missingTruth/about: tab texts.
const de50Hertz = what => (from, to) => `The truth series from <span class="mono">v2/target_50hertz.parquet</span>: ENTSO-E actual generation, ${what}, 50Hertz control area, MW (mean per quarter-hour), 15-min, ${from} – ${to}. As Predico does, points the curve omits repeat the previous value (curve type A03), each series before summing.`;
const COUNTRIES = [
{ id: "es", name: "Spain", program: "EDP", data: "es-mfrr", catalog: true, city: "Madrid", tz: "Europe/Madrid", gateLocal: "17:00 Madrid on D-1", source: "ESIOS",
missingTruth: "Quarter-hours without truth (no mFRR activation, so no price) are left out.",
about: (from, to) => `The truth series from <span class="mono">v1/target.parquet</span>: mFRR activated <b>energy</b> (net, MWh per quarter-hour, upward positive) and mFRR <b>price</b> (EUR/MWh), Spain, 15-min, ${from} – ${to}. Missing energy is filled with 0 (no activation); missing price stays empty. No data during the Iberian blackout (28 Apr – 6 May 2025). Prices rise from Jul 2026 (monthly mean ~100 → ~157 EUR/MWh).`,
vars: [{ id: "energy", name: "mFRR energy", resource: "spain_mfrr_energy" }, { id: "price", name: "mFRR price", resource: "spain_mfrr_price" }] },
{ id: "de", name: "Germany", program: "50Hertz", city: "Berlin", tz: "Europe/Berlin", gateLocal: "15:00 Berlin on D-1", source: "ENTSO-E", missingTruth: "",
vars: [
{ id: "wind", name: "Wind", resource: "germany_wind_50hertz_ca", data: "de-wind", about: de50Hertz("wind offshore + onshore (A75 B18 + B19)") },
{ id: "solar", name: "Solar", resource: "germany_solar_50hertz_ca", data: "de-solar", about: de50Hertz("solar (A75 B16)") },
] },
{ id: "be", name: "Belgium", program: "Elia", data: "be-solar", city: "Brussels", tz: "Europe/Brussels", gateLocal: "10:00 Brussels on D-1", source: "Elia", missingTruth: "",
about: (from, to) => `The truth series from <span class="mono">target_elia.parquet</span>: Elia's measured PV generation, Belgium total (TSO + DSO connected), MW (mean per quarter-hour), 15-min, ${from} – ${to}. Elia dataset ods032 up to 25 Oct 2026, then ods209 (and ods210 for the latest hours), as Predico scores it. The same series scores the D+1 and the intraday sessions.`,
vars: [
{ id: "solar", name: "Solar D+1", resource: "belgium_solar_opendata_elia" },
{ id: "solar-intraday", name: "Solar intraday", resource: "belgium_solar_opendata_elia", intraday: true, gateLocal: "every hour, intraday" },
] },
];
const TABS = { live: renderLive, backtest: renderBacktest, target: renderTarget, inputs: root => (mk().catalog ? renderInputs : renderFeatures)(root) };
const TAB_NAMES = { live: "Live", backtest: "Backtest", target: "Target data", inputs: "Input features" };
const saved = (() => { try { return JSON.parse(localStorage.getItem("sel")) ?? {}; } catch { return {}; } })();
let sel = { c: "es", v: "energy", tab: "live" };
// Hash: #<country>/<variable>/<section>; missing parts fall back to the last choice.
function parseHash() {
const [a, b, c] = location.hash.slice(1).split("/");
if (TABS[a]) return { ...sel, ...saved, tab: a }; // old links: #live, #backtest, ...
const C = COUNTRIES.find(x => x.id === a) ?? COUNTRIES.find(x => x.id === saved.c) ?? COUNTRIES[0];
const V = C.vars.find(x => x.id === b) ?? C.vars.find(x => x.id === saved.v) ?? C.vars[0];
return { c: C.id, v: V.id, tab: TABS[c] ? c : TABS[saved.tab] ? saved.tab : "live" };
}
const href = (c, v, tab) => `#${c}/${v}/${tab}`;
// Live and backtest of the intraday sessions: not wired yet, say so without inventing data.
function intradaySoon(root, tab) {
const m = mk();
root.innerHTML = `<div class="head"><div><h1>${esc(TAB_NAMES[tab])}: ${esc(m.name)}</h1></div></div>
<div class="card soon"><span class="pill muted">being added</span>
<p class="lede">Predico's Elia program also runs <b>hourly intraday sessions</b> for Belgian solar, with challenges that start the same day. Our hourly intraday submissions are being added; the D+1 forecast does not cover them. ${tab === "live" ? "Their forecasts appear here after each gate, once they run." : "A backtest follows once the intraday runs exist."}</p>
<dl><dt>Predico resource</dt><dd>${esc(m.resource)}</dd><dt>Gates</dt><dd>every hour, intraday</dd><dt>Truth</dt><dd>Elia Open Data, Belgium total (same as D+1)</dd>
<dt>Benchmark</dt><dd>Elia's most recent forecast (mostrecentforecast in ods032/ods209/ods210)</dd></dl>
<p class="sub"><a href="${href(m.c, m.v, "target")}">Target data</a> and <a href="${href(m.c, m.v, "inputs")}">input features</a> (with the weather runs against the hourly gates) are the D+1 ones.</p></div>`;
}
function drawNav() {
const C = COUNTRIES.find(x => x.id === sel.c);
document.getElementById("countries").innerHTML = COUNTRIES.map(c => `<a href="${href(c.id, c.id === sel.c ? sel.v : c.vars[0].id, sel.tab)}"${c.id === sel.c ? ' aria-current="page"' : ""}>${esc(c.name)}</a>`).join("");
document.getElementById("vars").innerHTML = C.vars.map(v => `<a class="chip" href="${href(C.id, v.id, sel.tab)}"${v.id === sel.v ? ' aria-current="page"' : ""}>${esc(v.name)}</a>`).join("");
document.getElementById("sections").innerHTML = Object.entries(TAB_NAMES).map(([k, l]) => `<a href="${href(sel.c, sel.v, k)}"${k === sel.tab ? ' aria-current="page"' : ""}>${esc(l)}</a>`).join("");
}
function show() {
sel = parseHash();
try { localStorage.setItem("sel", JSON.stringify(sel)); } catch {}
drawNav();
const tab = sel.tab;
document.querySelectorAll("section[data-tab]").forEach(s => (s.hidden = s.dataset.tab !== tab));
const root = document.getElementById(`tab-${tab}`), key = `${sel.c}/${sel.v}`;
if (root.dataset.key === key) return;
root.dataset.key = key;
if (mk().intraday && (tab === "live" || tab === "backtest")) return intradaySoon(root, tab);
root.innerHTML = `<p class="empty">Loading…</p>`;
TABS[tab](root).catch(e => { root.innerHTML = `<div class="card"><p class="empty">Could not load this tab: ${esc(e.message)}</p></div>`; console.error(e); });
}
addEventListener("hashchange", show);
(document.fonts?.ready ?? Promise.resolve()).then(show);
|