diff --git a/MSSPM_GuiDiagnostic/nmfDiagnosticTab01.cpp b/MSSPM_GuiDiagnostic/nmfDiagnosticTab01.cpp index 14b77aa..8f61cad 100755 --- a/MSSPM_GuiDiagnostic/nmfDiagnosticTab01.cpp +++ b/MSSPM_GuiDiagnostic/nmfDiagnosticTab01.cpp @@ -554,7 +554,7 @@ nmfDiagnostic_Tab1::callback_RunPB() } for (QString parameterName : vectorParameterNames) { -//qDebug() << "processing parameterName: " << parameterName; +//qDebug() << "\nprocessing parameterName: " << parameterName; nmfUtilsQt::updateProgressDlg(m_Logger,progressDlg,"Processing parameter: "+parameterName.toStdString(),pInc); EstParameter.clear(); @@ -571,7 +571,8 @@ nmfDiagnostic_Tab1::callback_RunPB() m_Diagnostic_Tabs->setCursor(Qt::ArrowCursor); return; } - +//qDebug() << "loaded name: " << parameterName; +//qDebug() << EstParameter; if (parameterName == surfaceParameter1Name) { surfaceParameter1 = EstParameter; } else if (parameterName == surfaceParameter2Name) { @@ -585,47 +586,59 @@ nmfDiagnostic_Tab1::callback_RunPB() inc = (estParameter - startVal)/numPoints; diagnosticParameterValue = startVal; - // Skip the current species if there's no estimated data for it. This cold be + // Skip the current species if there's no estimated data for it. This could be // the case for the covariate coefficient parameters if non have been estimated // for certain species. - if ((estParameter != 0) && (startVal != 0)) { + if (1) { // ((estParameter != 0) && (startVal != 0)) { // RSK revisit this logic for (int j=0; j<=totalNumPoints; ++j) { -//if (i == 0) { -//qDebug() << "p1: " << j << parameterName << " " << diagnosticParameterValue << " " << estParameter << inc; // << fitness; +//if (i == 9 && parameterName == "SurveyQ") { +// qDebug() << "\nparameter name: " << parameterName << estParameter << startVal << inc << diagnosticParameterValue; // << fitness; +//} + + if ((estParameter == 0) || (startVal == 0)) { + aDiagnosticTuple = std::make_tuple(SpeciesOrGuildNames[i],j,0,0); + DiagnosticTupleVector.push_back(aDiagnosticTuple); + } else { + try { + parameterItem = std::make_pair(parameterName,diagnosticParameterValue); +//if (i == 9 && parameterName == "SurveyQ") { +// qDebug() << "before calculateFitness call"; +//} + fitness = calculateFitness(i,{parameterItem}); +//if (i == 9 && parameterName == "SurveyQ") { +//qDebug() << "after calculateFitness call, fitness: " << fitness; //} - try { - parameterItem = std::make_pair(parameterName,diagnosticParameterValue); - fitness = calculateFitness(i,{parameterItem}); - } catch (...) { - msg = "Warning (2): Please run an Estimation prior to running this Diagnostic."; - m_Logger->logMsg(nmfConstants::Warning,msg.toStdString()); - QMessageBox::warning(m_Diagnostic_Tabs,tr("Warning"),"\n"+msg,QMessageBox::Ok); - progressDlg->close(); - emit EnableRunButtons(true); - m_Diagnostic_Tabs->setCursor(Qt::ArrowCursor); - return; - } - - if (fitness == -1) { - m_Diagnostic_Tabs->setCursor(Qt::ArrowCursor); - progressDlg->close(); - emit EnableRunButtons(true); - m_Diagnostic_Tabs->setCursor(Qt::ArrowCursor); - return; - } - - aDiagnosticTuple = std::make_tuple(SpeciesOrGuildNames[i], - diagnosticParameterValue-estParameter, - diagnosticParameterValue, - fitness); - - DiagnosticTupleVector.push_back(aDiagnosticTuple); - diagnosticParameterValue += inc; + } catch (...) { + msg = "Warning (2): Please run an Estimation prior to running this Diagnostic."; + m_Logger->logMsg(nmfConstants::Warning,msg.toStdString()); + QMessageBox::warning(m_Diagnostic_Tabs,tr("Warning"),"\n"+msg,QMessageBox::Ok); + progressDlg->close(); + emit EnableRunButtons(true); + m_Diagnostic_Tabs->setCursor(Qt::ArrowCursor); + return; + } + + if (fitness == -1) { + m_Diagnostic_Tabs->setCursor(Qt::ArrowCursor); + progressDlg->close(); + emit EnableRunButtons(true); + m_Diagnostic_Tabs->setCursor(Qt::ArrowCursor); + return; + } + + aDiagnosticTuple = std::make_tuple(SpeciesOrGuildNames[i], + diagnosticParameterValue-estParameter, + diagnosticParameterValue, + fitness); + + DiagnosticTupleVector.push_back(aDiagnosticTuple); + diagnosticParameterValue += inc; + } // end else } // end for } // end if } // end for - +//std::cout << "Upt: before updateParameterTable" << std::endl; updateParameterTable(NumSpeciesOrGuilds, totalNumPoints, Algorithm,Minimizer,ObjectiveCriterion,Scaling, isAggProdStr,parameterName,DiagnosticTupleVector); @@ -747,6 +760,7 @@ nmfDiagnostic_Tab1::calculateFitness(const int& SpeciesOrGuildNum, int GrowthRateOffset,GrowthRateCovarOffset,CarryingCapacityOffset,CarryingCapacityCovarOffset; int CatchabilityOffset,CatchabilityCovarOffset; int SurveyQOffset,SurveyQCovarOffset; + int InitBiomassOffset = 0; double unused2[] = {0}; double retv = 0; std::string Algorithm; @@ -784,12 +798,16 @@ nmfDiagnostic_Tab1::calculateFitness(const int& SpeciesOrGuildNum, isAggProdStr = std::to_string(isAggProd); // Load up parameters (covariates are included from within the following load methods) - int initBiomassOffset = 0; loadOutputParameters(nmfConstantsMSSPM::TableOutputInitBiomass,NumSpeciesOrGuilds, Algorithm,Minimizer,ObjectiveCriterion,Scaling,isAggProdStr,initBiomassParameters); offset = initBiomassParameters.size(); loadGrowthParameters(NumSpeciesOrGuilds,Algorithm,Minimizer,ObjectiveCriterion,Scaling,isAggProdStr,growthParameters, offset,GrowthRateOffset,GrowthRateCovarOffset,CarryingCapacityOffset,CarryingCapacityCovarOffset); +//std::cout << "GrowthRateOffset: " << GrowthRateOffset << std::endl; +//std::cout << "GrowthRateCovarOffset: " << GrowthRateCovarOffset << std::endl; +//std::cout << "CarryingCapacityOffset: " << CarryingCapacityOffset << std::endl; +//std::cout << "CarryingCapacityCovarOffset: " << CarryingCapacityCovarOffset << std::endl; + offset = CarryingCapacityCovarOffset; loadHarvestParameters(NumSpeciesOrGuilds,Algorithm,Minimizer,ObjectiveCriterion,Scaling, harvestParameters,offset,CatchabilityOffset,CatchabilityCovarOffset); @@ -798,27 +816,34 @@ nmfDiagnostic_Tab1::calculateFitness(const int& SpeciesOrGuildNum, offset += competitionParameters.size(); loadPredationParameters(NumSpeciesOrGuilds,Algorithm,Minimizer,ObjectiveCriterion,Scaling,isAggProdStr,predationParameters); offset += predationParameters.size(); - SurveyQOffset = offset; +// SurveyQOffset = offset; loadOutputParameters(nmfConstantsMSSPM::TableOutputSurveyQ,NumSpeciesOrGuilds, Algorithm,Minimizer,ObjectiveCriterion,Scaling,isAggProdStr,surveyQParameters); SurveyQCovarOffset = offset + surveyQParameters.size(); -// loadOutputParameters(nmfConstantsMSSPM::TableOutputSurveyQCovariateCoeffs,NumSpeciesOrGuilds, -// Algorithm,Minimizer,ObjectiveCriterion,Scaling,isAggProdStr,surveyQCovCoeffParameters); + loadOutputParameters(nmfConstantsMSSPM::TableOutputSurveyQCovariateCoeffs,NumSpeciesOrGuilds, + Algorithm,Minimizer,ObjectiveCriterion,Scaling,isAggProdStr,surveyQCovCoeffParameters); //qDebug() << "RSK - TBD implement surveyQCovCoeff logic"; +//qDebug() << "SurveyQCovarOffset: " << SurveyQCovarOffset; // Append all of the Estimated Parameters to: parameters + InitBiomassOffset = parameters.size(); nmfUtils::append(initBiomassParameters,parameters); nmfUtils::append(growthParameters, parameters); nmfUtils::append(harvestParameters, parameters); nmfUtils::append(competitionParameters,parameters); nmfUtils::append(predationParameters, parameters); + SurveyQOffset = parameters.size(); nmfUtils::append(surveyQParameters, parameters); + SurveyQCovarOffset = parameters.size(); + nmfUtils::append(surveyQCovCoeffParameters, parameters); +//std::cout << "ParameterData size: " << ParameterData.size() << std::endl; // Modify the estimated parameter data with the diagnostic "tweaks" to the estimated parameters for (std::pair ParameterItem : ParameterData) { - offset = 0; +//std::cout << "parameter first: " << ParameterItem.first.toStdString() << std::endl; + offset = 0; // RSK bug is here I think... if (ParameterItem.first == "Initial Biomass (B₀)") { - offset = initBiomassOffset; + offset = InitBiomassOffset; } else if (ParameterItem.first == "Growth Rate (r)") { // RSK revisit this...make all the constants more consistent offset = GrowthRateOffset; } else if (ParameterItem.first == "Carrying Capacity (K)") { @@ -827,6 +852,7 @@ nmfDiagnostic_Tab1::calculateFitness(const int& SpeciesOrGuildNum, offset = CatchabilityOffset+NumSpeciesOrGuilds; } else if (ParameterItem.first == nmfConstantsMSSPM::ParameterNameSurveyQ) { offset = SurveyQOffset; +//std::cout << "SurveyQ offset: " << offset << std::endl; } else if (ParameterItem.first == nmfConstantsMSSPM::ParameterNameGrowthRateCovCoeff) { offset = GrowthRateCovarOffset; } else if (ParameterItem.first == nmfConstantsMSSPM::ParameterNameCarryingCapacityCovCoeff) { @@ -834,6 +860,7 @@ nmfDiagnostic_Tab1::calculateFitness(const int& SpeciesOrGuildNum, } else if (ParameterItem.first == nmfConstantsMSSPM::ParameterNameCatchabilityCovCoeff) { offset = CatchabilityCovarOffset; } else if (ParameterItem.first == nmfConstantsMSSPM::ParameterNameSurveyQCovCoeff) { +//qDebug() << "found SQCovCoeff"; offset = SurveyQCovarOffset; } else { msg = "Error: Invalid parameter name: " + ParameterItem.first.toStdString(); @@ -841,18 +868,14 @@ nmfDiagnostic_Tab1::calculateFitness(const int& SpeciesOrGuildNum, return -1; } parameters[offset+SpeciesOrGuildNum] = ParameterItem.second; -//if (SpeciesOrGuildNum == 0 && ParameterItem.first == nmfConstantsMSSPM::ParameterNameGrowthRateCovCoeff) { -//std::cout << "Assigning: parameters[" << offset+SpeciesOrGuildNum << "]: " << ParameterItem.second << std::endl; -//} } -// InitBiomass, r, r cov coeff, ... -//if (SpeciesOrGuildNum == 0) { -// std::cout << "rcv: "; -//for (int i=20; i<30; ++i) { -// std::cout << parameters[i] << ", "; -//} -//std::cout << std::endl; + +//for (int i=150;i<160;++i) { +//std::cout << " parameters[159]: " << parameters[159] << std::endl; //} + + + m_DatabasePtr->getAlgorithmIdentifiers( m_Diagnostic_Tabs,m_Logger,m_ProjectName,m_ModelName, Algorithm,Minimizer,ObjectiveCriterion, @@ -1247,6 +1270,7 @@ nmfDiagnostic_Tab1::updateParameterTable(const int& NumSpeciesOrGuilds, m_Logger->logMsg(nmfConstants::Error,"nmfDiagnostic_Tab1::updateParameterTable: DiagnosticTupleVector is empty"); return; } +//std::cout << "Upt: " << TableName << std::endl; cmd = "DELETE FROM " + TableName + " WHERE ProjectName = '" + m_ProjectName + @@ -1282,8 +1306,9 @@ nmfDiagnostic_Tab1::updateParameterTable(const int& NumSpeciesOrGuilds, cmd = cmd.substr(0,cmd.size()-1); errorMsg = m_DatabasePtr->nmfUpdateDatabase(cmd); if (nmfUtilsQt::isAnError(errorMsg)) { - m_Logger->logMsg(nmfConstants::Error,"[Error 2] UpdateParameterTable: Write table error: " + errorMsg); - m_Logger->logMsg(nmfConstants::Error,"cmd: " + cmd); + m_Logger->logMsg(nmfConstants::Warning,"[Warning 2] UpdateParameterTable: Write table error: " + errorMsg); + m_Logger->logMsg(nmfConstants::Warning,"This may be due to no covariate diagnostic data available."); + m_Logger->logMsg(nmfConstants::Warning,"cmd: " + cmd); return; } diff --git a/MSSPM_GuiSetup/nmfSetupTab04.cpp b/MSSPM_GuiSetup/nmfSetupTab04.cpp index 1cce238..ac0f5b7 100755 --- a/MSSPM_GuiSetup/nmfSetupTab04.cpp +++ b/MSSPM_GuiSetup/nmfSetupTab04.cpp @@ -49,7 +49,9 @@ nmfSetup_Tab4::nmfSetup_Tab4(QTabWidget* tabs, Setup_Tab4_HarvestHighlightPB = Setup_Tabs->findChild("HarvestHighlightPB"); Setup_Tab4_PredationHighlightPB = Setup_Tabs->findChild("PredationHighlightPB"); Setup_Tab4_CompetitionHighlightPB = Setup_Tabs->findChild("CompetitionHighlightPB"); - Setup_Tab4_NewModelPB = Setup_Tabs->findChild("Setup_Tab4_NewModelPB"); + Setup_Tab4_ModelNewPB = Setup_Tabs->findChild("Setup_Tab4_ModelNewPB"); + Setup_Tab4_ModelCopyPB = Setup_Tabs->findChild("Setup_Tab4_ModelCopyPB"); + Setup_Tab4_ModelDelPB = Setup_Tabs->findChild("Setup_Tab4_ModelDelPB"); Setup_Tab4_StartYearSB = Setup_Tabs->findChild("Setup_Tab4_StartYearSB"); Setup_Tab4_EndYearLE = Setup_Tabs->findChild("Setup_Tab4_EndYearLE"); Setup_Tab4_RunLengthSB = Setup_Tabs->findChild("Setup_Tab4_RunLengthSB"); @@ -63,10 +65,12 @@ nmfSetup_Tab4::nmfSetup_Tab4(QTabWidget* tabs, Setup_Tab4_PrevPB = Setup_Tabs->findChild("Setup_Tab4_PrevPB"); Setup_Tab4_NextPB = Setup_Tabs->findChild("Setup_Tab4_NextPB"); Setup_Tab4_LoadPB = Setup_Tabs->findChild("Setup_Tab4_LoadPB"); - Setup_Tab4_DelPB = Setup_Tabs->findChild("Setup_Tab4_DelModelPB"); Setup_Tab4_SavePB = Setup_Tabs->findChild("Setup_Tab4_SavePB"); Setup_Tab4_CalcPB = Setup_Tabs->findChild("Setup_Tab4_CalcPB"); + // RSK - Just do this temporarily until functionality is complete +// Setup_Tab4_ModelCopyPB->setVisible(false); + // Set widget parameters Setup_Tab4_LoadPB->show(); Setup_Tab4_LoadPB->setEnabled(true); @@ -101,14 +105,16 @@ nmfSetup_Tab4::nmfSetup_Tab4(QTabWidget* tabs, this, SLOT(callback_PredationHighlightPB())); connect(Setup_Tab4_CompetitionHighlightPB, SIGNAL(clicked()), this, SLOT(callback_CompetitionHighlightPB())); - connect(Setup_Tab4_NewModelPB, SIGNAL(clicked()), - this, SLOT(callback_NewModelPB())); + connect(Setup_Tab4_ModelNewPB, SIGNAL(clicked()), + this, SLOT(callback_ModelNewPB())); + connect(Setup_Tab4_ModelCopyPB, SIGNAL(clicked()), + this, SLOT(callback_ModelCopyPB())); + connect(Setup_Tab4_ModelDelPB, SIGNAL(clicked()), + this, SLOT(callback_ModelDelPB())); connect(Setup_Tab4_CalcPB, SIGNAL(clicked()), this, SLOT(callback_CalcPB())); connect(Setup_Tab4_LoadPB, SIGNAL(clicked()), this, SLOT(callback_LoadPB())); - connect(Setup_Tab4_DelPB, SIGNAL(clicked()), - this, SLOT(callback_DelPB())); connect(Setup_Tab4_SavePB, SIGNAL(clicked()), this, SLOT(callback_SavePB())); connect(Setup_Tab4_PrevPB, SIGNAL(clicked()), @@ -538,7 +544,7 @@ nmfSetup_Tab4::calculateSystemCarryingCapacity() void -nmfSetup_Tab4::callback_DelPB() +nmfSetup_Tab4::callback_ModelDelPB() { QMessageBox::StandardButton reply; QString msg; @@ -1185,17 +1191,98 @@ nmfSetup_Tab4::callback_CalcPB() } void -nmfSetup_Tab4::callback_NewModelPB() -{ - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::callback_NewModelPB start"); +nmfSetup_Tab4::callback_ModelCopyPB() +{ + m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::callback_CopyModelPB start"); + QString currentModelName = QString::fromStdString(m_ModelName); + QString newModelName = getNewModelName(); + if (! newModelName.isEmpty()) { + // Copy all currentModelName datasets to newModelName + copyModelDataInAllTables(currentModelName,newModelName); + setModelName(newModelName); + callback_SavePB(); + } +} + +bool +nmfSetup_Tab4::copyModelDataInAllTables(const QString& currentModelName, + const QString& newModelName) +{ + bool modelCopyAborted = false; + int NumTables = 0; + std::string temp_table = "temp_table"; + std::vector cmds; + std::vector allTables; + std::string table; + std::vector fields; + std::map > dataMap,dataMap2; + std::string queryStr; + std::string currentDBName = m_databasePtr->nmfGetCurrentDatabase(); + + // Get list of table names that have a ModelName column + fields = {"TABLE_NAME"}; + queryStr = "SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('ModelName') and TABLE_SCHEMA='" + currentDBName + "'"; + dataMap = m_databasePtr->nmfQueryDatabase(queryStr, fields); + NumTables = dataMap["TABLE_NAME"].size(); + + QProgressDialog progressDlg("\nCopying tables...", "Abort Copy", 0, NumTables, Setup_Tabs); + progressDlg.setWindowModality(Qt::WindowModal); + + for (int i=0; inmfQueryDatabase(queryStr, fields); + if (dataMap2["HasData"][0] == "1") { + // These commands copy all rows with the currentModelName to new rows with + // the newModelName replacing the currentModelName. + cmds.clear(); + cmds.push_back("CREATE TABLE " + temp_table + " AS SELECT * FROM " + table + + " WHERE ModelName=\"" + currentModelName.toStdString() + "\""); + cmds.push_back("UPDATE " + temp_table + " SET ModelName=\"" + newModelName.toStdString() + "\""); + cmds.push_back("INSERT INTO " + table + " SELECT * FROM " + temp_table); + cmds.push_back("DROP TABLE " + temp_table); + for (std::string cmd : cmds) { + std::string errorMsg = m_databasePtr->nmfUpdateDatabase(cmd); + if (nmfUtilsQt::isAnError(errorMsg)) { + m_logger->logMsg(nmfConstants::Error,"nmfSetup_Tab4::copyModelDataInAllTables: " + errorMsg); + m_logger->logMsg(nmfConstants::Error,"cmd: " + cmd); + } + } + } + } + progressDlg.setValue(NumTables); + + QString msg = (modelCopyAborted) ? "\nModel copy incomplete. Copy was aborted.\n" : "\nModel copy complete.\n"; + QMessageBox::information(Setup_Tabs, tr("Copy"), + tr(msg.toLatin1()), + QMessageBox::Ok); + + return true; +} +QString +nmfSetup_Tab4::getNewModelName() +{ bool ok; - QString ModelName = QInputDialog::getText(Setup_Tabs, - tr("New Model Name"), - tr("Enter New Model Name:"), - QLineEdit::Normal, "", &ok); - if (ok) { - if (ModelName.isEmpty()) { + bool ModelNameOK = false; + QString ModelName = ""; + + while (! ModelNameOK) { + ModelName = QInputDialog::getText(Setup_Tabs, + tr("New Model Name"), + tr("Enter New Model Name:"), + QLineEdit::Normal, "", &ok); + if (ok && ModelName.isEmpty()) { QMessageBox::warning(Setup_Tabs, tr("Illegal Name"), tr("\nModel Name cannot be blank.\n"), @@ -1207,143 +1294,152 @@ nmfSetup_Tab4::callback_NewModelPB() tr("\nModel Name already exists. Please select another name.\n"), QMessageBox::Ok); } else { - Setup_Tab4_ModelNameLE->setText(ModelName); - m_ModelName = ModelName.toStdString(); - - callback_SavePB(); - - populateNewModel(); + ModelNameOK = true; } } } - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::callback_NewModelPB end"); + return ModelName; } -bool -nmfSetup_Tab4::populateNewModel() +void +nmfSetup_Tab4::callback_ModelNewPB() { - // Find another model in same project - bool ok = false; - int RunLength; - int StartYear; - int NumSpeciesOrGuilds; - std::string queryStr; - std::string harvestTableName = ""; - std::string obsBiomassTableName; - std::string obsBiomassType; - std::string projectModel; - boost::numeric::ublas::matrix harvestData; - boost::numeric::ublas::matrix biomassData; - std::vector fields; - std::map > dataMap; - std::vector > allOtherProjectModelData; - std::pair projectModelToCopyDataFrom; - QStringList SpeciesList; - - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel start"); - - // Get Harvest table name - std::string HarvestForm = getHarvestFormCMB()->currentText().toStdString(); - if (HarvestForm == nmfConstantsMSSPM::HarvestCatch.toStdString()) { - harvestTableName = nmfConstantsMSSPM::TableHarvestCatch; - } else if (HarvestForm == nmfConstantsMSSPM::HarvestEffort.toStdString()) { - harvestTableName = nmfConstantsMSSPM::TableHarvestEffort; - } + m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::callback_ModelNewPB start"); - // Get data from other models in current project - fields = {"ProjectName","ModelName","HarvestForm","ObsBiomassType"}; - queryStr = "SELECT ProjectName,ModelName,HarvestForm,ObsBiomassType FROM " + - nmfConstantsMSSPM::TableModels + - " WHERE ProjectName = '" + m_ProjectName + - "' AND ModelName != '" + m_ModelName + "'"; - dataMap = m_databasePtr->nmfQueryDatabase(queryStr, fields); - int NumModels = (int)dataMap["ModelName"].size(); - if (NumModels == 0) { - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel No other models found in project"); - return false; - } else { - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel Found " + std::to_string(NumModels) + " other model(s) in project"); - m_logger->logMsg(nmfConstants::Normal,queryStr); + QString newModelName = getNewModelName(); + if (! newModelName.isEmpty()) { + setModelName(newModelName); + callback_SavePB(); } - for (int numModel=0; numModel 0) { - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel Start copy data from found model into current model"); - - projectModelToCopyDataFrom = allOtherProjectModelData[0]; - - // Get Harvest Data - if (! m_databasePtr->getSpecies(m_logger,NumSpeciesOrGuilds,SpeciesList)) { - return false; - } - if (! m_databasePtr->getRunLengthAndStartYear(m_logger,m_ProjectName,projectModelToCopyDataFrom.first, - RunLength,StartYear)) { - return false; - } - if (! m_databasePtr->getTimeSeriesData(Setup_Tabs,m_logger,m_ProjectName,projectModelToCopyDataFrom.first, - "",harvestTableName,NumSpeciesOrGuilds,RunLength,harvestData)) { - return false; - } - ok = replaceDuplicateData(harvestTableName,SpeciesList,harvestData); - if (! ok ) { - return false; - } - - // Copy Observation Data - obsBiomassTableName = "Biomass" + projectModelToCopyDataFrom.second; - if (! m_databasePtr->getTimeSeriesData(Setup_Tabs,m_logger,m_ProjectName,projectModelToCopyDataFrom.first, - "",obsBiomassTableName,NumSpeciesOrGuilds,RunLength,biomassData)) { - return false; - } - ok = replaceDuplicateData(obsBiomassTableName,SpeciesList,biomassData); - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel emitting ModelLoaded"); - if (ok) { - emit ModelLoaded(); - } - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel End copy data from found model into current model"); - } - m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel end"); - - return true; -} - -bool -nmfSetup_Tab4::replaceDuplicateData( - const std::string& tableName, - const QStringList& SpeciesList, - const boost::numeric::ublas::matrix& data) -{ - // Now put Biomass Data with new model name back into table (there might be an easier MySQL way to do this) - std::string cmd = "REPLACE INTO " + tableName + " (ProjectName,ModelName,SpeName,Year,Value) VALUES "; - for (int species=0; species<(int)data.size2(); ++species) { - for (int time=0; time<(int)data.size1(); ++time) { - cmd += "('" + m_ProjectName + - "','" + m_ModelName + - "','" + SpeciesList[species].toStdString() + - "'," + std::to_string(time) + - "," + std::to_string(data(time,species)) + "),"; - } - } - cmd = cmd.substr(0,cmd.size()-1); - std::string errorMsg = m_databasePtr->nmfUpdateDatabase(cmd); - if (nmfUtilsQt::isAnError(errorMsg)) { - m_logger->logMsg(nmfConstants::Error,"nmfSetup_Tab4::populateNewModel: Write table error: " + errorMsg); - m_logger->logMsg(nmfConstants::Error,"cmd: " + cmd); - return false; - } - return true; -} + m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::callback_ModelNewPB end"); +} + +//bool +//nmfSetup_Tab4::populateNewModel() +//{ +// // Find another model in same project +// bool ok = false; +// int RunLength; +// int StartYear; +// int NumSpeciesOrGuilds; +// std::string queryStr; +// std::string harvestTableName = ""; +// std::string obsBiomassTableName; +// std::string obsBiomassType; +// std::string projectModel; +// boost::numeric::ublas::matrix harvestData; +// boost::numeric::ublas::matrix biomassData; +// std::vector fields; +// std::map > dataMap; +// std::vector > allOtherProjectModelData; +// std::pair projectModelToCopyDataFrom; +// QStringList SpeciesList; + +// m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel start"); + +// // Get Harvest table name +// std::string HarvestForm = getHarvestFormCMB()->currentText().toStdString(); +// if (HarvestForm == nmfConstantsMSSPM::HarvestCatch.toStdString()) { +// harvestTableName = nmfConstantsMSSPM::TableHarvestCatch; +// } else if (HarvestForm == nmfConstantsMSSPM::HarvestEffort.toStdString()) { +// harvestTableName = nmfConstantsMSSPM::TableHarvestEffort; +// } + +// // Get data from other models in current project +// fields = {"ProjectName","ModelName","HarvestForm","ObsBiomassType"}; +// queryStr = "SELECT ProjectName,ModelName,HarvestForm,ObsBiomassType FROM " + +// nmfConstantsMSSPM::TableModels + +// " WHERE ProjectName = '" + m_ProjectName + +// "' AND ModelName != '" + m_ModelName + "'"; +// dataMap = m_databasePtr->nmfQueryDatabase(queryStr, fields); +// int NumModels = (int)dataMap["ModelName"].size(); +// if (NumModels == 0) { +// m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel No other models found in project"); +// return false; +// } else { +// m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel Found " + std::to_string(NumModels) + " other model(s) in project"); +// m_logger->logMsg(nmfConstants::Normal,queryStr); +// } +// for (int numModel=0; numModel 0) { +// m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel Start copy data from found model into current model"); + +// projectModelToCopyDataFrom = allOtherProjectModelData[0]; + +// // Get Harvest Data +// if (! m_databasePtr->getSpecies(m_logger,NumSpeciesOrGuilds,SpeciesList)) { +// return false; +// } +// if (! m_databasePtr->getRunLengthAndStartYear(m_logger,m_ProjectName,projectModelToCopyDataFrom.first, +// RunLength,StartYear)) { +// return false; +// } +// if (! m_databasePtr->getTimeSeriesData(Setup_Tabs,m_logger,m_ProjectName,projectModelToCopyDataFrom.first, +// "",harvestTableName,NumSpeciesOrGuilds,RunLength,harvestData)) { +// return false; +// } +// ok = replaceDuplicateData(harvestTableName,SpeciesList,harvestData); +// if (! ok ) { +// return false; +// } + +// // Copy Observation Data +// obsBiomassTableName = QString::fromStdString("biomass" + projectModelToCopyDataFrom.second).toLower().toStdString(); +// if (! m_databasePtr->getTimeSeriesData(Setup_Tabs,m_logger,m_ProjectName,projectModelToCopyDataFrom.first, +// "",obsBiomassTableName,NumSpeciesOrGuilds,RunLength,biomassData)) { +// return false; +// } +// ok = replaceDuplicateData(obsBiomassTableName,SpeciesList,biomassData); + +// m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel emitting ModelLoaded"); +// if (ok) { +// emit ModelLoaded(); +// } +// m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel End copy data from found model into current model"); +// } +// m_logger->logMsg(nmfConstants::Normal,"nmfSetup_Tab4::populateNewModel end"); + +// return true; +//} + +//bool +//nmfSetup_Tab4::replaceDuplicateData( +// const std::string& tableName, +// const QStringList& SpeciesList, +// const boost::numeric::ublas::matrix& data) +//{ +// // Now put Biomass Data with new model name back into table (there might be an easier MySQL way to do this) +// std::string cmd = "REPLACE INTO " + tableName + " (ProjectName,ModelName,SpeName,Year,Value) VALUES "; +// for (int species=0; species<(int)data.size2(); ++species) { +// for (int time=0; time<(int)data.size1(); ++time) { +// cmd += "('" + m_ProjectName + +// "','" + m_ModelName + +// "','" + SpeciesList[species].toStdString() + +// "'," + std::to_string(time) + +// "," + std::to_string(data(time,species)) + "),"; +// } +// } +// cmd = cmd.substr(0,cmd.size()-1); +// std::string errorMsg = m_databasePtr->nmfUpdateDatabase(cmd); +// if (nmfUtilsQt::isAnError(errorMsg)) { +// m_logger->logMsg(nmfConstants::Error,"nmfSetup_Tab4::populateNewModel: Write table error: " + errorMsg); +// m_logger->logMsg(nmfConstants::Error,"cmd: " + cmd); +// return false; +// } +// return true; +//} bool nmfSetup_Tab4::modelExists(QString ModelName) diff --git a/MSSPM_GuiSetup/nmfSetupTab04.h b/MSSPM_GuiSetup/nmfSetupTab04.h index b403529..2c727ae 100755 --- a/MSSPM_GuiSetup/nmfSetupTab04.h +++ b/MSSPM_GuiSetup/nmfSetupTab04.h @@ -84,42 +84,46 @@ class nmfSetup_Tab4: public QObject QWidget* Setup_Tab4_Widget; QPushButton* Setup_Tab4_SavePB; QPushButton* Setup_Tab4_LoadPB; - QPushButton* Setup_Tab4_DelPB; QPushButton* Setup_Tab4_NextPB; QPushButton* Setup_Tab4_PrevPB; - QPushButton* Setup_Tab4_NewModelPB; + QPushButton* Setup_Tab4_ModelNewPB; + QPushButton* Setup_Tab4_ModelCopyPB; + QPushButton* Setup_Tab4_ModelDelPB; QSpinBox* Setup_Tab4_NumberOfRunsSB; QSpinBox* Setup_Tab4_StartYearSB; QLineEdit* Setup_Tab4_EndYearLE; QSpinBox* Setup_Tab4_RunLengthSB; double calculateSystemCarryingCapacity(); + bool copyModelDataInAllTables(const QString& currentModelName, + const QString& newModelName); void getHighlightColors(QString& growthHighlightColor, - QString& harvestHighlightColor, - QString& competitionHighlightColor, - QString& predationHighlightColor, - QString& growthColorName, - QString& harvestColorName, - QString& competitionColorName, - QString& predationColorName); + QString& harvestHighlightColor, + QString& competitionHighlightColor, + QString& predationHighlightColor, + QString& growthColorName, + QString& harvestColorName, + QString& competitionColorName, + QString& predationColorName); + QString getNewModelName(); bool modelExists(QString ModelName); - bool saveModelData(bool verbose, - std::string CurrentSettingsName); + bool saveModelData(bool verbose,std::string CurrentSettingsName); void clearWidgets(); /** * @brief Populates new model data with data from other models in the project * @return True for success, false otherwise */ - bool populateNewModel(); +// bool populateNewModel(); void readSettings(); void setEstimatedParameterNames(); void setModelPreset(std::string modelName); void updateOutputWidget(); - bool replaceDuplicateData( - const std::string& tableName, - const QStringList& SpeciesList, - const boost::numeric::ublas::matrix& data); +// bool replaceDuplicateData( +// const std::string& tableName, +// const QStringList& SpeciesList, +// const boost::numeric::ublas::matrix& data); + public: /** * @brief nmfSetup_Tab4 : class constructor @@ -368,10 +372,6 @@ public Q_SLOTS: * @brief Callback invoked when the user clicks the Competition Form highlight button */ void callback_CompetitionHighlightPB(); - /** - * @brief Callback invoked when the user clicks the Delete Model button - */ - void callback_DelPB(); /** * @brief Callback invoked when the user changes the Model Equation text box font size * @param fontSize : new font size chosen by the user @@ -399,16 +399,24 @@ public Q_SLOTS: * @brief Callback invoked when the user clicks the Load Settings button */ void callback_LoadPB(); + /** + * @brief Callback invoked when the user clicks the Copy Model button + */ + void callback_ModelCopyPB(); + /** + * @brief Callback invoked when the user clicks the Delete Model button + */ + void callback_ModelDelPB(); + /** + * @brief Callback invoked when the user clicks the New Model button + */ + void callback_ModelNewPB(); /** * @brief Callback invoked when the user selects a Preset from the Model * Presets combo box * @param preset : name of Model Preset selected by the user */ void callback_ModelPresetsCMB(QString preset); - /** - * @brief Callback invoked when the user clicks the New Model button - */ - void callback_NewModelPB(); /** * @brief Callback invoked when the user clicks the Next Page button */ diff --git a/MSSPM_Main/forms/Setup/Setup_Tab04.ui b/MSSPM_Main/forms/Setup/Setup_Tab04.ui index 9758b8e..eaa4b28 100755 --- a/MSSPM_Main/forms/Setup/Setup_Tab04.ui +++ b/MSSPM_Main/forms/Setup/Setup_Tab04.ui @@ -160,7 +160,7 @@ - + 0 @@ -169,24 +169,24 @@ - 50 + 45 0 - 50 + 45 16777215 - Add new model to current project + Add a new model to the current project - Add new model to current project + Add a new model to the current project - <html><head/><body><p align="center"><span style=" font-weight:600;">New</span></p><p>This adds a new model to the current project. If the current project already has models associated with it, the data associated with those models, are copied to the new model. This assures that all models associated with a given project use the same data.</p></body></html> + <html><head/><body><p align="center"><span style=" font-weight:600;">New</span></p><p>This adds a new model to the current project. All tables must be populated by the user either entering the data manually or by importing CSV files.</p></body></html> New... @@ -194,7 +194,41 @@ - + + + + 0 + 0 + + + + + 45 + 0 + + + + + 45 + 16777215 + + + + Make a copy of the current model + + + Make a copy of the current model + + + <html><head/><body><p align="center"><span style=" font-weight:600;">Copy</span></p><p>This adds a new model to the current project and copies all of the data from the current model into the new model.</p></body></html> + + + Copy... + + + + + 0 diff --git a/MSSPM_Main/nmfMainWindow.cpp b/MSSPM_Main/nmfMainWindow.cpp index c24ad42..b5444c4 100755 --- a/MSSPM_Main/nmfMainWindow.cpp +++ b/MSSPM_Main/nmfMainWindow.cpp @@ -789,6 +789,8 @@ nmfMainWindow::getDiagnosticsData( DiagnosticsValue.clear(); DiagnosticsFitness.clear(); + nmfUtils::initialize(DiagnosticsValue, TotalNumPoints,NumSpeciesOrGuilds); + nmfUtils::initialize(DiagnosticsFitness,TotalNumPoints,NumSpeciesOrGuilds); // Load Diagnostics data fields = {"ProjectName","ModelName","Algorithm","Minimizer","ObjectiveCriterion","Scaling","isAggProd","SpeName","Value","Fitness"}; @@ -808,12 +810,17 @@ nmfMainWindow::getDiagnosticsData( errorMsg = "[Warning] getDiagnosticsData: No records found in table: " + TableName; // errorMsg += "\n" + queryStr; m_Logger->logMsg(nmfConstants::Warning,errorMsg); + for (int j=0; jgetSpecies(m_Logger,NumSpecies,SpeciesList)) { @@ -2072,7 +2079,7 @@ void nmfMainWindow::menu_about() { QString name = "Multi-Species Surplus Production Model"; - QString version = "MSSPM v1.3.4 "; + QString version = "MSSPM v1.3.5 "; QString specialAcknowledgement = ""; QString cppVersion = "C++??"; QString mysqlVersion = "?"; @@ -7662,17 +7669,15 @@ nmfMainWindow::showDiagnosticsChart2d(const QString& ScaleStr, Algorithm,Minimizer,ObjectiveCriterion,Scaling, isAggProdStr,DiagnosticsValue,DiagnosticsFitness)) { - Output_Controls_ptr->setOutputParameters2d3dPB(nmfConstantsMSSPM::ChartType2d); - m_ChartView2d->hide(); - msg = "No Diagnostic records found. Please make sure a Diagnostic has been run."; + //Output_Controls_ptr->setOutputParameters2d3dPB(nmfConstantsMSSPM::ChartType2d); + //m_ChartView2d->hide(); + msg = "No Diagnostic records found. Please make sure a Diagnostic has been run for all parameters."; m_Logger->logMsg(nmfConstants::Warning,msg.toStdString()); -// msg = "\nNo Diagnostic records found.\n\nPlease make sure a Diagnostic has been run.\n"; -// QMessageBox::warning(this, "Warning", msg, QMessageBox::Ok); - return false; + //return false; } showDiagnosticsFitnessVsParameter(NumPoints,ParameterName, - "Fitness Value", + "Fitness", NumSpeciesOrGuilds,OutputSpecies, SpeciesNum, DiagnosticsValue, @@ -8652,7 +8657,7 @@ nmfMainWindow::showDiagnosticsFitnessVsParameter( QStringList HoverLabels; ChartType = "Line"; - XLabel += " Percent Deviation"; + XLabel += " % Deviation"; MainTitle = YLabel + " vs " + XLabel + " for: " + OutputSpecies.toStdString(); // LineColors.push_back(QColor( 0,114,178)); // blue diff --git a/MSSPM_ParameterEstimationNLoptAlgorithm/NLopt_Estimator.cpp b/MSSPM_ParameterEstimationNLoptAlgorithm/NLopt_Estimator.cpp index d93af69..4f840eb 100755 --- a/MSSPM_ParameterEstimationNLoptAlgorithm/NLopt_Estimator.cpp +++ b/MSSPM_ParameterEstimationNLoptAlgorithm/NLopt_Estimator.cpp @@ -85,6 +85,12 @@ NLopt_Estimator::extractParameters(const nmfStructsQt::ModelDataStruct& NLoptDat std::vector& surveyQ, std::vector& surveyQCovariateCoeffs) { +//std::cout << "extractParameters: " << std::endl; +//for (int i=0; i<170; ++i) { +// if (i%10 == 0) {std::cout << std::endl;} +// std::cout << "EstParameters[" << i << "]: " << EstParameters[i] << std::endl; +//} + bool isLogistic = (NLoptDataStruct.GrowthForm == "Logistic"); bool isCatchability = (NLoptDataStruct.HarvestForm == nmfConstantsMSSPM::HarvestEffort.toStdString()) || (NLoptDataStruct.HarvestForm == nmfConstantsMSSPM::HarvestEffortFitToCatch.toStdString()); @@ -164,7 +170,7 @@ NLopt_Estimator::extractParameters(const nmfStructsQt::ModelDataStruct& NLoptDat offset += NumSpeciesOrGuilds; // 2* because of the Covariate Coeffs } - if (isCatchability) { + if (isCatchability) { for (int i=0; i