Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sim casal2 202206 #394

Merged
merged 3 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ TestModels/ORH3B/sim/*
Documentation/UserManual/CASAL2_Age.synctex.gz
Documentation/UserManual/CASAL2_Length.synctex.gz
*.txss
simCASAL2/simCASAL2.exe
35 changes: 22 additions & 13 deletions BuildSystem/buildtools/classes/ModelRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def start(self):
estimation_betadiff_dir_list = {"TwoSex", "SBW", "Simple","ComplexTag", "SexedLengthBased"} # requires a config-betadiff.csl2 file
estimation_gammadiff_dir_list = {"TwoSex", "SBW", "Simple", "SexedLengthBased"} # requires a config-gammadiff.csl2 file
estimation_adolc_dir_list = {"TwoSex", "SBW", "Simple", "SexedLengthBased"} # requires a config-adolc.csl2 file
simulate_dash_i_dir_list = {"ORH3B"} # if you change this you will need to formulate the report or python code below, not very general.
simulate_dash_i_dir_list = {"ORH3B", "SimAllObs"} # if you change this you will need to formulate the report or python code below, not very general.
run_dash_i_dir_list = {"Complex_input","TwoSex_input"}
resume_mcmc_from_mpd_dir_list = {"mcmc_start_mpd_mcmc_fixed","mcmc_start_mpd"}
resume_mcmc_dir_list = {"mcmc_resume"}
Expand Down Expand Up @@ -114,7 +114,7 @@ def start(self):
# test -s functionality
for folder in simulate_dash_i_dir_list:
os.chdir("../TestModels/" + folder)
# create sim directory casal will fail if this doesn't exist
# create sim directory casal will fail if this doesn't exist
if not os.path.exists("sim"):
os.mkdir("sim")
## first delete any previous simulated observations from previous model runners
Expand All @@ -131,17 +131,26 @@ def start(self):
elapsed = time.time() - start
print('[OK] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' seconds')
success_count += 1
# check the correct files were generated
if not os.path.exists("sim/CPUEandes.1_01"):
print('[FAILED] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' expected simulated file sim/CPUEandes.1_01')
fail_count += 1
if not os.path.exists("sim/CPUEandes.9_10"):
print('[FAILED] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' expected simulated file sim/CPUEandes.9_10')
fail_count += 1
if not os.path.exists("sim/Obs_Andes_LF.3_05"):
print('[FAILED] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' expected simulated file sim/Obs_Andes_LF.3_05')
fail_count += 1
os.chdir(cwd)
if folder == "ORH3B":
# check the correct files were generated
if not os.path.exists("sim/CPUEandes.1_01"):
print('[FAILED] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' expected simulated file sim/CPUEandes.1_01')
fail_count += 1
if not os.path.exists("sim/CPUEandes.9_10"):
print('[FAILED] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' expected simulated file sim/CPUEandes.9_10')
fail_count += 1
if not os.path.exists("sim/Obs_Andes_LF.3_05"):
print('[FAILED] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' expected simulated file sim/Obs_Andes_LF.3_05')
fail_count += 1
os.chdir(cwd)
else:
if not os.path.exists("sim/sim_all_obs.1_01"):
print('[FAILED] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' expected simulated file sim/sim_all_obs.1_01')
fail_count += 1
if not os.path.exists("sim/sim_all_obs.1_10"):
print('[FAILED] - ' + folder + ' -s run in ' + str(round(elapsed, 2)) + ' expected simulated file sim/sim_all_obs.1_10')
fail_count += 1
os.chdir(cwd)
# test -M mpd.log functionality
for folder in resume_mcmc_from_mpd_dir_list:
os.chdir("../TestModels/" + folder)
Expand Down
2 changes: 1 addition & 1 deletion CASAL2/source/License.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

const std::string license =
R"(
Casal2. Copyright (C) 2017 NIWA
Casal2. Copyright (C) 2022 NIWA

This program is free software: you can redistribute it and/or
modify it under the terms of version 2 of the GNU General
Expand Down
214 changes: 115 additions & 99 deletions CASAL2/source/Reports/Common/SimulatedObservation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,25 @@ SimulatedObservation::SimulatedObservation() {
* Validate object
*/
void SimulatedObservation::DoValidate(shared_ptr<Model> model) {
if (observation_label_ == "")
observation_label_ = label_;

}

/**
* Build method
*/
void SimulatedObservation::DoBuild(shared_ptr<Model> model) {
observation_ = model->managers()->observation()->GetObservation(observation_label_);
if (!observation_) {
#ifndef TESTMODE
LOG_WARNING() << "The report for " << PARAM_OBSERVATION << " with label '" << observation_label_ << "' was requested. This " << PARAM_OBSERVATION
<< " was not found in the input configuration file and the report will not be generated";
#endif
is_valid_ = false;

if (observation_label_ == "") {
// print them all in a single file
print_all_observations = true;
} else {
// if users supplied a label check it exists
Observation* observation = model->managers()->observation()->GetObservation(observation_label_);
if(!observation) {
LOG_WARNING() << "The report for " << PARAM_SIMULATED_OBSERVATION << " with label '" << observation_label_ << "' was requested. This " << PARAM_OBSERVATION
<< " was not found in the input configuration file and the report will not be generated";
is_valid_ = false;
}
}
}

Expand All @@ -57,124 +61,136 @@ void SimulatedObservation::DoBuild(shared_ptr<Model> model) {
void SimulatedObservation::DoExecute(shared_ptr<Model> model) {
if (!is_valid())
return;

cache_ << CONFIG_SECTION_SYMBOL << PARAM_OBSERVATION << " " << label_ << REPORT_EOL;
bool biomass_abundance_obs = false;
bool tag_recapture_obs = false;
bool tag_recapture_for_growth = false;

ParameterList& parameter_list = observation_->parameters();
const map<string, Parameter*>& parameters = parameter_list.parameters();
for (auto iter = parameters.begin(); iter != parameters.end(); ++iter) {
if (iter->first == PARAM_LIKELIHOOD) {
if (iter->second->values()[0] == PARAM_PSEUDO)
cache_ << PARAM_LIKELIHOOD << " " << parameter_list.Get(PARAM_SIMULATED_OBSERVATION)->values()[0] << REPORT_EOL;
else
cache_ << PARAM_LIKELIHOOD << " " << iter->second->values()[0] << REPORT_EOL;

continue;
observations::Manager& ObservationManager = *model->managers()->observation();

for(auto observation : ObservationManager.objects()) {
// only continue if we have found the observation that
// we are after. Note from CM: I went this route rather than using the manager->getobservation
// The code is nasty and I didn't want to duplicate it for the single case vs the multiple case
if(!print_all_observations) {
if(observation->label() != observation_label_)
continue;
}
// start printing out
cache_ << CONFIG_SECTION_SYMBOL << PARAM_OBSERVATION << " " << observation->label() << REPORT_EOL;
bool biomass_abundance_obs = false;
bool tag_recapture_obs = false;
bool tag_recapture_for_growth = false;

ParameterList& parameter_list = observation->parameters();
const map<string, Parameter*>& parameters = parameter_list.parameters();
for (auto iter = parameters.begin(); iter != parameters.end(); ++iter) {
if (iter->first == PARAM_LIKELIHOOD) {
if (iter->second->values()[0] == PARAM_PSEUDO)
cache_ << PARAM_LIKELIHOOD << " " << parameter_list.Get(PARAM_SIMULATED_OBSERVATION)->values()[0] << REPORT_EOL;
else
cache_ << PARAM_LIKELIHOOD << " " << iter->second->values()[0] << REPORT_EOL;

continue;
}

if (iter->first == PARAM_OBS || iter->first == PARAM_ERROR_VALUE || iter->first == PARAM_LABEL || iter->first == PARAM_SIMULATION_LIKELIHOOD)
continue;

if (iter->second->values().size() > 0) {
cache_ << iter->first << " ";
const vector<string>& values = iter->second->values();
for (string value : values) {
if (iter->first == PARAM_TYPE)
LOG_FINE() << "Type of observation simulating = " << value;
if ((iter->first == PARAM_TYPE && value == PARAM_BIOMASS) || (iter->first == PARAM_TYPE && value == PARAM_ABUNDANCE)) {
biomass_abundance_obs = true;
}
if ((iter->first == PARAM_TYPE && value == PARAM_TAG_RECAPTURE_BY_AGE) || (iter->first == PARAM_TYPE && value == PARAM_TAG_RECAPTURE_BY_LENGTH)) {
tag_recapture_obs = true;
}
if (iter->first == PARAM_TYPE && value == PARAM_TAG_RECAPTURE_BY_LENGTH_FOR_GROWTH) {
tag_recapture_for_growth = true;
if (iter->first == PARAM_OBS || iter->first == PARAM_ERROR_VALUE || iter->first == PARAM_LABEL || iter->first == PARAM_SIMULATION_LIKELIHOOD)
continue;

if (iter->second->values().size() > 0) {
cache_ << iter->first << " ";
const vector<string>& values = iter->second->values();
for (string value : values) {
if (iter->first == PARAM_TYPE)
LOG_FINE() << "Type of observation simulating = " << value;
if ((iter->first == PARAM_TYPE && value == PARAM_BIOMASS) || (iter->first == PARAM_TYPE && value == PARAM_ABUNDANCE)) {
biomass_abundance_obs = true;
}
if ((iter->first == PARAM_TYPE && value == PARAM_TAG_RECAPTURE_BY_AGE) || (iter->first == PARAM_TYPE && value == PARAM_TAG_RECAPTURE_BY_LENGTH)) {
tag_recapture_obs = true;
}
if (iter->first == PARAM_TYPE && value == PARAM_TAG_RECAPTURE_BY_LENGTH_FOR_GROWTH) {
tag_recapture_for_growth = true;
}
cache_ << value << " ";
}
cache_ << value << " ";
cache_ << REPORT_EOL;
}
cache_ << REPORT_EOL;
}
}

map<unsigned, vector<obs::Comparison> >& comparison = observation_->comparisons();
// Print Observations
map<unsigned, vector<obs::Comparison> >& comparison = observation->comparisons();
// Print Observations

if (biomass_abundance_obs) {
// biomass obs
cache_ << PARAM_TABLE << " " << PARAM_OBS << REPORT_EOL;
if (biomass_abundance_obs) {
// biomass obs
cache_ << PARAM_TABLE << " " << PARAM_OBS << REPORT_EOL;

for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second)
cache_ << comparison.observed_ << " " << comparison.error_value_;
cache_ << REPORT_EOL;
}
cache_ << PARAM_END_TABLE << REPORT_EOL;
} else if(tag_recapture_obs) {
// tag recapture
cache_ << PARAM_TABLE << " " << PARAM_RECAPTURED << REPORT_EOL;
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second) {
cache_ << comparison.observed_ << " ";
}
cache_ << REPORT_EOL;
}
cache_ << PARAM_END_TABLE << REPORT_EOL;
} else if(tag_recapture_for_growth) {
// tag recapture for growth
cache_ << PARAM_TABLE << " " << PARAM_RECAPTURED << REPORT_EOL;
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second) {
cache_ << comparison.observed_ << " ";
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second)
cache_ << comparison.observed_ << " " << comparison.error_value_;
cache_ << REPORT_EOL;
}
cache_ << REPORT_EOL;
}
cache_ << PARAM_END_TABLE << REPORT_EOL;
} else {
// proportion at age obs
cache_ << PARAM_TABLE << " " << PARAM_OBS << REPORT_EOL;
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second) {
cache_ << comparison.observed_ << " ";
cache_ << PARAM_END_TABLE << REPORT_EOL;
} else if(tag_recapture_obs) {
// tag recapture
cache_ << PARAM_TABLE << " " << PARAM_RECAPTURED << REPORT_EOL;
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second) {
cache_ << comparison.observed_ << " ";
}
cache_ << REPORT_EOL;
}
cache_ << REPORT_EOL;
}
cache_ << PARAM_END_TABLE << REPORT_EOL;
}

// if not biomass or tag growth print error values
if (!biomass_abundance_obs && !tag_recapture_for_growth) {
if(tag_recapture_obs) {
// tag recpat by length or age
cache_ << PARAM_TABLE << " " << PARAM_SCANNED << REPORT_EOL;
cache_ << PARAM_END_TABLE << REPORT_EOL;
} else if(tag_recapture_for_growth) {
// tag recapture for growth
cache_ << PARAM_TABLE << " " << PARAM_RECAPTURED << REPORT_EOL;
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second) {
cache_ << comparison.error_value_ << " ";
cache_ << comparison.observed_ << " ";
}
cache_ << REPORT_EOL;
}
cache_ << PARAM_END_TABLE << REPORT_EOL;
} else {
// proportion at age obs
cache_ << PARAM_TABLE << " " << PARAM_ERROR_VALUES << REPORT_EOL;
cache_ << PARAM_TABLE << " " << PARAM_OBS << REPORT_EOL;
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second) {
cache_ << comparison.error_value_ << " ";
cache_ << comparison.observed_ << " ";
}
cache_ << REPORT_EOL;
}
cache_ << PARAM_END_TABLE << REPORT_EOL;
}

// if not biomass or tag growth print error values
if (!biomass_abundance_obs && !tag_recapture_for_growth) {
if(tag_recapture_obs) {
// tag recpat by length or age
cache_ << PARAM_TABLE << " " << PARAM_SCANNED << REPORT_EOL;
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second) {
cache_ << comparison.error_value_ << " ";
}
cache_ << REPORT_EOL;
}
cache_ << PARAM_END_TABLE << REPORT_EOL;
} else {
// proportion at age obs
cache_ << PARAM_TABLE << " " << PARAM_ERROR_VALUES << REPORT_EOL;
for (auto iter = comparison.begin(); iter != comparison.end(); ++iter) {
cache_ << iter->first << " ";
for (obs::Comparison comparison : iter->second) {
cache_ << comparison.error_value_ << " ";
}
cache_ << REPORT_EOL;
}
cache_ << PARAM_END_TABLE << REPORT_EOL;
}
}
cache_ << REPORT_EOL;
cache_ << REPORT_EOL;
}
cache_ << REPORT_EOL;
ready_for_writing_ = true;
}

Expand Down
2 changes: 1 addition & 1 deletion CASAL2/source/Reports/Common/SimulatedObservation.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SimulatedObservation : public Report {
private:
// members
string observation_label_ = "";
Observation* observation_ = nullptr;
bool print_all_observations = false;
};

} /* namespace reports */
Expand Down
1 change: 0 additions & 1 deletion TestModels/ORH3B/Population.csl2
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ age_lengths age_len_label*2

@initialisation_phase Equilibrium_phase
type Derived
casal_initialisation_switch true


# CASAL order of processes in each time step:
Expand Down
Loading