Files
muSRFlow/tests/reference-fixtures.test.js
T
salman 9ee2ac88a8 Add multi-group asymmetry fits from a single run
Slash-separated forward,backward groups in LRBF (1,2/3,4) generate one
RUN block per group with its own alpha, amplitude ratio and phase,
backed by a generated FUNCTIONS amplitude line and per-group maps.
Read-back reconstructs the GUI state and reproduces the file exactly;
a reference fixture and real-musrfit integration (converged
chisq/NDF 1.025906) cover the round trip.
2026-09-22 13:55:06 +02:00

398 lines
15 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
// Reference-fixture regression tests. A generated golden MSR is captured once
// under tests/fixtures/reference/ from a fixed GUI configuration, and this test
// asserts that buildMSR() reproduces it byte-for-byte. This keeps each golden
// MSR hermetic (no musrfit required) while guarding generator regressions
// across the supported workflows.
//
// Regenerate a fixture intentionally by editing the config below, writing the
// output to tests/fixtures/reference/<name>.generated.msr, and re-running.
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const repoRoot = path.resolve(__dirname, "..");
function loadLibrary() {
const code = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
const sandbox = {
console: { log: function() {}, error: console.error, warn: console.warn },
window: { MSREditor: {}, MusrfitJob: {} },
alert: function(m) { throw new Error("Unexpected alert: " + m); }
};
sandbox.global = sandbox;
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
return sandbox;
}
// The canonical Asymmetry config used as the base for the other workflows. Only
// the fields that distinguish a workflow are overridden in each entry below.
const baseConfig = {
all: {
fitAsyType: "AsymmetryGLB",
Tis: "0",
Tfs: "8",
BINS: "100",
minType: "MIGRAD",
errEst: "HESSE",
fftUnits: "MHz",
fftPower: "12",
fftApod: "STRONG",
fftType: "power",
fftPhase: "8.5",
Tpis: "",
Tpfs: "",
VIEWBINS: "",
theoFunBlock: "asymmetry Asy\nsimplExpo Lam",
runNumbers: "514",
selYear: "2024",
instrName: "LEM",
dataRoot: "/psi.ch/data",
LRBF: "1 5,3 7",
valAlpha: "1.234",
errAlpha: "0.012",
minAlpha: "0.5",
maxAlpha: "1.5",
valAsy: "0.198",
errAsy: "0.006",
minAsy: "0.0",
maxAsy: "0.4",
valLam: "0.777",
errLam: "0.011",
minLam: "0.0",
maxLam: "5.0"
},
funOptions: [
{ value: "Alpha", disabled: false },
{ value: "Asy", disabled: false },
{ value: "Lam", disabled: false }
],
funLines: [],
sharedFlags: {}
};
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
function withConfig(overrides, rest) {
const config = clone(baseConfig);
Object.assign(config.all, overrides || {});
if (rest) {
if (rest.funOptions) config.funOptions = rest.funOptions;
if (rest.funLines) config.funLines = rest.funLines;
if (rest.sharedFlags) config.sharedFlags = rest.sharedFlags;
}
return config;
}
// name -> config factory. Each generated MSR must match the golden file
// tests/fixtures/reference/<name>.generated.msr exactly.
const fixtures = {
"asymmetry": baseConfig,
"bounds": withConfig({
fftType: "none",
fftUnits: "Gauss",
fftPower: "",
fftApod: "",
fftPhase: "",
valAlpha: "0.9", errAlpha: "0.01", minAlpha: "0.8", maxAlpha: "1.0",
valAsy: "0.2", errAsy: "0.01", minAsy: "0.1", maxAsy: "0.3",
valLam: "0.5", errLam: "0.01", minLam: "0.4", maxLam: "0.6"
}),
"t0background": withConfig({
t0BgCommonT0F: "1600",
t0BgCommonT0B: "1601",
t0BgCommonBgMode: "fixed",
t0BgCommonBgFixF: "11",
t0BgCommonBgFixB: "12"
}),
// Global-asymmetry fit over runs 2452-2460 with a shared phase (Phi). The
// generator emits a single-RUN template whose THEORY/FITPARAMETER cover the
// whole run family; muSR-ROOT msr2data expands it during the run.
"global": withConfig({
fitAsyType: "AsymmetryGLB",
Tis: "0.07",
Tfs: "9",
BINS: "50",
minType: "MINIMIZE",
fitObjective: "MAX_LIKELIHOOD",
fftUnits: "Gauss",
fftType: "power",
fftPhase: "",
fftPhaseCorrection: "off",
fftxi: "0",
fftxf: "11",
Tpis: "0",
Tpfs: "11",
Ypis: "-0.3",
Ypfs: "0.3",
VIEWBINS: "500",
theoFunBlock: "asymmetry Asy\nsimpleGss Sgm\nTFieldCos Phi Frq",
runNumbers: "2452-2460",
selYear: "2026",
instrName: "LEM",
dataRoot: "/mnt/fake",
LRBF: "21 25,23 27",
minAlpha: "", maxAlpha: "", minAsy: "", maxAsy: "",
valAlpha_2452: "0.9077", errAlpha_2452: "0.0016",
valAsy_2452: "0.0915", errAsy_2452: "0.0016",
valSgm_2452: "0.132", errSgm_2452: "0.013",
valPhi: "16.91", errPhi: "0.85",
valFrq_2452: "1.3664", errFrq_2452: "0.0012"
}, {
funOptions: [
{ value: "Alpha", disabled: false },
{ value: "Asy", disabled: false },
{ value: "Sgm", disabled: false },
{ value: "Phi", disabled: false },
{ value: "Frq", disabled: false }
],
sharedFlags: { Phi: true }
}),
// Multi-group asymmetry workflow: two forward,backward pairs from run 2452
// (LRBF "1,2/3,4"). The generator emits one RUN block per group with its own
// alpha and a map selecting ratio (map1) and phase (map2); the asymmetry
// line points at the generated amplitude function fun1 = Asy1 * map1.
"multiasym": withConfig({
fitAsyType: "AsymmetryGLB",
Tis: "0.07",
Tfs: "9",
BINS: "50",
minType: "MINIMIZE",
fitObjective: "MAX_LIKELIHOOD",
fftUnits: "Gauss",
fftType: "power",
fftPhase: "",
fftPhaseCorrection: "off",
fftxi: "0",
fftxf: "11",
Tpis: "0",
Tpfs: "11",
Ypis: "-0.3",
Ypfs: "0.3",
VIEWBINS: "500",
theoFunBlock: "asymmetry Asy1\nsimplExpo Lam\nTFieldCos Phi Frq",
runNumbers: "2452",
selYear: "2026",
instrName: "LEM",
dataRoot: "/mnt/fake",
LRBF: "1,2/3,4",
valAsy1_2452: "0.09", errAsy1_2452: "0.01",
valLam_2452: "0.5", errLam_2452: "0.01",
valFrq_2452: "1.3", errFrq_2452: "0.01",
valAlpha1_2452: "0.9", errAlpha1_2452: "0.01",
valAlpha2_2452: "0.95", errAlpha2_2452: "0.01",
valOne_2452: "1", errOne_2452: "0",
valr2_2452: "0.4", errr2_2452: "0.01",
valPhi1_2452: "16.9", errPhi1_2452: "0.8",
valPhi2_2452: "5.7", errPhi2_2452: "0.8"
}, {
funOptions: [
{ value: "Asy1", disabled: false },
{ value: "Lam", disabled: false },
{ value: "Frq", disabled: false },
{ value: "Alpha1", disabled: false },
{ value: "Alpha2", disabled: false },
{ value: "One", disabled: false },
{ value: "r2", disabled: false },
{ value: "Phi1", disabled: false },
{ value: "Phi2", disabled: false }
]
}),
// SingleHist detector-normalization workflow: four histogram groups each
// with its own No/NBg normalization and a detector-resolved phase, so the
// RUN blocks carry norm + map rather than a single asymmetry forward count.
"singlehist": withConfig({
fitAsyType: "SingleHist",
Tis: "0.07",
Tfs: "9",
BINS: "50",
minType: "MINIMIZE",
fitObjective: "MAX_LIKELIHOOD",
fftUnits: "Gauss",
fftType: "power",
fftPhase: "",
fftPhaseCorrection: "off",
fftxi: "0",
fftxf: "11",
Tpis: "0",
Tpfs: "11",
Ypis: "-0.3",
Ypfs: "0.3",
VIEWBINS: "500",
plotLifetimeCorrection: "1",
theoFunBlock: "asymmetry Asy\nsimpleGss Sgm\nTFieldCos Phi Frq",
runNumbers: "2452",
selYear: "2026",
instrName: "LEM",
dataRoot: "/mnt/fake",
LRBF: "21 25,23 27,22 26,24 28",
minAsy: "", maxAsy: "",
valAsy_2452: "0.09", errAsy_2452: "0.01",
valSgm_2452: "0.13", errSgm_2452: "0.01",
valPhi: "16.9", errPhi: "0.8",
valFrq_2452: "1.36", errFrq_2452: "0.01"
}, {
funOptions: [
{ value: "No1", disabled: false }, { value: "NBg1", disabled: false },
{ value: "No2", disabled: false }, { value: "NBg2", disabled: false },
{ value: "No3", disabled: false }, { value: "NBg3", disabled: false },
{ value: "No4", disabled: false }, { value: "NBg4", disabled: false },
{ value: "Asy", disabled: false }, { value: "Sgm", disabled: false },
{ value: "Phi", disabled: false }, { value: "Frq", disabled: false },
{ value: "PhiH2", disabled: false },
{ value: "PhiH3", disabled: false },
{ value: "PhiH4", disabled: false }
]
}),
// FUNCTIONS workflow: frequency expressed through a custom field parameter
// (fun1 = gamma_mu * Pfield), so the generated MSR carries a FUNCTIONS block.
"functions": withConfig({
fitAsyType: "AsymmetryGLB",
Tis: "0.07",
Tfs: "9",
BINS: "50",
minType: "MINIMIZE",
fitObjective: "MAX_LIKELIHOOD",
fftUnits: "Gauss",
fftType: "power",
fftPhase: "",
fftPhaseCorrection: "off",
fftxi: "0",
fftxf: "11",
Tpis: "0",
Tpfs: "11",
Ypis: "-0.3",
Ypfs: "0.3",
VIEWBINS: "500",
theoFunBlock: "asymmetry Asy\nsimpleGss Sgm\nTFieldCos Phi Frq",
runNumbers: "2452-2460",
selYear: "2026",
instrName: "LEM",
dataRoot: "/mnt/fake",
LRBF: "21 25,23 27",
minAlpha: "", maxAlpha: "", minAsy: "", maxAsy: "",
valAlpha_2452: "0.9077", errAlpha_2452: "0.0016",
valAsy_2452: "0.0915", errAsy_2452: "0.0016",
valSgm_2452: "0.132", errSgm_2452: "0.013",
valPhi: "16.91", errPhi: "0.85",
valPfield: "100", errPfield: "1"
}, {
funOptions: [
{ value: "Alpha", disabled: false },
{ value: "Asy", disabled: false },
{ value: "Sgm", disabled: false },
{ value: "Phi", disabled: false },
{ value: "Frq", disabled: true },
{ value: "Pfield", disabled: false }
],
funLines: ["Frq = gamma_mu * Pfield"],
sharedFlags: { Phi: true }
})
};
function referenceFixturePath(name) {
return path.join(__dirname, "fixtures", "reference", name);
}
const lib = loadLibrary();
for (const name of Object.keys(fixtures)) {
const msr = lib.buildMSR(fixtures[name]);
const golden = fs.readFileSync(referenceFixturePath(name + ".generated.msr"), "utf8");
assert.strictEqual(msr, golden,
"buildMSR() must reproduce the captured " + name + " reference fixture exactly. "
+ "If the generator intentionally changed, regenerate the fixture and re-run.");
}
// ----- Fitted reference fixtures -----
//
// A `.fitted.msr` is a real musrfit result captured as a reference. Unlike the
// generated goldens it is not regenerated by buildMSR(); instead it anchors the
// reverse-parsing path: muSRFlow must be able to read the fitted MSR back into
// the GUI state (fit statistic, parameters, theory, single-histogram settings).
function testFittedSingleHist(lib) {
const msr = fs.readFileSync(referenceFixturePath("singlehist.fitted.msr"), "utf8");
// The fit converged and the STATISTIC block parsed to a maxLH result.
const stat = lib.parseMSRFitStatistic(msr);
assert.strictEqual(stat.found, true);
assert.strictEqual(stat.converged, true);
assert.strictEqual(stat.kind, "maxLH");
assert.strictEqual(stat.ndf, "3644");
assert.strictEqual(stat.value, "1.040110");
// All 16 fitted parameters round-trip with run-suffixed names.
const params = lib.parseMSRParameters(msr);
assert.strictEqual(params.length, 16);
const names = params.map(function(p) { return p.name; });
for (const expected of ["Asy_2452", "Sgm_2452", "NBg1_2452", "No1_2452", "Ph_2452",
"alpha_LR_2452", "Field_2452"]) {
assert.ok(names.includes(expected), "expected fitted parameter " + expected);
}
// The model reconstructs to the GUI GaussianCos catalogue with its four
// single-histogram constraints (phase, frequency, and two normalizations).
const theory = lib.parseMSRTheoryModel(msr);
assert.strictEqual(theory.ok, true);
assert.strictEqual(theory.components.join(","), "GaussianCos");
const constraintTargets = theory.constraints.map(function(c) { return c.target; });
assert.strictEqual(constraintTargets.join(","), "Phi,Frq,No3,No4");
// The GUI settings recover a SingleHist, maximum-likelihood fit.
const gui = lib.parseMSRGUISettings(msr);
assert.strictEqual(gui.values.fitAsyType, "SingleHist");
assert.strictEqual(gui.values.fitObjective, "MAX_LIKELIHOOD");
assert.strictEqual(gui.values.runNumbers, "2452");
assert.strictEqual(gui.values.selYear, "2026");
assert.strictEqual(gui.values.instrName, "LEM");
assert.strictEqual(gui.values.fftUnits, "Gauss");
}
testFittedSingleHist(lib);
function testFittedGlobal(lib) {
const msr = fs.readFileSync(referenceFixturePath("global.fitted.msr"), "utf8");
// Converged chi-square global fit over 9 runs (NDF 8198).
const stat = lib.parseMSRFitStatistic(msr);
assert.strictEqual(stat.found, true);
assert.strictEqual(stat.converged, true);
assert.strictEqual(stat.kind, "chisq");
assert.strictEqual(stat.ndf, "8198");
assert.strictEqual(stat.value, "1.034138");
// 37 fitted parameters: one shared Phi plus 9 runs x 4 run-specific
// (Alpha, Asy, Sgm, Frq).
const params = lib.parseMSRParameters(msr);
assert.strictEqual(params.length, 37);
const names = params.map(function(p) { return p.name; });
assert.strictEqual(names.filter(function(n) { return n === "Phi"; }).length, 1);
for (const expected of ["Alpha_2452", "Asy_2460", "Sgm_2456", "Frq_2459", "Asy_2452"]) {
assert.ok(names.includes(expected), "expected fitted parameter " + expected);
}
// GUI settings recover a global asymmetry, max-likelihood fit over the
// expanded run list.
const gui = lib.parseMSRGUISettings(msr);
assert.strictEqual(gui.values.fitAsyType, "AsymmetryGLB");
assert.strictEqual(gui.values.fitObjective, "MAX_LIKELIHOOD");
assert.strictEqual(gui.values.runNumbers, "2452,2453,2454,2455,2456,2457,2458,2459,2460");
// Multi-run asymmetric THEORY (shared params through mapN) is deliberately
// NOT reconstructed: the GUI refuses to guess a merely-similar model. The
// receiver must see the safe refusal, not a silently-corrupted model.
const theory = lib.parseMSRTheoryModel(msr);
assert.strictEqual(theory.ok, false);
assert.ok(theory.error.length > 0);
}
testFittedGlobal(lib);
console.log("reference fixture tests passed");