Skip to content

Commit

Permalink
refactor x as shared pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Havron-NOAA committed Aug 1, 2024
1 parent 88b0438 commit 887f83b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 94 deletions.
11 changes: 6 additions & 5 deletions inst/include/common/data_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace fims_data_object {
template <typename Type>
struct DataObject : public fims_model_object::FIMSObject<Type> {
static uint32_t id_g; /**< id of the Data Object >*/
std::vector<Type> data; /**< vector of the data >*/
std::shared_ptr<fims::Vector<Type>> data; /**< vector of the data >*/
size_t dimensions; /**< dimension of the Data object >*/
size_t imax; /**<1st dimension of data object >*/
size_t jmax; /**< 2nd dimension of data object>*/
Expand All @@ -57,7 +57,8 @@ struct DataObject : public fims_model_object::FIMSObject<Type> {
* Constructs a one-dimensional data object.
*/
DataObject(size_t imax) : dimensions(1), imax(imax) {
data.resize(imax);
this->data = std::make_shared<fims::Vector<Type> > (imax);
//data.resize(imax);

this->id = DataObject<Type>::id_g++;
}
Expand All @@ -66,7 +67,7 @@ struct DataObject : public fims_model_object::FIMSObject<Type> {
* Constructs a two-dimensional data object.
*/
DataObject(size_t imax, size_t jmax) : dimensions(2), imax(imax), jmax(jmax) {
data.resize(imax * jmax);
this->data = std::make_shared<fims::Vector<Type> > (imax * jmax);
this->id = DataObject<Type>::id_g++;
}

Expand All @@ -75,7 +76,7 @@ struct DataObject : public fims_model_object::FIMSObject<Type> {
*/
DataObject(size_t imax, size_t jmax, size_t kmax)
: dimensions(3), imax(imax), jmax(jmax), kmax(kmax) {
data.resize(imax * jmax * kmax);
this->data = std::make_shared<fims::Vector<Type> > (imax * jmax * kmax);
this->id = DataObject<Type>::id_g++;
}

Expand All @@ -84,7 +85,7 @@ struct DataObject : public fims_model_object::FIMSObject<Type> {
*/
DataObject(size_t imax, size_t jmax, size_t kmax, size_t lmax)
: dimensions(4), imax(imax), jmax(jmax), kmax(kmax), lmax(lmax) {
data.resize(imax * jmax * kmax * lmax);
this->data = std::make_shared<fims::Vector<Type> >(imax * jmax * kmax * lmax);
this->id = DataObject<Type>::id_g++;
}

Expand Down
38 changes: 19 additions & 19 deletions inst/include/common/information.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ class Information {

void setup_priors(){
for(density_components_iterator it = density_components.begin(); it!= density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > n = (*it).second;
if(n->input_type == "prior"){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*it).second;
if(d->input_type == "prior"){
variable_map_iterator vmit;
vmit = this->variable_map.find(n->key[0]);
n->x = *(*vmit).second;
for(size_t i=1; i<n->key.size(); i++){
vmit = this->variable_map.find(n->key[i]);
n->x.insert(std::end(n->x),
vmit = this->variable_map.find(d->key[0]);
d->x = *(*vmit).second;
for(size_t i=1; i<d->key.size(); i++){
vmit = this->variable_map.find(d->key[i]);
d->x.insert(std::end(d->x),
std::begin(*(*vmit).second), std::end(*(*vmit).second));
}
}
Expand All @@ -174,13 +174,13 @@ class Information {
void setup_random_effects(){
for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > n = (*it).second;
if(n->input_type == "re"){
if(d->input_type == "re"){
variable_map_iterator vmit;
vmit = this->variable_map.find(n->key[0]);
n->x = *(*vmit).second;
for(size_t i=1; i<n->key.size(); i++){
vmit = this->variable_map.find(n->key[i]);
n->x.insert(std::end(n->x),
vmit = this->variable_map.find(d->key[0]);
d->x = *(*vmit).second;
for(size_t i=1; i<d->key.size(); i++){
vmit = this->variable_map.find(d->key[i]);
d->x.insert(std::end(d->x),
std::begin(*(*vmit).second), std::end(*(*vmit).second));
}
}
Expand All @@ -189,14 +189,14 @@ class Information {
void setup_data(){
for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > n = (*it).second;
if(n->input_type == "data"){
if(d->input_type == "data"){
variable_map_iterator vmit;
vmit = this->variable_map.find(n->key[0]);
n->expected_value = *(*vmit).second;
vmit = this->variable_map.find(d->key[0]);
d->expected_values = *(*vmit).second;

for(size_t i=1; i<n->key.size(); i++){
vmit = this->variable_map.find(n->key[i]);
n->expected_value.insert(std::end(n->expected_value),
for(size_t i=1; i<d->key.size(); i++){
vmit = this->variable_map.find(d->key[i]);
d->expected_values.insert(std::end(d->expected_values),
std::begin(*(*vmit).second), std::end(*(*vmit).second));
}
}
Expand Down
10 changes: 5 additions & 5 deletions inst/include/common/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Model { // may need singleton
#endif
// Loop over densities and evaluate joint negative log densities for priors
size_t n_priors = 0;
typename density_components_iterator d_it;
density_components_iterator d_it;
for(d_it = this->density_components.begin(); d_it!= this->density_components.end(); ++d_it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*d_it).second;
#ifdef TMB_MODEL
Expand Down Expand Up @@ -148,7 +148,7 @@ class Model { // may need singleton
<< this->fims_information->populations.size() << " populations."
<< std::endl;
for (p_it = this->fims_information->populations.begin();
p_it != this->fims_information->populations.end(); ++it) {
p_it != this->fims_information->populations.end(); ++p_it) {
//(*p_it).second points to the Population module
std::shared_ptr<fims_popdy::Population<Type> > p = (*p_it).second;
// link to TMB objective function
Expand All @@ -159,7 +159,7 @@ class Model { // may need singleton
p->Evaluate();
}

typename fims_info::Information<Type>::population_iterator f_it;
typename fims_info::Information<Type>::fleet_iterator f_it;
// Loop over fleets/surveys, and evaluate age comp and index expected values
for (f_it = this->fims_information->fleets.begin();
f_it != this->fims_information->fleets.end(); ++f_it) {
Expand All @@ -171,15 +171,15 @@ class Model { // may need singleton
MODEL_LOG << "Setting up pointer to fleet " << f->id << "."
<< std::endl;
f->evaluate_age_comp();
f->evaluate_index()
f->evaluate_index();
}

// Loop over and evaluate data joint negative log-likelihoods
for(d_it = this->density_components.begin(); d_it!= this->density_components.end(); ++d_it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*d_it).second;
#ifdef TMB_MODEL
d->of = this->of;
d->keep = this->keep;
//d->keep = this->keep;
#endif
if(d->input_type == "data"){
jnll -= d->evaluate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ 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 */
fims::Vector<Type> x; /**< input value of distribution function */
std::shared_ptr<fims::Vector<Type> > x; /**< input value of distribution function */
fims::Vector<Type> expected_values; /**< expected value of distribution function */
fims::Vector<Type> lpdf_vec; /**< vector to record observation level negative log-likelihood values */
std::string input_type; /**< string classifies the type of the negative log-likelihood; options are: prior, re, data */
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

/** @brief Constructor.
*/
Expand Down
11 changes: 3 additions & 8 deletions inst/include/distributions/functors/lognormal_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ namespace fims_distributions
fims::Vector<Type> log_logsd; /**< log of the standard deviation of the distribution on the log scale; can be a vector or scalar */
fims::Vector<Type> logmu; /**< mean of the distribution on the log scale; can be a vector or scalar */
fims::Vector<Type> logsd; /**< standard deviation of the distribution on the log scale; can be a vector or scalar */
std::vector<bool> is_na; /**< Boolean; if true, data observation is NA and the likelihood contribution is skipped */
#ifdef TMB_MODEL
::objective_function<Type> *of; /**< Pointer to the TMB objective function */
#endif
Type lpdf = 0.0; /**< total log probability density contribution of the distribution */
// data_indicator<tmbutils::vector<Type> , Type> keep; /**< Indicator used in TMB one-step-ahead residual calculations */

Expand All @@ -52,7 +48,6 @@ namespace fims_distributions
{
this->logmu.resize(this->x.size());
this->logsd.resize(this->x.size());
is_na.resize(this->x.size());
this->lpdf_vec.resize(this->x.size());
for (size_t i = 0; i < this->x.size(); i++)
{
Expand Down Expand Up @@ -83,14 +78,14 @@ namespace fims_distributions

#ifdef TMB_MODEL
if(this->input_type == "data"){
if(this->x->at(i) != this->x->na_value){
if(this->x->at(i) != this->na_value){
// this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x->at(i), logmu[i], logsd[i], true) - this->x->at(i);
this->lpdf_vec[i] = dnorm(this->x->at(i), logmu[i], logsd[i], true) - this->x->at(i);
} else {
this->lpdf_vec[i] = 0;
}
} else {
// this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x[i], logmu[i], logsd[i], true);
this->lpdf_vec[i] = dnorm(this->x[i], logmu[i], logsd[i], true);
this->lpdf_vec[i] = dnorm(this->x->at(i), logmu[i], logsd[i], true);
}

lpdf += this->lpdf_vec[i];
Expand Down
11 changes: 4 additions & 7 deletions inst/include/distributions/functors/multinomial_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ namespace fims_distributions
{
Type lpmf = 0.0; /**< total negative log-likelihood contribution of the distribution */
fims::Vector<size_t> dims; /**< Dimensions of the number of rows and columns of the multivariate dataset */
std::vector<bool> is_na; /**< Boolean; if true, data observation is NA and the likelihood contribution for the entire row is skipped */
#ifdef TMB_MODEL
::objective_function<Type> *of; /**< Pointer to the TMB objective function */
#endif

// data_indicator<tmbutils::vector<Type> , Type> keep; /**< Indicator used in TMB one-step-ahead residual calculations */

/** @brief Constructor.
Expand Down Expand Up @@ -75,7 +72,7 @@ namespace fims_distributions

#ifdef TMB_MODEL
for (size_t j = 0; j < dims[1]; j++){
if (this->x->at(i,j) != this->x->na_value) {
if (this->x->at(i)->at(j) != this->na_value) {
size_t idx = (i * dims[1]) + j;
} else {
containsNA - true;
Expand All @@ -85,8 +82,8 @@ namespace fims_distributions
if(!containsNA){
for (size_t j = 0; j < dims[1]; j++){
size_t idx = (i * dims[1]) + j;
x_vector[j] = this->x->at(i, j);
prob_vector[j] = this->expected_value[idx];
x_vector[j] = this->x->at(i)->at(j);
prob_vector[j] = this->expected_values[idx];
}
}

Expand Down
11 changes: 4 additions & 7 deletions inst/include/distributions/functors/normal_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ struct NormalLPDF : public DensityComponentBase<Type> {
fims::Vector<Type> mu; /**< mean of the distribution; can be a vector or scalar */
fims::Vector<Type> sd; /**< standard deviation of the distribution; can be a vector or scalar */
Type lpdf = 0.0; /**< total log probability density contribution of the distribution */
std::vector<bool> is_na; /**< Boolean; if true, data observation is NA and the likelihood contribution is skipped */
#ifdef TMB_MODEL
::objective_function<Type> *of; /**< Pointer to the TMB objective function */
#endif

//data_indicator<tmbutils::vector<Type> , Type> keep; /**< Indicator used in TMB one-step-ahead residual calculations */

/** @brief Constructor.
Expand Down Expand Up @@ -76,15 +73,15 @@ struct NormalLPDF : public DensityComponentBase<Type> {
}
#ifdef TMB_MODEL
if(this->input_type == "data"){
if(this->x->at(i) != this->x->na_value){
if(this->x->at(i) != this->na_value){
// this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x->at(i), mu[i], sd[i], true);
this->lpdf_vec[i] = dnorm(this->x->at(i), mu[i], sd[i], true);
} else {
this->lpdf_vec[i] = 0;
}

} else {
// this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x[i], mu[i], sd[i], true);
this->lpdf_vec[i] = dnorm(this->x[i], mu[i], sd[i], true);
this->lpdf_vec[i] = dnorm(this->x->at(i), mu[i], sd[i], true);
}
lpdf += this->lpdf_vec[i];
if(this->simulate_flag){
Expand Down
7 changes: 2 additions & 5 deletions inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ RCPP_MODULE(fims) {
.method("evaluate", &DnormDistributionsInterface::evaluate)
.field("x", &DnormDistributionsInterface::x)
.field("expected_values", &DnormDistributionsInterface::expected_values)
.field("log_sd", &DnormDistributionsInterface::log_sd)
.field("is_na", &DnormDistributionsInterface::is_na);
.field("log_sd", &DnormDistributionsInterface::log_sd);

Rcpp::class_<LogisticMaturityInterface>("LogisticMaturity")
.constructor()
Expand Down Expand Up @@ -475,16 +474,14 @@ RCPP_MODULE(fims) {
.field("input_type", &DlnormDistributionsInterface::input_type)
.field("x", &DlnormDistributionsInterface::x)
.field("expected_values", &DlnormDistributionsInterface::expected_values)
.field("log_logsd", &DlnormDistributionsInterface::log_logsd)
.field("is_na", &DlnormDistributionsInterface::is_na);
.field("log_logsd", &DlnormDistributionsInterface::log_logsd);

Rcpp::class_<DmultinomDistributionsInterface>("TMBDmultinomDistribution")
.constructor()
.method("evaluate", &DmultinomDistributionsInterface::evaluate)
.method("get_id", &DmultinomDistributionsInterface::get_id)
.field("x", &DmultinomDistributionsInterface::x)
.field("expected_values", &DmultinomDistributionsInterface::expected_values)
.field("is_na", &DmultinomDistributionsInterface::is_na)
.field("dims", &DmultinomDistributionsInterface::dims);
}

Expand Down
Loading

0 comments on commit 887f83b

Please sign in to comment.