void lmWorker::calculateChiSquare() { //Calculate ChiSquare(s) TelluriumData& modelData = *(TelluriumData*) mTheHost.mModelData.getValuePointer(); TelluriumData& obsData = *(TelluriumData*) mTheHost.mExperimentalData.getValuePointer(); Plugin* chi = mTheHost.mPM->getPlugin("tel_chisquare"); if(!chi) { throw(Exception("Failed to get chisquare plugin in lmfit plugin")); } Property<TelluriumData>* para = dynamic_cast<Property<TelluriumData>*>(chi->getProperty("ExperimentalData")); para->setValue(obsData); para = dynamic_cast<Property<TelluriumData>*>(chi->getProperty("ModelData")); para->setValue(modelData); Property<int>* intPara = dynamic_cast< Property<int>* >(chi->getProperty("NrOfModelParameters")); intPara->setValue(mLMData.nrOfParameters); //Calculate Chi square chi->execute(); Property<double>* chiSquare = dynamic_cast< Property<double>* >(chi->getProperty("ChiSquare")); Property<double>* rChiSquare = dynamic_cast< Property<double>* >(chi->getProperty("ReducedChiSquare")); mTheHost.mChiSquare.setValue(chiSquare->getValue()); mTheHost.mReducedChiSquare.setValue(rChiSquare->getValue()); Log(lInfo)<<"Chi Square = "<<chiSquare->getValue(); Log(lInfo)<<"Reduced Chi Square = "<<rChiSquare->getValue(); }
double nmWorker::getChi(const Properties& parameters) { Log(lDebug)<<"Getting chisquare using parameters: "<<parameters; //Reset RoadRunner mHost.mRRI->reset(); for(int i = 0; i < parameters.count(); i++) { Property<double> *para = (Property<double>*) (parameters[i]); mHost.mRRI->setValue(para->getName(), para->getValue()); } TelluriumData* expData = (TelluriumData*) mHost.mExperimentalData.getValueHandle(); rr::SimulateOptions options; options.start = expData->getTimeStart(); options.duration = expData->getTimeEnd() - expData->getTimeStart(); options.steps = expData->rSize() - 1; options.flags = options.flags | rr::SimulateOptions::RESET_MODEL; rr::RoadRunnerData *modelData = NULL; if(mHost.mRRI->simulate(&options)) { modelData = mHost.mRRI->getSimulationResult(); } TelluriumData& obsData = *(TelluriumData*) mHost.mExperimentalData.getValuePointer(); Plugin* chi = mHost.mPM->getPlugin("tel_chisquare"); Property<TelluriumData>* para = dynamic_cast<Property<TelluriumData>*>(chi->getProperty("ExperimentalData")); para->setValue(obsData); para = dynamic_cast<Property<TelluriumData>*>(chi->getProperty("ModelData")); para->setValue(TelluriumData(*(modelData))); Property<int>* intPara = dynamic_cast< Property<int>* >(chi->getProperty("NrOfModelParameters")); intPara->setValue(getNumberOfParameters()); //Calculate Chi square chi->execute(); Property<double>* chiSquare = dynamic_cast< Property<double>* >(chi->getProperty("ChiSquare")); return chiSquare->getValue(); }
double lmWorker::getChi(const Properties& parameters) { RRPLOG(lDebug)<<"Getting chisquare using parameters: "<<parameters; //Reset RoadRunner mRRI->reset(); for(int i = 0; i < parameters.count(); i++) { Property<double> *para = (Property<double>*) (parameters[i]); mRRI->setValue(para->getName(), para->getValue()); } rr::SimulateOptions options; options.start = mLMData.timeStart; options.duration = mLMData.timeEnd - mLMData.timeStart; options.steps = mLMData.nrOfTimePoints - 1; mRRI->reset(); const DoubleMatrix* modelData = NULL; if(mRRI->simulate(&options)) { modelData = mRRI->getSimulationData(); } TelluriumData& obsData = *(TelluriumData*) mTheHost.mExperimentalData.getValuePointer(); Plugin* chi = mTheHost.mPM->getPlugin("tel_chisquare"); Property<TelluriumData>* para = dynamic_cast<Property<TelluriumData>*>(chi->getProperty("ExperimentalData")); para->setValue(obsData); para = dynamic_cast<Property<TelluriumData>*>(chi->getProperty("ModelData")); TelluriumData td; td.setData(*modelData); para->setValue(td); Property<int>* intPara = dynamic_cast< Property<int>* >(chi->getProperty("NrOfModelParameters")); intPara->setValue(mLMData.nrOfParameters); //Calculate Chi square chi->execute(); Property<double>* chiSquare = dynamic_cast< Property<double>* >(chi->getProperty("ChiSquare")); return chiSquare->getValue(); }
bool TestModel::execute(bool inThread) { Log(lDebug)<<"Executing the TestModel plugin by Totte Karlsson"; RoadRunner rr; rr.load(mModel); rr::SimulateOptions opt; opt.start = 0; opt.duration = 10; opt.steps = 14; TelluriumData data(rr.simulate(&opt)); mTestData.setValue(data); //Add noise const PluginManager* PM = this->getPluginManager(); Plugin* noise = PM->getPlugin("AddNoise"); if(!noise) { stringstream msg; msg<<"The TestModel plugin dependes on the AddNoise plugin, which is not yet loaded."; throw(Exception(msg.str())); } mTestDataWithNoise.setValue(mTestData.getValue()); noise->setPropertyValue("Sigma", mSigma.getValueHandle()); noise->setPropertyValue("InputData", mTestDataWithNoise.getValueHandle()); noise->execute(); mTestDataWithNoise.setValue(noise->getPropertyValueHandle("InputData")); //Add weights addWeights(); return true; }
int main() { try { //Create a RoadRunner object RoadRunner rr("r:\\installs\\cg\\xe3\\debug\\rr_support"); //Get the plugin manager PluginManager& plugins = rr.getPluginManager(); if(!plugins.load()) { clog<<"Failed loading plugins..\n"; } if(plugins.getNumberOfPlugins() > 0) { cout<<"The following plugins are loaded:\n"; for(int i = 0; i < plugins.getNumberOfPlugins(); i++) { Plugin* aPlugin = plugins[i]; cout<<"Plugin "<<i<<": "<<aPlugin->getName()<<"\n"; cout<<aPlugin->getInfo(); aPlugin->execute(); } } plugins.unload(); Pause(true); rr.~RoadRunner(); } catch(const rr::Exception& ex) { clog<<"There was a problem: "<<ex.what(); } return 0; }