diff --git a/inst/include/interface/rcpp/rcpp_interface.hpp b/inst/include/interface/rcpp/rcpp_interface.hpp index 8c86c405..ccb2a620 100644 --- a/inst/include/interface/rcpp/rcpp_interface.hpp +++ b/inst/include/interface/rcpp/rcpp_interface.hpp @@ -452,7 +452,7 @@ RCPP_MODULE(fims) { .field("log_sd", &DnormDistributionsInterface::log_sd); Rcpp::class_("LogisticMaturity") - .constructor() + .constructor("inflection_point, slope") .field("inflection_point", &LogisticMaturityInterface::inflection_point) .field("slope", &LogisticMaturityInterface::slope) .method("get_id", &LogisticMaturityInterface::get_id) @@ -467,7 +467,7 @@ RCPP_MODULE(fims) { .method("evaluate", &LogisticSelectivityInterface::evaluate); Rcpp::class_("DoubleLogisticSelectivity") - .constructor() + .constructor("inflection_point_asc, slope_asc, inflection_point_desc, slope_desc") .field("inflection_point_asc", &DoubleLogisticSelectivityInterface::inflection_point_asc) .field("slope_asc", &DoubleLogisticSelectivityInterface::slope_asc) diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp index a845f33b..619c1e99 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp @@ -64,7 +64,23 @@ class LogisticMaturityInterface : public MaturityInterfaceBase { inflection_point; /**< the index value at which the response reaches .5 */ Parameter slope; /**< the width of the curve at the inflection_point */ - LogisticMaturityInterface() : MaturityInterfaceBase() {} + //' @rd + //' @name LogisticMaturityInterface + //' @title Constructor for initializing a LogisticMaturityInterface object + //' @description Initializes a LogisticMaturityInterface object with specified inflection point and slope parameters. + //' @param inflection_point A Parameter object representing the inflection point of the logistic selectivity curve. + //' @param slope A Parameter object representing the slope of the logistic selectivity curve. + //' @return No return value, as this is a constructor. + //' @examples + //' // R example of creating a LogisticMaturityInterface object + //' inflection <- methods::new(Parameter, 2, true) + //' slope <- methods::new(Parameter, 0.2, true) + //' logistic_maturity <- methods::new(LogisticMaturityInterface, inflection, slope) + LogisticMaturityInterface(Parameter inflection_point, Parameter slope) : MaturityInterfaceBase() + { + this->inflection_point = inflection_point; + this->slope = slope; + } virtual ~LogisticMaturityInterface() {} diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp index 2b90b407..b01b3daa 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp @@ -1,5 +1,5 @@ /* - * File: rcpp_selectivity.hpp + * File: rcpp_selectivity.hpp * * This File is part of the NOAA, National Marine Fisheries Service * Fisheries Integrated Modeling System project. See LICENSE @@ -20,16 +20,18 @@ * @brief SelectivityInterfaceBase class should be inherited to * define different Rcpp interfaces for each possible Selectivity function */ -class SelectivityInterfaceBase : public FIMSRcppInterfaceBase { - public: +class SelectivityInterfaceBase : public FIMSRcppInterfaceBase +{ +public: static uint32_t id_g; /**< static id of the recruitment interface base*/ uint32_t id; /**< id of the recruitment interface base */ // live objects in C++ are objects that have been created and live in memory - static std::map + static std::map live_objects; /**< map associating the ids of SelectivityInterfaceBase to the objects */ - SelectivityInterfaceBase() { + SelectivityInterfaceBase() + { this->id = SelectivityInterfaceBase::id_g++; /* Create instance of map: key is id and value is pointer to SelectivityInterfaceBase */ @@ -51,15 +53,16 @@ class SelectivityInterfaceBase : public FIMSRcppInterfaceBase { }; uint32_t SelectivityInterfaceBase::id_g = 1; -std::map +std::map SelectivityInterfaceBase::live_objects; /** * @brief Rcpp interface for logistic selectivity as an S4 object. To * instantiate from R: logistic_selectivity <- new(logistic_selectivity) */ -class LogisticSelectivityInterface : public SelectivityInterfaceBase { - public: +class LogisticSelectivityInterface : public SelectivityInterfaceBase +{ +public: Parameter inflection_point; /**< the index value at which the response reaches .5 */ Parameter slope; /**< the width of the curve at the inflection_point */ @@ -91,7 +94,8 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase { * @param x The independent variable in the logistic function (e.g., age or * size in selectivity). */ - virtual double evaluate(double x) { + virtual double evaluate(double x) + { fims_popdy::LogisticSelectivity LogisticSel; LogisticSel.inflection_point.resize(1); LogisticSel.inflection_point[0] = this->inflection_point.value_m; @@ -103,33 +107,42 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase { #ifdef TMB_MODEL template - bool add_to_fims_tmb_internal() { - std::shared_ptr > info = + bool add_to_fims_tmb_internal() + { + std::shared_ptr> info = fims_info::Information::GetInstance(); - std::shared_ptr > selectivity = - std::make_shared >(); + std::shared_ptr> selectivity = + std::make_shared>(); // set relative info selectivity->id = this->id; selectivity->inflection_point.resize(1); selectivity->inflection_point[0] = this->inflection_point.value_m; - if (this->inflection_point.estimated_m) { + if (this->inflection_point.estimated_m) + { info->RegisterParameterName("logistic selectivity inflection_point"); - if (this->inflection_point.is_random_effect_m) { + if (this->inflection_point.is_random_effect_m) + { info->RegisterRandomEffect(selectivity->inflection_point[0]); - } else { + } + else + { info->RegisterParameter(selectivity->inflection_point[0]); } } info->variable_map[this->inflection_point.id_m] = &(selectivity)->inflection_point; selectivity->slope.resize(1); selectivity->slope[0] = this->slope.value_m; - if (this->slope.estimated_m) { + if (this->slope.estimated_m) + { info->RegisterParameterName("logistic selectivity slope"); - if (this->slope.is_random_effect_m) { + if (this->slope.is_random_effect_m) + { info->RegisterRandomEffect(selectivity->slope[0]); - } else { + } + else + { info->RegisterParameter(selectivity->slope[0]); } } @@ -143,7 +156,8 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase { /** @brief this adds the parameter values and derivatives to the TMB model * object */ - virtual bool add_to_fims_tmb() { + virtual bool add_to_fims_tmb() + { this->add_to_fims_tmb_internal(); this->add_to_fims_tmb_internal(); this->add_to_fims_tmb_internal(); @@ -159,16 +173,48 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase { * @brief Rcpp interface for logistic selectivity as an S4 object. To * instantiate from R: logistic_selectivity <- new(logistic_selectivity) */ -class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { - public: - Parameter inflection_point_asc; /**< the index value at which the response - reaches .5 */ - Parameter slope_asc; /**< the width of the curve at the inflection_point */ +class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase +{ +public: + Parameter inflection_point_asc; /**< the index value at which the response + reaches .5 */ + Parameter slope_asc; /**< the width of the curve at the inflection_point */ Parameter inflection_point_desc; /**< the index value at which the response reaches .5 */ - Parameter slope_desc; /**< the width of the curve at the inflection_point */ + Parameter slope_desc; /**< the width of the curve at the inflection_point */ - DoubleLogisticSelectivityInterface() : SelectivityInterfaceBase() {} + //' @rd + //' @name DoubleLogisticSelectivityInterface + //' @title Constructor for initializing a DoubleLogisticSelectivityInterface object + //' @description Initializes a `DoubleLogisticSelectivityInterface` object with specified ascending and descending limb inflection point and slope parameters. + //' @param inflection_point_asc A Parameter object representing the inflection point of the ascending limb of the double logistic selectivity curve. + //' @param slope_asc A Parameter object representing the slope of the ascending limb of the double logistic selectivity curve. + //' @param inflection_point_desc A Parameter object representing the inflection point of the descending limb of the double logistic selectivity curve. + //' @param slope_desc A Parameter object representing the slope of the descending limb of the double logistic selectivity curve. + //' @return No return value, as this is a constructor. + //' @examples + //' // R example of creating a DoubleLogisticSelectivityInterface object + //' inflection_point_asc <- methods::new(Parameter, 2, true) + //' slope_asc <- methods::new(Parameter, 0.2, true) + //' inflection_point_desc <- methods::new(Parameter, 2, true) + //' slope_desc <- methods::new(Parameter, 0.2, true) + //' double_logistic_selectivity <- methods::new( + //' DoubleLogisticSelectivityInterface, + //' inflection_point_asc, + //' slope_asc, + //' inflection_point_desc, + //' slope_desc + //' ) + DoubleLogisticSelectivityInterface(Parameter inflection_point_asc, + Parameter slope_asc, + Parameter inflection_point_desc, + Parameter slope_desc) : + SelectivityInterfaceBase() { + this->inflection_point_asc = inflection_point_asc; + this->slope_asc = slope_asc; + this->inflection_point_desc = inflection_point_desc; + this->slope_desc = slope_desc; + } virtual ~DoubleLogisticSelectivityInterface() {} @@ -179,7 +225,8 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { * @param x The independent variable in the logistic function (e.g., age or * size in selectivity). */ - virtual double evaluate(double x) { + virtual double evaluate(double x) + { fims_popdy::DoubleLogisticSelectivity DoubleLogisticSel; DoubleLogisticSel.inflection_point_asc.resize(1); DoubleLogisticSel.inflection_point_asc[0] = this->inflection_point_asc.value_m; @@ -196,55 +243,72 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { #ifdef TMB_MODEL template - bool add_to_fims_tmb_internal() { - std::shared_ptr > info = + bool add_to_fims_tmb_internal() + { + std::shared_ptr> info = fims_info::Information::GetInstance(); - std::shared_ptr > selectivity = - std::make_shared >(); + std::shared_ptr> selectivity = + std::make_shared>(); // set relative info selectivity->id = this->id; selectivity->inflection_point_asc.resize(1); selectivity->inflection_point_asc[0] = this->inflection_point_asc.value_m; - if (this->inflection_point_asc.estimated_m) { + if (this->inflection_point_asc.estimated_m) + { info->RegisterParameterName("double logistic selectivity inflection_point_asc"); - if (this->inflection_point_asc.is_random_effect_m) { + if (this->inflection_point_asc.is_random_effect_m) + { info->RegisterRandomEffect(selectivity->inflection_point_asc[0]); - } else { + } + else + { info->RegisterParameter(selectivity->inflection_point_asc[0]); } } info->variable_map[this->inflection_point_asc.id_m] = &(selectivity)->inflection_point_asc; selectivity->slope_asc.resize(1); selectivity->slope_asc[0] = this->slope_asc.value_m; - if (this->slope_asc.estimated_m) { + if (this->slope_asc.estimated_m) + { info->RegisterParameterName("double logistic selectivity slope_asc"); - if (this->slope_asc.is_random_effect_m) { + if (this->slope_asc.is_random_effect_m) + { info->RegisterRandomEffect(selectivity->slope_asc[0]); - } else { + } + else + { info->RegisterParameter(selectivity->slope_asc[0]); } } info->variable_map[this->slope_asc.id_m] = &(selectivity)->slope_asc; selectivity->inflection_point_desc.resize(1); selectivity->inflection_point_desc[0] = this->inflection_point_desc.value_m; - if (this->inflection_point_desc.estimated_m) { + if (this->inflection_point_desc.estimated_m) + { info->RegisterParameterName("double logistic selectivity inflection_point_desc"); - if (this->inflection_point_desc.is_random_effect_m) { + if (this->inflection_point_desc.is_random_effect_m) + { info->RegisterRandomEffect(selectivity->inflection_point_desc[0]); - } else { + } + else + { info->RegisterParameter(selectivity->inflection_point_desc[0]); } } info->variable_map[this->inflection_point_desc.id_m] = &(selectivity)->inflection_point_desc; selectivity->slope_desc.resize(1); selectivity->slope_desc[0] = this->slope_desc.value_m; - if (this->slope_desc.estimated_m) { + if (this->slope_desc.estimated_m) + { info->RegisterParameterName("double logistic selectivity slope_desc"); - if (this->slope_desc.is_random_effect_m) { + if (this->slope_desc.is_random_effect_m) + { info->RegisterRandomEffect(selectivity->slope_desc[0]); - } else { + } + else + { info->RegisterParameter(selectivity->slope_desc[0]); } } @@ -258,7 +322,8 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { /** @brief this adds the parameter values and derivatives to the TMB model * object */ - virtual bool add_to_fims_tmb() { + virtual bool add_to_fims_tmb() + { this->add_to_fims_tmb_internal(); this->add_to_fims_tmb_internal(); this->add_to_fims_tmb_internal(); diff --git a/man/DoubleLogisticSelectivityInterface.Rd b/man/DoubleLogisticSelectivityInterface.Rd new file mode 100644 index 00000000..6937b14c --- /dev/null +++ b/man/DoubleLogisticSelectivityInterface.Rd @@ -0,0 +1,22 @@ +\name{Constructor for initializing a DoubleLogisticSelectivityInterface object} +\title{Constructor for initializing a DoubleLogisticSelectivityInterface object} +\description{ +Initializes a `DoubleLogisticSelectivityInterface` object with specified ascending and descending limb inflection point and slope parameters. +} +\arguments{ +\item{inflection_point_asc}{A Parameter object representing the inflection point of the ascending limb of the double logistic selectivity curve. + //'} +\item{slope_asc}{A Parameter object representing the slope of the ascending limb of the double logistic selectivity curve. + //'} +\item{inflection_point_desc}{A Parameter object representing the inflection point of the descending limb of the double logistic selectivity curve. + //'} +\item{slope_desc}{A Parameter object representing the slope of the descending limb of the double logistic selectivity curve. + //'} +} +\value{ +No return value, as this is a constructor. +} +\examples{ + +} + diff --git a/man/LogisticMaturityInterface.Rd b/man/LogisticMaturityInterface.Rd new file mode 100644 index 00000000..5a0aae2c --- /dev/null +++ b/man/LogisticMaturityInterface.Rd @@ -0,0 +1,18 @@ +\name{Constructor for initializing a LogisticMaturityInterface object} +\title{Constructor for initializing a LogisticMaturityInterface object} +\description{ +Initializes a LogisticMaturityInterface object with specified inflection point and slope parameters. +} +\arguments{ +\item{inflection_point}{A Parameter object representing the inflection point of the logistic selectivity curve. + //'} +\item{slope}{A Parameter object representing the slope of the logistic selectivity curve. + //'} +} +\value{ +No return value, as this is a constructor. +} +\examples{ + +} + diff --git a/man/format_rd_content.Rd b/man/format_rd_content.Rd index 5bb8857b..5424aee7 100644 --- a/man/format_rd_content.Rd +++ b/man/format_rd_content.Rd @@ -10,12 +10,12 @@ format_rd_content(rd_block) \item{rd_block}{A string containing the raw RD block extracted from a C++ or a header file.} } \value{ -A formatted string representing the content of the \code{.Rd} file, -including sections such as \verb{@title}, \verb{@description}, \verb{@param}, \verb{@return}, -and \verb{@examples}. +A formatted string representing the content of the .Rd file, +including sections such as title, description, param, return, +and examples. } \description{ This function takes an RD block extracted from a C++ or a header file, parses its -components, and formats it into a string suitable for inclusion in an \code{.Rd} +components, and formats it into a string suitable for inclusion in an .Rd documentation file. } diff --git a/man/process_cpp_file.Rd b/man/process_cpp_file.Rd index ea9cab71..09641ead 100644 --- a/man/process_cpp_file.Rd +++ b/man/process_cpp_file.Rd @@ -12,22 +12,5 @@ process_cpp_file(file_path) \description{ This function reads a specified C++ file, searches for RD blocks (documentation comments) within the file, and writes the extracted -and formatted RD content to an \code{.Rd} file in the \verb{man/} directory. -} -\details{ -The function identifies RD blocks in three formats: -\itemize{ -\item Block comments: \verb{/** @rd ... */}. -\item Line comments: \verb{// @rd ...} -\item Line comments with a single quote: \verb{//' @rd ...} -} - -After extracting these blocks, it saves the results in an \code{.Rd} file named -after the function name extracted from the \verb{@name} tag within the RD block. -If no RD blocks are found, the function will output a message indicating this. -} -\examples{ -\dontrun{ -process_cpp_file("my_cpp_file.cpp") -} +and formatted RD content to an .Rd file in the man/ directory. } diff --git a/man/process_directory.Rd b/man/process_directory.Rd index 3acb2283..95910180 100644 --- a/man/process_directory.Rd +++ b/man/process_directory.Rd @@ -16,14 +16,3 @@ directory, including subdirectories, by applying the \code{process_cpp_file} function to each file. It searches for RD blocks in the files and generates corresponding \code{.Rd} files. } -\details{ -The function uses the \code{fs::dir_ls} function to list all \code{.cpp} and \code{.hpp} -files within the directory and its subdirectories. It then processes each -file individually, extracting and formatting RD blocks and creating \code{.Rd} -files. -} -\examples{ -\dontrun{ -process_directory("inst/include/interface/rcpp") -} -} diff --git a/tests/testthat/helper-integration-tests-setup.R b/tests/testthat/helper-integration-tests-setup.R index 7cfc0529..571d3952 100644 --- a/tests/testthat/helper-integration-tests-setup.R +++ b/tests/testthat/helper-integration-tests-setup.R @@ -120,13 +120,11 @@ setup_and_run_FIMS <- function(iter_id, ewaa_growth$weights <- om_input$W.mt # Maturity - maturity <- new(LogisticMaturity) - maturity$inflection_point$value <- om_input$A50.mat - maturity$inflection_point$is_random_effect <- FALSE - maturity$inflection_point$estimated <- FALSE - maturity$slope$value <- om_input$slope - maturity$slope$is_random_effect <- FALSE - maturity$slope$estimated <- FALSE + maturity <- new( + LogisticMaturity, + new(Parameter, om_input$A50.mat, FALSE), + new(Parameter, om_input$slope, FALSE) + ) # Fleet # Create the fishing fleet diff --git a/tests/testthat/test-rcpp-maturity-interface.R b/tests/testthat/test-rcpp-maturity-interface.R index 4ded4336..63a775fd 100644 --- a/tests/testthat/test-rcpp-maturity-interface.R +++ b/tests/testthat/test-rcpp-maturity-interface.R @@ -1,19 +1,14 @@ test_that("Maturity input settings work as expected", { # Create maturity1 - maturity1 <- new(LogisticMaturity) - - maturity1$inflection_point$value <- 10.0 - maturity1$inflection_point$min <- 8.0 - maturity1$inflection_point$max <- 12.0 - maturity1$inflection_point$is_random_effect <- TRUE - maturity1$inflection_point$estimated <- TRUE - maturity1$slope$value <- 0.2 + maturity1 <- new( + LogisticMaturity, + new(Parameter, 10, TRUE), + new(Parameter, 0.2, TRUE) + ) expect_equal(maturity1$get_id(), 1) expect_equal(maturity1$inflection_point$value, 10.0) - expect_equal(maturity1$inflection_point$min, 8.0) - expect_equal(maturity1$inflection_point$max, 12.0) - expect_true(maturity1$inflection_point$is_random_effect) + expect_false(maturity1$inflection_point$is_random_effect) expect_true(maturity1$inflection_point$estimated) expect_equal(maturity1$slope$value, 0.2) expect_equal(maturity1$evaluate(10.0), 0.5) diff --git a/tests/testthat/test-rcpp-selectivity-interface.R b/tests/testthat/test-rcpp-selectivity-interface.R index 6655191b..4a480cc7 100644 --- a/tests/testthat/test-rcpp-selectivity-interface.R +++ b/tests/testthat/test-rcpp-selectivity-interface.R @@ -33,12 +33,13 @@ test_that("LogisticSelectivityInterface is initialized correctly", { test_that("DoubleLogisticSelectivity input settings work as expected", { # Test double logistic - selectivity <- new(DoubleLogisticSelectivity) - - selectivity$inflection_point_asc$value <- 10.5 - selectivity$slope_asc$value <- 0.2 - selectivity$inflection_point_desc$value <- 15.0 - selectivity$slope_desc$value <- 0.05 + selectivity <- new( + DoubleLogisticSelectivity, + new(Parameter, 10.5, TRUE), + new(Parameter, 0.2, TRUE), + new(Parameter, 15.0, TRUE), + new(Parameter, 0.05, TRUE) + ) expect_equal(selectivity$get_id(), 1) expect_equal(selectivity$inflection_point_asc$value, 10.5) diff --git a/vignettes/fims-demo.Rmd b/vignettes/fims-demo.Rmd index 5ec9d5b9..61eb7cec 100644 --- a/vignettes/fims-demo.Rmd +++ b/vignettes/fims-demo.Rmd @@ -116,13 +116,6 @@ fishing_fleet_selectivity <- new( fishing_fleet_selectivity_inflection, fishing_fleet_selectivity_slope ) -# fishing_fleet_selectivity <- methods::new(LogisticSelectivity) -# fishing_fleet_selectivity$inflection_point$value <- 2.0 -# fishing_fleet_selectivity$inflection_point$is_random_effect <- FALSE -# fishing_fleet_selectivity$inflection_point$estimated <- TRUE -# fishing_fleet_selectivity$slope$value <- 1.0 -# fishing_fleet_selectivity$slope$is_random_effect <- FALSE -# fishing_fleet_selectivity$slope$estimated <- TRUE ``` #### Creating the Fleet Object @@ -212,14 +205,6 @@ survey_fleet_selectivity <- new( survey_fleet_selectivity_inflection, survey_fleet_selectivity_slope ) - -# survey_fleet_selectivity <- new(LogisticSelectivity) -# survey_fleet_selectivity$inflection_point$value <- 1.5 -# survey_fleet_selectivity$inflection_point$is_random_effect <- FALSE -# survey_fleet_selectivity$inflection_point$estimated <- TRUE -# survey_fleet_selectivity$slope$value <- 2.0 -# survey_fleet_selectivity$slope$is_random_effect <- FALSE -# survey_fleet_selectivity$slope$estimated <- TRUE ``` @@ -325,13 +310,13 @@ ewaa_growth$weights <- c( Each population will also need a maturity model. Here we define a logistic maturity model. ```{r maturity} # Maturity -maturity <- new(LogisticMaturity) -maturity$inflection_point$value <- 2.25 -maturity$inflection_point$is_random_effect <- FALSE -maturity$inflection_point$estimated <- FALSE -maturity$slope$value <- 3 -maturity$slope$is_random_effect <- FALSE -maturity$slope$estimated <- FALSE +maturity_inflection <- new(Parameter, 2.25, FALSE) +maturity_slope <- new(Parameter, 3.0, FALSE) +maturity <- new( + LogisticMaturity, + maturity_inflection, + maturity_slope + ) ``` Now that our life history sub-models are defined, lets define the actual population.