Skip to content

Commit

Permalink
refactor data objects to be set from distributions interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Havron-NOAA committed Aug 2, 2024
1 parent 58c6bc2 commit d5444fd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 112 deletions.
101 changes: 31 additions & 70 deletions inst/include/common/information.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<fims_popdy::Fleet<Type> > 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<uint32_t>(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<uint32_t>(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
Expand Down Expand Up @@ -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<fims_distributions::DensityComponentBase<Type> > 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<uint32_t>(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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct DensityComponentBase : public fims_model_object::FIMSObject<Type> {
// 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<fims_data_object::DataObject<Type>>
observed_values; /**< observed data*/
fims::Vector<Type > x; /**< input value of distribution function for proirs or random effects*/
Expand All @@ -44,7 +45,7 @@ struct DensityComponentBase : public fims_model_object::FIMSObject<Type> {
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<uint32_t> 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<Type> *of; /**< Pointer to the TMB objective function */
#endif
Expand Down
8 changes: 6 additions & 2 deletions inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_<AgeCompDataInterface>("AgeComp")
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
28 changes: 0 additions & 28 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ std::map<uint32_t, FleetInterfaceBase*> 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:
Expand All @@ -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
*
Expand All @@ -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;
Expand Down
32 changes: 29 additions & 3 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class DistributionsInterfaceBase : public FIMSRcppInterfaceBase {
static std::map<uint32_t, DistributionsInterfaceBase *> 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++;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -93,8 +109,6 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase {
* @tparam T
* @return log pdf
*/


virtual double evaluate() {
fims_distributions::NormalLPDF<double> dnorm;
dnorm.x.resize(this->x.size());
Expand All @@ -112,6 +126,8 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase {
return dnorm.evaluate();
}



#ifdef TMB_MODEL

template <typename Type>
Expand All @@ -123,6 +139,10 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase {
std::make_shared<fims_distributions::NormalLPDF<Type>>();

// 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; i<this->x.size(); i++){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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; i<this->x.size(); i++){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -308,6 +332,8 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase {
std::make_shared<fims_distributions::MultinomialLPMF<Type>>();

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; i<this->x.size(); i++){
distribution->x[i] = this->x[i].value_m;
Expand Down
8 changes: 0 additions & 8 deletions inst/include/population_dynamics/fleet/fleet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
std::shared_ptr<SelectivityBase<Type>>
selectivity; /*!< selectivity component*/

int fleet_observed_index_data_id_m = -999; /*!< id of index data */
std::shared_ptr<fims_data_object::DataObject<Type>>
observed_index_data; /*!< observed index data*/

int fleet_observed_agecomp_data_id_m = -999; /*!< id of age comp data */
std::shared_ptr<fims_data_object::DataObject<Type>>
observed_agecomp_data; /*!< observed agecomp data*/

// Mortality and catchability
fims::Vector<Type>
log_Fmort; /*!< estimated parameter: log Fishing mortality*/
Expand Down

0 comments on commit d5444fd

Please sign in to comment.