Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get categories of CategoricalVariable #81

Merged
merged 4 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ categorical_variable_get_size_of <- function(variable, values) {
.Call(`_individual_categorical_variable_get_size_of`, variable, values)
}

categorical_variable_get_categories <- function(variable) {
.Call(`_individual_categorical_variable_get_categories`, variable)
}

categorical_variable_queue_update_vector <- function(variable, value, index) {
invisible(.Call(`_individual_categorical_variable_queue_update_vector`, variable, value, index))
}
Expand Down
8 changes: 8 additions & 0 deletions R/categorical_variable.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ CategoricalVariable <- R6::R6Class(
categorical_variable_get_size_of(self$.variable, values)
},

#' @description return a character vector of possible values.
#' Note that the order of the returned vector may not be the same order
#' that was given when the variable was intitialized, due to the underlying
#' unordered storage type.
get_categories = function() {
categorical_variable_get_categories(self$.variable)
},

#' @description queue an update for this variable
#' @param values the new values
#' @param index the indices of individuals whose value will be updated
Expand Down
14 changes: 14 additions & 0 deletions man/CategoricalVariable.Rd

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

12 changes: 12 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// categorical_variable_get_categories
std::vector<std::string> categorical_variable_get_categories(Rcpp::XPtr<CategoricalVariable> variable);
RcppExport SEXP _individual_categorical_variable_get_categories(SEXP variableSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::XPtr<CategoricalVariable> >::type variable(variableSEXP);
rcpp_result_gen = Rcpp::wrap(categorical_variable_get_categories(variable));
return rcpp_result_gen;
END_RCPP
}
// categorical_variable_queue_update_vector
void categorical_variable_queue_update_vector(Rcpp::XPtr<CategoricalVariable> variable, const std::string& value, std::vector<size_t>& index);
RcppExport SEXP _individual_categorical_variable_queue_update_vector(SEXP variableSEXP, SEXP valueSEXP, SEXP indexSEXP) {
Expand Down Expand Up @@ -814,6 +825,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_individual_categorical_variable_queue_update", (DL_FUNC) &_individual_categorical_variable_queue_update, 3},
{"_individual_categorical_variable_get_index_of", (DL_FUNC) &_individual_categorical_variable_get_index_of, 2},
{"_individual_categorical_variable_get_size_of", (DL_FUNC) &_individual_categorical_variable_get_size_of, 2},
{"_individual_categorical_variable_get_categories", (DL_FUNC) &_individual_categorical_variable_get_categories, 1},
{"_individual_categorical_variable_queue_update_vector", (DL_FUNC) &_individual_categorical_variable_queue_update_vector, 3},
{"_individual_categorical_variable_update", (DL_FUNC) &_individual_categorical_variable_update, 1},
{"_individual_dummy", (DL_FUNC) &_individual_dummy, 0},
Expand Down
12 changes: 12 additions & 0 deletions src/categorical_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ int categorical_variable_get_size_of(
return variable->get_size_of(values);
}

//[[Rcpp::export]]
std::vector<std::string> categorical_variable_get_categories(
Rcpp::XPtr<CategoricalVariable> variable
) {
std::vector<std::string> categories;
categories.reserve(variable->indices.size());
for (const auto& it : variable->indices) {
categories.emplace_back(it.first);
}
return categories;
}

//[[Rcpp::export]]
void categorical_variable_queue_update_vector(
Rcpp::XPtr<CategoricalVariable> variable,
Expand Down
8 changes: 4 additions & 4 deletions src/prefab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Rcpp::XPtr<process_t> fixed_probability_multinomial_process_internal(

// make pointer to lambda function and return XPtr to R
return Rcpp::XPtr<process_t>(
new process_t([variable,source_state,destination_states,rate,cdf](size_t t){
new process_t([variable,source_state,destination_states,rate,cdf](size_t t){

// sample leavers
individual_index_t leaving_individuals(variable->get_index_of(source_state));
Expand Down Expand Up @@ -73,7 +73,7 @@ Rcpp::XPtr<process_t> multi_probability_multinomial_process_internal(

// make pointer to lambda function and return XPtr to R
return Rcpp::XPtr<process_t>(
new process_t([variable,source_state,destination_states,rate_variable,cdf](size_t t){
new process_t([variable,source_state,destination_states,rate_variable,cdf](size_t t){

// sample leavers with their unique prob
individual_index_t leaving_individuals(variable->get_index_of(source_state));
Expand All @@ -83,7 +83,7 @@ Rcpp::XPtr<process_t> multi_probability_multinomial_process_internal(
// empty bitsets to put them (their destinations)
std::vector<individual_index_t> destination_individuals;
size_t n = destination_states.size();
for(size_t i=0; i<n; i++) {
for (size_t i=0; i<n; i++) {
destination_individuals.emplace_back(leaving_individuals.max_size());
}

Expand Down Expand Up @@ -117,7 +117,7 @@ Rcpp::XPtr<process_t> multi_probability_bernoulli_process_internal(

// make pointer to lambda function and return XPtr to R
return Rcpp::XPtr<process_t>(
new process_t([variable,rate_variable,from,to](size_t t){
new process_t([variable,rate_variable,from,to](size_t t){

// sample leavers with their unique prob
individual_index_t leaving_individuals(variable->get_index_of(from));
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ test_that("getting the size of CategoricalVariable category which does not exist
expect_error(state$get_size_of('X'))
})

test_that("can retrieve categories of CategoricalVariable", {
values <- c("S","E","I","R")
var <- CategoricalVariable$new(categories = values,initial_values = rep(values,2))
expect_setequal(categorical_variable_get_categories(var$.variable), values)
})

test_that("getting variables works", {
size <- 10
sequence <- DoubleVariable$new(seq_len(size))
Expand Down