diff --git a/inst/include/common/information.hpp b/inst/include/common/information.hpp index fe407c21d..92a1adbf9 100644 --- a/inst/include/common/information.hpp +++ b/inst/include/common/information.hpp @@ -225,82 +225,12 @@ class Information { << " fleets." << std::endl; for (fleet_iterator it = this->fleets.begin(); it != this->fleets.end(); ++it) { - // Initialize fleet object std::shared_ptr > f = (*it).second; INFO_LOG << "Initializing fleet " << f->id << "." << std::endl; f->Initialize(f->nyears, f->nages); - INFO_LOG << "Expecting to import " << this->data_objects.size() - << " data objects." << std::endl; - - INFO_LOG << "Checking for available fleet index data objects." - << std::endl; - // set index data - if (f->fleet_observed_index_data_id_m != -999) { - uint32_t observed_index_id = - static_cast(f->fleet_observed_index_data_id_m); - data_iterator it = this->data_objects.find(observed_index_id); - INFO_LOG << "Input fleet index id = " << observed_index_id << "." - << std::endl; - - if (it != this->data_objects.end()) { - f->observed_index_data = (*it).second; - INFO_LOG << "Index data successfully set." << std::endl; - DATA_LOG << "" << std::endl; - DATA_LOG << "Observed input for fleet " << f->id << ", index " - << observed_index_id << ": \n " - << f->observed_index_data->at(1) << std::endl; - } else { - valid_model = false; - ERROR_LOG << "Error: Expected data observations not defined for fleet" - << f->id << ", index " << observed_index_id << std::endl; - exit(1); - } - - } else { - valid_model = false; - ERROR_LOG << "Error: No index data observed for fleet " << f->id - << ". FIMS requires index data for all fleets." << std::endl; - exit(1); - } - // end set index data - - INFO_LOG << "Checking for available fleet age comp data objects." - << std::endl; - // set age composition data - if (f->fleet_observed_agecomp_data_id_m != -999) { - uint32_t observed_agecomp_id = - static_cast(f->fleet_observed_agecomp_data_id_m); - data_iterator it = this->data_objects.find(observed_agecomp_id); - INFO_LOG << "Input fleet age comp id = " << observed_agecomp_id << "." - << std::endl; - - if (it != this->data_objects.end()) { - f->observed_agecomp_data = (*it).second; - INFO_LOG << "Age comp data successfully set." << std::endl; - DATA_LOG << "" << std::endl; - DATA_LOG << "Observed input age comp for fleet " << f->id << ", comp " - << observed_agecomp_id << ": \n " - << f->observed_agecomp_data->at(1) << std::endl; - } else { - valid_model = false; - ERROR_LOG << "Error: Expected age comp data observations not defined " - "for fleet " - << f->id << ", index " << observed_agecomp_id << std::endl; - exit(1); - } - - } else { - valid_model = false; - ERROR_LOG << "Error: No age comp data observed for fleet " << f->id - << ". FIMS requires age comp data for all fleets." - << std::endl; - exit(1); - } - // end set composition data - INFO_LOG << "Checking for available fleet selectivity pattern." << std::endl; // set selectivity model @@ -334,6 +264,37 @@ class Information { } // end set selectivity } + INFO_LOG << "Expecting to import " << this->data_objects.size() + << " data objects." << std::endl; + for(density_components_iterator it = this->density_components.begin(); + it!= this->density_components.end(); ++it){ + std::shared_ptr > d = (*it).second; + INFO_LOG << "Checking for available density components data objects." + << std::endl; + //set data objects + if(d->input_type == "data" & d->observed_data_id_m != -999){ + uint32_t observed_data_id = static_cast(d->observed_data_id_m); + data_iterator it = this->data_objects.find(observed_data_id); + INFO_LOG << "Input data id = " << observed_data_id << "." << std::endl; + + if (it != this->data_objects.end()) { + d->observed_values = (*it).second; + INFO_LOG << "Data for density component, " << d->id << " successfully set." << std::endl; + DATA_LOG << "" << std::endl; + } else { + valid_model = false; + ERROR_LOG << "Error: Expected data observations not defined for density component " + << d->id << ", observed data " << observed_data_id << std::endl; + exit(1); + } + + } else { + valid_model = false; + ERROR_LOG << "Error: No data input for density " << d->id << std::endl; + exit(1); + } + // end set data + } INFO_LOG << "Initializing population objects for " << this->populations.size() << " populations." << std::endl; diff --git a/inst/include/distributions/functors/density_components_base.hpp b/inst/include/distributions/functors/density_components_base.hpp index eb9e5d453..60a8b944c 100644 --- a/inst/include/distributions/functors/density_components_base.hpp +++ b/inst/include/distributions/functors/density_components_base.hpp @@ -35,6 +35,7 @@ struct DensityComponentBase : public fims_model_object::FIMSObject { // Assigning each one its own ID is a way to keep track of // all the instances of the DensityComponentBase class. static uint32_t id_g; /**< global unique identifier for distribution modules */ + size_t observed_data_id_m = -999; /*!< id of observed data component*/ std::shared_ptr> observed_values; /**< observed data*/ fims::Vector x; /**< input value of distribution function for proirs or random effects*/ @@ -44,7 +45,7 @@ struct DensityComponentBase : public fims_model_object::FIMSObject { bool osa_flag = false; /**< Boolean; if true, osa residuals are calculated */ bool simulate_flag = false; /**< Boolean; if true, data are simulated from the distribution */ std::vector key; /**< unique id for variable map that points to a fims::Vector */ - Type na_value = -999; /**< specifying the NA value >*/ + #ifdef TMB_MODEL ::objective_function *of; /**< Pointer to the TMB objective function */ #endif diff --git a/inst/include/interface/rcpp/rcpp_interface.hpp b/inst/include/interface/rcpp/rcpp_interface.hpp index 5661e2689..65ffd408c 100644 --- a/inst/include/interface/rcpp/rcpp_interface.hpp +++ b/inst/include/interface/rcpp/rcpp_interface.hpp @@ -385,8 +385,6 @@ RCPP_MODULE(fims) { .field("nyears", &FleetInterface::nyears) .field("estimate_q", &FleetInterface::estimate_q) .field("random_q", &FleetInterface::random_q) - .method("SetObservedAgeCompData", &FleetInterface::SetObservedAgeCompData) - .method("SetObservedIndexData", &FleetInterface::SetObservedIndexData) .method("SetSelectivity", &FleetInterface::SetSelectivity); Rcpp::class_("AgeComp") @@ -421,6 +419,8 @@ RCPP_MODULE(fims) { .constructor() .method("get_id", &DnormDistributionsInterface::get_id) .method("evaluate", &DnormDistributionsInterface::evaluate) + .method("set_observed_data", &DnormDistributionsInterface::set_observed_data) + .method("set_distribution_links", &DnormDistributionsInterface::set_distribution_links) .field("x", &DnormDistributionsInterface::x) .field("expected_values", &DnormDistributionsInterface::expected_values) .field("log_sd", &DnormDistributionsInterface::log_sd); @@ -462,6 +462,8 @@ RCPP_MODULE(fims) { .constructor() .method("get_id", &DlnormDistributionsInterface::get_id) .method("evaluate", &DlnormDistributionsInterface::evaluate) + .method("set_observed_data", &DlnormDistributionsInterface::set_observed_data) + .method("set_distribution_links", &DlnormDistributionsInterface::set_distribution_links) .field("input_type", &DlnormDistributionsInterface::input_type) .field("x", &DlnormDistributionsInterface::x) .field("expected_values", &DlnormDistributionsInterface::expected_values) @@ -471,6 +473,8 @@ RCPP_MODULE(fims) { .constructor() .method("evaluate", &DmultinomDistributionsInterface::evaluate) .method("get_id", &DmultinomDistributionsInterface::get_id) + .method("set_observed_data", &DmultinomDistributionsInterface::set_observed_data) + .method("set_distribution_links", &DmultinomDistributionsInterface::set_distribution_links) .field("x", &DmultinomDistributionsInterface::x) .field("expected_values", &DmultinomDistributionsInterface::expected_values) .field("dims", &DmultinomDistributionsInterface::dims); diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp index d196082b0..0d667925d 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp @@ -51,10 +51,6 @@ std::map FleetInterfaceBase::live_objects; * */ class FleetInterface : public FleetInterfaceBase { - int interface_observed_agecomp_data_id_m = - -999; /**< id of observed agecomp data object*/ - int interface_observed_index_data_id_m = - -999; /**< id of observed index data object*/ int interface_selectivity_id_m = -999; /**< id of selectivity component*/ public: @@ -77,25 +73,6 @@ class FleetInterface : public FleetInterfaceBase { /** @brief returns the id for the fleet interface */ virtual uint32_t get_id() { return this->id; } - /** - * @brief Set the unique id for the Observed Age Comp Data object - * - * @param observed_agecomp_data_id Unique id for the Observed Age Comp Data - * object - */ - void SetObservedAgeCompData(int observed_agecomp_data_id) { - interface_observed_agecomp_data_id_m = observed_agecomp_data_id; - } - - /** - * @brief Set the unique id for the Observed Index Data object - * - * @param observed_index_data_id Unique id for the Observed Index Data object - */ - void SetObservedIndexData(int observed_index_data_id) { - interface_observed_index_data_id_m = observed_index_data_id; - } - /** * @brief Set the unique id for the Selectivity object * @@ -119,11 +96,6 @@ class FleetInterface : public FleetInterfaceBase { fleet->is_survey = this->is_survey; fleet->nages = this->nages; fleet->nyears = this->nyears; - fleet->fleet_agecomp_likelihood_id_m = interface_agecomp_likelihood_id_m; - fleet->fleet_index_likelihood_id_m = interface_index_likelihood_id_m; - fleet->fleet_observed_agecomp_data_id_m = - interface_observed_agecomp_data_id_m; - fleet->fleet_observed_index_data_id_m = interface_observed_index_data_id_m; fleet->fleet_selectivity_id_m = interface_selectivity_id_m; fleet->log_q = this->log_q; diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp index e02180d0e..ef77b169b 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp @@ -28,6 +28,8 @@ class DistributionsInterfaceBase : public FIMSRcppInterfaceBase { static std::map live_objects; /**< map relating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase objects */ + uint32_t interface_observed_data_id_m = + -999; /**< id of observed data object*/ DistributionsInterfaceBase() { this->id_m = DistributionsInterfaceBase::id_g++; @@ -43,8 +45,11 @@ DistributionsInterfaceBase objects */ */ virtual uint32_t get_id() = 0; - /** +/** * @brief set_distribution_links sets pointers for data observations, random effects, or priors + * + * @param input_type String that sets whether the distribution type is: priors, random_effects, or data. + * @param ids Vector of unique ids for each linked parameter/s, derived value/s, or observed data vector */ void set_distribution_links(std::string input_type, Rcpp::IntegerVector ids){ this->input_type_m = input_type; @@ -54,6 +59,17 @@ DistributionsInterfaceBase objects */ } } + + /** + * @brief Set the unique id for the Observed Data object + * + * @param observed_data_id Unique id for the Observed Age Comp Data + * object + */ + void set_observed_data(int observed_data_id) { + interface_observed_data_id_m = observed_data_id; + } + /** @brief evaluate method for child distribution interface objects to inherit */ virtual double evaluate() = 0; @@ -93,8 +109,6 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { * @tparam T * @return log pdf */ - - virtual double evaluate() { fims_distributions::NormalLPDF dnorm; dnorm.x.resize(this->x.size()); @@ -112,6 +126,8 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { return dnorm.evaluate(); } + + #ifdef TMB_MODEL template @@ -123,6 +139,10 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { std::make_shared>(); // interface to data/parameter value + + distribution->observed_data_id_m = + interface_observed_data_id_m; + distribution->id = this->id_m; distribution->x.resize(this->x.size()); for(int i=0; ix.size(); i++){ @@ -180,6 +200,7 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { */ virtual uint32_t get_id() { return this->id_m; } + /** * @brief Evaluate lognormal probability density function, default returns the * log of the pdf @@ -217,6 +238,8 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { // set relative info distribution->id = this->id_m; + distribution->observed_data_id_m = + interface_observed_data_id_m; distribution->input_type = this->input_type; distribution->x.resize(this->x.size()); for(int i=0; ix.size(); i++){ @@ -273,6 +296,7 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase { virtual uint32_t get_id() { return this->id_m; } + /** * @brief Evaluate multinom probability density function, default returns the * log of the pdf @@ -308,6 +332,8 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase { std::make_shared>(); distribution->id = this->id_m; + distribution->observed_data_id_m = + interface_observed_data_id_m; distribution->x.resize(this->x.size()); for(int i=0; ix.size(); i++){ distribution->x[i] = this->x[i].value_m; diff --git a/inst/include/population_dynamics/fleet/fleet.hpp b/inst/include/population_dynamics/fleet/fleet.hpp index 897cf7d6a..3087e9c65 100644 --- a/inst/include/population_dynamics/fleet/fleet.hpp +++ b/inst/include/population_dynamics/fleet/fleet.hpp @@ -33,14 +33,6 @@ struct Fleet : public fims_model_object::FIMSObject { std::shared_ptr> selectivity; /*!< selectivity component*/ - int fleet_observed_index_data_id_m = -999; /*!< id of index data */ - std::shared_ptr> - observed_index_data; /*!< observed index data*/ - - int fleet_observed_agecomp_data_id_m = -999; /*!< id of age comp data */ - std::shared_ptr> - observed_agecomp_data; /*!< observed agecomp data*/ - // Mortality and catchability fims::Vector log_Fmort; /*!< estimated parameter: log Fishing mortality*/