Skip to content

Commit

Permalink
407 updating log files (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristineStawitz-NOAA authored Nov 2, 2023
2 parents 6dc7754 + 6258392 commit d21d033
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 199 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/run-googletest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
# Use windows-2019 instead of windows-latest to avoid using Mingw-w64
os: [ubuntu-latest, macos-latest, windows-2019]
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

Expand Down
16 changes: 15 additions & 1 deletion inst/include/common/def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@
#include <memory>
#include <vector>

std::ofstream FIMS_LOG("fims.log"); /**< Log file */
//The following rows initialize default log files for outputing model progress
//comments used to assist in diagnosing model issues and tracking progress. These
//files will only be created if a logs folder is added to the root model directory.
std::ofstream FIMS_LOG ("logs/fims.log"); /**< Generic log file */
std::ofstream INFO_LOG("logs/info.log"); /**< Information.hpp log file */
std::ofstream ERROR_LOG("logs/error.log"); /**< Error tracking log file */
std::ofstream DATA_LOG("logs/data.log"); /**< Data input tracking log file */
std::ofstream MODEL_LOG("logs/model.log"); /**< Model.hpp log file */
std::ofstream FLEET_LOG("logs/fleet.log"); /**< Fleet module log file */
std::ofstream POPULATION_LOG("logs/population.log"); /**< Populations module log file */
std::ofstream RECRUITMENT_LOG("logs/recruitment.log"); /**< Recruitment module log file */
std::ofstream GROWTH_LOG("logs/growth.log"); /**< Growth module log file */
std::ofstream MATURITY_LOG("logs/maturity.log"); /**< Maturity module log file */
std::ofstream SELECTIVITY_LOG("logs/selectivity.log"); /**< Selectivity module log file */
std::ofstream DEBUG_LOG("logs/debug/debug.log"); /**< Development debugging log file */

#ifdef TMB_MODEL
// simplify access to singletons
Expand Down
5 changes: 3 additions & 2 deletions inst/include/common/fims_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ inline const Type logit(const Type &a, const Type &b, const Type &x) {
*/
template <class Type>
inline const Type inv_logit(const Type &a, const Type &b, const Type &logit_x) {
return a + (b - a) / (1 + fims_math::exp(-logit_x));
return a + (b - a) / (1.0 + fims_math::exp(-logit_x));
}

/**
Expand Down Expand Up @@ -194,9 +194,10 @@ const Type ad_fabs(const Type &x, Type C = 1e-5) {
* @param C default = 1e-5
* @return
*/

template <typename Type>
inline const Type ad_min(const Type &a, const Type &b, Type C = 1e-5) {
return (a + b - fims_math::ad_fabs(a - b, C)) * .5;
return (a + b - fims_math::ad_fabs(a - b, C)) * 0.5;
}

/**
Expand Down
232 changes: 112 additions & 120 deletions inst/include/common/information.hpp

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions inst/include/common/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,18 @@ class Model { // may need singleton

// Loop over populations, evaluate, and sum up the recruitment likelihood
// component


typename fims_info::Information<Type>::population_iterator it;
MODEL_LOG << "Evaluating expected values and summing recruitment nlls for " << this->fims_information->populations.size() << " populations." << std::endl;

for (it = this->fims_information->populations.begin();
it != this->fims_information->populations.end(); ++it) {
//(*it).second points to each individual Population module
FIMS_LOG << "inside pop loop" << std::endl;
//(*it).second points to the Population module
MODEL_LOG << "Setting up pointer to population " << (*it).second->id << "." << std::endl;
// Prepare recruitment
(*it).second->recruitment->Prepare();
FIMS_LOG << "recruitment prepare works" << std::endl;
MODEL_LOG << "Recruitment for population successfully prepared" << std::endl;
// link to TMB objective function
#ifdef TMB_MODEL
(*it).second->of = this->of;
Expand All @@ -106,21 +110,28 @@ class Model { // may need singleton
(*it).second->Evaluate();
// Recrtuiment negative log-likelihood
rec_nll += (*it).second->recruitment->evaluate_nll();
FIMS_LOG << "rec nll: " << rec_nll << std::endl;
MODEL_LOG << "Recruitment negative log-likelihood is: " << rec_nll << std::endl;
}
MODEL_LOG << "All populations successfully evaluated." << std::endl;

// Loop over fleets/surveys, and sum up age comp and index nlls

typename fims_info::Information<Type>::fleet_iterator jt;
MODEL_LOG << "Evaluating expected values and summing nlls for " << this->fims_information->fleets.size() << " fleets." << std::endl;

for (jt = this->fims_information->fleets.begin();
jt != this->fims_information->fleets.end(); ++jt) {
//(*jt).second points to each individual Fleet module
#ifdef TMB_MODEL
(*jt).second->of = this->of;
#endif
MODEL_LOG << "Setting up pointer to fleet " << (*jt).second->id << "." << std::endl;
age_comp_nll += (*jt).second->evaluate_age_comp_nll();
MODEL_LOG << "Sum of survey and age comp negative log-likelihood is: " << age_comp_nll
<< std::endl;
index_nll += (*jt).second->evaluate_index_nll();
}

MODEL_LOG << "All fleets successfully evaluated." << std::endl;
// Loop over populations and fleets/surveys and fill in reporting

// initiate population index for structuring report out objects
Expand Down
2 changes: 1 addition & 1 deletion inst/include/distributions/functors/tmb_distributions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct Dlnorm : public DistributionsBase<Type> {
/**
* @brief Probability density function of the lognormal distribution.
*
* \f[ \frac{1.0}{ xsd\sqrt{2\pi} }exp(-\frac{(ln(x) - mean)^{2}}{2sd^{2}})
* \f[ \frac{1.0}{ xsd\sqrt{2.0\pi} }exp(-\frac{(ln(x) - mean)^{2.0}}{2.0sd^{2.0}})
* \f]
*
* @param do_log Boolean; if true, log densities are returned
Expand Down
161 changes: 161 additions & 0 deletions inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,154 @@ Rcpp::NumericVector get_random_parameters_vector() {
return p;
}


/**
* Clears the contents of info log file.
*/
void clear_info_log() {
//First flush the output stream to make sure nothing
//is left in the stream memory bufffer.
INFO_LOG.flush();

//Next an new stream is opened and closed to
//overwrite the file.
std::ofstream CLEAR_LOG("logs/info.log");
CLEAR_LOG.close();

//Finally the stream output location is reset back to the start
//of the file.
INFO_LOG.seekp(0);
}

/**
* Clears the contents of fims log file.
*/
void clear_fims_log() {
FIMS_LOG.flush();
std::ofstream CLEAR_LOG("logs/fims.log");
CLEAR_LOG.close();
FIMS_LOG.seekp(0);
}

/**
* Clears the contents of data log file.
*/
void clear_data_log() {
DATA_LOG.flush();
std::ofstream CLEAR_LOG("logs/data.log");
CLEAR_LOG.close();
DATA_LOG.seekp(0);
}

/**
* Clears the contents of error log file.
*/
void clear_error_log() {
ERROR_LOG.flush();
std::ofstream CLEAR_LOG("logs/error.log");
CLEAR_LOG.close();
ERROR_LOG.seekp(0);
}

/**
* Clears the contents of model log file.
*/
void clear_model_log() {
MODEL_LOG.flush();
std::ofstream CLEAR_LOG("logs/model.log");
CLEAR_LOG.close();
MODEL_LOG.seekp(0);
}

/**
* Clears the contents of fleet log file.
*/
void clear_fleet_log() {
FLEET_LOG.flush();
std::ofstream CLEAR_LOG("logs/fleet.log");
CLEAR_LOG.close();
FLEET_LOG.seekp(0);
}

/**
* Clears the contents of population log file.
*/
void clear_population_log() {
POPULATION_LOG.flush();
std::ofstream CLEAR_LOG("logs/population.log");
CLEAR_LOG.close();
POPULATION_LOG.seekp(0);
}

/**
* Clears the contents of maturity log file.
*/
void clear_maturity_log() {
MATURITY_LOG.flush();
std::ofstream CLEAR_LOG("logs/maturity.log");
CLEAR_LOG.close();
MATURITY_LOG.seekp(0);
}

/**
* Clears the contents of recruitment log file.
*/
void clear_recruitment_log() {
RECRUITMENT_LOG.flush();
std::ofstream CLEAR_LOG("logs/recruitment.log");
CLEAR_LOG.close();
RECRUITMENT_LOG.seekp(0);
}

/**
* Clears the contents of growth log file.
*/
void clear_growth_log() {
GROWTH_LOG.flush();
std::ofstream CLEAR_LOG("logs/growth.log");
CLEAR_LOG.close();
GROWTH_LOG.seekp(0);
}

/**
* Clears the contents of selectivity log file.
*/
void clear_selectivity_log() {
SELECTIVITY_LOG.flush();
std::ofstream CLEAR_LOG("logs/selectivity.log");
CLEAR_LOG.close();
SELECTIVITY_LOG.seekp(0);
}

/**
* Clears the contents of debug log file.
*/
void clear_debug_log() {
DEBUG_LOG.flush();
std::ofstream CLEAR_LOG("logs/debug/debug.log");
CLEAR_LOG.close();
DEBUG_LOG.seekp(0);
}


/**
* Clears the contents of log files.
*/
void clear_logs() {
clear_fims_log();
clear_info_log();
clear_data_log();
clear_error_log();
clear_model_log();
clear_fleet_log();
clear_population_log();
clear_recruitment_log();
clear_growth_log();
clear_maturity_log();
clear_selectivity_log();
clear_debug_log();
}

template <typename Type>
void clear_internal() {
std::shared_ptr<fims_info::Information<Type>> d0 =
Expand Down Expand Up @@ -176,6 +324,19 @@ RCPP_MODULE(fims) {
Rcpp::function("get_fixed", &get_fixed_parameters_vector);
Rcpp::function("get_random", &get_random_parameters_vector);
Rcpp::function("clear", clear);
Rcpp::function("clear_logs", clear_logs);
Rcpp::function("clear_fims_log", clear_fims_log);
Rcpp::function("clear_info_log", clear_info_log);
Rcpp::function("clear_error_log", clear_error_log);
Rcpp::function("clear_data_log", clear_data_log);
Rcpp::function("clear_population_log", clear_population_log);
Rcpp::function("clear_model_log", clear_model_log);
Rcpp::function("clear_recruitment_log", clear_recruitment_log);
Rcpp::function("clear_fleet_log", clear_fleet_log);
Rcpp::function("clear_growth_log", clear_growth_log);
Rcpp::function("clear_maturity_log", clear_maturity_log);
Rcpp::function("clear_selectivity_log", clear_selectivity_log);
Rcpp::function("clear_debug_log", clear_debug_log);

Rcpp::class_<Parameter>("Parameter")
.constructor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class BevertonHoltRecruitmentInterface : public RecruitmentInterfaceBase {
for (int i = 0; i < deviations.size(); i++) {
NLL.recruit_deviations[i] = deviations[i];
}
FIMS_LOG << "Rec devs being passed to C++ are " << deviations << std::endl;
RECRUITMENT_LOG << "Rec devs being passed to C++ are " << deviations << std::endl;
NLL.estimate_recruit_deviations = this->estimate_deviations;
return NLL.evaluate_nll();
}
Expand Down
33 changes: 20 additions & 13 deletions inst/include/population_dynamics/fleet/fleet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
0); /*!<model expected weight at age*/
this->q = fims_math::exp(this->log_q);
for (size_t year = 0; year < this->nyears; year++) {
FIMS_LOG << "input F mort " << this->log_Fmort[year] << std::endl;
FIMS_LOG << "input q " << this->log_q << std::endl;

FLEET_LOG << "input F mort " << this->log_Fmort[year] << std::endl;
FLEET_LOG << "input q " << this->log_q << std::endl;
this->Fmort[year] = fims_math::exp(this->log_Fmort[year]);

}
}

Expand All @@ -153,10 +155,12 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
size_t dims = this->observed_agecomp_data->get_imax() *
this->observed_agecomp_data->get_jmax();
if (dims != this->catch_numbers_at_age.size()) {
fims::fims_log::get("fleet.log")
<< "Error: observed age comp is of size " << dims
<< " and expected is of size " << this->age_composition.size()
<< std::endl;

ERROR_LOG << "Error: observed age comp is of size "
<< dims << " and expected is of size "
<< this->age_composition.size() << std::endl;
exit(1);

} else {
for (size_t y = 0; y < this->nyears; y++) {
// EigenVector declares a vector type from the Eigen library, which is
Expand All @@ -179,8 +183,8 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
sum; // probabilities for ages

observed_acomp[a] = this->observed_agecomp_data->at(y, a);
fims::fims_log::get("fleet.log")
<< " age " << a << " in year " << y

FLEET_LOG << " age " << a << " in year " << y
<< "has expected: " << expected_acomp[a]
<< " and observed: " << observed_acomp[a] << std::endl;
}
Expand All @@ -189,7 +193,7 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
nll -= dmultinom.evaluate(true);
}
}

FLEET_LOG << "Age comp negative log-likelihood for fleet," << this->id << nll << std::endl;
#endif
return nll;
}
Expand All @@ -204,14 +208,17 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
dnorm.x = fims_math::log(this->observed_index_data->at(i));
dnorm.mean = fims_math::log(this->expected_index[i]);
nll -= dnorm.evaluate(true);
fims::fims_log::get("fleet.log")
<< "observed likelihood component: " << i << " is "

FLEET_LOG
<< "observed index data: " << i << " is "
<< this->observed_index_data->at(i)
<< " and expected is: " << this->expected_index[i] << std::endl;
}
fims::fims_log::get("fleet.log")
FLEET_LOG
<< " log obs error is: " << this->log_obs_error << std::endl;
fims::fims_log::get("fleet.log") << " sd is: " << dnorm.sd << std::endl;
FLEET_LOG << " sd is: " << dnorm.sd << std::endl;
FLEET_LOG << " index nll: " << nll << std::endl;

#endif
return nll;
}
Expand Down
Loading

0 comments on commit d21d033

Please sign in to comment.