BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
FitSessionController.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Fit/FitSessionController.cpp
6 //! @brief Implements class FitSessionController
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
20 #include "GUI/Model/Job/JobItem.h"
21 #include "GUI/Util/Error.h"
22 #include "GUI/Util/Path.h"
23 #include "GUI/View/Fit/FitLog.h"
27 
28 namespace {
29 
30 const bool use_fit_objective = true;
31 }
32 
34  : QObject(parent)
35  , m_jobItem(nullptr)
36  , m_runFitManager(new FitWorkerLauncher(this))
37  , m_observer(new GUIFitObserver)
38  , m_fitlog(new FitLog(this))
39  , m_block_progress_update(false)
40 {
41  connect(m_observer.get(), &GUIFitObserver::updateReady, this,
43 
50 }
51 
53 
55 {
56  if (m_jobItem && m_jobItem != item)
57  throw Error("FitSuiteManager::setItem() -> Item was already set.");
58 
59  m_jobItem = item;
60  ASSERT(m_jobItem);
61 
62  // no need to unsubscribe from jobItem on jobItem destroy. FitSessionManager deletes
63  // controller right after the jobItem.
64 
65  // Propagates update interval from FitSuiteItem to fit observer.
67  [this](const QString& name) {
69  m_observer->setInterval(m_jobItem->fitSuiteItem()->updateInterval());
70  },
71  this);
72 }
73 
75 {
76  if (!m_jobItem)
77  return;
78 
79  try {
80  m_objectiveBuilder = std::make_unique<FitObjectiveBuilder>(m_jobItem);
81  m_observer->setInterval(m_jobItem->fitSuiteItem()->updateInterval());
82  m_objectiveBuilder->attachObserver(m_observer);
83  m_observer->finishedPlotting();
85  } catch (std::exception& e) {
87  m_fitlog->append(e.what(), FitLogLevel::Error);
88  emit fittingError(QString::fromStdString(e.what()));
89  }
90 }
91 
93 {
94  return m_fitlog.get();
95 }
96 
98 {
100 }
101 
103 {
104  auto progressInfo = m_observer->progressInfo();
105  m_jobItem->dataItem()->setRawDataVector(progressInfo.simValues());
106 
107  updateIterationCount(progressInfo);
108 
109  if (!use_fit_objective)
110  updateFitParameterValues(progressInfo);
111 
112  updateLog(progressInfo);
113 
114  if (!progressInfo.logInfo().empty())
115  m_fitlog->append(progressInfo.logInfo(), FitLogLevel::Default);
116 
117  m_observer->finishedPlotting();
118 }
119 
121 {
122  m_fitlog->clearLog();
123 
127  m_jobItem->setEndTime(QDateTime());
128 
129  emit fittingStarted();
130 }
131 
133 {
137  m_jobItem->setProgress(100);
138 
139  if (m_jobItem->isCompleted())
140  m_fitlog->append("Done", FitLogLevel::Success);
141 
142  emit fittingFinished();
143 }
144 
145 void FitSessionController::onFittingError(const QString& text)
146 {
147  QString message;
148  message.append("Current settings cause fitting failure.\n\n");
149  message.append(text);
150  m_fitlog->append(message.toStdString(), FitLogLevel::Error);
152 
153  emit fittingError(message);
154 }
155 
157 {
158  FitSuiteItem* fitSuiteItem = m_jobItem->fitSuiteItem();
159  // FIXME FitFlowWidget updates chi2 and n_iteration on P_ITERATION_COUNT change
160  // The order of two lines below is important
161  fitSuiteItem->setChi2(info.chi2());
162  fitSuiteItem->setIterationCount(info.iterationCount());
163 }
164 
166 {
167  QVector<double> values = QVector<double>::fromStdVector(info.parValues());
170 }
171 
173 {
174  QString message = QString("NCalls:%1 chi2:%2 \n").arg(info.iterationCount()).arg(info.chi2());
176  int index(0);
177  QVector<double> values = QVector<double>::fromStdVector(info.parValues());
178  for (FitParameterItem* item : fitParContainer->fitParameterItems()) {
179  if (item->linkItems().empty())
180  continue;
181  QString parinfo = QString(" %1 %2\n").arg(item->displayName()).arg(values[index++]);
182  message.append(parinfo);
183  }
184  m_fitlog->append(message.toStdString(), FitLogLevel::Default);
185 }
Defines error class.
Defines class FitLog.
Defines class FitObjectiveBuilder.
Defines class FitParameterContainerItem.
Defines class FitParameterItem.
Defines class FitSessionController.
Defines class FitSuiteItem.
Implements class FitWorkerLauncher.
Implements class GUIFitObserver.
Defines class IntensityDataItem.
Defines class JobItem.
@ Completed
the job was successfully completed
@ Failed
the job aborted because it hit an error
@ Fitting
the job is busy fitting
Defines class Helpers functions.
void setRawDataVector(const std::vector< double > &data)
Sets the raw data vector from external source. Checks only the equality of data size; no dimension ch...
Definition: DataItem.cpp:29
the collected messages of a fitting session
Definition: FitLog.h:27
The FitParameterContainerItem class is a collection of all defined fit parameters in JobItem.
void setValuesInParameterContainer(const QVector< double > &values, class ParameterContainerItem *parameterContainer)
Propagate values to the corresponding parameter tree items of parameterContainer.
QVector< FitParameterItem * > fitParameterItems() const
FitParameterItems is a collection of items to define fit parameters in GUI.
The FitProgressInfo class contains all essential information about fit progress. It is send from GUIF...
std::vector< double > parValues() const
int iterationCount() const
double chi2() const
void onFittingError(const QString &text)
std::shared_ptr< GUIFitObserver > m_observer
std::unique_ptr< FitLog > m_fitlog
~FitSessionController() override
void fittingError(const QString &message)
void updateIterationCount(const FitProgressInfo &info)
FitWorkerLauncher * m_runFitManager
void updateFitParameterValues(const FitProgressInfo &info)
void updateLog(const FitProgressInfo &info)
FitSessionController(QObject *parent=nullptr)
std::shared_ptr< FitObjectiveBuilder > m_objectiveBuilder
void setItem(JobItem *item)
void setChi2(double chi2)
int updateInterval() const
void setIterationCount(int count)
static bool isUpdateIntervalPropertyName(const QString &name)
const QDateTime & fitEnd() const
void runFitting(std::shared_ptr< FitObjectiveBuilder > suite)
void fittingError(const QString &message)
const QDateTime & fitStart() const
Serves as observer for FitObjective and saves fit iteration data for later display in GUI widgets.
void updateReady()
void setEndTime(const QDateTime &end_time)
Definition: JobItem.cpp:175
void setStatus(JobStatus status)
Definition: JobItem.cpp:111
void setProgress(int progress)
Definition: JobItem.cpp:206
FitParameterContainerItem * fitParameterContainerItem()
Definition: JobItem.cpp:282
FitSuiteItem * fitSuiteItem()
Definition: JobItem.cpp:257
bool isCompleted() const
Definition: JobItem.cpp:134
DataItem * dataItem()
Definition: JobItem.cpp:101
ParameterContainerItem * parameterContainerItem()
Definition: JobItem.cpp:271
JobStatus getStatus() const
Definition: JobItem.cpp:106
void setBeginTime(const QDateTime &begin_time)
Definition: JobItem.cpp:164
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:39
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
QString const & name(EShape k)
Definition: particles.cpp:20