File size: 17,945 Bytes
fce6c09
 
 
 
 
 
3d60772
 
 
 
 
 
1263fa8
 
3d60772
 
 
 
 
 
 
1263fa8
 
 
 
 
 
 
3d60772
 
 
 
 
 
 
 
 
 
 
 
 
 
fce6c09
 
 
 
3d60772
1263fa8
3d60772
 
fce6c09
 
 
 
 
3d60772
1263fa8
3d60772
 
fce6c09
 
 
 
 
3d60772
1263fa8
3d60772
 
43604b6
fce6c09
 
 
 
3d60772
1263fa8
3d60772
 
fce6c09
 
baf1286
 
 
3d60772
 
 
 
baf1286
 
 
 
 
3d60772
 
 
 
baf1286
 
43604b6
 
 
3d60772
1263fa8
3d60772
 
43604b6
0817e0a
4bcf26d
 
 
 
1263fa8
4bcf26d
 
 
 
 
 
 
 
1263fa8
4bcf26d
 
 
 
0817e0a
 
 
3d60772
1263fa8
3d60772
 
0817e0a
 
 
 
 
3d60772
1263fa8
3d60772
 
0817e0a
 
221faf8
 
 
eb6584e
 
 
 
221faf8
eb6584e
1ad9fc2
 
 
 
1263fa8
1ad9fc2
 
 
 
 
 
 
 
 
 
 
 
1263fa8
1ad9fc2
 
 
 
 
 
 
 
ad11aa3
 
 
 
1263fa8
ad11aa3
 
 
 
 
 
 
 
763c770
 
 
 
1263fa8
763c770
 
 
 
 
 
 
 
1263fa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221faf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e13282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0817e0a
 
 
1ad9fc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0817e0a
1ad9fc2
 
 
 
 
0817e0a
 
 
1ad9fc2
 
 
 
0817e0a
 
 
fce6c09
 
 
221faf8
 
 
 
 
 
 
3e13282
 
221faf8
 
 
3e13282
 
221faf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fce6c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
from pathlib import Path


# Taxonomy
# --------
# Every method carries four independent tags, each scoped to its own axis:
#
# ``parallelism`` β€” how the search explores parameter space:
#   "serial"               β€” a single point estimate advanced step by step (ADAM, LM).
#   "serial (short window)" β€” serial, but each step advances a sliding statistics window by
#                             a fraction of its length (RL-*-SHORT) rather than a full window.
#   "parallel-independent" β€” a population of candidates updated with no coupling
#                             between members (ABC's accepted samples, HM's per-wave
#                             resampling from the non-implausible region).
#   "parallel-interacting" β€” an ensemble whose members are coupled through a shared
#                             update each iteration (any Kalman-based method).
#
# ``update_type`` β€” the mechanism driving each update step:
#   "gradient (exact)"  β€” follows the loss gradient or a Gauss-Newton approximation of it,
#                          differentiated through the forward model (ADAM, LM).
#   "gradient (Kalman)" β€” a (possibly linearized/unscented) Kalman-style ensemble update,
#                          i.e. an ensemble-estimated gradient/Gauss-Newton step.
#   "gradient (score)"  β€” a gradient built from a learned/estimated score or critic rather
#                          than a simulator Jacobian (*-KGMM, *-DSM, RL-*).
#   "general"           β€” anything else (e.g. ABC's rejection sampling, HM's implausibility cuts).
#
# ``method_goal`` β€” what the method is built to report:
#   "optimization" β€” a single best-fit parameter estimate.
#   "uq"           β€” the full posterior / parameter uncertainty. UQ methods can still
#                    be scored on the Optimization leaderboard (usually less
#                    competitive there, since they're not optimizing for speed-to-target).
#
# ``emulator_use`` β€” when/whether a surrogate model of the forward model is used:
#   "none"            β€” samples/evaluates the true forward model throughout.
#   "within-optimize" β€” refits a surrogate at each iteration of the search (HM waves).
#   "after-optimize"  β€” fits a surrogate once, after calibration finishes (CES).
#
# Note: Kalman methods are Bayesian in spirit too (approximate Gaussian posterior
# updates) β€” ``update_type`` is about mechanism, not a "Bayesian vs not" philosophy.
KNOWN_METHODS = {
    "teki": {
        "abbreviation": "TEKI",
        "Method": "Tikhonov Regularized Ensemble Kalman Inversion",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["teki"],
    },
    "etki": {
        "abbreviation": "ETKI",
        "Method": "Ensemble Transform Kalman Inversion",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["etki"],
    },
    "iekf": {
        "abbreviation": "IEKF",
        "Method": "Iterative Ensemble Kalman Filter",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "uq",
        "emulator_use": "none",
        "aliases": ["iekf", "gnsl", "gnki"],
    },
    "uki": {
        "abbreviation": "UKI",
        "Method": "Unscented Kalman Inversion",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["uki"],
    },
    "abc": {
        "abbreviation": "ABC",
        "Method": "Approximate Bayesian Calibration",
        "parallelism": "parallel-independent",
        "update_type": "general",
        "method_goal": "uq",
        "emulator_use": "none",
        "aliases": ["abc"],
    },
    "hm": {
        "abbreviation": "HM",
        "Method": "History Matching",
        "parallelism": "parallel-independent",
        "update_type": "general",
        "method_goal": "uq",
        "emulator_use": "within-optimize",
        "aliases": ["hm"],
    },
    "ces-eki-dmc": {
        "abbreviation": "CES-EKI-DMC",
        "Method": "Calibrate Emulate Sample (EKI-DataMisfitController)",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "uq",
        "emulator_use": "after-optimize",
        "aliases": ["ces-eki-dmc"]
    },
    "ces-eki-const": {
        "abbreviation": "CES-EKI-CONST",
        "Method": "Calibrate Emulate Sample (EKI-Constant Scheduler)",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "uq",
        "emulator_use": "after-optimize",
        "aliases": ["ces-eki-const"]
    },
    "ces-iekf-const": {
        "abbreviation": "CES-IEKF-CONST",
        "Method": "Calibrate Emulate Sample (IEKF-Constant Scheduler)",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "uq",
        "emulator_use": "after-optimize",
        "aliases": ["ces-iekf-const"]
    },
    "adam": {
        "abbreviation": "ADAM",
        "Method": "Adaptive Moment Estimation",
        "parallelism": "serial",
        "update_type": "gradient (exact)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["adam"],
    },
    "lm": {
        "abbreviation": "LM",
        "Method": "Levenberg-Marquardt",
        "parallelism": "serial",
        "update_type": "gradient (exact)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["lm", "levenberg_marquardt", "levenberg-marquardt", "gradient_descent"],
    },
    "gboed-tmcmc": {
        "abbreviation": "GBOED-TMCMC",
        "Method": "Goal-oriented Bayesian Optimal Experimental Design (TMCMC)",
        "parallelism": "parallel-interacting",
        "update_type": "general",
        "method_goal": "uq",
        "emulator_use": "within-optimize",
        "aliases": ["gboed", "gboed-tmcmc", "boed", "gboed-tmcmc-eig", "boed-tmcmc-eig", "tmcmc-eig"],
    },
    "lm-kgmm": {
        "abbreviation": "LM-KGMM",
        "Method": "Score-based Levenberg-Marquardt (KGMM)",
        "parallelism": "serial",
        "update_type": "gradient (score)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": [
            "lm-kgmm",
            "score-based lm (gfdt, kgmm) (extended window)",
            "score-based lm (gfdt, kgmm) (parallel branches)",
        ],
    },
    "lm-dsm": {
        "abbreviation": "LM-DSM",
        "Method": "Score-based Levenberg-Marquardt (DSM)",
        "parallelism": "serial",
        "update_type": "gradient (score)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": [
            "lm-dsm",
            "score-based lm (gfdt, dsm) (extended window)",
            "score-based lm (gfdt, dsm) (parallel branches)",
        ],
    },
    "adam-kgmm": {
        "abbreviation": "ADAM-KGMM",
        "Method": "Score-based Adam (KGMM)",
        "parallelism": "serial",
        "update_type": "gradient (score)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": [
            "adam-kgmm",
            "score-based adam (gfdt, kgmm) (extended window)",
            "score-based adam (gfdt, kgmm) (parallel branches)",
        ],
    },
    "adam-dsm": {
        "abbreviation": "ADAM-DSM",
        "Method": "Score-based Adam (DSM)",
        "parallelism": "serial",
        "update_type": "gradient (score)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": [
            "adam-dsm",
            "score-based adam (gfdt, dsm) (extended window)",
            "score-based adam (gfdt, dsm) (parallel branches)",
        ],
    },
    "rl-ddpg-short": {
        "abbreviation": "RL-DDPG-SHORT",
        "Method": "Reinforcement Learning (DDPG, short RL step)",
        "parallelism": "serial (short window)",
        "update_type": "gradient (score)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["rl-ddpg-short"],
    },
    "rl-ddpg": {
        "abbreviation": "RL-DDPG",
        "Method": "Reinforcement Learning (DDPG)",
        "parallelism": "serial",
        "update_type": "gradient (score)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["rl-ddpg", "rl-ddpg-long"],
    },
    "rl-td3-short": {
        "abbreviation": "RL-TD3-SHORT",
        "Method": "Reinforcement Learning (TD3, short RL step)",
        "parallelism": "serial (short window)",
        "update_type": "gradient (score)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["rl-td3-short"],
    },
    "rl-td3": {
        "abbreviation": "RL-TD3",
        "Method": "Reinforcement Learning (TD3)",
        "parallelism": "serial",
        "update_type": "gradient (score)",
        "method_goal": "optimization",
        "emulator_use": "none",
        "aliases": ["rl-td3", "rl-td3-long"],
    },
    "ies-iekf": {
        "abbreviation": "IES-IEKF",
        "Method": "Iterated Emulate-Sample (IEKF)",
        "parallelism": "parallel-interacting",
        "update_type": "general",
        "method_goal": "uq",
        "emulator_use": "within-optimize",
        "aliases": ["ies-iekf", "boed-iekf"],
    },
    "gboed-iekf": {
        "abbreviation": "GBOED-IEKF",
        "Method": "Goal-oriented Bayesian Optimal Experimental Design (IEKF)",
        "parallelism": "parallel-interacting",
        "update_type": "general",
        "method_goal": "uq",
        "emulator_use": "within-optimize",
        "aliases": ["gboed-iekf", "gboed-iekf-eig", "boed-iekf-eig"],
    },
    "ies-tmcmc": {
        "abbreviation": "IES-TMCMC",
        "Method": "Iterated Emulate-Sample (TMCMC)",
        "parallelism": "parallel-interacting",
        "update_type": "general",
        "method_goal": "uq",
        "emulator_use": "within-optimize",
        "aliases": ["ies-tmcmc", "boed-tmcmc"],
    },
    "eks": {
        "abbreviation": "EKS",
        "Method": "Ensemble Kalman Sampler (EKS a.k.a ALDI)",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "uq",
        "emulator_use": "none",
        "aliases": ["eks", "sampler"],
    },
    "ces-eks": {
        "abbreviation": "CES-EKS",
        "Method": "Calibrate Emulate Sample (EKS)",
        "parallelism": "parallel-interacting",
        "update_type": "gradient (Kalman)",
        "method_goal": "uq",
        "emulator_use": "after-optimize",
        "aliases": ["ces-eks"],
    },
    "ies-eks": {
        "abbreviation": "IES-EKS",
        "Method": "Iterated Emulate-Sample (EKS)",
        "parallelism": "parallel-interacting",
        "update_type": "general",
        "method_goal": "uq",
        "emulator_use": "within-optimize",
        "aliases": ["ies-eks", "boed-eks"],
    },
    "gboed-eks": {
        "abbreviation": "GBOED-EKS",
        "Method": "Goal-oriented Bayesian Optimal Experimental Design (EKS)",
        "parallelism": "parallel-interacting",
        "update_type": "general",
        "method_goal": "uq",
        "emulator_use": "within-optimize",
        "aliases": ["gboed-eks", "gboed-eks-eig", "boed-eks-eig"],
    },
}


# One slot per method in KNOWN_METHODS declaration order. New methods appended
# to KNOWN_METHODS get the next slot.
#
# Generated by a greedy farthest-point search in OKLab: each slot was chosen to
# maximize its minimum distance to every slot before it, simultaneously under
# normal vision and simulated protan/deutan color-vision deficiency (Machado,
# Oliveira & Fernandes 2009 severity-1.0 transforms β€” the same model used by
# the dataviz skill's validate_palette.js). Because this optimizes the running
# ALL-PAIRS minimum (not just adjacent-slot pairs), the printed Ξ”E at slot k
# below is a genuine worst-case guarantee for any subset of the first k slots
# shown together β€” relevant here because any subset of methods can appear
# together via the "Methods to display in charts" multiselect, not just
# neighbors in this list.
#
# Honest limits (OKLab Ξ”E x100; CVD = min(protan, deutan)):
#   - Slots  0-15 (16 colors): CVD >= 8.0  β€” clears the full colorblind-safety
#     target for every pair among any subset of these.
#   - Slots 16-27            : CVD in [6.0, 8.0) β€” the legal "floor" band per
#     the skill (secondary encoding required). This app already provides it:
#     every legend and tooltip in leaderboard.py shows the method abbreviation
#     as text, so identity is never color-alone even where hue alone is weak.
#   - Slots 28-29            : CVD ~5.8, just under the 6.0 floor for their
#     closest pair β€” weakest two slots, kept only for the requested headroom.
#   - Normal-vision floor (>=15, a hard gate when checked in isolation): holds
#     for the first ~6 slots against ALL other slots; beyond that, some pairs
#     among 30 simultaneously-distinguishable colors are close even for
#     full-color vision. This is a geometry limit of a finite perceptual
#     space, not a search-quality issue β€” the reference palette in the dataviz
#     skill's own docs states plainly that no ordering clears all-pairs floors
#     past ~8 categories. 30 slots were requested anyway for growth headroom;
#     rely on the legend/tooltip text labels (already present) for identity,
#     not on hue alone, especially past slot ~16.
# Re-run the search (seeded with all prior slots fixed) if extending further.
_METHOD_PALETTE = [
    "#d80065", "#007fff", "#ff9700", "#00cae4", "#6d00b8", "#970f00",
    "#007ca4", "#de8580", "#00a0d7", "#fa0000", "#0100ff", "#90006d",
    "#e500a0", "#007656", "#008200", "#006dd1", "#00b7ad", "#47b3ff",
    "#0087d2", "#e97e00", "#ec9f72", "#4b4589", "#00d0ae", "#c87181",
    "#5194ff", "#9c5683", "#0058ff", "#892348", "#e2003d", "#00b5d5",
]

# Stable abbreviation β†’ hex color mapping.  Import this wherever Altair charts are built
# so every plot in the app assigns the same color to each method.  With 30 palette
# slots and 14 registered methods, every method gets a unique color β€” the modulo
# below only matters as a defensive fallback if KNOWN_METHODS grows past 30 without
# the palette being extended first (see comment above).
METHOD_COLORS: dict[str, str] = {
    meta["abbreviation"]: _METHOD_PALETTE[i % len(_METHOD_PALETTE)]
    for i, meta in enumerate(KNOWN_METHODS.values())
}


# Method families used to group rows in the suitability tables (the online table in
# common/leaderboard.py and the LaTeX tables from offline-plots/suitability_tables.py).
# The idea is to put methods of similar construction β€” and so similar performance β€”
# next to each other.  Dict order = family order; list order = row order within a
# family.  A family only appears on a board if one of its methods has data there.
# Every method that can appear in a suitability table must be listed here.
SUITABILITY_FAMILIES: dict[str, list[str]] = {
    "CES": ["CES-EKI-CONST", "CES-EKI-DMC", "CES-EKS", "CES-IEKF-CONST"],
    "Kalman": ["EKS", "ETKI", "IEKF", "TEKI", "UKI"],
    "Gradient": ["LM", "ADAM"],
    "GFDT": ["LM-DSM", "ADAM-DSM", "LM-KGMM", "ADAM-KGMM"],
    "RL": ["RL-DDPG", "RL-TD3", "RL-DDPG-SHORT", "RL-TD3-SHORT"],
    "IES": ["IES-EKS", "IES-IEKF", "IES-TMCMC"],
    "GBOED": ["GBOED-EKS", "GBOED-IEKF", "GBOED-TMCMC"],
    "General": ["ABC", "HM"],
}

# Spelled-out display names for the SUITABILITY_FAMILIES keys (suitability table,
# home-page key, and the grouped method selector on the leaderboards).
FAMILY_FULL_NAMES: dict[str, str] = {
    "CES": "Calibrate, Emulate, Sample (CES)",
    "Kalman": "Kalman",
    "Gradient": "Gradient",
    "GFDT": "Generalized Fluctuation-Dissipation (GFDT)",
    "RL": "Reinforcement Learning (RL)",
    "IES": "Iterated Emulate-Sample (IES)",
    "GBOED": "Goal-oriented Bayesian Experimental Design (GBOED)",
    "General": "General",
}


def family_full_name(family: str) -> str:
    return FAMILY_FULL_NAMES.get(family, family)


def build_alias_lookup() -> dict[str, str]:
    lookup: dict[str, str] = {}
    for canonical_name, meta in KNOWN_METHODS.items():
        lookup[canonical_name] = canonical_name
        lookup[canonical_name.upper()] = canonical_name
        for alias in meta.get("aliases", []):
            lookup[alias.lower()] = canonical_name
            lookup[alias.upper()] = canonical_name
    return lookup


ALIAS_TO_CANONICAL = build_alias_lookup()


def normalize_method_name(name: object) -> str:
    text = str(name).strip()

    if text.startswith("b'") and text.endswith("'"):
        text = text[2:-1]
    elif text.startswith('b"') and text.endswith('"'):
        text = text[2:-1]

    text = text.strip("\"'").strip()
    return text.lower()


def canonicalize_method_name(name: object) -> str:
    normalized = normalize_method_name(name)
    return ALIAS_TO_CANONICAL.get(normalized, normalized)


def get_method_meta(canonical_name: str) -> dict[str, str]:
    return KNOWN_METHODS.get(canonical_name, {})


def dump_method_registry_snapshot(project_root: Path, observed_methods: set[str]) -> None:
    snapshot = {
        "known_methods": KNOWN_METHODS,
        "observed_methods": sorted(observed_methods),
        "unmapped_observed_methods": sorted([method for method in observed_methods if method not in KNOWN_METHODS]),
    }
    cache_dir = project_root / ".cache"
    cache_dir.mkdir(parents=True, exist_ok=True)
    target_file = cache_dir / "known_methods_snapshot.json"
    target_file.write_text(json.dumps(snapshot, indent=2), encoding="utf-8")