Use msr2data after a series fit, electron side.
This commit is contained in:
1 parent
4ec6c7eb02
commit
a1b374b543
4 files changed
+328
-6
No files matched your search
+140
@@ -969,6 +969,146 @@ async function runSeriesMSRCandidateLocal(receipt, candidate, context = {}) {
|
||||
|
||||
window.runSeriesMSRCandidateLocal = runSeriesMSRCandidateLocal;
|
||||
|
||||
// Collect independent fitted MSRs without fitting them again. msr2data's
|
||||
// generated files, rather than its historically inconsistent exit status, are
|
||||
// authoritative: any non-empty requested .dat is registered immediately, and
|
||||
// any structurally valid combined MSR is safe to hand to musrview.
|
||||
async function collectIndependentSeriesArtifactsLocal(spec) {
|
||||
let result = {
|
||||
ok: false,
|
||||
reason: "invalid-series-artifact-spec",
|
||||
parameterDataPath: "",
|
||||
combinedMSRPath: "",
|
||||
logPath: "",
|
||||
plotStarted: false,
|
||||
warnings: []
|
||||
};
|
||||
if (!spec || spec.ok !== true || !Array.isArray(spec.runs)
|
||||
|| spec.runs.length < 2 || !Array.isArray(spec.msrPaths)
|
||||
|| spec.msrPaths.length !== spec.runs.length
|
||||
|| !/^_[A-Za-z0-9._+\-]+$/.test(spec.fileNameSuffix || "")) {
|
||||
return result;
|
||||
}
|
||||
|
||||
let workPath = nodePath.resolve(spec.workPath);
|
||||
let suffix = spec.fileNameSuffix;
|
||||
let baseRun = spec.runs[0];
|
||||
for (let index = 0; index < spec.runs.length; index++) {
|
||||
if (!/^\d+$/.test(spec.runs[index])
|
||||
|| nodePath.resolve(spec.msrPaths[index])
|
||||
!== nodePath.join(workPath, spec.runs[index] + suffix + ".msr")
|
||||
|| !isSafeSeriesRegularFile(spec.msrPaths[index])) {
|
||||
result.reason = "series-artifact-source-invalid";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
let generatedName = baseRun + "+global" + suffix + ".msr";
|
||||
let generatedPath = nodePath.join(workPath, generatedName);
|
||||
let combinedName = baseRun + "+series" + suffix + ".msr";
|
||||
let combinedPath = nodePath.join(workPath, combinedName);
|
||||
let parameterName = baseRun + "+series" + suffix + "_par.dat";
|
||||
let parameterPath = nodePath.join(workPath, parameterName);
|
||||
let logName = baseRun + "+series" + suffix + ".out";
|
||||
let logPath = nodePath.join(workPath, logName);
|
||||
result.logPath = logPath;
|
||||
|
||||
try {
|
||||
for (let stalePath of [generatedPath, combinedPath, parameterPath]) {
|
||||
if (!nodeFs.existsSync(stalePath)) continue;
|
||||
if (!isSafeSeriesRegularFile(stalePath)) {
|
||||
result.reason = "series-artifact-output-invalid";
|
||||
return result;
|
||||
}
|
||||
nodeFs.unlinkSync(stalePath);
|
||||
}
|
||||
nodeFs.writeFileSync(logPath,
|
||||
"# muSRFlow independent series collection\n# Working directory: "
|
||||
+ workPath + "\n", { encoding: "utf8", flag: "w" });
|
||||
|
||||
let runArgument = "[" + spec.runs.join(" ") + "]";
|
||||
let args = [runArgument, suffix, "new", "global", "data", "-o", parameterName];
|
||||
let msr2data = resolveLocalCommand("msr2data");
|
||||
let commandLine = displayCommand(msr2data, args);
|
||||
appendFitLog(logPath, "\n$ " + commandLine + "\n");
|
||||
if (typeof appendJobLog === "function") {
|
||||
appendJobLog("Starting independent series collection: " + commandLine);
|
||||
}
|
||||
let env = Object.assign({}, process.env);
|
||||
env.PATH = ["/usr/local/bin", "/usr/bin", "/bin", env.PATH || ""].join(":");
|
||||
let commandResult = nodeChildProcess.spawnSync(msr2data, args, {
|
||||
cwd: workPath,
|
||||
env: env,
|
||||
encoding: "utf8",
|
||||
shell: false
|
||||
});
|
||||
if (commandResult.stdout) {
|
||||
appendProcessOutput("independent series msr2data", commandResult.stdout, logPath);
|
||||
}
|
||||
if (commandResult.stderr) {
|
||||
appendProcessOutput("independent series msr2data", commandResult.stderr, logPath);
|
||||
}
|
||||
let code = typeof commandResult.status === "number" ? commandResult.status : 1;
|
||||
appendFitLog(logPath, "independent series msr2data exited with code " + code + "\n");
|
||||
|
||||
if (isSafeSeriesRegularFile(parameterPath)
|
||||
&& nodeFs.statSync(parameterPath).size > 0) {
|
||||
try {
|
||||
cleanMusrfitParameterDataHeader(parameterPath);
|
||||
} catch (error) {
|
||||
result.warnings.push("Could not normalize the parameter-table header.");
|
||||
}
|
||||
// A non-empty table is usable even if header cleanup or the external
|
||||
// program's status is imperfect.
|
||||
result.parameterDataPath = parameterPath;
|
||||
} else {
|
||||
result.warnings.push("msr2data did not produce parameter data.");
|
||||
}
|
||||
|
||||
if (isSafeSeriesRegularFile(generatedPath)) {
|
||||
let generatedText = nodeFs.readFileSync(generatedPath, "utf8");
|
||||
if (isValidGeneratedGlobalMSR(generatedText)) {
|
||||
let combinedText = normalizeMSRPlotRuns(generatedText, {}, spec.runs);
|
||||
nodeFs.writeFileSync(combinedPath, combinedText, {
|
||||
encoding: "utf8", flag: "wx"
|
||||
});
|
||||
result.combinedMSRPath = combinedPath;
|
||||
nodeFs.unlinkSync(generatedPath);
|
||||
} else {
|
||||
result.warnings.push("msr2data produced an invalid combined display MSR.");
|
||||
}
|
||||
} else {
|
||||
result.warnings.push("msr2data did not produce a combined display MSR.");
|
||||
}
|
||||
if (code !== 0 && (result.parameterDataPath || result.combinedMSRPath)) {
|
||||
result.warnings.push("msr2data returned code " + code
|
||||
+ " but produced usable series artifacts.");
|
||||
}
|
||||
|
||||
if (result.combinedMSRPath) {
|
||||
let musrviewArgs = [];
|
||||
if (spec.fftv == "y") musrviewArgs.push("-f");
|
||||
musrviewArgs.push(combinedName);
|
||||
let viewId = window.MusrfitRunner.nextViewId++;
|
||||
runLocalProcess("musrview series #" + viewId,
|
||||
resolveLocalCommand("musrview"), musrviewArgs, workPath, logPath,
|
||||
{ reportStatus: false });
|
||||
result.plotStarted = true;
|
||||
}
|
||||
result.ok = Boolean(result.parameterDataPath || result.combinedMSRPath);
|
||||
result.reason = result.ok
|
||||
? "series-artifacts-collected" : "series-artifacts-not-produced";
|
||||
return result;
|
||||
} catch (error) {
|
||||
result.reason = "series-artifact-collection-failed";
|
||||
result.warnings.push(error && error.message
|
||||
? error.message : "Series post-processing failed.");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
window.collectIndependentSeriesArtifactsLocal = collectIndependentSeriesArtifactsLocal;
|
||||
|
||||
function exportLocalMusrfitParameterData(resolvedPath, options, logFile) {
|
||||
let runs = String(options.runs || "").trim().split(/\s+/).filter(Boolean);
|
||||
let baseRun = String(options.baseRun || (runs.length ? runs[0] : ""));
|
||||
|
||||
+95
-1
@@ -8895,6 +8895,92 @@ function seriesDataSetText(dataSet) {
|
||||
return normalized ? [normalized.main].concat(normalized.add).join("+") : "";
|
||||
}
|
||||
|
||||
// Describe the fitted per-run files that may be collected after an independent
|
||||
// series completes. This is deliberately separate from candidate preparation:
|
||||
// collection never changes, refits, or reclassifies an individual result.
|
||||
function buildIndependentSeriesArtifactSpec(plan, config = {}) {
|
||||
let result = {
|
||||
ok: false,
|
||||
reason: "invalid-series-artifact-plan",
|
||||
workPath: String(config.workPath || ""),
|
||||
fileNameSuffix: String(config.fileNameSuffix || ""),
|
||||
runs: [],
|
||||
msrPaths: [],
|
||||
fftv: config.fftv == "y" ? "y" : "n"
|
||||
};
|
||||
if (!plan || plan.ok !== true || plan.mode != "series"
|
||||
|| !Array.isArray(plan.entries) || plan.entries.length < 2
|
||||
|| result.workPath === "" || result.fileNameSuffix === "") {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (let entry of plan.entries) {
|
||||
if (!entry || entry.action == "blocked" || !entry.dataSet
|
||||
|| !Number.isInteger(entry.dataSet.main) || entry.dataSet.main <= 0
|
||||
|| typeof entry.msrPath !== "string" || entry.msrPath === "") {
|
||||
result.reason = "incomplete-series-artifact-plan";
|
||||
return result;
|
||||
}
|
||||
result.runs.push(entry.dataSet.main.toString());
|
||||
result.msrPaths.push(entry.msrPath);
|
||||
}
|
||||
if (new Set(result.runs).size !== result.runs.length) {
|
||||
result.reason = "duplicate-series-artifact-run";
|
||||
return result;
|
||||
}
|
||||
result.baseRun = result.runs[0];
|
||||
result.ok = true;
|
||||
result.reason = "series-artifact-plan-ready";
|
||||
return result;
|
||||
}
|
||||
|
||||
async function collectIndependentSeriesArtifacts(plan, config, webMode) {
|
||||
let spec = buildIndependentSeriesArtifactSpec(plan, config);
|
||||
if (!spec.ok) {
|
||||
return { ok: false, reason: spec.reason, warnings: [
|
||||
"The completed series could not be prepared for parameter collection."
|
||||
] };
|
||||
}
|
||||
let collector = webMode
|
||||
? window.collectIndependentSeriesArtifactsWeb
|
||||
: window.collectIndependentSeriesArtifactsLocal;
|
||||
if (typeof collector !== "function") {
|
||||
return { ok: false, reason: "series-artifact-collector-unavailable", warnings: [
|
||||
"Series parameter collection is unavailable."
|
||||
] };
|
||||
}
|
||||
|
||||
updateJobStatus({
|
||||
state: "Collecting series",
|
||||
message: "Collecting fitted-run parameters.",
|
||||
messageDetail: "Running msr2data on the fitted and reused independent-run MSRs.",
|
||||
noticeLevel: "info"
|
||||
});
|
||||
appendJobLog("Collecting " + spec.runs.length
|
||||
+ " independent fitted runs with msr2data.");
|
||||
let artifacts;
|
||||
try {
|
||||
artifacts = await collector(spec);
|
||||
} catch (error) {
|
||||
artifacts = {
|
||||
ok: false,
|
||||
reason: "series-artifact-collection-failed",
|
||||
warnings: [error && error.message ? error.message : "Series collection failed."]
|
||||
};
|
||||
}
|
||||
artifacts = artifacts && typeof artifacts === "object" ? artifacts : {};
|
||||
let status = {};
|
||||
if (artifacts.parameterDataPath) status.parameterDataPath = artifacts.parameterDataPath;
|
||||
if (artifacts.combinedMSRPath) status.resultMSR = artifacts.combinedMSRPath;
|
||||
if (artifacts.logPath) status.logPath = artifacts.logPath;
|
||||
if (artifacts.plotPath) status.plotPath = artifacts.plotPath;
|
||||
if (Object.keys(status).length > 0) updateJobStatus(status);
|
||||
for (let warning of (Array.isArray(artifacts.warnings) ? artifacts.warnings : [])) {
|
||||
if (warning) appendJobLog("Series post-processing warning: " + warning);
|
||||
}
|
||||
return artifacts;
|
||||
}
|
||||
|
||||
// When no fitted reference exists, make the first selected data set runnable
|
||||
// directly from the current GUI model. This is the series equivalent of the
|
||||
// normal single-run template: it never enters msr2data/global preparation.
|
||||
@@ -9217,11 +9303,19 @@ async function runIndependentSeriesFit(execution, webMode) {
|
||||
});
|
||||
|
||||
if (sequence.ok) {
|
||||
let artifacts = await collectIndependentSeriesArtifacts(plan, Object.assign({}, config, {
|
||||
fftv: execution && execution.fftv ? execution.fftv : "n"
|
||||
}), webMode);
|
||||
let message = sequence.reason == "series-already-complete"
|
||||
? "All selected runs are already fitted."
|
||||
: "Independent series fit completed: " + fitted + " fitted, "
|
||||
+ reused + " reused.";
|
||||
finishJobStatus(1, 0, message);
|
||||
if (artifacts.parameterDataPath) message += " Parameter table prepared.";
|
||||
if (artifacts.plotStarted || artifacts.plotPath) message += " Combined plot opened.";
|
||||
let warnings = Array.isArray(artifacts.warnings)
|
||||
? artifacts.warnings.filter(Boolean) : [];
|
||||
finishJobStatus(1, 0, message
|
||||
+ (warnings.length ? "\nPost-processing warning: " + warnings.join("; ") : ""));
|
||||
return 1;
|
||||
}
|
||||
if (sequence.reason == "series-sequence-completed-with-blocked-runs"
|
||||
|
||||
@@ -131,9 +131,12 @@ async function testMissingReferenceBootstrapsFirstRun() {
|
||||
lib.synchronizeLastSeriesResult = async function() {};
|
||||
|
||||
const originalEntries = [
|
||||
{ run: 514, action: "fit", dataSet: { main: 514, add: [] } },
|
||||
{ run: 515, action: "fit", dataSet: { main: 515, add: [] } },
|
||||
{ run: 516, action: "fit", dataSet: { main: 516, add: [] } }
|
||||
{ run: 514, action: "fit", dataSet: { main: 514, add: [] },
|
||||
msrPath: "/work/514_asy.msr" },
|
||||
{ run: 515, action: "fit", dataSet: { main: 515, add: [] },
|
||||
msrPath: "/work/515_asy.msr" },
|
||||
{ run: 516, action: "fit", dataSet: { main: 516, add: [] },
|
||||
msrPath: "/work/516_asy.msr" }
|
||||
];
|
||||
const originalPlan = {
|
||||
ok: true,
|
||||
@@ -159,7 +162,8 @@ async function testMissingReferenceBootstrapsFirstRun() {
|
||||
mode: "series",
|
||||
entries: [
|
||||
{ run: 514, action: "skip", reusable: true, msrExists: true,
|
||||
msrText: "fitted", dataSet: { main: 514, add: [] } },
|
||||
msrText: "fitted", dataSet: { main: 514, add: [] },
|
||||
msrPath: "/work/514_asy.msr" },
|
||||
originalEntries[1],
|
||||
originalEntries[2]
|
||||
],
|
||||
@@ -219,6 +223,17 @@ async function testMissingReferenceBootstrapsFirstRun() {
|
||||
};
|
||||
lib.window.MusrfitSeriesTransactionAdapter = {};
|
||||
lib.window.runSeriesMSRCandidateLocal = async function() { return { ok: 1 }; };
|
||||
let artifactSpec = null;
|
||||
lib.window.collectIndependentSeriesArtifactsLocal = async function(spec) {
|
||||
artifactSpec = spec;
|
||||
return {
|
||||
ok: true,
|
||||
parameterDataPath: "/work/514+series_asy_par.dat",
|
||||
combinedMSRPath: "/work/514+series_asy.msr",
|
||||
plotStarted: true,
|
||||
warnings: []
|
||||
};
|
||||
};
|
||||
let finished = null;
|
||||
lib.finishJobStatus = function(ok, code, message) {
|
||||
finished = { ok: ok, code: code, message: message };
|
||||
@@ -231,10 +246,15 @@ async function testMissingReferenceBootstrapsFirstRun() {
|
||||
assert.strictEqual(bootstrapPrepared, 1);
|
||||
assert.strictEqual(remainingPrepared, 1);
|
||||
assert.strictEqual(sequenceCalls, 2);
|
||||
assert.deepStrictEqual(Array.from(artifactSpec.runs), ["514", "515", "516"]);
|
||||
assert.deepStrictEqual(Array.from(artifactSpec.msrPaths), [
|
||||
"/work/514_asy.msr", "/work/515_asy.msr", "/work/516_asy.msr"
|
||||
]);
|
||||
assert.deepStrictEqual(finished, {
|
||||
ok: 1,
|
||||
code: 0,
|
||||
message: "Independent series fit completed: 3 fitted, 0 reused."
|
||||
+ " Parameter table prepared. Combined plot opened."
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -197,8 +197,76 @@ async function testElectronAdapterRetainsRejectedNewMSR() {
|
||||
}
|
||||
}
|
||||
|
||||
async function testElectronSeriesArtifactCollection() {
|
||||
const tempDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-series-collect-"));
|
||||
const statusUpdates = [];
|
||||
const syncCalls = [];
|
||||
const asyncCalls = [];
|
||||
try {
|
||||
for (const run of [514, 515]) {
|
||||
fs.writeFileSync(path.join(tempDirectory, run + "_asy.msr"),
|
||||
runnableMSR(run, true), "utf8");
|
||||
}
|
||||
const fakeChildProcess = {
|
||||
spawnSync: function(command, args, options) {
|
||||
syncCalls.push({ command: command, args: args.slice(), cwd: options.cwd });
|
||||
fs.writeFileSync(path.join(options.cwd, "514+global_asy.msr"),
|
||||
runnableMSR(514, false).replace(
|
||||
"RUN /data/run514 MUE4 PSI MUSR-ROOT",
|
||||
"RUN /data/run514 MUE4 PSI MUSR-ROOT\nRUN /data/run515 MUE4 PSI MUSR-ROOT"
|
||||
), "utf8");
|
||||
fs.writeFileSync(path.join(options.cwd, "514+series_asy_par.dat"),
|
||||
"RUN Asy_ Asy_PosErr\n514 0.2 0.01\n515 0.21 0.01\n", "utf8");
|
||||
return { status: 7, stdout: "artifacts written\n", stderr: "" };
|
||||
},
|
||||
spawn: function(command, args, options) {
|
||||
asyncCalls.push({ command: command, args: args.slice(), cwd: options.cwd });
|
||||
const child = new events.EventEmitter();
|
||||
child.stdout = new events.EventEmitter();
|
||||
child.stderr = new events.EventEmitter();
|
||||
setImmediate(function() { child.emit("close", 0, null); });
|
||||
return child;
|
||||
}
|
||||
};
|
||||
const sandbox = loadElectronSandbox(fakeChildProcess, statusUpdates);
|
||||
const result = await sandbox.window.collectIndependentSeriesArtifactsLocal({
|
||||
ok: true,
|
||||
workPath: tempDirectory,
|
||||
fileNameSuffix: "_asy",
|
||||
runs: ["514", "515"],
|
||||
msrPaths: [
|
||||
path.join(tempDirectory, "514_asy.msr"),
|
||||
path.join(tempDirectory, "515_asy.msr")
|
||||
],
|
||||
fftv: "n"
|
||||
});
|
||||
|
||||
assert.strictEqual(result.ok, true);
|
||||
assert.strictEqual(result.parameterDataPath,
|
||||
path.join(tempDirectory, "514+series_asy_par.dat"));
|
||||
assert.strictEqual(result.combinedMSRPath,
|
||||
path.join(tempDirectory, "514+series_asy.msr"));
|
||||
assert.strictEqual(result.plotStarted, true);
|
||||
assert.match(result.warnings.join("\n"), /returned code 7/);
|
||||
assert.deepStrictEqual(Array.from(syncCalls[0].args),
|
||||
["[514 515]", "_asy", "new", "global", "data", "-o",
|
||||
"514+series_asy_par.dat"]);
|
||||
assert.deepStrictEqual(Array.from(asyncCalls[0].args), ["514+series_asy.msr"]);
|
||||
assert.match(fs.readFileSync(result.parameterDataPath, "utf8"),
|
||||
/^RUN Asy Asy_PosErr/m);
|
||||
assert.match(fs.readFileSync(result.combinedMSRPath, "utf8"),
|
||||
/^runs\s+1\s+2$/m);
|
||||
assert.strictEqual(fs.existsSync(path.join(tempDirectory,
|
||||
"514+global_asy.msr")), false);
|
||||
await new Promise(function(resolve) { setImmediate(resolve); });
|
||||
} finally {
|
||||
fs.rmSync(tempDirectory, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
Promise.resolve().then(testElectronAdapterFitsOnlyOneMSR)
|
||||
.then(testElectronAdapterRetainsRejectedNewMSR).then(function() {
|
||||
.then(testElectronAdapterRetainsRejectedNewMSR)
|
||||
.then(testElectronSeriesArtifactCollection).then(function() {
|
||||
console.log("series runner adapter tests passed");
|
||||
}).catch(function(error) {
|
||||
console.error(error);
|
||||
|
||||
Reference in new issue
Block a user