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

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.

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 () override
 
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
 

Constructor & Destructor Documentation

◆ FitSessionController()

FitSessionController::FitSessionController ( QObject *  parent = nullptr)

Definition at line 33 of file FitSessionController.cpp.

34  : QObject(parent)
35  , m_jobItem(nullptr)
38  , m_fitlog(new FitLog(this))
40 {
41  connect(m_observer.get(), &GUIFitObserver::updateReady, this,
43 
50 }
the collected messages of a fitting session
Definition: FitLog.h:27
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 ( )
overridedefault

Member Function Documentation

◆ fitLog()

FitLog * FitSessionController::fitLog ( )

Definition at line 92 of file FitSessionController.cpp.

93 {
94  return m_fitlog.get();
95 }

References m_fitlog.

Referenced by FitActivityPanel::setItem().

◆ 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 145 of file FitSessionController.cpp.

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 }
void fittingError(const QString &message)
const QDateTime & fitEnd() const
void setEndTime(const QDateTime &end_time)
Definition: JobItem.cpp:175

References Error, FitWorkerLauncher::fitEnd(), fittingError(), m_fitlog, m_jobItem, m_runFitManager, and JobItem::setEndTime().

Referenced by FitSessionController().

Here is the call graph for this function:

◆ onFittingFinished

void FitSessionController::onFittingFinished ( )
privateslot

Definition at line 132 of file FitSessionController.cpp.

133 {
137  m_jobItem->setProgress(100);
138 
139  if (m_jobItem->isCompleted())
140  m_fitlog->append("Done", FitLogLevel::Success);
141 
142  emit fittingFinished();
143 }
@ Completed
the job was successfully completed
@ Failed
the job aborted because it hit an error
void setStatus(JobStatus status)
Definition: JobItem.cpp:111
void setProgress(int progress)
Definition: JobItem.cpp:206
bool isCompleted() const
Definition: JobItem.cpp:134
JobStatus getStatus() const
Definition: JobItem.cpp:106

References Completed, Failed, FitWorkerLauncher::fitEnd(), fittingFinished(), JobItem::getStatus(), JobItem::isCompleted(), m_fitlog, m_jobItem, m_runFitManager, JobItem::setEndTime(), JobItem::setProgress(), JobItem::setStatus(), and Success.

Referenced by FitSessionController().

Here is the call graph for this function:

◆ onFittingStarted

void FitSessionController::onFittingStarted ( )
privateslot

Definition at line 120 of file FitSessionController.cpp.

121 {
122  m_fitlog->clearLog();
123 
127  m_jobItem->setEndTime(QDateTime());
128 
129  emit fittingStarted();
130 }
@ Fitting
the job is busy fitting
const QDateTime & fitStart() const
void setBeginTime(const QDateTime &begin_time)
Definition: JobItem.cpp:164

References FitWorkerLauncher::fitStart(), Fitting, fittingStarted(), m_fitlog, m_jobItem, m_runFitManager, JobItem::setBeginTime(), 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 102 of file FitSessionController.cpp.

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 }
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
void updateIterationCount(const FitProgressInfo &info)
void updateFitParameterValues(const FitProgressInfo &info)
void updateLog(const FitProgressInfo &info)
DataItem * dataItem()
Definition: JobItem.cpp:101

References JobItem::dataItem(), 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(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 }
std::shared_ptr< FitObjectiveBuilder > m_objectiveBuilder
int updateInterval() const
void runFitting(std::shared_ptr< FitObjectiveBuilder > suite)
FitSuiteItem * fitSuiteItem()
Definition: JobItem.cpp:257

References Error, Failed, JobItem::fitSuiteItem(), fittingError(), m_fitlog, m_jobItem, m_objectiveBuilder, m_observer, m_runFitManager, FitWorkerLauncher::runFitting(), JobItem::setStatus(), and FitSuiteItem::updateInterval().

Referenced by FitSessionWidget::setSessionController().

Here is the call graph for this function:

◆ onStopFittingRequest

void FitSessionController::onStopFittingRequest ( )
slot

Definition at line 97 of file FitSessionController.cpp.

98 {
100 }

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 54 of file FitSessionController.cpp.

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 }
static bool isUpdateIntervalPropertyName(const QString &name)
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

References Error, JobItem::fitSuiteItem(), FitSuiteItem::isUpdateIntervalPropertyName(), m_jobItem, m_observer, SessionItem::mapper(), GUI::RealSpace::Particles::name(), ModelMapper::setOnPropertyChange(), and FitSuiteItem::updateInterval().

Here is the call graph for this function:

◆ updateFitParameterValues

void FitSessionController::updateFitParameterValues ( const FitProgressInfo info)
privateslot

Definition at line 165 of file FitSessionController.cpp.

166 {
167  QVector<double> values = QVector<double>::fromStdVector(info.parValues());
170 }
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.
std::vector< double > parValues() const
FitParameterContainerItem * fitParameterContainerItem()
Definition: JobItem.cpp:282
ParameterContainerItem * parameterContainerItem()
Definition: JobItem.cpp:271

References JobItem::fitParameterContainerItem(), 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 156 of file FitSessionController.cpp.

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 }
int iterationCount() const
double chi2() const
void setChi2(double chi2)
void setIterationCount(int count)

References FitProgressInfo::chi2(), JobItem::fitSuiteItem(), FitProgressInfo::iterationCount(), m_jobItem, FitSuiteItem::setChi2(), and FitSuiteItem::setIterationCount().

Referenced by onObserverUpdate().

Here is the call graph for this function:

◆ updateLog

void FitSessionController::updateLog ( const FitProgressInfo info)
privateslot

Definition at line 172 of file FitSessionController.cpp.

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 }
QVector< FitParameterItem * > fitParameterItems() const
FitParameterItems is a collection of items to define fit parameters in GUI.

References FitProgressInfo::chi2(), Default, JobItem::fitParameterContainerItem(), FitParameterContainerItem::fitParameterItems(), FitProgressInfo::iterationCount(), m_fitlog, m_jobItem, and FitProgressInfo::parValues().

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: