Mantid
Loading...
Searching...
No Matches
FitScriptGeneratorModel.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2020 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include "DllOption.h"
10#include "MantidAPI/IFunction.h"
18
19#include <memory>
20#include <string>
21#include <tuple>
22#include <utility>
23#include <vector>
24
25namespace MantidQt {
26namespace MantidWidgets {
27
28class IFitScriptGeneratorPresenter;
29
35public:
40
41 void subscribePresenter(IFitScriptGeneratorPresenter *presenter) override;
42
43 void removeDomain(FitDomainIndex domainIndex) override;
44 void addWorkspaceDomain(std::string const &workspaceName, WorkspaceIndex workspaceIndex, double startX,
45 double endX) override;
46 [[nodiscard]] bool hasWorkspaceDomain(std::string const &workspaceName, WorkspaceIndex workspaceIndex) const override;
47
48 void renameWorkspace(std::string const &workspaceName, std::string const &newName) override;
49
50 [[nodiscard]] bool updateStartX(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
51 double startX) override;
52 [[nodiscard]] bool updateEndX(std::string const &workspaceName, WorkspaceIndex workspaceIndex, double endX) override;
53
54 void removeFunction(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
55 std::string const &function) override;
56 void addFunction(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
57 std::string const &function) override;
58 void setFunction(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
59 std::string const &function) override;
60 [[nodiscard]] Mantid::API::IFunction_sptr getFunction(std::string const &workspaceName,
61 WorkspaceIndex workspaceIndex) const override;
62
63 [[nodiscard]] std::string getEquivalentFunctionIndexForDomain(std::string const &workspaceName,
64 WorkspaceIndex workspaceIndex,
65 std::string const &functionIndex) const override;
66 [[nodiscard]] std::string getEquivalentFunctionIndexForDomain(FitDomainIndex domainIndex,
67 std::string const &functionIndex) const override;
68 [[nodiscard]] std::string getEquivalentParameterTieForDomain(std::string const &workspaceName,
69 WorkspaceIndex workspaceIndex,
70 std::string const &fullParameter,
71 std::string const &fullTie) const override;
72 [[nodiscard]] std::string getAdjustedFunctionIndex(std::string const &parameter) const override;
73 [[nodiscard]] std::string getFullParameter(FitDomainIndex domainIndex, std::string const &parameter) const override;
74 [[nodiscard]] std::string getFullTie(FitDomainIndex domainIndex, std::string const &tie) const override;
75
76 void updateParameterValue(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
77 std::string const &fullParameter, double newValue) override;
78 void updateAttributeValue(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
79 std::string const &fullAttribute,
80 Mantid::API::IFunction::Attribute const &newValue) override;
81
82 void updateParameterTie(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
83 std::string const &fullParameter, std::string const &tie) override;
84 void removeParameterConstraint(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
85 std::string const &fullParameter) override;
86 void updateParameterConstraint(std::string const &workspaceName, WorkspaceIndex workspaceIndex,
87 std::string const &functionIndex, std::string const &constraint) override;
88
89 [[nodiscard]] inline std::vector<GlobalTie> getGlobalTies() const noexcept override { return m_globalTies; }
90
91 void setGlobalParameters(std::vector<std::string> const &parameters) override;
92
93 [[nodiscard]] inline std::vector<GlobalParameter> getGlobalParameters() const noexcept override {
94 return m_globalParameters;
95 }
96
97 void setOutputBaseName(std::string const &outputBaseName) override;
98
99 void setFittingMode(FittingMode fittingMode) override;
100 [[nodiscard]] inline FittingMode getFittingMode() const noexcept override { return m_fittingMode; }
101
102 [[nodiscard]] bool isSimultaneousMode() const override;
103
104 [[nodiscard]] bool hasParameter(FitDomainIndex domainIndex, std::string const &fullParameter) const override;
105
106 void setParameterValue(FitDomainIndex domainIndex, std::string const &fullParameter, double value) override;
107 void setParameterFixed(FitDomainIndex domainIndex, std::string const &fullParameter, bool fix) override;
108 void setParameterTie(FitDomainIndex domainIndex, std::string const &fullParameter, std::string const &tie) override;
109 void setParameterConstraint(FitDomainIndex domainIndex, std::string const &fullParameter,
110 std::string const &constraint) override;
111
112 [[nodiscard]] std::string getDomainName(FitDomainIndex domainIndex) const override;
113 [[nodiscard]] double getParameterValue(FitDomainIndex domainIndex, std::string const &fullParameter) const override;
114 [[nodiscard]] bool isParameterFixed(FitDomainIndex domainIndex, std::string const &fullParameter) const override;
115 [[nodiscard]] std::string getParameterTie(FitDomainIndex domainIndex,
116 std::string const &fullParameter) const override;
117 [[nodiscard]] std::string getParameterConstraint(FitDomainIndex domainIndex,
118 std::string const &fullParameter) const override;
119
120 [[nodiscard]] inline std::size_t numberOfDomains() const noexcept override { return m_fitDomains.size(); }
121
122 std::tuple<bool, std::string> isValid() const override;
123
124 std::string generatePythonFitScript(
125 std::tuple<std::string, std::string, std::string, std::string, std::string, bool> const &fitOptions,
126 std::string const &filepath = "") override;
127
128private:
129 [[nodiscard]] FitDomainIndex findDomainIndex(std::string const &workspaceName, WorkspaceIndex workspaceIndex) const;
130 [[nodiscard]] std::vector<std::unique_ptr<FitDomain>>::const_iterator
131 findWorkspaceDomain(std::string const &workspaceName, WorkspaceIndex workspaceIndex) const;
132
133 [[nodiscard]] std::string getEquivalentParameterTieForDomain(FitDomainIndex domainIndex,
134 std::string const &fullParameter,
135 std::string const &fullTie) const;
136
137 void updateParameterTie(FitDomainIndex domainIndex, std::string const &fullParameter, std::string const &fullTie);
138 void updateLocalParameterTie(FitDomainIndex domainIndex, std::string const &fullParameter,
139 std::string const &fullTie);
140 void updateGlobalParameterTie(FitDomainIndex domainIndex, std::string const &fullParameter,
141 std::string const &fullTie);
142
143 void updateParameterValuesWithLocalTieTo(FitDomainIndex domainIndex, std::string const &parameter, double newValue);
144 void updateParameterValuesWithGlobalTieTo(std::string const &fullParameter, double newValue);
145 void updateParameterValueInGlobalTie(GlobalTie const &globalTie, double newValue);
146
147 [[nodiscard]] bool validParameter(std::string const &fullParameter) const;
148 [[nodiscard]] bool validParameter(FitDomainIndex domainIndex, std::string const &fullParameter) const;
149 [[nodiscard]] bool validTie(std::string const &fullTie) const;
150 [[nodiscard]] bool validGlobalTie(std::string const &fullParameter, std::string const &fullTie) const;
151
152 [[nodiscard]] bool isParameterValueWithinConstraints(FitDomainIndex domainIndex, std::string const &fullParameter,
153 double value) const;
154
155 void clearGlobalTie(std::string const &fullParameter);
156 [[nodiscard]] bool hasGlobalTie(std::string const &fullParameter) const;
157 [[nodiscard]] std::vector<GlobalTie>::const_iterator findGlobalTie(std::string const &fullParameter) const;
158 void checkGlobalTies();
159
160 void checkParameterIsInAllDomains(std::string const &globalParameter) const;
161 void checkGlobalParameterhasNoTies(std::string const &globalParameter) const;
162 void checkParameterIsNotGlobal(std::string const &fullParameter) const;
163
164 void tryToAdjustParameterInGlobalTieIfInvalidated(GlobalTie &globalTie);
165 void tryToAdjustTieInGlobalTieIfInvalidated(GlobalTie &globalTie);
166
167 template <typename Getter>
168 auto getParameterProperty(Getter &&func, FitDomainIndex domainIndex, std::string const &fullParameter) const;
169
170 [[nodiscard]] bool checkFunctionExistsInAllDomains() const;
171 [[nodiscard]] bool checkFunctionIsSameForAllDomains() const;
172 [[nodiscard]] std::string generatePermissibleWarnings() const;
173
174 [[nodiscard]] std::vector<std::string> getInputWorkspaces() const;
175 [[nodiscard]] std::vector<std::size_t> getWorkspaceIndices() const;
176 [[nodiscard]] std::vector<double> getStartXs() const;
177 [[nodiscard]] std::vector<double> getEndXs() const;
178
179 template <typename T, typename Function> std::vector<T> transformDomains(Function const &func) const;
180
181 [[nodiscard]] std::string getFittingType() const;
182 [[nodiscard]] Mantid::API::IFunction_sptr getFunction() const;
183 [[nodiscard]] Mantid::API::IFunction_sptr getMultiDomainFunction() const;
184
185 void addGlobalParameterTies(Mantid::API::MultiDomainFunction_sptr &function) const;
186 std::string constructGlobalParameterTie(GlobalParameter const &globalParameter) const;
187
188 void addGlobalTies(Mantid::API::MultiDomainFunction_sptr &function) const;
189
191 std::string m_outputBaseName;
192 std::vector<std::unique_ptr<FitDomain>> m_fitDomains;
193 // A vector of global parameters. E.g. f0.A0
194 std::vector<GlobalParameter> m_globalParameters;
195 // A vector of global ties. E.g. f0.f0.A0=f1.f0.A0
196 std::vector<GlobalTie> m_globalTies;
198};
199
200template <typename Getter>
202 std::string const &fullParameter) const {
203 auto const parameter = getAdjustedFunctionIndex(fullParameter);
204 if (domainIndex.value < numberOfDomains())
205 return std::invoke(std::forward<Getter>(func), m_fitDomains[domainIndex.value], parameter);
206
207 throw std::runtime_error("The domain index provided does not exist.");
208}
209
210} // namespace MantidWidgets
211} // namespace MantidQt
#define EXPORT_OPT_MANTIDQT_COMMON
Definition: DllOption.h:15
double value
The value of the point.
Definition: FitMW.cpp:51
This class stores the domain and fit data to be fitted to.
FittingMode getFittingMode() const noexcept override
std::vector< GlobalTie > getGlobalTies() const noexcept override
std::vector< std::unique_ptr< FitDomain > > m_fitDomains
std::string getAdjustedFunctionIndex(std::string const &parameter) const override
FitScriptGeneratorModel & operator=(FitScriptGeneratorModel const &model)=delete
FitScriptGeneratorModel(FitScriptGeneratorModel const &model)=delete
std::vector< GlobalParameter > getGlobalParameters() const noexcept override
std::size_t numberOfDomains() const noexcept override
auto getParameterProperty(Getter &&func, FitDomainIndex domainIndex, std::string const &fullParameter) const
Attribute is a non-fitting parameter.
Definition: IFunction.h:282
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
std::shared_ptr< MultiDomainFunction > MultiDomainFunction_sptr
Shared pointer to Mantid::API::MultiDomainFunction.
Definition: IFunction_fwd.h:35
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition: IFunction.h:732
void renameWorkspace(const IAlgorithm_sptr &renamer, const Workspace_sptr &workspace, const std::string &newName)
This struct stores the name of a global parameter which is shared across ALL domains in a multi datas...
This struct stores the data associated with a global tie.
A struct to impliment strongly typed integers, without implicit conversion.
Definition: IndexTypes.h:24
IntImplementationType value
Definition: IndexTypes.h:26