FIMS  v0.8.0
Loading...
Searching...
No Matches
rcpp_interface_base.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_INTERFACE_BASE_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_INTERFACE_BASE_HPP
11
12#include <RcppCommon.h>
13#include <map>
14#include <vector>
15
16#include "../../../common/def.hpp"
17#include "../../../common/information.hpp"
18#include "../../interface.hpp"
20#include <limits>
21
22#define RCPP_NO_SUGAR
23#include <Rcpp.h>
24
31class Parameter {
32 public:
36 static uint32_t id_g;
44 double initial_value_m = 0.0;
48 double final_value_m = 0.0;
49
54 double uncertainty_m = -999.0;
59 double min_m = -std::numeric_limits<double>::infinity();
64 double max_m = std::numeric_limits<double>::infinity();
70
74 Parameter(double value, double min, double max, std::string estimation_type)
75 : id_m(Parameter::id_g++),
76 initial_value_m(value),
77 min_m(min),
78 max_m(max),
80
91
96 // Check for self-assignment!
97 if (this == &right) // Same object?
98 return *this; // Yes, so skip assignment, and just return *this.
99 this->id_m = right.id_m;
100 this->initial_value_m = right.initial_value_m;
101 this->estimation_type_m = right.estimation_type_m;
102 this->min_m = right.min_m;
103 this->max_m = right.max_m;
104 return *this;
105 }
106
110 Parameter(double value) {
111 initial_value_m = value;
113 }
114
120 initial_value_m = 0;
122 }
123};
128
135inline double sanitize_val(double x) {
136 if (std::isnan(x) || std::isinf(x)) {
137 return -999.0;
138 }
139 return x;
140}
141
149std::ostream& operator<<(std::ostream& out, const Parameter& p) {
150 out << "{\"id\": " << p.id_m
151 << ",\n\"value\": " << sanitize_val(p.initial_value_m)
152 << ",\n\"estimated_value\": " << sanitize_val(p.final_value_m)
153 << ",\n\"uncertainty\": " << sanitize_val(p.uncertainty_m)
154 << ",\n\"min\": ";
155 if (p.min_m == -std::numeric_limits<double>::infinity()) {
156 out << "\"-Infinity\"";
157 } else {
158 out << p.min_m;
159 }
160 out << ",\n\"max\": ";
161 if (p.max_m == std::numeric_limits<double>::infinity()) {
162 out << "\"Infinity\"";
163 } else {
164 out << p.max_m;
165 }
166
167 out << ",\n\"estimation_type\": \"" << p.estimation_type_m << "\"\n}";
168
169 return out;
170}
171
179 public:
187 std::shared_ptr<std::vector<Parameter>> storage_m;
192
197 this->id_m = ParameterVector::id_g++;
198 this->storage_m = std::make_shared<std::vector<Parameter>>();
199 this->storage_m->resize(1); // push_back(Rcpp::wrap(p));
200 }
201
207
212 this->id_m = ParameterVector::id_g++;
213 this->storage_m = std::make_shared<std::vector<Parameter>>();
214 this->storage_m->resize(size);
215 for (size_t i = 0; i < size; i++) {
216 storage_m->at(i) = Parameter();
217 }
218 }
219
225 ParameterVector(Rcpp::NumericVector x, size_t size) {
226 if (static_cast<size_t>(x.size()) != size) {
227 throw std::invalid_argument(
228 "Error in call to ParameterVector(Rcpp::NumericVector x, size_t "
229 "size): x.size() != size argument.");
230 } else {
231 this->id_m = ParameterVector::id_g++;
232 this->storage_m = std::make_shared<std::vector<Parameter>>();
233 // Use std::min to avoid comparing signed and unsigned types
234 size_t n = std::min(static_cast<size_t>(x.size()), size);
235 this->storage_m->resize(n);
236 for (size_t i = 0; i < n; i++) {
237 storage_m->at(i).initial_value_m = x[i];
238 }
239 }
240 }
241
247 this->id_m = ParameterVector::id_g++;
248 this->storage_m = std::make_shared<std::vector<Parameter>>();
249 this->storage_m->resize(v.size());
250 for (size_t i = 0; i < v.size(); i++) {
251 storage_m->at(i).initial_value_m = v[i];
252 }
253 }
254
259 virtual ~ParameterVector() {}
260
264 virtual uint32_t get_id() { return this->id_m; }
265
270 inline Parameter& operator[](size_t pos) { return this->storage_m->at(pos); }
271
278 if (static_cast<size_t>(pos) == 0 ||
279 static_cast<size_t>(pos) > this->storage_m->size()) {
280 throw std::invalid_argument("ParameterVector: Index out of range");
281 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
282 fims::to_string(this->size()));
283 return NULL;
284 }
285 return Rcpp::wrap(this->storage_m->at(pos - 1));
286 }
287
295 Parameter& get(size_t pos) {
296 if (pos >= this->storage_m->size()) {
297 throw std::invalid_argument("ParameterVector: Index out of range");
298 }
299 return (this->storage_m->at(pos));
300 }
301
311 void set(size_t pos, const Parameter& p) { this->storage_m->at(pos) = p; }
312
316 size_t size() { return this->storage_m->size(); }
317
323 void resize(size_t size) { this->storage_m->resize(size); }
324
333 for (size_t i = 0; i < this->storage_m->size(); i++) {
334 if (estimable) {
335 this->storage_m->at(i).estimation_type_m.set("fixed_effects");
336 } else {
337 this->storage_m->at(i).estimation_type_m.set("constant");
338 }
339 }
340 }
341
350 for (size_t i = 0; i < this->storage_m->size(); i++) {
351 if (random) {
352 this->storage_m->at(i).estimation_type_m.set("random_effects");
353 } else {
354 this->storage_m->at(i).estimation_type_m.set("constant");
355 }
356 }
357 }
358
366 void fill(double value) {
367 for (size_t i = 0; i < this->storage_m->size(); i++) {
368 storage_m->at(i).initial_value_m = value;
369 }
370 }
371
378 void fill_min(double value) {
379 for (size_t i = 0; i < this->storage_m->size(); i++) {
380 storage_m->at(i).min_m = value;
381 }
382 }
383
390 void fill_max(double value) {
391 for (size_t i = 0; i < this->storage_m->size(); i++) {
392 storage_m->at(i).max_m = value;
393 }
394 }
395
400 void show() {
401 Rcpp::Rcout << this->storage_m->data() << "\n";
402
403 for (size_t i = 0; i < this->storage_m->size(); i++) {
404 Rcpp::Rcout << storage_m->at(i) << " ";
405 }
406 }
407};
409
417std::ostream& operator<<(std::ostream& out, ParameterVector& v) {
418 out << "[";
419 size_t size = v.size();
420 for (size_t i = 0; i < size - 1; i++) {
421 out << v[i] << ", ";
422 }
423 out << v[size - 1] << "]";
424 return out;
425}
426
435 public:
443 std::shared_ptr<std::vector<double>> storage_m;
448
453 this->id_m = RealVector::id_g++;
454 this->storage_m = std::make_shared<std::vector<double>>();
455 this->storage_m->resize(1);
456 }
457
463
467 RealVector(size_t size) {
468 this->id_m = RealVector::id_g++;
469 this->storage_m = std::make_shared<std::vector<double>>();
470 this->storage_m->resize(size);
471 }
472
478 RealVector(Rcpp::NumericVector x, size_t size) {
479 this->id_m = RealVector::id_g++;
480 this->storage_m = std::make_shared<std::vector<double>>();
481 this->storage_m->assign(x.begin(), x.end());
482 }
483
489 this->id_m = RealVector::id_g++;
490 this->storage_m = std::make_shared<std::vector<double>>();
491 this->storage_m->resize(v.size());
492 for (size_t i = 0; i < v.size(); i++) {
493 storage_m->at(i) = v[i];
494 }
495 }
496
501 virtual ~RealVector() {}
502
509 RealVector& operator=(const Rcpp::NumericVector& v) {
510 this->storage_m->assign(v.begin(), v.end());
511 return *this;
512 }
513
517 virtual uint32_t get_id() { return this->id_m; }
518
524 void fromRVector(const Rcpp::NumericVector& orig) {
525 this->storage_m->resize(orig.size());
526 for (size_t i = 0; i < this->storage_m->size(); i++) {
527 this->storage_m->at(i) = orig[i];
528 }
529 }
530
536 Rcpp::NumericVector toRVector() {
537 Rcpp::NumericVector ret(this->storage_m->size());
538 for (size_t i = 0; i < this->size(); i++) {
539 ret[i] = this->storage_m->at(i);
540 }
541
542 return ret;
543 }
544
549 inline double& operator[](size_t pos) { return this->storage_m->at(pos); }
550
557 if (static_cast<size_t>(pos) == 0 ||
558 static_cast<size_t>(pos) > this->storage_m->size()) {
559 throw std::invalid_argument("RealVector: Index out of range");
560 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
561 fims::to_string(this->size()));
562 return NULL;
563 }
564 return Rcpp::wrap(this->storage_m->at(pos - 1));
565 }
566
574 double& get(size_t pos) {
575 if (pos >= this->storage_m->size()) {
576 throw std::invalid_argument("RealVector: Index out of range");
577 }
578 return (this->storage_m->at(pos));
579 }
580
590 void set(size_t pos, const double& p) { this->storage_m->at(pos) = p; }
591
595 size_t size() { return this->storage_m->size(); }
596
602 void resize(size_t size) { this->storage_m->resize(size); }
603
611 void fill(double value) {
612 for (size_t i = 0; i < this->storage_m->size(); i++) {
613 storage_m->at(i) = value;
614 }
615 }
616
621 void show() {
622 Rcpp::Rcout << this->storage_m->data() << "\n";
623
624 for (size_t i = 0; i < this->storage_m->size(); i++) {
625 Rcpp::Rcout << storage_m->at(i) << " ";
626 }
627 }
628};
630
635 public:
639 bool finalized = false;
643 static std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
645
649 virtual bool add_to_fims_tmb() {
650 Rcpp::Rcout << "fims_rcpp_interface_base::add_to_fims_tmb(): Not yet "
651 "implemented.\n";
652 return false;
653 }
654
659 virtual void finalize() {}
660
664 virtual std::string to_json() {
665 FIMS_WARNING_LOG("Method not yet defined.");
666 return "{\"name\": \"not yet implemented\"}";
667 }
672 void get_se_values(std::string name,
673 std::map<std::string, std::vector<double>>& se_values,
675 auto se_vals = se_values.find(name);
676 if (se_vals != se_values.end()) {
677 std::vector<double>& se_vals_vector = (*se_vals).second;
678 std::vector<double> uncertainty_std(
679 se_vals_vector.begin(), se_vals_vector.begin() + values.size());
680 std::vector<double> temp(se_vals_vector.begin() + values.size(),
681 se_vals_vector.end());
684 values = uncertainty;
685 } else {
686 std::fill(values.begin(), values.end(), -999);
687 }
688 }
689
701 virtual void set_uncertainty(
702 std::map<std::string, std::vector<double>>& se_values) {
703 FIMS_WARNING_LOG("Method not yet defined.");
704 }
705
712 std::string value_to_string(double value) {
713 std::stringstream ss;
714 if (value == std::numeric_limits<double>::infinity()) {
715 ss << "\"Infinity\"";
716 } else if (value == -std::numeric_limits<double>::infinity()) {
717 ss << "\"-Infinity\"";
718 } else if (value != value) {
719 ss << "-999";
720 } else {
721 // Set precision (R default is 16)
722 ss << std::fixed << std::setprecision(16) << value;
723 }
724 return ss.str();
725 }
730 std::stringstream ss;
731
732 for (size_t i = 0; i < rep; i++) {
733 for (size_t j = start; j < end; j++) {
734 ss << j << ", ";
735 }
736 if (i < (rep - 1)) {
737 ss << end << ", ";
738 } else {
739 ss << end;
740 }
741 }
742 return ss.str();
743 }
744};
745std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
747
748#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:634
std::string make_dimensions(uint32_t start, uint32_t end, uint32_t rep=1)
Make a string of dimensions for the model.
Definition rcpp_interface_base.hpp:729
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:639
std::string value_to_string(double value)
Report the parameter value as a string.
Definition rcpp_interface_base.hpp:712
virtual void set_uncertainty(std::map< std::string, std::vector< double > > &se_values)
Set uncertainty values for the interface object.
Definition rcpp_interface_base.hpp:701
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:644
void get_se_values(std::string name, std::map< std::string, std::vector< double > > &se_values, fims::Vector< double > &values)
Method to extract standard error values from the se_values working map.
Definition rcpp_interface_base.hpp:672
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:649
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_interface_base.hpp:664
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_interface_base.hpp:659
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:178
ParameterVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:211
void show()
The printing methods for a ParameterVector.
Definition rcpp_interface_base.hpp:400
void set_all_random(bool random)
Sets all Parameters within a ParameterVector as random effects.
Definition rcpp_interface_base.hpp:349
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:323
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:316
ParameterVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a parameter vector.
Definition rcpp_interface_base.hpp:225
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:183
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:191
SEXP at(R_xlen_t pos)
The accessor where the first index starts at one. This function is for calling accessing from R.
Definition rcpp_interface_base.hpp:277
ParameterVector(const fims::Vector< double > &v)
The constructor for initializing a parameter vector.
Definition rcpp_interface_base.hpp:246
ParameterVector(const ParameterVector &other)
The constructor.
Definition rcpp_interface_base.hpp:205
virtual uint32_t get_id()
Gets the ID of the ParameterVector object.
Definition rcpp_interface_base.hpp:264
void fill_max(double value)
Assigns the given values to the maximum value of all elements in the vector.
Definition rcpp_interface_base.hpp:390
void fill(double value)
Sets the value of all Parameters in the ParameterVector to the provided value.
Definition rcpp_interface_base.hpp:366
Parameter & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:270
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:295
void set(size_t pos, const Parameter &p)
An internal setter for setting a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:311
ParameterVector()
The constructor.
Definition rcpp_interface_base.hpp:196
std::shared_ptr< std::vector< Parameter > > storage_m
Parameter storage.
Definition rcpp_interface_base.hpp:187
virtual ~ParameterVector()
Destroy the Parameter Vector object.
Definition rcpp_interface_base.hpp:259
void fill_min(double value)
Assigns the given values to the minimum value of all elements in the vector.
Definition rcpp_interface_base.hpp:378
void set_all_estimable(bool estimable)
Sets all Parameters within a ParameterVector as estimable.
Definition rcpp_interface_base.hpp:332
An Rcpp interface that defines the Parameter class.
Definition rcpp_interface_base.hpp:31
double initial_value_m
The initial value of the parameter.
Definition rcpp_interface_base.hpp:44
Parameter(double value)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:110
double uncertainty_m
The standard error of the parameter estimate, where the default is -999.0.
Definition rcpp_interface_base.hpp:54
Parameter & operator=(const Parameter &right)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:95
Parameter(double value, double min, double max, std::string estimation_type)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:74
Parameter()
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:119
double final_value_m
The final value of the parameter.
Definition rcpp_interface_base.hpp:48
SharedString estimation_type_m
A string indicating the estimation type. Options are: constant, fixed_effects, or random_effects,...
Definition rcpp_interface_base.hpp:69
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:40
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:36
double max_m
The maximum possible parameter value, where the default is positive infinity.
Definition rcpp_interface_base.hpp:64
Parameter(const Parameter &other)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:84
double min_m
The minimum possible parameter value, where the default is negative infinity.
Definition rcpp_interface_base.hpp:59
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:434
double & get(size_t pos)
An internal accessor for calling a position of a RealVector from R.
Definition rcpp_interface_base.hpp:574
virtual ~RealVector()
Destroy the real Vector object.
Definition rcpp_interface_base.hpp:501
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:595
RealVector(const RealVector &other)
The constructor.
Definition rcpp_interface_base.hpp:461
void set(size_t pos, const double &p)
An internal setter for setting a position of a RealVector from R.
Definition rcpp_interface_base.hpp:590
RealVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:467
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:602
std::shared_ptr< std::vector< double > > storage_m
real storage.
Definition rcpp_interface_base.hpp:443
RealVector & operator=(const Rcpp::NumericVector &v)
Definition rcpp_interface_base.hpp:509
uint32_t id_m
The local ID of the RealVector object.
Definition rcpp_interface_base.hpp:447
void fill(double value)
Sets the value of all elements in the RealVector to the provided value.
Definition rcpp_interface_base.hpp:611
Rcpp::NumericVector toRVector()
Definition rcpp_interface_base.hpp:536
static uint32_t id_g
The static ID of the RealVector object.
Definition rcpp_interface_base.hpp:439
virtual uint32_t get_id()
Gets the ID of the RealVector object.
Definition rcpp_interface_base.hpp:517
RealVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:478
SEXP at(R_xlen_t pos)
The accessor where the first index starts at one. This function is for calling accessing from R.
Definition rcpp_interface_base.hpp:556
double & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:549
RealVector(const fims::Vector< double > &v)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:488
void show()
The printing methods for a RealVector.
Definition rcpp_interface_base.hpp:621
void fromRVector(const Rcpp::NumericVector &orig)
Definition rcpp_interface_base.hpp:524
RealVector()
The constructor.
Definition rcpp_interface_base.hpp:452
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1508
Definition fims_vector.hpp:27
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:598
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:606
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:279
std::ostream & operator<<(std::ostream &out, const Parameter &p)
Output for std::ostream& for a parameter.
Definition rcpp_interface_base.hpp:149
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:135
Code to create shared pointers to allow for the wrapper functions in R to work correctly so the live ...