Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sortTies is a bottleneck when adding ties to a multi-domain function #38962

Open
RichardWaiteSTFC opened this issue Feb 24, 2025 · 0 comments
Open
Assignees
Labels
Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing) ISIS Team: Core Issue and pull requests managed by the Core subteam at ISIS ISIS Team: Diffraction Issue and pull requests managed by the Diffraction subteam at ISIS
Milestone

Comments

@RichardWaiteSTFC
Copy link
Contributor

RichardWaiteSTFC commented Feb 24, 2025

Describe the bug

In PR #38515 a MultiDomainFunction is used to share peak parameters across adjacent pixels.

However the execution time for the algorithm IntegratePeaks1DProfile has increased by an order of magnitude (or more!) - the bottleneck is due to sortTies being run for each tie added. The actual fit is very quick.

void IFunction::sortTies(const bool checkOnly) {

To Reproduce
(1) Run this script in debug with a breakpoint at the line above

from mantid.simpleapi import *
from mantid.api import MultiDomainFunction, FunctionFactory
from mantid.fitfunctions import FunctionWrapper
                          

comp_func = CompositeFunction(Gaussian(Height=1, Sigma=0.3, PeakCentre=3), FunctionWrapper("FlatBackground"), NumDeriv=True)
ws = CreateSampleWorkspace(OutputWorkspace='ws', Function='User Defined', UserDefinedFunction=str(comp_func), XUnit='DeltaE', 
                           XMax=7, BinWidth=0.1, NumBanks=1, BankPixelWidth=2)

func = MultiDomainFunction()        
md_fit_kwargs = {}
for ispec in range(ws.getNumberHistograms()):
    func.add(comp_func.function)
    func.setDomainIndex(ispec,ispec)
    key_suffix = f"_{ispec}" if ispec > 0 else ""
    md_fit_kwargs["InputWorkspace" + key_suffix] = ws.name()
    md_fit_kwargs["StartX" + key_suffix] = 0.1
    md_fit_kwargs["EndX" + key_suffix] = 6.8
    md_fit_kwargs["WorkspaceIndex" + key_suffix] = ispec

add_ties = True
if add_ties:
    ties = []
    for idom in range(1, ws.getNumberHistograms()):
        for ifunc in range(func[0].nFunctions()):
            for ipar in range(func[0][ifunc].nParams()):
                par = func[0][ifunc].getParamName(ipar)
                ties.append(f"f{idom}.f{ifunc}.{par}=f0.f{ifunc}.{par}")
    func_str = f"{str(func)};ties=({','.join(ties)})"
else:
    func_str = str(func)
    
out = Fit(Function=func_str, Output='ws', StoreInADS=True, CreateOutput=True, OutputCompositeMembers=True,
          **md_fit_kwargs)

You will see sortTies gets called for every tie

(2) Increase the number of spectra

ws = CreateSampleWorkspace(OutputWorkspace='ws', Function='User Defined', UserDefinedFunction=str(comp_func), XUnit='DeltaE', 
                           XMax=7, BinWidth=0.1, NumBanks=25, BankPixelWidth=2)

Observe that it takes a lot longer - if you set this too high (e.g. NumBanks=100 Mantid will hang)

Expected behavior
Would call sortTies once after all ties added?

Screenshots

Platform/Version (please complete the following information):

  • OS: [e.g. Windows, RHEL 7, Ubuntu, macOS]
  • OS Version:
  • Mantid Version [e.g. 6.0.0]

Additional context

@RichardWaiteSTFC RichardWaiteSTFC added the Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing) label Feb 24, 2025
@RichardWaiteSTFC RichardWaiteSTFC added this to the Release 6.13 milestone Feb 24, 2025
@RichardWaiteSTFC RichardWaiteSTFC added ISIS Team: Diffraction Issue and pull requests managed by the Diffraction subteam at ISIS ISIS Team: Core Issue and pull requests managed by the Core subteam at ISIS labels Feb 24, 2025
@RichardWaiteSTFC RichardWaiteSTFC changed the title sortTies is a bottleneck when adding ties to a multi-=domain function sortTies is a bottleneck when adding ties to a multi-domain function Feb 25, 2025
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) ISIS Team: Core Issue and pull requests managed by the Core subteam at ISIS ISIS Team: Diffraction Issue and pull requests managed by the Diffraction subteam at ISIS
Projects
Status: No status
Status: Todo
Development

No branches or pull requests

2 participants