COPASI API  4.16.103
CExperimentSet.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 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 <algorithm>
16 #include <limits>
17 #include <cmath>
18 
19 #include "copasi.h"
20 
21 #include "CExperimentSet.h"
22 #include "CExperiment.h"
23 
26 #include "report/CKeyFactory.h"
27 #include "utilities/utility.h"
29 
31  const std::string & name):
32  CCopasiParameterGroup(name, pParent, "CExperimentSet"),
33  mpExperiments(NULL),
34  mNonExperiments(0),
35  mDependentObjects(0),
36  mDependentObjectiveValues(0),
37  mDependentRMS(0),
38  mDependentErrorMean(0),
39  mDependentErrorMeanSD(0),
40  mDependentDataCount(0),
41  mValidValueCount(0)
43 
45  const CCopasiContainer * pParent):
46  CCopasiParameterGroup(src, static_cast< const CCopasiContainer * >((pParent != NULL) ? pParent : src.getObjectDataModel())),
47  mpExperiments(NULL),
48  mNonExperiments(0),
49  mDependentObjects(0),
50  mDependentObjectiveValues(0),
51  mDependentRMS(0),
52  mDependentErrorMean(0),
53  mDependentErrorMeanSD(0),
54  mDependentDataCount(0),
55  mValidValueCount(0)
57 
59  const CCopasiContainer * pParent):
60  CCopasiParameterGroup(group, static_cast< const CCopasiContainer * >((pParent != NULL) ? pParent : group.getObjectDataModel())),
61  mpExperiments(NULL),
62  mNonExperiments(0),
63  mDependentObjects(0),
64  mDependentObjectiveValues(0),
65  mDependentRMS(0),
66  mDependentErrorMean(0),
67  mDependentErrorMeanSD(0),
68  mDependentDataCount(0),
69  mValidValueCount(0)
71 
73 
75 {elevateChildren();}
76 
78 {
79  index_iterator it = mValue.pGROUP->begin();
80  index_iterator end = mValue.pGROUP->end();
81 
82  for (; it != end; ++it)
83  {
84  if (dynamic_cast< CCopasiParameterGroup * >(*it) == NULL) continue;
85 
86  if (!elevate<CExperiment, CCopasiParameterGroup>(*it)) return false;
87  }
88 
89  mpExperiments = static_cast<std::vector<CExperiment * > * >(mValue.pVOID);
90 
91  sort();
92 
93  return true;
94 }
95 
96 bool CExperimentSet::compile(const std::vector< CCopasiContainer * > listOfContainer)
97 {
98  bool success = true;
99 
100  // First we need to sort the experiments so that we can make use of continued
101  // file reading.
102  sort();
103 
104  std::set< CCopasiObject * > DependentObjects;
105 
106  std::ifstream in;
107  std::string CurrentFileName("");
108  size_t CurrentLineNumber = 1;
109 
110  std::vector< CExperiment * >::iterator it = mpExperiments->begin() + mNonExperiments;
111  std::vector< CExperiment * >::iterator end = mpExperiments->end();
112 
113  for (; it != end; ++it)
114  {
115  if (CurrentFileName != (*it)->getFileName())
116  {
117  CurrentFileName = (*it)->getFileName();
118  CurrentLineNumber = 1;
119 
120  if (in.is_open())
121  {
122  in.close();
123  in.clear();
124  }
125 
126  in.open(CLocaleString::fromUtf8(CurrentFileName).c_str(), std::ios::binary);
127 
128  if (in.fail())
129  {
130  CCopasiMessage(CCopasiMessage::ERROR, MCFitting + 8, CurrentFileName.c_str());
131  return false; // File can not be opened.
132  }
133  }
134 
135  if (!(*it)->read(in, CurrentLineNumber)) return false;
136 
137  if (!(*it)->compile(listOfContainer)) return false;
138 
139  const std::map< CCopasiObject *, size_t > & ExpDependentObjects
140  = (*it)->getDependentObjects();
141  std::map< CCopasiObject *, size_t >::const_iterator itObject
142  = ExpDependentObjects.begin();
143  std::map< CCopasiObject *, size_t >::const_iterator endObject
144  = ExpDependentObjects.end();
145 
146  for (; itObject != endObject; ++itObject)
147  DependentObjects.insert(itObject->first);
148  }
149 
150  mDependentObjects.resize(DependentObjects.size());
151  CCopasiObject ** ppInsert = mDependentObjects.array();
152  std::set< CCopasiObject * >::const_iterator itObject = DependentObjects.begin();
153  std::set< CCopasiObject * >::const_iterator endObject = DependentObjects.end();
154 
155  for (; itObject != endObject; ++itObject, ++ppInsert)
156  *ppInsert = *itObject;
157 
158  // Allocation and initialization of statistical information
160  mDependentObjectiveValues = std::numeric_limits<C_FLOAT64>::quiet_NaN();
161 
163  mDependentRMS = std::numeric_limits<C_FLOAT64>::quiet_NaN();
164 
166  mDependentErrorMean = std::numeric_limits<C_FLOAT64>::quiet_NaN();
167 
169  mDependentErrorMeanSD = std::numeric_limits<C_FLOAT64>::quiet_NaN();
170 
172  mDependentDataCount = std::numeric_limits<size_t>::quiet_NaN();
173 
174  return success;
175 }
176 
178 {
181 
183  mDependentRMS = 0.0;
184 
186  mDependentErrorMean = 0.0;
187 
189  mDependentErrorMeanSD = 0.0;
190 
193  mValidValueCount = 0;
194 
195  // calculate the per experiment and per dependent value statistics.
196  std::vector< CExperiment * >::iterator it = mpExperiments->begin() + mNonExperiments;
197  std::vector< CExperiment * >::iterator end = mpExperiments->end();
198 
199  size_t i, Count;
200  C_FLOAT64 Tmp;
201 
202  for (; it != end; ++it)
203  {
204  (*it)->calculateStatistics();
205 
206  CCopasiObject *const* ppObject = mDependentObjects.array();
207  CCopasiObject *const* ppEnd = ppObject + mDependentObjects.size();
208 
209  for (i = 0; ppObject != ppEnd; ++ppObject, ++i)
210  {
211  Count = (*it)->getColumnValidValueCount(*ppObject);
212 
213  if (Count)
214  {
215  mDependentObjectiveValues[i] += (*it)->getObjectiveValue(*ppObject);
216 
217  Tmp = (*it)->getRMS(*ppObject);
218  mDependentRMS[i] += Tmp * Tmp * Count;
219 
220  mDependentErrorMean[i] += (*it)->getErrorSum(*ppObject);
221 
222  mDependentDataCount[i] += Count;
223  mValidValueCount += Count;
224  }
225  }
226  }
227 
228  size_t imax = mDependentObjects.size();
229 
230  for (i = 0; i != imax; i++)
231  {
232  Count = mDependentDataCount[i];
233 
234  if (Count)
235  {
236  mDependentRMS[i] = sqrt(mDependentRMS[i] / Count);
237  mDependentErrorMean[i] /= Count;
238  }
239  else
240  {
241  mDependentRMS[i] = std::numeric_limits<C_FLOAT64>::quiet_NaN();
242  mDependentErrorMean[i] = std::numeric_limits<C_FLOAT64>::quiet_NaN();
243  }
244  }
245 
246  it = mpExperiments->begin() + mNonExperiments;
247 
248  // We need to loop again to calculate the std. deviation.
249  for (; it != end; ++it) //over experiments
250  {
251  CCopasiObject *const* ppObject = mDependentObjects.array();
252  CCopasiObject *const* ppEnd = ppObject + mDependentObjects.size();
253 
254  for (i = 0; ppObject != ppEnd; ++ppObject, ++i)
255  {
256  Count = (*it)->getColumnValidValueCount(*ppObject);
257 
258  if (Count)
260  (*it)->getErrorMeanSD(*ppObject, mDependentErrorMean[i]);
261  }
262  }
263 
264  for (i = 0; i != imax; i++)
265  {
266  Count = mDependentDataCount[i];
267 
268  if (Count)
269  mDependentErrorMeanSD[i] = sqrt(mDependentErrorMeanSD[i] / Count);
270  else
271  mDependentErrorMeanSD[i] = std::numeric_limits<C_FLOAT64>::quiet_NaN();
272  }
273 
274  // This is the time to call the output handler to plot the fitted points.
275  for (it = mpExperiments->begin() + mNonExperiments, imax = 0; it != end; ++it)
276  imax = std::max(imax, (*it)->getDependentData().numRows());
277 
278  CCopasiTask * pParentTask = dynamic_cast< CCopasiTask *>(getObjectAncestor("Task"));
279  assert(pParentTask != NULL);
280 
281  for (i = 0; i < imax; i++)
282  {
283  for (it = mpExperiments->begin() + mNonExperiments; it != end; ++it)
284  (*it)->updateFittedPointValues(i, (*it)->getExperimentType() != CCopasiTask::timeCourse); //false means without simulated data
285 
286  pParentTask->output(COutputInterface::AFTER);
287  }
288 
289  //now the extended time series
290  for (it = mpExperiments->begin() + mNonExperiments, imax = 0; it != end; ++it)
291  imax = std::max(imax, (*it)->extendedTimeSeriesSize());
292 
293  for (i = 0; i < imax; i++)
294  {
295  for (it = mpExperiments->begin() + mNonExperiments; it != end; ++it)
296  {
297  if ((*it)->getExperimentType() == CCopasiTask::timeCourse)
298  {
299  (*it)->updateFittedPointValuesFromExtendedTimeSeries(i);
300  }
301  }
302 
303  pParentTask->output(COutputInterface::AFTER);
304  }
305 
306  return true;
307 }
308 
310 {return mDependentObjects;}
311 
314 
316 {return mDependentRMS;}
317 
319 {return mDependentErrorMean;}
320 
322 {return mDependentErrorMeanSD;}
323 
325 {return size() - mNonExperiments;}
326 
328 {
329  // We need to make sure that the experiment name is unique.
330  std::string name = experiment.getObjectName();
331 
332  int i = 0;
333 
334  while (getParameter(name))
335  {
336  i++;
337  name = StringPrint("%s_%d", experiment.getObjectName().c_str(), i);
338  }
339 
340  CExperiment * pExperiment = new CExperiment(experiment);
341  pExperiment->setObjectName(name);
342  addParameter(pExperiment);
343 
344  sort();
345 
346  return pExperiment;
347 }
348 
349 void CExperimentSet::removeExperiment(const size_t & index)
351 
353 {return (*mpExperiments)[index + mNonExperiments];}
354 
355 const CExperiment * CExperimentSet::getExperiment(const size_t & index) const
356 {return (*mpExperiments)[index + mNonExperiments];}
357 
358 CExperiment * CExperimentSet::getExperiment(const std::string & name)
359 {return static_cast<CExperiment *>(getGroup(name));}
360 
361 const CExperiment * CExperimentSet::getExperiment(const std::string & name) const
362 {return static_cast<const CExperiment *>(getGroup(name));}
363 
365 {
366  std::vector< CExperiment * >::const_iterator it = mpExperiments->begin() + mNonExperiments;
367  std::vector< CExperiment * >::const_iterator end = mpExperiments->end();
368 
369  for (; it != end; ++it)
370  {
371  if ((*it)->getExperimentType() == type)
372  {
373  return true;
374  }
375  }
376 
377  return false;
378 }
379 
380 const CCopasiTask::Type & CExperimentSet::getExperimentType(const size_t & index) const
381 {return getExperiment(index)->getExperimentType();}
382 
383 const CMatrix< C_FLOAT64 > & CExperimentSet::getIndependentData(const size_t & index) const
384 {return getExperiment(index)->getIndependentData();}
385 
386 const CMatrix< C_FLOAT64 > & CExperimentSet::getDependentData(const size_t & index) const
387 {return getExperiment(index)->getDependentData();}
388 
389 size_t CExperimentSet::keyToIndex(const std::string & key) const
390 {
391  const CExperiment * pExp = dynamic_cast<const CExperiment *>(CCopasiRootContainer::getKeyFactory()->get(key));
392 
393  if (!pExp) return C_INVALID_INDEX;
394 
395  size_t i, imax = size();
396 
397  for (i = 0; i < imax; i++)
398  if (pExp == getExperiment(i)) return i;
399 
400  return C_INVALID_INDEX;
401 }
402 
404 {
405  // First we make sure that all experiments are at the end of the group
406  index_iterator it = beginIndex();
407  index_iterator end = endIndex();
408 
409  index_iterator swapTarget = beginIndex();
410  mNonExperiments = 0;
411 
412  for (; it != end; ++it)
413  if (dynamic_cast< CExperiment * >(*it) == NULL)
414  {
415  if (it != swapTarget)
416  swap(it, swapTarget);
417 
418  swapTarget++;
419  mNonExperiments++;
420  }
421 
422  // Now sort the experiments
423  std::vector< CExperiment * >::iterator startSort = mpExperiments->begin() + mNonExperiments;
424  std::vector< CExperiment * >::iterator endSort = mpExperiments->end();
425 
426  std::sort(startSort, endSort, &CExperiment::compare);
427 
428  return;
429 }
430 
431 std::vector< std::string > CExperimentSet::getFileNames() const
432 {
433  std::vector< std::string > List;
434  std::string currentFile = "";
435 
436  std::vector< CExperiment * >::iterator it = mpExperiments->begin() + mNonExperiments;
437  std::vector< CExperiment * >::iterator end = mpExperiments->end();
438 
439  for (; it != end; ++it)
440  if (currentFile != (*it)->getFileName())
441  {
442  currentFile = (*it)->getFileName();
443  List.push_back(currentFile);
444  }
445 
446  return List;
447 }
448 
450 {
451  size_t Count = 0;
452  std::vector< CExperiment * >::iterator it = mpExperiments->begin() + mNonExperiments;
453  std::vector< CExperiment * >::iterator end = mpExperiments->end();
454 
455  for (; it != end; ++it)
456  Count += (*it)->getDependentData().numRows() * (*it)->getDependentData().numCols();
457 
458  return Count;
459 }
460 
462 {
463  return mValidValueCount;
464 }
465 
467 {
468  std::vector< CExperiment * >::iterator it = mpExperiments->begin() + mNonExperiments;
469  std::vector< CExperiment * >::iterator end = mpExperiments->end();
470 
471  for (; it != end; ++it)
472  {
473  (*it)->fixBuild55();
474  }
475 
476  return;
477 }
478 
480  const std::string & name):
481  CExperimentSet(name, pParent),
482  mpWeight(NULL),
483  mpThreshold(NULL)
485 
487  const CCopasiContainer * pParent):
488  CExperimentSet(src, pParent),
489  mpWeight(NULL),
490  mpThreshold(NULL)
492 
494  const CCopasiContainer * pParent):
495  CExperimentSet(group, pParent),
496  mpWeight(NULL),
497  mpThreshold(NULL)
499 
501 
503 {
504  if (weight < 0.0)
505  *mpWeight = 0.0;
506  else if (weight > 1.0)
507  *mpWeight = 1.0;
508  else
509  *mpWeight = weight;
510 }
511 
513 {return *mpWeight;}
514 
515 void CCrossValidationSet::setThreshold(const unsigned C_INT32 & threshold)
516 {*mpThreshold = threshold;}
517 
519 {return *mpThreshold;}
520 
522 {
523  mpWeight =
525 
526  mpThreshold =
527  assertParameter("Threshold", CCopasiParameter::UINT, (unsigned C_INT32) 5)->getValue().pUINT;
528 
529  elevateChildren();
530 }
std::vector< std::string > getFileNames() const
CCopasiContainer * getObjectAncestor(const std::string &type) const
const CMatrix< C_FLOAT64 > & getIndependentData() const
bool swap(const size_t &iFrom, const size_t &iTo)
const CVector< CCopasiObject * > & getDependentObjects() const
const CMatrix< C_FLOAT64 > & getDependentData() const
const std::string & getObjectName() const
CCrossValidationSet(const CCopasiContainer *pParent, const std::string &name="Validation Set")
CCopasiObject * get(const std::string &key)
std::vector< CCopasiParameter * > * pGROUP
index_iterator endIndex() const
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
#define C_INVALID_INDEX
Definition: copasi.h:222
virtual void output(const COutputInterface::Activity &activity)
CVector< C_FLOAT64 > mDependentRMS
CExperiment * getExperiment(const size_t &index)
const CVector< C_FLOAT64 > & getDependentRMS() const
#define C_INT32
Definition: copasi.h:90
bool calculateStatistics()
size_t mValidValueCount
const size_t & getValidValueCount() const
bool removeParameter(const std::string &name)
CVector< CCopasiObject * > mDependentObjects
virtual bool elevateChildren()
const C_FLOAT64 & getWeight() const
CVector< C_FLOAT64 > mDependentErrorMeanSD
const CVector< C_FLOAT64 > & getDependentObjectiveValues() const
CExperiment * addExperiment(const CExperiment &experiment)
static bool compare(const CExperiment *lhs, const CExperiment *rhs)
#define MCFitting
index_iterator beginIndex() const
const CVector< C_FLOAT64 > & getDependentErrorMean() const
const Value & getValue() const
void setWeight(const C_FLOAT64 &weight)
bool hasDataForTaskType(const CCopasiTask::Type &type) const
unsigned C_INT32 * pUINT
bool compile(const std::vector< CCopasiContainer * > listOfContainer=CCopasiContainer::EmptyList)
const CCopasiTask::Type & getExperimentType(const size_t &index) const
CCopasiParameter * getParameter(const std::string &name)
CExperimentSet(const CCopasiContainer *pParent, const std::string &name="Experiment Set")
unsigned C_INT32 * mpThreshold
size_t keyToIndex(const std::string &key) const
size_t size() const
Definition: CVector.h:100
virtual ~CExperimentSet()
void initializeParameter()
CVector< C_FLOAT64 > mDependentObjectiveValues
const CVector< C_FLOAT64 > & getDependentErrorMeanSD() const
const CCopasiTask::Type & getExperimentType() const
static CKeyFactory * getKeyFactory()
void setThreshold(const unsigned C_INT32 &threshold)
#define C_FLOAT64
Definition: copasi.h:92
CType * array()
Definition: CVector.h:139
size_t getDataPointCount() const
const unsigned C_INT32 & getThreshold() const
std::vector< CExperiment * > * mpExperiments
size_t mNonExperiments
std::string StringPrint(const char *format,...)
Definition: utility.cpp:87
bool addParameter(const CCopasiParameter &parameter)
CCopasiParameter * assertParameter(const std::string &name, const CCopasiParameter::Type type, const CType &defaultValue)
const CMatrix< C_FLOAT64 > & getDependentData(const size_t &index) const
CCopasiParameterGroup * getGroup(const std::string &name)
static CLocaleString fromUtf8(const std::string &utf8)
bool setObjectName(const std::string &name)
virtual ~CCrossValidationSet()
parameterGroup::iterator index_iterator
const CMatrix< C_FLOAT64 > & getIndependentData(const size_t &index) const
size_t getExperimentCount() const
CVector< C_FLOAT64 > mDependentErrorMean
CVector< size_t > mDependentDataCount
void removeExperiment(const size_t &index)
#define max(a, b)
Definition: f2c.h:176