Skip to content

Commit

Permalink
refactor(D*Distribution): Removes TMB from names
Browse files Browse the repository at this point in the history
Removes TMB from Distribution names and files to clean up the code and
allow the downstream R interface to also remove these names. This is part
of ensuring that the code is portable. If we change from TMB in the future
we do not want to have to rename these things when a distribution is
just a distribution, it does not have to come from TMB. Thank you
@msupernaw for these changes.
  • Loading branch information
msupernaw authored and kellijohnson-NOAA committed Nov 25, 2024
1 parent 1c55ee1 commit 7f72769
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 82 deletions.
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
export(AgeComp)
export(BevertonHoltRecruitment)
export(CreateTMBModel)
export(DlnormDistribution)
export(DmultinomDistribution)
export(DnormDistribution)
export(DoubleLogisticSelectivity)
export(EWAAgrowth)
export(FIMSFrame)
Expand All @@ -15,9 +18,6 @@ export(Parameter)
export(ParameterVector)
export(Population)
export(SetFIMSFunctions)
export(TMBDlnormDistribution)
export(TMBDmultinomDistribution)
export(TMBDnormDistribution)
export(ToJSON)
export(clear)
export(clear_logs)
Expand Down
6 changes: 3 additions & 3 deletions R/FIMS-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
#' @export Index
#' @export Population
#' @export ParameterVector
#' @export TMBDnormDistribution
#' @export DnormDistribution
#' @export LogisticMaturity
#' @export LogisticSelectivity
#' @export DoubleLogisticSelectivity
#' @export EWAAgrowth
#' @export TMBDlnormDistribution
#' @export TMBDmultinomDistribution
#' @export DlnormDistribution
#' @export DmultinomDistribution
## usethis namespace: end
NULL
30 changes: 15 additions & 15 deletions R/create_default_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
#' fleet1 <- survey1 <- list(
#' selectivity = list(form = "LogisticSelectivity"),
#' data_distribution = c(
#' Index = "TMBDlnormDistribution",
#' AgeComp = "TMBDmultinomDistribution"
#' Index = "DlnormDistribution",
#' AgeComp = "DmultinomDistribution"
#' )
#' )
#' default_parameters <- fims_frame |>
#' create_default_parameters(
#' fleets = list(fleet1 = fleet1, survey1 = survey1),
#' recruitment = list(
#' form = "BevertonHoltRecruitment",
#' process_distribution = c(log_devs = "TMBDnormDistribution")
#' process_distribution = c(log_devs = "DnormDistribution")
#' ),
#' growth = list(form = "EWAAgrowth"),
#' maturity = list(form = "LogisticMaturity")
Expand All @@ -46,7 +46,7 @@ create_default_parameters <- function(
fleets,
recruitment = list(
form = "BevertonHoltRecruitment",
process_distribution = c(log_devs = "TMBDnormDistribution")
process_distribution = c(log_devs = "DnormDistribution")
),
growth = list(form = "EWAAgrowth"),
maturity = list(form = "LogisticMaturity")) {
Expand Down Expand Up @@ -296,10 +296,10 @@ create_default_fleet <- function(fleets,
(\(x) x$uncertainty)()

index_distribution_default <- switch(index_distribution,
"TMBDnormDistribution" = create_default_TMBDnormDistribution(
"DnormDistribution" = create_default_DnormDistribution(
value = index_uncertainty, input_type = "data", data = data
),
"TMBDlnormDistribution" = create_default_TMBDlnormDistribution(
"DlnormDistribution" = create_default_DlnormDistribution(
value = index_uncertainty, input_type = "data", data = data
)
)
Expand Down Expand Up @@ -358,16 +358,16 @@ create_default_BevertonHoltRecruitment <- function(data) {
return(default)
}

#' Create Default TMBDnormDistribution Parameters
#' Create Default DnormDistribution Parameters
#'
#' @description
#' Create default parameters for TMBDnormDistribution.
#' Create default parameters for DnormDistribution.
#' @param value Default value for `log_sd`.
#' @param data An S4 object. FIMS input data.
#' @param input_type A character. Specifies if input is data or process.
#' @return A list of default parameters for TMBDnormDistribution.
#' @return A list of default parameters for DnormDistribution.
#' @noRd
create_default_TMBDnormDistribution <- function(value = log(0.4), data, input_type = "data") {
create_default_DnormDistribution <- function(value = log(0.4), data, input_type = "data") {
# Check if input_type is valid
valid_input_types <- c("data", "process")
check_valid_input(input = input_type, valid_options = valid_input_types, arg_name = "input_type")
Expand Down Expand Up @@ -396,16 +396,16 @@ create_default_TMBDnormDistribution <- function(value = log(0.4), data, input_ty
return(default)
}

#' Create Default TMBDlnormDistribution Parameters
#' Create Default DlnormDistribution Parameters
#'
#' @description
#' Create default parameters for TMBDlnormDistribution.
#' Create default parameters for DlnormDistribution.
#' @param value Default value for `log_sd`.
#' @param data An S4 object. FIMS input data.
#' @param input_type A character. Specifies if input is data or process.
#' @return A list of default parameters for TMBDlnormDistribution.
#' @return A list of default parameters for DlnormDistribution.
#' @noRd
create_default_TMBDlnormDistribution <- function(value = 0.1, data, input_type = "data") {
create_default_DlnormDistribution <- function(value = 0.1, data, input_type = "data") {
# Validate input value
if (!is.numeric(value) || any(value <= 0, na.rm = TRUE)) {
cli::cli_abort("The {.var value} argument must be positive numeric values.")
Expand Down Expand Up @@ -469,7 +469,7 @@ create_default_recruitment <- function(recruitment, data) {
distribution_default <- NULL
if (!is.null(distribution_input)) {
distribution_default <- switch(distribution_input,
"TMBDnormDistribution" = create_default_TMBDnormDistribution(
"DnormDistribution" = create_default_DnormDistribution(
value = 0.1,
data = data,
input_type = "process"
Expand Down
10 changes: 5 additions & 5 deletions R/distribution_formulas.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ initialize_data_distribution <- function(
# Set up distribution based on `family` argument`
if (family[["family"]] == "lognormal") {
# create new Rcpp module
new_module <- new(TMBDlnormDistribution)
new_module <- new(DlnormDistribution)

# populate logged standard deviation parameter with log of input
new_module$log_sd <- new(
Expand All @@ -186,7 +186,7 @@ initialize_data_distribution <- function(

if (family[["family"]] == "gaussian") {
# create new Rcpp module
new_module <- new(TMBDnormDistribution)
new_module <- new(DnormDistribution)

# populate logged standard deviation parameter with log of input
new_module$log_sd$resize(length(sd$value))
Expand All @@ -206,7 +206,7 @@ initialize_data_distribution <- function(

if (family[["family"]] == "multinomial") {
#create new Rcpp module
new_module <- new(TMBDmultinomDistribution)
new_module <- new(DmultinomDistribution)
}

# setup link to observed data
Expand Down Expand Up @@ -267,7 +267,7 @@ initialize_process_distribution <- function(module,
# Set up distribution based on `family` argument`
if (family[["family"]] == "lognormal") {
# create new Rcpp module
new_module <- new(TMBDlnormDistribution)
new_module <- new(DlnormDistribution)

# populate logged standard deviation parameter with log of input
new_module$log_sd <- new(
Expand All @@ -287,7 +287,7 @@ initialize_process_distribution <- function(module,

if (family[["family"]] == "gaussian") {
# create new Rcpp module
new_module <- new(TMBDnormDistribution)
new_module <- new(DnormDistribution)

# populate logged standard deviation parameter with log of input
new_module$log_sd$resize(length(sd$value))
Expand Down
10 changes: 5 additions & 5 deletions inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "rcpp_objects/rcpp_population.hpp"
#include "rcpp_objects/rcpp_recruitment.hpp"
#include "rcpp_objects/rcpp_selectivity.hpp"
#include "rcpp_objects/rcpp_tmb_distribution.hpp"
#include "rcpp_objects/rcpp_distribution.hpp"


SEXP FIMS_objective_function;
Expand Down Expand Up @@ -429,7 +429,7 @@ void clear() {
DoubleLogisticSelectivityInterface::id_g = 1;
DoubleLogisticSelectivityInterface::live_objects.clear();

// rcpp_tmb_distribution.hpp
// rcpp_distribution.hpp
DistributionsInterfaceBase::id_g = 1;
DistributionsInterfaceBase::live_objects.clear();

Expand Down Expand Up @@ -724,7 +724,7 @@ RCPP_MODULE(fims) {
.method("get_id", &EWAAGrowthInterface::get_id)
.method("evaluate", &EWAAGrowthInterface::evaluate);

Rcpp::class_<DnormDistributionsInterface>("TMBDnormDistribution")
Rcpp::class_<DnormDistributionsInterface>("DnormDistribution")
.constructor()
.method("get_id", &DnormDistributionsInterface::get_id, "Returns a unique ID for the Dnorm distribution class.")
.method("evaluate", &DnormDistributionsInterface::evaluate, "Evaluates the normal distribution given input data and parameter values.")
Expand All @@ -734,7 +734,7 @@ RCPP_MODULE(fims) {
.field("expected_values", &DnormDistributionsInterface::expected_values, "Mean of the distribution.")
.field("log_sd", &DnormDistributionsInterface::log_sd, "The natural log of the standard deviation.");

Rcpp::class_<DlnormDistributionsInterface>("TMBDlnormDistribution")
Rcpp::class_<DlnormDistributionsInterface>("DlnormDistribution")
.constructor()
.method("get_id", &DlnormDistributionsInterface::get_id, "Returns a unique ID for the Dnorm distribution class.")
.method("evaluate", &DlnormDistributionsInterface::evaluate, "Evaluates the normal distribution given input data and parameter values.")
Expand All @@ -744,7 +744,7 @@ RCPP_MODULE(fims) {
.field("expected_values", &DlnormDistributionsInterface::expected_values, "Mean of the distribution on the log scale.")
.field("log_sd", &DlnormDistributionsInterface::log_sd, "The natural log of the standard deviation of the distribution on the log scale.");

Rcpp::class_<DmultinomDistributionsInterface>("TMBDmultinomDistribution")
Rcpp::class_<DmultinomDistributionsInterface>("DmultinomDistribution")
.constructor()
.method("get_id", &DmultinomDistributionsInterface::get_id, "Returns a unique ID for the Dnorm distribution class.")
.method("evaluate", &DmultinomDistributionsInterface::evaluate, "Evaluates the normal distribution given input data and parameter values.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* source folder for reuse information.
*
*/
#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_TMB_DISTRIBUTION_HPP
#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_TMB_DISTRIBUTION_HPP
#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_DISTRIBUTION_HPP
#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_DISTRIBUTION_HPP

#include "../../../distributions/distributions.hpp"
#include "../../interface.hpp"
Expand Down Expand Up @@ -83,7 +83,7 @@ std::map<uint32_t,
/**
* @brief Rcpp interface for Dnorm as an S4 object. To instantiate
* from R:
* dnorm_ <- new(TMBDnormDistribution)
* dnorm_ <- new(DnormDistribution)
*
*/
class DnormDistributionsInterface : public DistributionsInterfaceBase {
Expand Down Expand Up @@ -280,7 +280,7 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase {
/**
* @brief Rcpp interface for Dlnorm as an S4 object. To instantiate
* from R:
* dlnorm_ <- new(TMBDlnormDistribution)
* dlnorm_ <- new(DlnormDistribution)
*
*/
class DlnormDistributionsInterface : public DistributionsInterfaceBase {
Expand Down Expand Up @@ -484,7 +484,7 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase {
/**
* @brief Rcpp interface for Dmultinom as an S4 object. To instantiate
* from R:
* dmultinom_ <- new(TMBDmultinomDistribution)
* dmultinom_ <- new(DmultinomDistribution)
*
*/
// template <typename Type>
Expand Down
8 changes: 4 additions & 4 deletions man/create_default_parameters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions tests/testthat/helper-integration-tests-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ setup_and_run_FIMS_without_wrappers <- function(iter_id,
fishing_fleet$SetObservedAgeCompData(fishing_fleet_age_comp$get_id())

# Set up fishery index data using the lognormal
fishing_fleet_index_distribution <- methods::new(TMBDlnormDistribution)
fishing_fleet_index_distribution <- methods::new(DlnormDistribution)
# lognormal observation error transformed on the log scale
fishing_fleet_index_distribution$log_sd$resize(om_input$nyr)
for (y in 1:om_input$nyr) {
Expand All @@ -106,7 +106,7 @@ setup_and_run_FIMS_without_wrappers <- function(iter_id,
fishing_fleet_index_distribution$set_distribution_links("data", fishing_fleet$log_expected_index$get_id())

# Set up fishery age composition data using the multinomial
fishing_fleet_agecomp_distribution <- methods::new(TMBDmultinomDistribution)
fishing_fleet_agecomp_distribution <- methods::new(DmultinomDistribution)
fishing_fleet_agecomp_distribution$set_observed_data(fishing_fleet$GetObservedAgeCompDataID())
fishing_fleet_agecomp_distribution$set_distribution_links("data", fishing_fleet$proportion_catch_numbers_at_age$get_id())

Expand Down Expand Up @@ -141,7 +141,7 @@ setup_and_run_FIMS_without_wrappers <- function(iter_id,
survey_fleet$SetObservedAgeCompData(survey_fleet_age_comp$get_id())

# Set up survey index data using the lognormal
survey_fleet_index_distribution <- methods::new(TMBDlnormDistribution)
survey_fleet_index_distribution <- methods::new(DlnormDistribution)
# lognormal observation error transformed on the log scale
# sd = sqrt(log(cv^2 + 1)), sd is log transformed
survey_fleet_index_distribution$log_sd$resize(om_input$nyr)
Expand All @@ -155,7 +155,7 @@ setup_and_run_FIMS_without_wrappers <- function(iter_id,

# Age composition data

survey_fleet_agecomp_distribution <- methods::new(TMBDmultinomDistribution)
survey_fleet_agecomp_distribution <- methods::new(DmultinomDistribution)
survey_fleet_agecomp_distribution$set_observed_data(survey_fleet$GetObservedAgeCompDataID())
survey_fleet_agecomp_distribution$set_distribution_links("data", survey_fleet$proportion_catch_numbers_at_age$get_id())

Expand Down Expand Up @@ -186,7 +186,7 @@ setup_and_run_FIMS_without_wrappers <- function(iter_id,
for (y in 1:(om_input$nyr - 1)) {
recruitment$log_devs[y]$value <- om_input$logR.resid[y + 1]
}
recruitment_distribution <- new(TMBDnormDistribution)
recruitment_distribution <- new(DnormDistribution)
# set up logR_sd using the normal log_sd parameter
# logR_sd is NOT logged. It needs to enter the model logged b/c the exp() is
# taken before the likelihood calculation
Expand Down Expand Up @@ -449,15 +449,15 @@ setup_and_run_FIMS_with_wrappers <- function(iter_id,
fleet1 = list(
selectivity = list(form = "LogisticSelectivity"),
data_distribution = c(
Index = "TMBDlnormDistribution",
AgeComp = "TMBDmultinomDistribution"
Index = "DlnormDistribution",
AgeComp = "DmultinomDistribution"
)
),
survey1 = list(
selectivity = list(form = "LogisticSelectivity"),
data_distribution = c(
Index = "TMBDlnormDistribution",
AgeComp = "TMBDmultinomDistribution"
Index = "DlnormDistribution",
AgeComp = "DmultinomDistribution"
)
)
)
Expand All @@ -467,7 +467,7 @@ setup_and_run_FIMS_with_wrappers <- function(iter_id,
fleets = fleets,
recruitment = list(
form = "BevertonHoltRecruitment",
process_distribution = c(log_devs = "TMBDnormDistribution")
process_distribution = c(log_devs = "DnormDistribution")
),
growth = list(form = "EWAAgrowth"),
maturity = list(form = "LogisticMaturity")
Expand All @@ -489,7 +489,7 @@ setup_and_run_FIMS_with_wrappers <- function(iter_id,
BevertonHoltRecruitment.log_rzero.value = log(om_input$R0),
BevertonHoltRecruitment.log_devs.value = om_input$logR.resid[-1],
BevertonHoltRecruitment.log_devs.estimated = FALSE,
TMBDnormDistribution.log_sd.value = om_input$logR_sd
DnormDistribution.log_sd.value = om_input$logR_sd
),
maturity = list(
LogisticMaturity.inflection_point.value = om_input$A50.mat,
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-create_default_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ data <- FIMSFrame(data_mile1)
fleet1 <- survey1 <- list(
selectivity = list(form = "LogisticSelectivity"),
data_distribution = c(
Index = "TMBDlnormDistribution",
AgeComp = "TMBDmultinomDistribution"
Index = "DlnormDistribution",
AgeComp = "DmultinomDistribution"
)
)

Expand All @@ -31,8 +31,8 @@ test_that("create_default_parameters detects missing fleet names", {
invalid_fleet <- list(
selectivity = list(form = "LogisticSelectivity"),
data_distribution = c(
Index = "TMBDlnormDistribution",
AgeComp = "TMBDmultinomDistribution"
Index = "DlnormDistribution",
AgeComp = "DmultinomDistribution"
)
)

Expand Down
Loading

0 comments on commit 7f72769

Please sign in to comment.