Skip to content

Commit

Permalink
refactor variable map
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Havron-NOAA committed Jul 26, 2024
1 parent c93e9f7 commit 6345abd
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 46 deletions.
4 changes: 2 additions & 2 deletions inst/include/common/information.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class Information {
uint32_t, std::shared_ptr<VonBertalanffy<Type> > >::iterator
vb_iterator;

std::unordered_map<std::string, fims::Vector<Type>* > variable_map;
typedef typename std::unordered_map<std::string, fims::Vector<Type>* >::iterator
std::unordered_map<uint32_t, fims::Vector<Type>* > variable_map;
typedef typename std::unordered_map<uint32_t, fims::Vector<Type>* >::iterator
variable_map_iterator;


Expand Down
2 changes: 1 addition & 1 deletion inst/include/distributions/density_components_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fims::Vector<Type> expected_value;
fims::Vector<Type> log_likelihood_vec;
std::string input_type;
bool simulate_flag;
std::vector<std::string> key;
std::vector<uint32_t> key;
#ifdef TMB_MODEL
::objective_function<Type>
*of;
Expand Down
4 changes: 3 additions & 1 deletion inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ RCPP_MODULE(growth) {
.method("resize", &VariableVector::resize)
.method("set_all_estimable", &VariableVector::set_all_estimable)
.method("set_all_random_effects", &VariableVector::set_all_random_effects)
.method("fill", &VariableVector::fill);
.method("fill", &VariableVector::fill)
.method("get_id", &VariableVector::get_id);
Rcpp::class_<PopulationInterface>("Population")
.constructor()
.field("ages", &PopulationInterface::ages)
.field("length", &PopulationInterface::length)
.field("u", &PopulationInterface::u)
.method("get_id", &PopulationInterface::get_id)
.method("set_growth", &PopulationInterface::SetGrowth)
Expand Down
42 changes: 15 additions & 27 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_distributions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class NormalLPDFInterface : public DensityComponentsInterface{
VariableVector log_sd;
std::vector<double> log_likelihood_vec;
std::string input_type;
std::vector<std::string> key;
std::vector<std::uint32_t> key;

bool simulate_flag = false;
bool osa_flag;
Expand All @@ -64,16 +64,12 @@ class NormalLPDFInterface : public DensityComponentsInterface{
return "NormalLPDFInterface";
}

void SetDistributionLinks(std::string input_type, Rcpp::IntegerVector module_id,
Rcpp::StringVector module_name, Rcpp::StringVector name){
void SetDistributionLinks(std::string input_type, Rcpp::IntegerVector ids){
this->input_type = input_type;

std::stringstream ss;
this->key.resize(module_id.size());
for(int i=0; i<module_id.size(); i++){
ss << module_name[i] << "_" << module_id[i] << "_" << name[i];
this->key[i] = ss.str();
ss.str("");
this->key.resize(ids.size());
for(size_t i=0; i<ids.size(); i++){
this->key[i] = ids[i];
}
}

Expand Down Expand Up @@ -194,7 +190,7 @@ class MVNormLPDFInterface : public DensityComponentsInterface{
VariableVector logit_phi;
std::vector<double> log_likelihood_vec;
std::string input_type;
std::vector<std::string> key;
std::vector<uint32_t> key;

bool simulate_flag = false;

Expand All @@ -207,16 +203,12 @@ class MVNormLPDFInterface : public DensityComponentsInterface{
return "MVNormLPDFInterface";
}

void SetDistributionLinks(std::string input_type, Rcpp::IntegerVector module_id,
Rcpp::StringVector module_name, Rcpp::StringVector name){
void SetDistributionLinks(std::string input_type, Rcpp::IntegerVector ids){
this->input_type = input_type;

std::stringstream ss;
this->key.resize(module_id.size());
for(int i=0; i<module_id.size(); i++){
ss << module_name[i] << "_" << module_id[i] << "_" << name[i];
this->key[i] = ss.str();
ss.str("");
this->key.resize(ids.size());
for(size_t i=0; i<ids.size(); i++){
this->key[i] = ids[i];
}
}

Expand Down Expand Up @@ -348,7 +340,7 @@ class AR1LPDFInterface : public DensityComponentsInterface{
VariableVector rho;
std::vector<double> log_likelihood_vec;
std::string input_type = "re";
std::vector<std::string> key;
std::vector<uint32_t> key;

bool simulate_flag = false;

Expand All @@ -361,16 +353,12 @@ class AR1LPDFInterface : public DensityComponentsInterface{
return "AR1LPDFInterface";
}

void SetDistributionLinks(std::string input_type, Rcpp::IntegerVector module_id,
Rcpp::StringVector module_name, Rcpp::StringVector name){
void SetDistributionLinks(std::string input_type, Rcpp::IntegerVector ids){
this->input_type = input_type;

std::stringstream ss;
this->key.resize(module_id.size());
for(int i=0; i<module_id.size(); i++){
ss << module_name[i] << "_" << module_id[i] << "_" << name[i];
this->key[i] = ss.str();
ss.str("");
this->key.resize(ids.size());
for(size_t i=0; i<ids.size(); i++){
this->key[i] = ids[i];
}
}

Expand Down
14 changes: 7 additions & 7 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class vonBertalanffyInterface : public GrowthInterfaceBase {

ss << this->get_module_name() << "_" << this->id;// << "_" << this->r_name;

std::string key = ss.str();
std::string name_key = ss.str();
ss.str("");

vb->id = this->id;
Expand All @@ -87,8 +87,8 @@ class vonBertalanffyInterface : public GrowthInterfaceBase {
vb->logk.resize(1);
vb->logk[0] = this->logk.value;
//need to set up maps even when parameters are not estimable (e.g. for penalties or random effects)
ss << key << "_logk";
info->variable_map[ss.str()] = &(vb)->logk;
ss << name_key << "_logk";
info->variable_map[this->logk.id] = &(vb)->logk;

if (this->logk.estimable) {
model->parameters.push_back(&(vb)->logk[0]);
Expand All @@ -99,8 +99,8 @@ class vonBertalanffyInterface : public GrowthInterfaceBase {
//initialize l_inf
vb->l_inf.resize(1);
vb->l_inf[0] = this->l_inf.value;
ss << key << "_l_inf";
info->variable_map[ss.str()] = &(vb)->l_inf;
ss << name_key << "_l_inf";
info->variable_map[this->l_inf.id] = &(vb)->l_inf;

if (this->l_inf.estimable) {
model->parameters.push_back(&(vb)->l_inf[0]);
Expand All @@ -111,8 +111,8 @@ class vonBertalanffyInterface : public GrowthInterfaceBase {
//initialize a_min
vb->a_min.resize(1);
vb->a_min[0] = this->a_min.value;
ss << key << "_a_min";
info->variable_map[ss.str()] = &(vb)->a_min;
ss << name_key << "_a_min";
info->variable_map[this->a_min.id] = &(vb)->a_min;

if (this->a_min.estimable) {
model->parameters.push_back(&(vb)->a_min[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ class Variable {
}

Variable(double d) {
this->id = Variable::id_g++;
Variable::parameters.push_back(this);
this->value = d;
}

Variable(const Variable& other){
this->id = other.id;
this->estimable = other.estimable;
this->is_random_effect = other.is_random_effect;
this->value = other.value;
Expand Down Expand Up @@ -91,6 +89,8 @@ class VariableVector{
this->storage_m.push_back(Rcpp::wrap(v));
}
}

virtual uint32_t get_id() { return this->id; }

/**
* Accessor. First index starts is zero.
Expand Down
14 changes: 8 additions & 6 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PopulationInterface : public PopulationInterfaceBase {
Rcpp::NumericVector ages;
VariableVector u;
uint32_t growth_id; /**< id of the growth function*/

VariableVector length;

PopulationInterface() : PopulationInterfaceBase() {}

Expand Down Expand Up @@ -75,15 +75,16 @@ class PopulationInterface : public PopulationInterfaceBase {
pop->ages[i] = this->ages[i];
}
//Set Population length
pop->length.resize(ages.size());
std::stringstream ss;
pop->length.resizethis->ages.size());
/*std::stringstream ss;
ss << this->get_module_name() << "_" << this->id;
std::string key = ss.str();
ss.str("");
ss << key << "_length";
info->variable_map[ss.str()] = &(pop)->length;
ss.str("");

*/
info->variable_map[this->length.id] = &(pop)->length;

if(this->u.size() == 0){
pop->u.resize(ages.size());
Expand All @@ -99,13 +100,14 @@ class PopulationInterface : public PopulationInterfaceBase {
}
}
}

/*
ss << this->get_module_name() << "_" << this->id;
ss.str("");
ss << key << "_u";
info->variable_map[ss.str()] = &(pop)->u;
ss.str("");

*/
info->variable_map[this->u.id] = &(pop)->u;

info->pop_models[pop->id] = pop;
model->pop_models[pop->id] = pop;
Expand Down

0 comments on commit 6345abd

Please sign in to comment.