BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FitSessionController Class Reference

Controls all activity related to the single fitting task for JobItem. More...

Inheritance diagram for FitSessionController:
[legend]
Collaboration diagram for FitSessionController:
[legend]

Public Slots

void onStartFittingRequest ()
 
void onStopFittingRequest ()
 

Signals

void fittingError (const QString &message)
 
void fittingFinished ()
 
void fittingStarted ()
 

Public Member Functions

 FitSessionController (QObject *parent=nullptr)
 
 ~FitSessionController ()
 
FitLogfitLog ()
 
void setItem (JobItem *item)
 

Private Slots

void onFittingError (const QString &text)
 
void onFittingFinished ()
 
void onFittingStarted ()
 
void onObserverUpdate ()
 
void updateFitParameterValues (const FitProgressInfo &info)
 
void updateIterationCount (const FitProgressInfo &info)
 
void updateLog (const FitProgressInfo &info)
 

Private Attributes

bool m_block_progress_update
 
std::unique_ptr< FitLogm_fitlog
 
JobItemm_jobItem
 
std::shared_ptr< FitObjectiveBuilderm_objectiveBuilder
 
std::shared_ptr< GUIFitObserverm_observer
 
FitWorkerLauncherm_runFitManager
 

Detailed Description

Controls all activity related to the single fitting task for JobItem.

Provides interaction between FitSessionWidget and fit observers.

Definition at line 31 of file FitSessionController.h.

Constructor & Destructor Documentation

◆ FitSessionController()

FitSessionController::FitSessionController ( QObject *  parent = nullptr)

Definition at line 30 of file FitSessionController.cpp.

31  : QObject(parent)
32  , m_jobItem(nullptr)
35  , m_fitlog(new FitLog)
37 {
38  connect(m_observer.get(), &GUIFitObserver::updateReady, this,
40 
47 }
Definition: FitLog.h:24
void onFittingError(const QString &text)
std::shared_ptr< GUIFitObserver > m_observer
std::unique_ptr< FitLog > m_fitlog
FitWorkerLauncher * m_runFitManager
void fittingError(const QString &message)
Serves as observer for FitObjective and saves fit iteration data for later display in GUI widgets.
void updateReady()

References FitWorkerLauncher::fittingError(), FitWorkerLauncher::fittingFinished(), FitWorkerLauncher::fittingStarted(), m_observer, m_runFitManager, onFittingError(), onFittingFinished(), onFittingStarted(), onObserverUpdate(), and GUIFitObserver::updateReady().

Here is the call graph for this function:

◆ ~FitSessionController()

FitSessionController::~FitSessionController ( )
default

Member Function Documentation

◆ fitLog()

FitLog * FitSessionController::fitLog ( )

Definition at line 94 of file FitSessionController.cpp.

95 {
96  return m_fitlog.get();
97 }

References m_fitlog.

Referenced by FitSessionManager::disableLogging(), and FitSessionManager::sessionController().

◆ fittingError

void FitSessionController::fittingError ( const QString &  message)
signal

◆ fittingFinished

void FitSessionController::fittingFinished ( )
signal

Referenced by onFittingFinished().

◆ fittingStarted

void FitSessionController::fittingStarted ( )
signal

Referenced by onFittingStarted().

◆ onFittingError

void FitSessionController::onFittingError ( const QString &  text)
privateslot

Definition at line 149 of file FitSessionController.cpp.

150 {
151  QString message;
152  message.append("Current settings cause fitting failure.\n\n");
153  message.append(text);
154  m_fitlog->append(message.toStdString(), FitLogFlags::ERROR);
155 
156  emit fittingError(message);
157 }
void fittingError(const QString &message)

References FitLogFlags::ERROR, fittingError(), and m_fitlog.

Referenced by FitSessionController().

◆ onFittingFinished

void FitSessionController::onFittingFinished ( )
privateslot

Definition at line 135 of file FitSessionController.cpp.

136 {
137  if (m_jobItem->getStatus() != "Failed")
138  m_jobItem->setStatus("Completed");
140  m_jobItem->setProgress(100);
142 
143  if (m_jobItem->isCompleted())
144  m_fitlog->append("Done", FitLogFlags::SUCCESS);
145 
146  emit fittingFinished();
147 }
int getDuration()
Returns duration of fit in msec.
void setProgress(int progress)
Definition: JobItem.cpp:204
bool isCompleted() const
Definition: JobItem.cpp:150
QString getStatus() const
Definition: JobItem.cpp:123
void setDuration(int duration)
Definition: JobItem.cpp:181
void setStatus(const QString &status)
Definition: JobItem.cpp:128
void setEndTime(const QString &end_time)
Definition: JobItem.cpp:175
QString currentDateTime()
Definition: GUIHelpers.cpp:210

References GUIHelpers::currentDateTime(), fittingFinished(), FitWorkerLauncher::getDuration(), JobItem::getStatus(), JobItem::isCompleted(), m_fitlog, m_jobItem, m_runFitManager, JobItem::setDuration(), JobItem::setEndTime(), JobItem::setProgress(), JobItem::setStatus(), and FitLogFlags::SUCCESS.

Referenced by FitSessionController().

Here is the call graph for this function:

◆ onFittingStarted

void FitSessionController::onFittingStarted ( )
privateslot

Definition at line 122 of file FitSessionController.cpp.

123 {
124  m_fitlog->clearLog();
125 
126  m_jobItem->setStatus("Fitting");
129  m_jobItem->setEndTime(QString());
131 
132  emit fittingStarted();
133 }
void setBeginTime(const QString &begin_time)
Definition: JobItem.cpp:170

References GUIHelpers::currentDateTime(), fittingStarted(), m_fitlog, m_jobItem, JobItem::setBeginTime(), JobItem::setDuration(), JobItem::setEndTime(), JobItem::setProgress(), and JobItem::setStatus().

Referenced by FitSessionController().

Here is the call graph for this function:

◆ onObserverUpdate

void FitSessionController::onObserverUpdate ( )
privateslot

Definition at line 104 of file FitSessionController.cpp.

105 {
106  auto progressInfo = m_observer->progressInfo();
107  m_jobItem->dataItem()->setRawDataVector(progressInfo.simValues());
108 
109  updateIterationCount(progressInfo);
110 
111  if (!use_fit_objective)
112  updateFitParameterValues(progressInfo);
113 
114  updateLog(progressInfo);
115 
116  if (!progressInfo.logInfo().empty())
117  m_fitlog->append(progressInfo.logInfo(), FitLogFlags::DEFAULT);
118 
119  m_observer->finishedPlotting();
120 }
void setRawDataVector(std::vector< double > data)
Sets the raw data vector from external source Checks only the equality of data size,...
Definition: DataItem.cpp:30
void updateIterationCount(const FitProgressInfo &info)
void updateFitParameterValues(const FitProgressInfo &info)
void updateLog(const FitProgressInfo &info)
DataItem * dataItem()
Definition: JobItem.cpp:118

References JobItem::dataItem(), FitLogFlags::DEFAULT, m_fitlog, m_jobItem, m_observer, DataItem::setRawDataVector(), updateFitParameterValues(), updateIterationCount(), and updateLog().

Referenced by FitSessionController().

Here is the call graph for this function:

◆ onStartFittingRequest

void FitSessionController::onStartFittingRequest ( )
slot

Definition at line 74 of file FitSessionController.cpp.

75 {
76  if (!m_jobItem)
77  return;
78 
79  try {
80  m_objectiveBuilder = std::make_unique<FitObjectiveBuilder>(m_jobItem);
81  m_observer->setInterval(
83  m_objectiveBuilder->attachObserver(m_observer);
84  m_observer->finishedPlotting();
86 
87  } catch (std::exception& e) {
88  m_jobItem->setStatus("Failed");
89  m_fitlog->append(e.what(), FitLogFlags::ERROR);
90  emit fittingError(QString::fromStdString(e.what()));
91  }
92 }
std::shared_ptr< FitObjectiveBuilder > m_objectiveBuilder
static const QString P_UPDATE_INTERVAL
Definition: FitSuiteItem.h:26
void runFitting(std::shared_ptr< FitObjectiveBuilder > suite)
FitSuiteItem * fitSuiteItem()
Definition: JobItem.cpp:235
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.

References FitLogFlags::ERROR, JobItem::fitSuiteItem(), fittingError(), SessionItem::getItemValue(), m_fitlog, m_jobItem, m_objectiveBuilder, m_observer, m_runFitManager, FitSuiteItem::P_UPDATE_INTERVAL, FitWorkerLauncher::runFitting(), and JobItem::setStatus().

Referenced by FitSessionWidget::setSessionController().

Here is the call graph for this function:

◆ onStopFittingRequest

void FitSessionController::onStopFittingRequest ( )
slot

Definition at line 99 of file FitSessionController.cpp.

100 {
102 }

References FitWorkerLauncher::interruptFitting(), and m_runFitManager.

Referenced by FitSessionWidget::setSessionController().

Here is the call graph for this function:

◆ setItem()

void FitSessionController::setItem ( JobItem item)

Definition at line 51 of file FitSessionController.cpp.

52 {
53  if (m_jobItem && m_jobItem != item)
54  throw GUIHelpers::Error("FitSuiteManager::setItem() -> Item was already set.");
55 
56  m_jobItem = item;
58 
59  // no need to unsubscribe from jobItem on jobItem destroy. FitSessionManager deletes
60  // controller right after the jobItem.
61 
62  // Propagates update interval from FitSuiteItem to fit observer.
64  [this](const QString& name) {
66  m_observer->setInterval(m_jobItem->fitSuiteItem()
68  .toInt());
69  }
70  },
71  this);
72 }
#define ASSERT(condition)
Definition: Assert.h:31
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=0)
Definition: ModelMapper.cpp:35
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
QString const & name(EShape k)
Definition: particles.cpp:21

References ASSERT, JobItem::fitSuiteItem(), SessionItem::getItemValue(), m_jobItem, m_observer, SessionItem::mapper(), RealSpace::Particles::name(), FitSuiteItem::P_UPDATE_INTERVAL, and ModelMapper::setOnPropertyChange().

Here is the call graph for this function:

◆ updateFitParameterValues

void FitSessionController::updateFitParameterValues ( const FitProgressInfo info)
privateslot

Definition at line 168 of file FitSessionController.cpp.

169 {
170  QVector<double> values = GUIHelpers::fromStdVector(info.parValues());
173 }
void setValuesInParameterContainer(const QVector< double > &values, class ParameterContainerItem *parameterContainer)
Propagate values to the corresponding parameter tree items of parameterContainer.
std::vector< double > parValues() const
FitParameterContainerItem * fitParameterContainerItem()
Definition: JobItem.cpp:251
ParameterContainerItem * parameterContainerItem()
Definition: JobItem.cpp:240
QVector< double > fromStdVector(const std::vector< double > &data)
Definition: GUIHelpers.cpp:225

References JobItem::fitParameterContainerItem(), GUIHelpers::fromStdVector(), m_jobItem, JobItem::parameterContainerItem(), FitProgressInfo::parValues(), and FitParameterContainerItem::setValuesInParameterContainer().

Referenced by onObserverUpdate().

Here is the call graph for this function:

◆ updateIterationCount

void FitSessionController::updateIterationCount ( const FitProgressInfo info)
privateslot

Definition at line 159 of file FitSessionController.cpp.

160 {
161  FitSuiteItem* fitSuiteItem = m_jobItem->fitSuiteItem();
162  // FIXME FitFlowWidget updates chi2 and n_iteration on P_ITERATION_COUNT change
163  // The order of two lines below is important
164  fitSuiteItem->setItemValue(FitSuiteItem::P_CHI2, info.chi2());
166 }
int iterationCount() const
double chi2() const
static const QString P_CHI2
Definition: FitSuiteItem.h:28
static const QString P_ITERATION_COUNT
Definition: FitSuiteItem.h:27
void setItemValue(const QString &tag, const QVariant &variant)
Directly set value of item under given tag.

References FitProgressInfo::chi2(), JobItem::fitSuiteItem(), FitProgressInfo::iterationCount(), m_jobItem, FitSuiteItem::P_CHI2, FitSuiteItem::P_ITERATION_COUNT, and SessionItem::setItemValue().

Referenced by onObserverUpdate().

Here is the call graph for this function:

◆ updateLog

void FitSessionController::updateLog ( const FitProgressInfo info)
privateslot

Definition at line 175 of file FitSessionController.cpp.

176 {
177  QString message = QString("NCalls:%1 chi2:%2 \n").arg(info.iterationCount()).arg(info.chi2());
179  int index(0);
180  QVector<double> values = GUIHelpers::fromStdVector(info.parValues());
181  for (auto item : fitParContainer->getItems(FitParameterContainerItem::T_FIT_PARAMETERS)) {
182  if (item->getItems(FitParameterItem::T_LINK).empty())
183  continue;
184  QString parinfo = QString(" %1 %2\n").arg(item->displayName()).arg(values[index++]);
185  message.append(parinfo);
186  }
187  m_fitlog->append(message.toStdString(), FitLogFlags::DEFAULT);
188 }
static const QString T_FIT_PARAMETERS
static const QString T_LINK
QVector< SessionItem * > getItems(const QString &tag="") const
Returns vector of all items of given tag.

References FitProgressInfo::chi2(), FitLogFlags::DEFAULT, JobItem::fitParameterContainerItem(), GUIHelpers::fromStdVector(), SessionItem::getItems(), FitProgressInfo::iterationCount(), m_fitlog, m_jobItem, FitProgressInfo::parValues(), FitParameterContainerItem::T_FIT_PARAMETERS, and FitParameterItem::T_LINK.

Referenced by onObserverUpdate().

Here is the call graph for this function:

Member Data Documentation

◆ m_block_progress_update

bool FitSessionController::m_block_progress_update
private

Definition at line 65 of file FitSessionController.h.

◆ m_fitlog

std::unique_ptr<FitLog> FitSessionController::m_fitlog
private

◆ m_jobItem

◆ m_objectiveBuilder

std::shared_ptr<FitObjectiveBuilder> FitSessionController::m_objectiveBuilder
private

Definition at line 64 of file FitSessionController.h.

Referenced by onStartFittingRequest().

◆ m_observer

std::shared_ptr<GUIFitObserver> FitSessionController::m_observer
private

◆ m_runFitManager

FitWorkerLauncher* FitSessionController::m_runFitManager
private

The documentation for this class was generated from the following files: