COPASI API  4.16.103
CQFittingWidget.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "CQFittingWidget.h"
16 
17 #include <QtGui/QLabel>
18 #include <QtGui/QToolButton>
19 
20 #include "UI/CQTaskBtnWidget.h"
21 #include "UI/CQTaskHeaderWidget.h"
22 #include "UI/CQTaskMethodWidget.h"
23 #include "UI/CQFittingItemWidget.h"
24 #include "UI/CProgressBar.h"
25 #include "UI/CQExperimentData.h"
26 
28 #include <copasi/UI/CQMessageBox.h>
29 
30 #include "report/CKeyFactory.h"
40 
42 #include <copasi/model/CModel.h>
43 
44 /*
45  * Constructs a CQFittingWidget which is a child of 'parent', with the
46  * name 'name'.'
47  */
48 CQFittingWidget::CQFittingWidget(QWidget* parent, const char* name)
49  : TaskWidget(parent, name)
50 {
51  setupUi(this);
52 
53  init();
54 }
55 
56 /*
57  * Destroys the object and frees any allocated resources
58  */
60 {
61  destroy();
62  // no need to delete child widgets, Qt does it all for us
63 }
64 
66 {
67  CFitTask * pTask =
68  dynamic_cast< CFitTask * >(mpTask);
69 
70  if (!pTask) return false;
71 
72  saveCommon();
73  saveMethod();
74 
75  CFitProblem * pProblem =
76  dynamic_cast<CFitProblem *>(mpTask->getProblem());
77 
78  if (!pProblem) return false;
79 
80  std::map<std::string, std::string> ExperimentMap;
81  std::map<std::string, std::string> CrossValidationMap;
82 
83  // Save experiment set
84  CExperimentSet * pExperimentSet =
85  dynamic_cast<CExperimentSet *>(pProblem->getGroup("Experiment Set"));
86 
87  std::string Name;
88  CExperiment * pExperiment;
89  size_t i, imax;
90 
91  std::set<CExperiment *> DealtWith;
92 
93  for (i = pExperimentSet->getExperimentCount() - 1; i != C_INVALID_INDEX; i--)
94  {
95  pExperiment =
96  dynamic_cast<CExperiment *>(CCopasiRootContainer::getKeyFactory()->get(mExperimentKeyMap[pExperimentSet->getExperiment(i)->CCopasiParameter::getKey()]));
97 
98  if (pExperiment && pExperiment == mpExperimentSet->getExperiment(pExperiment->getObjectName()))
99  {
100  if (!(*pExperimentSet->getExperiment(i) == *pExperiment))
101  {
102  *pExperimentSet->getExperiment(i) = *pExperiment;
103  mChanged = true;
104  }
105 
106  DealtWith.insert(pExperiment);
107  }
108  else
109  {
110  mExperimentKeyMap.erase(pExperimentSet->getExperiment(i)->CCopasiParameter::getKey());
111  pExperimentSet->removeExperiment(i);
112  mChanged = true;
113  }
114  }
115 
116  for (i = 0, imax = mpExperimentSet->getExperimentCount(); i < imax; i++)
117  {
118  pExperiment = mpExperimentSet->getExperiment(i);
119 
120  if (DealtWith.count(pExperiment)) continue;
121 
122  pExperiment = pExperimentSet->addExperiment(*pExperiment);
123  mExperimentKeyMap[pExperiment->CCopasiParameter::getKey()] =
124  mpExperimentSet->getExperiment(i)->CCopasiParameter::getKey();
125  mChanged = true;
126  }
127 
128  // We need to invert the key map for saving!
129  std::map<std::string, std::string>::iterator it = mExperimentKeyMap.begin();
130  std::map<std::string, std::string>::iterator end = mExperimentKeyMap.end();
131 
132  for (; it != end; ++it)
133  ExperimentMap[it->second] = it->first;
134 
135  // Save cross validation experiment set
136  CCrossValidationSet * pCrossValidationSet =
137  dynamic_cast<CCrossValidationSet *>(pProblem->getGroup("Validation Set"));
138 
139  if (pCrossValidationSet->getWeight() != mpCrossValidationSet->getWeight())
140  {
141  pCrossValidationSet->setWeight(mpCrossValidationSet->getWeight());
142  mChanged = true;
143  }
144 
145  if (pCrossValidationSet->getThreshold() != mpCrossValidationSet->getThreshold())
146  {
147  pCrossValidationSet->setThreshold(mpCrossValidationSet->getThreshold());
148  mChanged = true;
149  }
150 
151  DealtWith.clear();
152 
153  for (i = pCrossValidationSet->getExperimentCount() - 1; i != C_INVALID_INDEX; i--)
154  {
155  pExperiment =
156  dynamic_cast<CExperiment *>(CCopasiRootContainer::getKeyFactory()->get(mCrossValidationKeyMap[pCrossValidationSet->getExperiment(i)->CCopasiParameter::getKey()]));
157 
158  if (pExperiment && pExperiment == mpCrossValidationSet->getExperiment(pExperiment->getObjectName()))
159  {
160  if (!(*pCrossValidationSet->getExperiment(i) == *pExperiment))
161  {
162  *pCrossValidationSet->getExperiment(i) = *pExperiment;
163  mChanged = true;
164  }
165 
166  DealtWith.insert(pExperiment);
167  }
168  else
169  {
170  mCrossValidationKeyMap.erase(pCrossValidationSet->getExperiment(i)->CCopasiParameter::getKey());
171  pCrossValidationSet->removeExperiment(i);
172  mChanged = true;
173  }
174  }
175 
176  for (i = 0, imax = mpCrossValidationSet->getExperimentCount(); i < imax; i++)
177  {
178  pExperiment = mpCrossValidationSet->getExperiment(i);
179 
180  if (DealtWith.count(pExperiment)) continue;
181 
182  pExperiment = pCrossValidationSet->addExperiment(*pExperiment);
183  mCrossValidationKeyMap[pExperiment->CCopasiParameter::getKey()] =
184  mpCrossValidationSet->getExperiment(i)->CCopasiParameter::getKey();
185  mChanged = true;
186  }
187 
188  // We need to invert the key map for saving!
189  it = mCrossValidationKeyMap.begin();
190  end = mCrossValidationKeyMap.end();
191 
192  for (; it != end; ++it)
193  CrossValidationMap[it->second] = it->first;
194 
195  if (mpCheckRandomize->isChecked() != pProblem->getRandomizeStartValues())
196  {
197  mChanged = true;
198  pProblem->setRandomizeStartValues(mpCheckRandomize->isChecked());
199  }
200 
201  if (mpCreateParameterSets->isChecked() != pProblem->getCreateParameterSets())
202  {
203  mChanged = true;
204  pProblem->setCreateParameterSets(mpCreateParameterSets->isChecked());
205  }
206 
207  if (mpCheckStatistics->isChecked() != pProblem->getCalculateStatistics())
208  {
209  mChanged = true;
210  pProblem->setCalculateStatistics(mpCheckStatistics->isChecked());
211  }
212 
213  mChanged |= mpParameters->save(&ExperimentMap, &CrossValidationMap);
214  mChanged |= mpConstraints->save(&ExperimentMap, &CrossValidationMap);
215 
216  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
217 
218  if (mChanged)(*CCopasiRootContainer::getDatamodelList())[0]->changed();
219 
220  mChanged = false;
221  return true;
222 }
223 
225 {
226  CFitTask * pTask =
227  dynamic_cast< CFitTask * >(mpTask);
228 
229  if (!pTask) return false;
230 
231  loadCommon();
232  loadMethod();
233 
234  CFitProblem * pProblem =
235  dynamic_cast<CFitProblem *>(mpTask->getProblem());
236 
237  if (!pProblem) return false;
238 
240  CExperimentSet * pExperimentSet =
241  dynamic_cast<CExperimentSet *>(pProblem->getGroup("Experiment Set"));
242  mpExperimentSet = new CExperimentSet(*pExperimentSet);
243 
244  mExperimentKeyMap.clear();
245  size_t i, imax = mpExperimentSet->getExperimentCount();
246 
247  for (i = 0; i < imax; i++)
248  mExperimentKeyMap[pExperimentSet->getExperiment(i)->CCopasiParameter::getKey()] =
249  mpExperimentSet->getExperiment(i)->CCopasiParameter::getKey();
250 
252  CCrossValidationSet * pCrossValidationSet =
253  dynamic_cast<CCrossValidationSet *>(pProblem->getGroup("Validation Set"));
254  mpCrossValidationSet = new CCrossValidationSet(*pCrossValidationSet);
255 
256  mCrossValidationKeyMap.clear();
258 
259  for (i = 0; i < imax; i++)
260  mCrossValidationKeyMap[pCrossValidationSet->getExperiment(i)->CCopasiParameter::getKey()] =
261  mpCrossValidationSet->getExperiment(i)->CCopasiParameter::getKey();
262 
263  mpCheckRandomize->setChecked(pProblem->getRandomizeStartValues());
264  mpCreateParameterSets->setChecked(pProblem->getCreateParameterSets());
265  mpCheckStatistics->setChecked(pProblem->getCalculateStatistics());
266 
267  mpParameters->load(mpDataModel, pProblem->getGroup("OptimizationItemList"), &mExperimentKeyMap, &mCrossValidationKeyMap);
268 
270  {
271  CQMessageBox::critical(this, "Error loading Parameter estimation task",
273  QMessageBox::Ok | QMessageBox::Default,
274  QMessageBox::NoButton);
275  }
276 
277  mpParameters->setExperimentSet(const_cast<const CExperimentSet *&>(mpExperimentSet));
278  mpParameters->setCrossValidationSet(const_cast<const CCrossValidationSet *&>(mpCrossValidationSet));
279 
280  mpConstraints->load(mpDataModel, pProblem->getGroup("OptimizationConstraintList"), &mExperimentKeyMap, &mCrossValidationKeyMap);
281  mpConstraints->setExperimentSet(const_cast<const CExperimentSet *&>(mpExperimentSet));
282  mpConstraints->setCrossValidationSet(const_cast<const CCrossValidationSet *&>(mpCrossValidationSet));
283 
284  mChanged = false;
285 
286  return true;
287 }
288 
290 {return CFitMethod::createMethod(type);}
291 
293 {
294  CFitTask * pTask =
295  dynamic_cast< CFitTask * >(CCopasiRootContainer::getKeyFactory()->get(mKey));
296 
297  if (!pTask) return false;
298 
300 
301  if (!commonBeforeRunTask()) return false;
302 
303  bool success = commonRunTask();
304 
305  return success;
306 }
307 
309 {
310  bool result = TaskWidget::taskFinishedEvent();
311 
312  CFitTask * pTask =
313  dynamic_cast< CFitTask * >(CCopasiRootContainer::getKeyFactory()->get(mKey));
314 
315  if (!pTask) return false;
316 
317  size_t finalCount = pTask->getObjectDataModel()->getModel()->getModelParameterSets().size();
318 
319  for (size_t i = mnParamterSetsBeforeRun; i < finalCount; ++i)
320  {
322  }
323 
324  return true;
325 }
326 
328 {
329  CQExperimentData * pDialog = new CQExperimentData(this);
330  pDialog->load(mpExperimentSet, mpDataModel);
331 
332  connect(pDialog, SIGNAL(experimentChanged()), mpParameters, SLOT(slotExperimentChanged()));
333  connect(pDialog, SIGNAL(experimentChanged()), mpConstraints, SLOT(slotExperimentChanged()));
334 
335  pDialog->exec();
336 
337  pdelete(pDialog);
338 }
339 
340 void CQFittingWidget::slotPageChange(QWidget * currentPage)
341 {
342  if (mpTabWidget->tabText(mpTabWidget->indexOf(currentPage)).contains("Parameters", Qt::CaseSensitive))
343  mpCurrentList = mpParameters;
344  else
345  mpCurrentList = mpConstraints;
346 }
347 
349 {
350  mpHeaderWidget->setTaskName("Parameter Estimation");
351 
352  verticalLayout->insertWidget(0, mpHeaderWidget);
353  // verticalLayout->insertSpacing(1, 14); // space between header and body
354 
357  verticalLayout->addWidget(mpMethodWidget);
358 
359  verticalLayout->addWidget(mpBtnWidget);
360 
361  mpParameters->setItemType(CQFittingItemWidget::FIT_ITEM);
362  connect(mpParameters, SIGNAL(numberChanged(int)), this, SLOT(slotParameterNumberChanged(int)));
363 
364  mpConstraints->setItemType(CQFittingItemWidget::FIT_CONSTRAINT);
365  connect(mpConstraints, SIGNAL(numberChanged(int)), this, SLOT(slotConstraintNumberChanged(int)));
366 
367  mpCurrentList = mpParameters;
368  mpExperimentSet = NULL;
369  mpCrossValidationSet = NULL;
370 }
371 
373 {
374  QString TabLabel = "Parameters (" + QString::number(number) + ")";
375  mpTabWidget->setTabText(mpTabWidget->indexOf(mpParameters), TabLabel);
376 }
377 
379 {
380  QString TabLabel = "Constraints (" + QString::number(number) + ")";
381  mpTabWidget->setTabText(mpTabWidget->indexOf(mpConstraints), TabLabel);
382 }
383 
385 {
388 }
389 
391 {
392  CQExperimentData * pDialog = new CQExperimentData(this);
394 
395  connect(pDialog, SIGNAL(experimentChanged()), mpParameters, SLOT(slotCrossValidationChanged()));
396  connect(pDialog, SIGNAL(experimentChanged()), mpConstraints, SLOT(slotCrossValidationChanged()));
397 
398  pDialog->exec();
399 
400  pdelete(pDialog);
401 }
CCopasiDataModel * getObjectDataModel()
static const CCopasiMessage & peekLastMessage()
std::map< std::string, std::string > mCrossValidationKeyMap
void slotPageChange(QWidget *currentPage)
bool saveMethod()
Definition: TaskWidget.cpp:308
#define pdelete(p)
Definition: copasi.h:215
CQFittingWidget(QWidget *parent=0, const char *name=0)
CCopasiProblem * getProblem()
bool loadCommon()
Definition: TaskWidget.cpp:247
const std::string & getObjectName() const
virtual size_t size() const
CCopasiObject * get(const std::string &key)
static std::string getAllMessageText(const bool &chronological=true)
virtual CCopasiMethod * createMethod(const CCopasiMethod::SubType &type)
size_t mnParamterSetsBeforeRun
const bool & getCreateParameterSets() const
bool saveCommon()
Definition: TaskWidget.cpp:258
bool loadMethod()
Definition: TaskWidget.cpp:284
CExperimentSet * mpExperimentSet
#define C_INVALID_INDEX
Definition: copasi.h:222
const bool & getRandomizeStartValues() const
CExperiment * getExperiment(const size_t &index)
virtual bool taskFinishedEvent()
bool setTaskName(const std::string &name)
static const unsigned int ValidMethods[]
Definition: COptTask.h:50
virtual bool runTask()
bool mChanged
Definition: TaskWidget.h:133
void slotConstraintNumberChanged(int number)
virtual bool taskFinishedEvent()
Definition: TaskWidget.cpp:508
virtual const std::string & getKey() const
CTSSATask * pTask
const C_FLOAT64 & getWeight() const
CExperiment * addExperiment(const CExperiment &experiment)
const CCopasiMessage::Type & getType() const
void setWeight(const C_FLOAT64 &weight)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
CCopasiTask * mpTask
Definition: TaskWidget.h:130
void showMethodParameters(const bool &show)
const CCopasiVectorN< CModelParameterSet > & getModelParameterSets() const
Definition: CModel.cpp:1066
CCrossValidationSet * mpCrossValidationSet
CQTaskMethodWidget * mpMethodWidget
Definition: TaskWidget.h:123
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
virtual bool saveTask()
CCopasiDataModel * mpDataModel
Definition: copasiWidget.h:65
void setRandomizeStartValues(const bool &randomize)
virtual bool load(CExperimentSet *pExperimentSet, CCopasiDataModel *pDataModel)
static CKeyFactory * getKeyFactory()
bool commonRunTask()
Definition: TaskWidget.cpp:395
CQTaskBtnWidget * mpBtnWidget
Definition: TaskWidget.h:124
static StandardButton critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
CQTaskHeaderWidget * mpHeaderWidget
Definition: TaskWidget.h:122
const unsigned C_INT32 & getThreshold() const
static COptMethod * createMethod(CCopasiMethod::SubType subType=CCopasiMethod::EvolutionaryProgram)
Definition: CFitMethod.cpp:26
CQFittingItemWidget * mpCurrentList
const bool & getCalculateStatistics() const
virtual bool loadTask()
void setCreateParameterSets(const bool &create)
CCopasiParameterGroup * getGroup(const std::string &name)
bool commonBeforeRunTask()
Definition: TaskWidget.cpp:317
if(!yymsg) yymsg
std::string mKey
Definition: copasiWidget.h:63
void slotParameterNumberChanged(int number)
void slotCrossValidationData()
size_t getExperimentCount() const
void setCalculateStatistics(const bool &calculate)
void removeExperiment(const size_t &index)
void setValidMethods(const unsigned int *validMethods)
std::map< std::string, std::string > mExperimentKeyMap