From a036bb141420868f208c76b521536bf01c71a317 Mon Sep 17 00:00:00 2001 From: Anna Petrasova Date: Fri, 26 Apr 2024 14:18:01 -0400 Subject: [PATCH] Pass multiple random seeds to pops-core --- R/checks.R | 2 +- R/helpers.R | 1 + R/pops_model.R | 8 ++++---- inst/extdata/simple2x2/randoms.csv | 8 ++++---- inst/include/PoPS_RcppExports.h | 4 ++-- src/RcppExports.cpp | 6 +++--- src/pops.cpp | 6 +++++- 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/R/checks.R b/R/checks.R index 36fd62db..34b1a191 100644 --- a/R/checks.R +++ b/R/checks.R @@ -686,7 +686,7 @@ random_seeds_file_checks <- function(x, number_of_iterations = 1) { if (checks_passed) { random_seeds <- read.table(x, sep = ",", header = TRUE) - if (base::ncol(random_seeds) != 9 || base::nrow(random_seeds) <= number_of_iterations) { + if (base::ncol(random_seeds) != 10 || base::nrow(random_seeds) <= number_of_iterations) { checks_passed <- FALSE failed_check <- random_seeds_dimensions_error } diff --git a/R/helpers.R b/R/helpers.R index 88b1e382..b2625107 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -234,6 +234,7 @@ create_random_seeds <- function(n) { anthropogenic_dispersal = sample(1:999999999999, n, replace = FALSE), establishment = sample(1:999999999, n, replace = FALSE), weather = sample(1:999999999, n, replace = FALSE), + lethal_temperature = sample(1:999999999, n, replace = FALSE), movement = sample(1:999999999, n, replace = FALSE), overpopulation = sample(1:999999999, n, replace = FALSE), survival_rate = sample(1:999999999, n, replace = FALSE), diff --git a/R/pops_model.R b/R/pops_model.R index 53319722..876eeac9 100644 --- a/R/pops_model.R +++ b/R/pops_model.R @@ -75,8 +75,8 @@ #' of dispersers that fall to the soil and survive. #' @param soil_reservoirs list of matrices with soil pests created from soil_pest_file. #' @param random_seeds vector of random seeds in the order of "disperser_generation", -#' "natural_dispersal", "anthropogenic_dispersal", "establishment", "weather", "movement", -#' "overpopulation", "survival_rate", "soil" +#' "natural_dispersal", "anthropogenic_dispersal", "establishment", "weather", +#' "lethal_temperature", "movement", "overpopulation", "survival_rate", "soil" #' #' @return list of vector matrices of infected and susceptible hosts per #' simulated year and associated statistics (e.g. spread rate) @@ -218,8 +218,8 @@ pops_model <- data <- suppressWarnings(pops_model_cpp(random_seed = random_seed, - multiple_random_seeds, - random_seeds, + multiple_random_seeds = multiple_random_seeds, + random_seeds = random_seeds, lethal_temperature = lethal_temperature, lethal_temperature_month = lethal_temperature_month, host_pools = host_pools, diff --git a/inst/extdata/simple2x2/randoms.csv b/inst/extdata/simple2x2/randoms.csv index 7ea22eaf..90f0341a 100644 --- a/inst/extdata/simple2x2/randoms.csv +++ b/inst/extdata/simple2x2/randoms.csv @@ -1,4 +1,4 @@ -"disperser_generation","natural_dispersal","anthropogenic_dispersal","establishment","weather","movement","overpopulation","survival_rate","soil" -996292699,421557257,385173832685,233255382,277700357,5882493,141871612,243207946,297251982 -339502238,794154015,385173832685,868856436,605415172,268034709,468192168,938812543,964803710 -709470618,221623141,385173832685,137416076,182481148,728626102,535321254,54098512,597436749 +"disperser_generation","natural_dispersal","anthropogenic_dispersal","establishment","weather","lethal_temperature","movement","overpopulation","survival_rate","soil" +996292699,421557257,385173832685,233255382,277700357,609919634,5882493,141871612,243207946,297251982 +339502238,794154015,385173832685,868856436,605415172,609919634,268034709,468192168,938812543,964803710 +709470618,221623141,385173832685,137416076,182481148,609919634,728626102,535321254,54098512,597436749 diff --git a/inst/include/PoPS_RcppExports.h b/inst/include/PoPS_RcppExports.h index b0e48deb..fc907865 100644 --- a/inst/include/PoPS_RcppExports.h +++ b/inst/include/PoPS_RcppExports.h @@ -24,11 +24,11 @@ namespace PoPS { } } - inline List pops_model_cpp(int random_seed, bool multiple_random_seeds, std::vector random_seeds, double lethal_temperature, int lethal_temperature_month, std::vector host_pools, IntegerMatrix total_populations, std::vector> competency_table, std::vector> pest_host_table, IntegerMatrix quarantine_areas, std::string quarantine_directions, std::vector treatment_maps, std::vector treatment_dates, std::vector pesticide_duration, std::vector> movements, std::vector movements_dates, std::vector temperature, std::vector survival_rates, std::vector weather_coefficient, std::vector weather_coefficient_sd, List bbox, List res, List rows_cols, std::vector soil_reservoirs, double reproductive_rate, std::vector> spatial_indices, List season_month_start_end, List frequency_config, List bool_config, std::string start_date = "2018-01-01", std::string end_date = "2018-12-31", std::string treatment_method = "ratio", std::string natural_kernel_type = "cauchy", std::string anthropogenic_kernel_type = "cauchy", double percent_natural_dispersal = 0.0, double natural_distance_scale = 21, double anthropogenic_distance_scale = 0.0, std::string natural_dir = "NONE", double natural_kappa = 0, std::string anthropogenic_dir = "NONE", double anthropogenic_kappa = 0, Nullable frequencies_n_config = R_NilValue, std::string model_type_ = "SI", int latency_period = 0, double establishment_probability = 0, double dispersal_percentage = 0.99, int survival_rate_month = 0, int survival_rate_day = 0, Nullable overpopulation_config = R_NilValue, Nullable network_config = R_NilValue, Nullable network_data_config = R_NilValue, int weather_size = 0, std::string weather_type = "deterministic", double dispersers_to_soils_percentage = 0) { + inline List pops_model_cpp(int random_seed, bool multiple_random_seeds, std::vector random_seeds, double lethal_temperature, int lethal_temperature_month, std::vector host_pools, IntegerMatrix total_populations, std::vector> competency_table, std::vector> pest_host_table, IntegerMatrix quarantine_areas, std::string quarantine_directions, std::vector treatment_maps, std::vector treatment_dates, std::vector pesticide_duration, std::vector> movements, std::vector movements_dates, std::vector temperature, std::vector survival_rates, std::vector weather_coefficient, std::vector weather_coefficient_sd, List bbox, List res, List rows_cols, std::vector soil_reservoirs, double reproductive_rate, std::vector> spatial_indices, List season_month_start_end, List frequency_config, List bool_config, std::string start_date = "2018-01-01", std::string end_date = "2018-12-31", std::string treatment_method = "ratio", std::string natural_kernel_type = "cauchy", std::string anthropogenic_kernel_type = "cauchy", double percent_natural_dispersal = 0.0, double natural_distance_scale = 21, double anthropogenic_distance_scale = 0.0, std::string natural_dir = "NONE", double natural_kappa = 0, std::string anthropogenic_dir = "NONE", double anthropogenic_kappa = 0, Nullable frequencies_n_config = R_NilValue, std::string model_type_ = "SI", int latency_period = 0, double establishment_probability = 0, double dispersal_percentage = 0.99, int survival_rate_month = 0, int survival_rate_day = 0, Nullable overpopulation_config = R_NilValue, Nullable network_config = R_NilValue, Nullable network_data_config = R_NilValue, int weather_size = 0, std::string weather_type = "deterministic", double dispersers_to_soils_percentage = 0) { typedef SEXP(*Ptr_pops_model_cpp)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP); static Ptr_pops_model_cpp p_pops_model_cpp = NULL; if (p_pops_model_cpp == NULL) { - validateSignature("List(*pops_model_cpp)(int,bool,std::vector,double,int,std::vector,IntegerMatrix,std::vector>,std::vector>,IntegerMatrix,std::string,std::vector,std::vector,std::vector,std::vector>,std::vector,std::vector,std::vector,std::vector,std::vector,List,List,List,std::vector,double,std::vector>,List,List,List,std::string,std::string,std::string,std::string,std::string,double,double,double,std::string,double,std::string,double,Nullable,std::string,int,double,double,int,int,Nullable,Nullable,Nullable,int,std::string,double)"); + validateSignature("List(*pops_model_cpp)(int,bool,std::vector,double,int,std::vector,IntegerMatrix,std::vector>,std::vector>,IntegerMatrix,std::string,std::vector,std::vector,std::vector,std::vector>,std::vector,std::vector,std::vector,std::vector,std::vector,List,List,List,std::vector,double,std::vector>,List,List,List,std::string,std::string,std::string,std::string,std::string,double,double,double,std::string,double,std::string,double,Nullable,std::string,int,double,double,int,int,Nullable,Nullable,Nullable,int,std::string,double)"); p_pops_model_cpp = (Ptr_pops_model_cpp)R_GetCCallable("PoPS", "_PoPS_pops_model_cpp"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index fbddfed5..7740fcba 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -14,13 +14,13 @@ Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); #endif // pops_model_cpp -List pops_model_cpp(int random_seed, bool multiple_random_seeds, std::vector random_seeds, double lethal_temperature, int lethal_temperature_month, std::vector host_pools, IntegerMatrix total_populations, std::vector> competency_table, std::vector> pest_host_table, IntegerMatrix quarantine_areas, std::string quarantine_directions, std::vector treatment_maps, std::vector treatment_dates, std::vector pesticide_duration, std::vector> movements, std::vector movements_dates, std::vector temperature, std::vector survival_rates, std::vector weather_coefficient, std::vector weather_coefficient_sd, List bbox, List res, List rows_cols, std::vector soil_reservoirs, double reproductive_rate, std::vector> spatial_indices, List season_month_start_end, List frequency_config, List bool_config, std::string start_date, std::string end_date, std::string treatment_method, std::string natural_kernel_type, std::string anthropogenic_kernel_type, double percent_natural_dispersal, double natural_distance_scale, double anthropogenic_distance_scale, std::string natural_dir, double natural_kappa, std::string anthropogenic_dir, double anthropogenic_kappa, Nullable frequencies_n_config, std::string model_type_, int latency_period, double establishment_probability, double dispersal_percentage, int survival_rate_month, int survival_rate_day, Nullable overpopulation_config, Nullable network_config, Nullable network_data_config, int weather_size, std::string weather_type, double dispersers_to_soils_percentage); +List pops_model_cpp(int random_seed, bool multiple_random_seeds, std::vector random_seeds, double lethal_temperature, int lethal_temperature_month, std::vector host_pools, IntegerMatrix total_populations, std::vector> competency_table, std::vector> pest_host_table, IntegerMatrix quarantine_areas, std::string quarantine_directions, std::vector treatment_maps, std::vector treatment_dates, std::vector pesticide_duration, std::vector> movements, std::vector movements_dates, std::vector temperature, std::vector survival_rates, std::vector weather_coefficient, std::vector weather_coefficient_sd, List bbox, List res, List rows_cols, std::vector soil_reservoirs, double reproductive_rate, std::vector> spatial_indices, List season_month_start_end, List frequency_config, List bool_config, std::string start_date, std::string end_date, std::string treatment_method, std::string natural_kernel_type, std::string anthropogenic_kernel_type, double percent_natural_dispersal, double natural_distance_scale, double anthropogenic_distance_scale, std::string natural_dir, double natural_kappa, std::string anthropogenic_dir, double anthropogenic_kappa, Nullable frequencies_n_config, std::string model_type_, int latency_period, double establishment_probability, double dispersal_percentage, int survival_rate_month, int survival_rate_day, Nullable overpopulation_config, Nullable network_config, Nullable network_data_config, int weather_size, std::string weather_type, double dispersers_to_soils_percentage); static SEXP _PoPS_pops_model_cpp_try(SEXP random_seedSEXP, SEXP multiple_random_seedsSEXP, SEXP random_seedsSEXP, SEXP lethal_temperatureSEXP, SEXP lethal_temperature_monthSEXP, SEXP host_poolsSEXP, SEXP total_populationsSEXP, SEXP competency_tableSEXP, SEXP pest_host_tableSEXP, SEXP quarantine_areasSEXP, SEXP quarantine_directionsSEXP, SEXP treatment_mapsSEXP, SEXP treatment_datesSEXP, SEXP pesticide_durationSEXP, SEXP movementsSEXP, SEXP movements_datesSEXP, SEXP temperatureSEXP, SEXP survival_ratesSEXP, SEXP weather_coefficientSEXP, SEXP weather_coefficient_sdSEXP, SEXP bboxSEXP, SEXP resSEXP, SEXP rows_colsSEXP, SEXP soil_reservoirsSEXP, SEXP reproductive_rateSEXP, SEXP spatial_indicesSEXP, SEXP season_month_start_endSEXP, SEXP frequency_configSEXP, SEXP bool_configSEXP, SEXP start_dateSEXP, SEXP end_dateSEXP, SEXP treatment_methodSEXP, SEXP natural_kernel_typeSEXP, SEXP anthropogenic_kernel_typeSEXP, SEXP percent_natural_dispersalSEXP, SEXP natural_distance_scaleSEXP, SEXP anthropogenic_distance_scaleSEXP, SEXP natural_dirSEXP, SEXP natural_kappaSEXP, SEXP anthropogenic_dirSEXP, SEXP anthropogenic_kappaSEXP, SEXP frequencies_n_configSEXP, SEXP model_type_SEXP, SEXP latency_periodSEXP, SEXP establishment_probabilitySEXP, SEXP dispersal_percentageSEXP, SEXP survival_rate_monthSEXP, SEXP survival_rate_daySEXP, SEXP overpopulation_configSEXP, SEXP network_configSEXP, SEXP network_data_configSEXP, SEXP weather_sizeSEXP, SEXP weather_typeSEXP, SEXP dispersers_to_soils_percentageSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::traits::input_parameter< int >::type random_seed(random_seedSEXP); Rcpp::traits::input_parameter< bool >::type multiple_random_seeds(multiple_random_seedsSEXP); - Rcpp::traits::input_parameter< std::vector >::type random_seeds(random_seedsSEXP); + Rcpp::traits::input_parameter< std::vector >::type random_seeds(random_seedsSEXP); Rcpp::traits::input_parameter< double >::type lethal_temperature(lethal_temperatureSEXP); Rcpp::traits::input_parameter< int >::type lethal_temperature_month(lethal_temperature_monthSEXP); Rcpp::traits::input_parameter< std::vector >::type host_pools(host_poolsSEXP); @@ -105,7 +105,7 @@ RcppExport SEXP _PoPS_pops_model_cpp(SEXP random_seedSEXP, SEXP multiple_random_ static int _PoPS_RcppExport_validate(const char* sig) { static std::set signatures; if (signatures.empty()) { - signatures.insert("List(*pops_model_cpp)(int,bool,std::vector,double,int,std::vector,IntegerMatrix,std::vector>,std::vector>,IntegerMatrix,std::string,std::vector,std::vector,std::vector,std::vector>,std::vector,std::vector,std::vector,std::vector,std::vector,List,List,List,std::vector,double,std::vector>,List,List,List,std::string,std::string,std::string,std::string,std::string,double,double,double,std::string,double,std::string,double,Nullable,std::string,int,double,double,int,int,Nullable,Nullable,Nullable,int,std::string,double)"); + signatures.insert("List(*pops_model_cpp)(int,bool,std::vector,double,int,std::vector,IntegerMatrix,std::vector>,std::vector>,IntegerMatrix,std::string,std::vector,std::vector,std::vector,std::vector>,std::vector,std::vector,std::vector,std::vector,std::vector,List,List,List,std::vector,double,std::vector>,List,List,List,std::string,std::string,std::string,std::string,std::string,double,double,double,std::string,double,std::string,double,Nullable,std::string,int,double,double,int,int,Nullable,Nullable,Nullable,int,std::string,double)"); } return signatures.find(sig) != signatures.end(); } diff --git a/src/pops.cpp b/src/pops.cpp index e76c7463..ea8359cd 100644 --- a/src/pops.cpp +++ b/src/pops.cpp @@ -62,7 +62,7 @@ struct OutputHostPool { List pops_model_cpp( int random_seed, bool multiple_random_seeds, - std::vector random_seeds, + std::vector random_seeds, double lethal_temperature, int lethal_temperature_month, std::vector host_pools, @@ -117,6 +117,10 @@ List pops_model_cpp( { Config config; config.random_seed = random_seed; + config.multiple_random_seeds = multiple_random_seeds; + if (multiple_random_seeds) { + config.read_seeds(random_seeds); + } config.rows = rows_cols["num_rows"]; config.cols = rows_cols["num_cols"]; config.ew_res = res["ew_res"];