diff --git a/docs/source/runs-fitting.rst b/docs/source/runs-fitting.rst index 4384448..18d1988 100644 --- a/docs/source/runs-fitting.rst +++ b/docs/source/runs-fitting.rst @@ -108,9 +108,12 @@ Suggest fit ----------- The **Suggest fit** button at the lower-right of the application panel compares -the current instrument, fit type, histogram layout, run count, and available -run metadata with a scrubbed index of expert historical analyses. It presents -up to five complete fit recipes and changes nothing until **Apply** is pressed. +the current instrument, run count, and available run metadata with a scrubbed +index of expert historical analyses. It presents up to five complete fit +recipes and changes nothing until **Apply** is pressed. The current Type and +Histograms controls do not constrain the search: each result states the fit +type and detector grouping learned from its nearest historical context, and +**Apply** updates both controls together with the model. For oscillating LEM recipes, applying a suggestion also reproduces the dominant training conventions: diff --git a/muSRFitlib.js b/muSRFitlib.js index 7b73e69..4bb0dcc 100644 --- a/muSRFitlib.js +++ b/muSRFitlib.js @@ -7048,11 +7048,12 @@ function defaultHistograms(force) // Construct only the structured fields retained by the scrubbed expert index. // Header-derived field information is used when already available; opening the // dialog never starts a download or fit as a hidden side effect. -function buildModelRecommendationInput() +function buildModelRecommendationInput(fitTypeOverride) { prepCFG(); let dataSets = getActiveRunSelectionDataSets(All); - let fitType = getFitType(All); + let fitType = fitTypeOverride === 0 || fitTypeOverride === 2 + ? fitTypeOverride : getFitType(All); let histogramText = String(All.LRBF || ""); let histogramSets = []; let logicalRuns = dataSets.length; @@ -7363,8 +7364,11 @@ async function showModelSuggestions() } } - let input = buildModelRecommendationInput(); - if (!input.n_runs) { + let inputByFitType = { + 0: buildModelRecommendationInput(0), + 2: buildModelRecommendationInput(2) + }; + if (!inputByFitType[0].n_runs && !inputByFitType[2].n_runs) { contextElement.textContent = "Enter a valid run selection before requesting suggestions."; dialog.showModal(); return; @@ -7372,25 +7376,54 @@ async function showModelSuggestions() let bestBySignature = new Map(); let totalBySignature = new Map(); for (let entry of index.entries) { + let candidateFitTypes = Array.isArray(entry.input.fittypes) + ? Array.from(new Set(entry.input.fittypes)) : []; + if (candidateFitTypes.length !== 1 + || (candidateFitTypes[0] !== 0 && candidateFitTypes[0] !== 2)) { + continue; + } + let fitType = candidateFitTypes[0]; + let candidateKey = fitType + "|" + entry.signature; + let candidateHistogramSets = entry.input.measurement + && Array.isArray(entry.input.measurement.histogram_sets) + ? entry.input.measurement.histogram_sets : []; + let candidateHistogramGroups = []; + for (let histogramSet of candidateHistogramSets) { + let forward = Array.isArray(histogramSet.forward) + ? histogramSet.forward.join(" ").trim() : ""; + let backward = Array.isArray(histogramSet.backward) + ? histogramSet.backward.join(" ").trim() : ""; + let group = fitType === 0 ? forward + : (forward && backward ? forward + "," + backward : ""); + if (group && !candidateHistogramGroups.includes(group)) { + candidateHistogramGroups.push(group); + } + } + let candidateHistogramText = candidateHistogramGroups.join(fitType === 0 ? "," : "/"); + if (!candidateHistogramText) continue; totalBySignature.set( - entry.signature, - (totalBySignature.get(entry.signature) || 0) + Number(entry.count || 0) + candidateKey, + (totalBySignature.get(candidateKey) || 0) + Number(entry.count || 0) ); - let score = scoreModelRecommendation(input, entry.input, index.weights); - let previous = bestBySignature.get(entry.signature); + let score = scoreModelRecommendation(inputByFitType[fitType], entry.input, index.weights); + let previous = bestBySignature.get(candidateKey); if (!previous || score > previous.score) { - bestBySignature.set(entry.signature, { + bestBySignature.set(candidateKey, { + key: candidateKey, signature: entry.signature, recipe: entry.recipe, + fitType: fitType, + histogramText: candidateHistogramText, score: score }); } } let ranked = Array.from(bestBySignature.values()).sort(function(left, right) { if (right.score !== left.score) return right.score - left.score; - return (totalBySignature.get(right.signature) || 0) - - (totalBySignature.get(left.signature) || 0); + return (totalBySignature.get(right.key) || 0) + - (totalBySignature.get(left.key) || 0); }).slice(0, 5); + let input = inputByFitType[2]; let contextValues = []; let field = input.conditions.field; let temperature = input.conditions.temperature; @@ -7417,14 +7450,18 @@ async function showModelSuggestions() + ". No sample title or file path is used."; for (let rank = 0; rank < ranked.length; rank++) { let suggestion = ranked[rank]; - let expandedRecipe = buildSuggestedModelRecipe(suggestion.recipe, input, index); + let suggestionInput = inputByFitType[suggestion.fitType]; + let expandedRecipe = buildSuggestedModelRecipe(suggestion.recipe, suggestionInput, index); let row = document.createElement("div"); row.className = "model-suggestion-result"; let title = document.createElement("strong"); - title.textContent = (rank + 1) + ". " + suggestion.recipe.join(" + "); + let fitTypeLabel = suggestion.fitType === 0 ? "Single Hist" : "Asymmetry"; + title.textContent = (rank + 1) + ". " + fitTypeLabel + ": " + + suggestion.recipe.join(" + "); let evidence = document.createElement("span"); - evidence.textContent = "Nearest-context score " + suggestion.score.toFixed(2) - + "; " + (totalBySignature.get(suggestion.signature) || 0) + evidence.textContent = "Histograms " + suggestion.histogramText + + "; nearest-context score " + suggestion.score.toFixed(2) + + "; " + (totalBySignature.get(suggestion.key) || 0) + " historical training record(s)" + (expandedRecipe.labels.length ? "; " + expandedRecipe.labels.join(", ") : ""); @@ -7432,7 +7469,9 @@ async function showModelSuggestions() apply.type = "button"; apply.textContent = "Apply"; apply.addEventListener("click", function() { - applyModelSuggestion(suggestion.recipe); + applyModelSuggestion( + suggestion.recipe, suggestion.fitType, suggestion.histogramText + ); }); row.appendChild(title); row.appendChild(evidence); @@ -7442,14 +7481,27 @@ async function showModelSuggestions() if (!dialog.open) dialog.showModal(); } -function applyModelSuggestion(recipe) +function applyModelSuggestion(recipe, fitType, histogramText) { if (!Array.isArray(recipe) || recipe.length === 0) return; for (let model of recipe) { if (!funcDictionary[model]) return; } + if (fitType === 0 || fitType === 2) { + let fitTypeValue = fitType === 0 ? "SingleHist" : "AsymmetryGLB"; + let fitTypeElement = document.getElementById("fitAsyType"); + if (fitTypeElement) fitTypeElement.value = fitTypeValue; + All.fitAsyType = fitTypeValue; + if (typeof histogramText === "string" && histogramText.trim()) { + let histogramElement = document.getElementById("LRBF"); + if (histogramElement) histogramElement.value = histogramText; + All.LRBF = histogramText; + } + updatePlotOptionVisibility(); + clearTabs(true); + } let index = window.muSRFlowModelIndex || {}; - let input = buildModelRecommendationInput(); + let input = buildModelRecommendationInput(fitType); let expandedRecipe = buildSuggestedModelRecipe(recipe, input, index); if (!applyMSRTheoryModel(expandedRecipe.parsed, All)) return; for (let parameterName of Object.keys(expandedRecipe.defaults)) { diff --git a/tests/model-recommendation.test.js b/tests/model-recommendation.test.js index 164bbd2..7314003 100644 --- a/tests/model-recommendation.test.js +++ b/tests/model-recommendation.test.js @@ -141,15 +141,22 @@ function testExplicitRecipeApplication() { let applied = null; let rebuilt = 0; let closed = 0; + let plotVisibilityUpdates = 0; + let clearedTabs = 0; const parameterTab = { innerHTML: "old" }; + const fitTypeSelect = { value: "AsymmetryGLB" }; + const histogramInput = { value: "21 25,23 27" }; sandbox.document = { getElementById: function(id) { - return id === "Params" ? parameterTab : null; + if (id === "Params") return parameterTab; + if (id === "fitAsyType") return fitTypeSelect; + if (id === "LRBF") return histogramInput; + return null; } }; - sandbox.buildModelRecommendationInput = function() { + sandbox.buildModelRecommendationInput = function(fitType) { return { - instrument: "LEM", n_runs: 1, fittypes: [2], conditions: {}, + instrument: "LEM", n_runs: 1, fittypes: [fitType], conditions: {}, measurement: { histogram_sets: [] } }; }; @@ -163,15 +170,30 @@ function testExplicitRecipeApplication() { sandbox.closeModelSuggestions = function() { closed++; }; - sandbox.applyModelSuggestion(["GaussianCos", "Exponential"]); + sandbox.updatePlotOptionVisibility = function() { + plotVisibilityUpdates++; + }; + sandbox.clearTabs = function(reinitialize) { + assert.strictEqual(reinitialize, true); + clearedTabs++; + }; + sandbox.applyModelSuggestion( + ["GaussianCos", "Exponential"], 0, "21 25,22 26,23 27,24 28" + ); assert.deepStrictEqual( JSON.parse(JSON.stringify(applied.components)), ["GaussianCos", "Exponential"] ); assert.strictEqual(sandbox.All.valField1, "100"); + assert.strictEqual(sandbox.All.fitAsyType, "SingleHist"); + assert.strictEqual(sandbox.All.LRBF, "21 25,22 26,23 27,24 28"); + assert.strictEqual(fitTypeSelect.value, "SingleHist"); + assert.strictEqual(histogramInput.value, "21 25,22 26,23 27,24 28"); assert.strictEqual(parameterTab.innerHTML, ""); assert.strictEqual(rebuilt, 1); assert.strictEqual(closed, 1); + assert.strictEqual(plotVisibilityUpdates, 1); + assert.strictEqual(clearedTabs, 1); } function testMarkupContract() {