Skip to content

Fix Parameter Loading Bug for conflicted names#41380

Open
andy-bridger wants to merge 4 commits into
mainfrom
fix-parameter-name-conflict-bug
Open

Fix Parameter Loading Bug for conflicted names#41380
andy-bridger wants to merge 4 commits into
mainfrom
fix-parameter-name-conflict-bug

Conversation

@andy-bridger
Copy link
Copy Markdown
Collaborator

@andy-bridger andy-bridger commented May 11, 2026

Description of work

Currently there is a bug for loading default fitting parameters from Parameters files which is that the parameter is cached in the IDF, not by name and function, just by name. This can cause functions with identical parameter names to overwrite in the cache e.g. Bk2BkExpConvPV:Gamma and IkedaCarpenterPV:Gamma.

Additionally this is stored in the parameter map only as the name (so had both survived the first step they would be lost here)

When the parameter is loaded by being found by paramMap.getRecursive(detector, "Gamma", "fitting") IFunction will find the first reference to Gamma, check that the function name matches and if it doesn't (because it has been overwritten) loading is skipped and the parameter hints are not used.

The change has been:

  • Make the IDF cache store with the full function + parameter
  • The parameter map use the full function + parameter

Closes #xxxx.

To test:

ENGINX_305738_361838_bank_dSpacing.nxs.txt

You can run the following script on main and this branch:

# import mantid algorithms, numpy and matplotlib
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np
from Engineering.texture.TextureUtils import _estimate_intensity_background_and_centre

ws = Load(Filename = r"C:\Users\kcd17618\Documents\MantidScripts\LoadParameterNameConflictBug\ENGINX_305738_361838_bank_dSpacing.nxs")

hkl, peak_info = "220", (1.922, 1.81, 2.02)

ispec = 0
peak_types = ("IkedaCarpenterPV", "Bk2BkExpConvPV")
niters = 0

PARAM_FILE = r"instrument\ENGIN-X_Parameters.xml"
print(f"Applying parameter file: {PARAM_FILE}")
LoadParameterFile(Workspace=ws, Filename=PARAM_FILE)

for peak_type in peak_types:
    spec = ExtractSingleSpectrum(InputWorkspace=ws, OutputWorkspace=f"spec_{peak_type}",
                          WorkspaceIndex=ispec)

    PEAK_D, x_lo, x_hi = peak_info

    istart = spec.yIndexOfX(x_lo, 0)
    iend = spec.yIndexOfX(x_hi, 0)
    intens, sigma, bg, centre = _estimate_intensity_background_and_centre(spec, 0, istart, iend, PEAK_D)
    print(intens)

    fn = (
        f"name=LinearBackground, A0 = {bg};"
        f"name={peak_type},X0={centre}"
    )


    fitter = Fit(
        Function=fn,
        InputWorkspace=f"spec_{peak_type}",
        WorkspaceIndex=0,
        StartX=x_lo,
        EndX=x_hi,
        CreateOutput=True,
        Output=f"fit_{peak_type}",
        MaxIterations=niters,
        Minimizer= "Levenberg-Marquardt",#,AbsError=1e-08,RelError=1e-08",
        CostFunction = "Rwp",
        #StepSizeMethod = "Sqrt epsilon"
    )

    peak_params = mtd[f"fit_{peak_type}_Parameters"]

    def print_params(params):
        print(f"  Parameter Table: {params.name()}")
        print(f"\n  {'Name':<30}  {'Value':>14}  {'Error':>14}")
        print(f"  {'-'*60}")
        for i in range(params.rowCount()):
            name = params.cell(i, 0)
            val  = params.cell(i, 1)
            err  = params.cell(i, 2)
            print(f"  {name:<30}  {val:>14.6g}  {err:>14.6g}")
        print(f"\n")
            
    print_params(peak_params)

Main results:

  Parameter Table: fit_IkedaCarpenterPV_Parameters

  Name                                     Value           Error
  ------------------------------------------------------------
  f0.A0                                 0.113169             nan
  f0.A1                                        0             nan
  f1.I                                         0             nan
  f1.Alpha0                          8.69507e-05             nan
  f1.Alpha1                          8.15163e-05             nan
  f1.Beta0                            0.00173358             nan
  f1.Kappa                               19.3057               0
  f1.SigmaSquared                    3.79149e-06             nan
  **f1.Gamma                            0.00015706             nan**
  f1.X0                                  1.91343             inf
  Cost function value                    35701.7               0


  Parameter Table: fit_Bk2BkExpConvPV_Parameters

  Name                                     Value           Error
  ------------------------------------------------------------
  f0.A0                                 0.113169             nan
  f0.A1                                        0             nan
  f1.X0                                  1.91343             nan
  f1.Intensity                                 0             nan
  f1.Alpha                               2643.68               0
  f1.Beta                                382.058               0
  f1.Sigma2                            0.0679335             nan
  **f1.Gamma                                     0             inf**
  Cost function value                    35701.7               0

This branch:

  Parameter Table: fit_IkedaCarpenterPV_Parameters

  Name                                     Value           Error
  ------------------------------------------------------------
  f0.A0                                 0.113573             nan
  f0.A1                                        0             nan
  f1.I                                         0             nan
  f1.Alpha0                          0.000186208               0
  f1.Alpha1                          5.43506e-05               0
  f1.Beta0                            0.00228346               0
  f1.Kappa                               19.3057               0
  f1.SigmaSquared                    3.79338e-06             nan
 ** f1.Gamma                            0.00015726             nan**
  f1.X0                                  1.91368             inf
  Cost function value                    35698.3               0


  Parameter Table: fit_Bk2BkExpConvPV_Parameters

  Name                                     Value           Error
  ------------------------------------------------------------
  f0.A0                                 0.113573             nan
  f0.A1                                        0             nan
  f1.X0                                  1.91368             nan
  f1.Intensity                                 0             nan
  f1.Alpha                               2643.02               0
  f1.Beta                                381.986               0
  f1.Sigma2                             0.067962             nan
  **f1.Gamma                           0.000535338             inf**
  Cost function value                    35698.3               0


Reviewer

Your comments will be used as part of the gatekeeper process. Comment clearly on what you have checked and tested during your review. Provide an audit trail for any changes requested.

As per the review guidelines:

  • Is the code of an acceptable quality? (Code standards/GUI standards)
  • Has a thorough functional test been performed? Do the changes handle unexpected input/situations?
  • Are appropriately scoped unit and/or system tests provided?
  • Do the release notes conform to the guidelines and describe the changes appropriately?
  • Has the relevant (user and developer) documentation been added/updated?
  • If the PR author isn’t in the mantid-developers or mantid-contributors teams, add a review comment rerun ci to authorize/rerun the CI

Gatekeeper

As per the gatekeeping guidelines:

  • Has a thorough first line review been conducted, including functional testing?
  • At a high-level, is the code quality sufficient?
  • Are the base, milestone and labels correct?

@github-actions github-actions Bot added this to the Release 6.16 milestone May 11, 2026
@andy-bridger andy-bridger force-pushed the fix-parameter-name-conflict-bug branch from bbcde68 to 519cee5 Compare May 11, 2026 08:31
@andy-bridger andy-bridger added Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing) Framework Issues and pull requests related to components in the Framework labels May 11, 2026
@sf1919 sf1919 moved this to In Progress in ISIS core workstream v6.16.0 May 11, 2026
@andy-bridger andy-bridger marked this pull request as ready for review May 11, 2026 10:50
@sf1919 sf1919 moved this from In Progress to Waiting for Review in ISIS core workstream v6.16.0 May 12, 2026
@warunawickramasingha warunawickramasingha self-assigned this May 18, 2026
@warunawickramasingha warunawickramasingha moved this from Waiting for Review to In Review in ISIS core workstream v6.16.0 May 18, 2026
Copy link
Copy Markdown
Contributor

@warunawickramasingha warunawickramasingha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code changes look good and tested to be giving the results as described. Only a couple of doubts to clarify.

  1. I can see some differences in fitting parameter vales in the two tables from the main branch and this branch. Is that expected?
  2. Are the new methods in the ParameterMap supposed to be thread safe?

Comment on lines +1276 to +1278
const auto lastNonSpace = fittingFunction.find_last_not_of(" \t");
if (firstNonSpace != std::string::npos)
fittingFunction = fittingFunction.substr(firstNonSpace, lastNonSpace - firstNonSpace + 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To save the second scan incase of an empty string

Suggested change
const auto lastNonSpace = fittingFunction.find_last_not_of(" \t");
if (firstNonSpace != std::string::npos)
fittingFunction = fittingFunction.substr(firstNonSpace, lastNonSpace - firstNonSpace + 1);
if (firstNonSpace != std::string::npos) {
const auto lastNonSpace = fittingFunction.find_last_not_of(" \t");
fittingFunction = fittingFunction.substr(firstNonSpace, lastNonSpace - firstNonSpace + 1);
}

if (existing->type() == "fitting" && strcasecmp(existing->nameAsCString(), name.c_str()) == 0) {
try {
if (existing->value<FitParameter>().getFunction() == fittingFunction) {
std::atomic_store(&(itr->second), param);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this addFittingParameter method supposed to be thread safe?

@sf1919 sf1919 modified the milestones: Release 6.16, Release 6.17 May 21, 2026
@github-actions github-actions Bot added the Has Conflicts Used by the bot to label pull requests that have conflicts label May 21, 2026
@github-actions
Copy link
Copy Markdown
Contributor

👋 Hi, @andy-bridger,

Conflicts have been detected against the base branch. Please rebase your branch against the base branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing) Framework Issues and pull requests related to components in the Framework Has Conflicts Used by the bot to label pull requests that have conflicts

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

3 participants