Skip to content
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
5 changes: 2 additions & 3 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ jobs:
config:
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest", http-user-agent: "R/4.1.0 (ubuntu-20.04) R (4.1.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
- {os: ubuntu-latest, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
Expand All @@ -43,7 +42,7 @@ jobs:
shell: Rscript {0}

- name: Restore R package cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: individual
Title: Framework for Specifying and Simulating Individual Based Models
Version: 0.1.17
Version: 0.1.18
Authors@R: c(
person(
given = "Giovanni",
Expand Down Expand Up @@ -65,7 +65,7 @@ Suggests:
testthat (>= 2.1.0),
xml2,
bench
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
VignetteBuilder: knitr
LinkingTo:
Rcpp,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(RaggedInteger)
export(Render)
export(TargetedEvent)
export(bernoulli_process)
export(bitset_count_and)
export(categorical_count_renderer_process)
export(filter_bitset)
export(fixed_probability_multinomial_process)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# individual 0.1.18

* add $get_values to CategoricalVariable
* add $get_modulo_differences to IntegerVariable
* add bitset_count_and for quick intersection counts

# individual 0.1.17

* Add a `copy_from` method to the `Bitset` class.
Expand Down
16 changes: 16 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ bitset_choose <- function(b, k) {
invisible(.Call(`_individual_bitset_choose`, b, k))
}

bitset_count_and_cpp <- function(a, b) {
.Call(`_individual_bitset_count_and_cpp`, a, b)
}

create_categorical_variable <- function(categories, values) {
.Call(`_individual_create_categorical_variable`, categories, values)
}
Expand Down Expand Up @@ -125,6 +129,14 @@ categorical_variable_queue_shrink_bitset <- function(variable, index) {
invisible(.Call(`_individual_categorical_variable_queue_shrink_bitset`, variable, index))
}

categorical_variable_get_values <- function(variable) {
.Call(`_individual_categorical_variable_get_values`, variable)
}

categorical_variable_get_values_with_index <- function(variable, index) {
.Call(`_individual_categorical_variable_get_values_with_index`, variable, index)
}

create_double_variable <- function(values) {
.Call(`_individual_create_double_variable`, values)
}
Expand Down Expand Up @@ -289,6 +301,10 @@ integer_variable_get_values <- function(variable) {
.Call(`_individual_integer_variable_get_values`, variable)
}

integer_variable_get_modulo_differences <- function(variable, value, difference) {
.Call(`_individual_integer_variable_get_modulo_differences`, variable, value, difference)
}

integer_variable_get_values_at_index <- function(variable, index) {
.Call(`_individual_integer_variable_get_values_at_index`, variable, index)
}
Expand Down
12 changes: 12 additions & 0 deletions R/bitset.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,15 @@ filter_bitset = function(bitset, other) {
}
}
}

#' @title Count bitset and
#' @description This non-modifying function returns the number of intersecting
#' elements between two bitsets \code{a} and \code{b}. This should be faster than
#' writing \code{a$copy()$and(b)$size()} as it avoids the memory allocations of $copy().
#'
#' @param a a \code{\link{Bitset}}
#' @param b another \code{\link{Bitset}}
#' @export
bitset_count_and = function(a, b) {
bitset_count_and_cpp(a$.bitset, b$.bitset)
}
14 changes: 14 additions & 0 deletions R/categorical_variable.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ CategoricalVariable <- R6Class(
categorical_variable_get_categories(self$.variable)
},

#' @description return the value of the variable for the given individuals
#' @param index the indices of individuals whose categories will be returned
get_values = function(index = NULL) {
stopifnot(is.finite(index))
stopifnot(index > 0)
if (length(index) == 0){
results <- categorical_variable_get_values(self$.variable)
}
else{
results <- categorical_variable_get_values_with_index(self$.variable, index)
}
return(results)
},

#' @description queue an update for this variable
#' @param value the new value
#' @param index the indices of individuals whose value will be updated
Expand Down
11 changes: 11 additions & 0 deletions R/integer_variable.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ IntegerVariable <- R6Class(

},

#' @description Return a vector of individuals with 0 modulo difference from input value
#' and the distance being compared
#' @param value the value to check
#' @param difference the difference to check, e.g. difference = 2 checks whether the
#' difference is even
get_modulo_differences = function(value, difference){
stopifnot(is.finite(value))
stopifnot(is.finite(difference))
integer_variable_get_modulo_differences(self$.variable, value, difference)
},

#' @description Return a \code{\link[individual]{Bitset}} for individuals with some subset of values.
#' Either search for indices corresponding to values in \code{set}, or
#' for indices corresponding to values in range \eqn{[a,b]}. Either \code{set}
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ reference:
- RaggedDouble
- Bitset
- filter_bitset
- bitset_count_and
- title: "Events & Rendering"
desc: "Classes for events and rendering output."
- contents:
Expand Down
45 changes: 44 additions & 1 deletion inst/include/CategoricalVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class CategoricalVariable : public Variable {
virtual void resize() override;
virtual size_t size() const override;
virtual void update() override;
virtual std::vector<std::string> get_values(const std::vector<size_t>&);
virtual std::vector<std::string> get_values();
};


Expand Down Expand Up @@ -228,5 +230,46 @@ inline size_t CategoricalVariable::size() const {
inline const std::vector<std::string>& CategoricalVariable::get_categories() const {
return categories;
}

//' @title get values at index given by a vector
inline std::vector<std::string> CategoricalVariable::get_values(const std::vector<size_t>& index){

// Generate empty output vector
auto result = std::vector<std::string>(index.size());
for (auto i = 0u; i < index.size(); ++i) {
// Determine which category the individual is within
for (auto cat: categories) {
if (indices.find(cat) == indices.end()) {
std::stringstream message;
message << "unknown category: " << cat;
Rcpp::stop(message.str());
}
if (indices.at(cat).find(index[i]) != indices.at(cat).end()) {
result[i] = cat;
break;
}
}
}
return result;
}
//' @title get values of full variable
inline std::vector<std::string> CategoricalVariable::get_values(){

// Generate empty output vector
auto result = std::vector<std::string>(size());
for(auto i = 0u; i < size(); ++i){
// Determine which category the individual is within
for (auto cat: categories) {
if (indices.find(cat) == indices.end()) {
std::stringstream message;
message << "unknown category: " << cat;
Rcpp::stop(message.str());
}
if (indices.at(cat).find(i) != indices.at(cat).end()) {
result[i] = cat;
break;
}
}
}
return result;
}
#endif /* INST_INCLUDE_CATEGORICAL_VARIABLE_H_ */
18 changes: 18 additions & 0 deletions inst/include/IntegerVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct IntegerVariable : public NumericVariable<int> {
virtual size_t get_size_of_set(const std::vector<int>&) const;
virtual size_t get_size_of_set(const int) const;
virtual size_t get_size_of_range(const int, const int) const;
virtual individual_index_t get_modulo_differences(const int, const int) const;
};

inline IntegerVariable::IntegerVariable(const std::vector<int>& values)
Expand Down Expand Up @@ -66,6 +67,23 @@ inline individual_index_t IntegerVariable::get_index_of_set(
return result;
}

//' @title return bitset giving index of individuals whose difference between
// the current value and the queried value is a multiple of the queried difference
inline individual_index_t IntegerVariable::get_modulo_differences(
const int value,
const int difference
) const {

auto result = individual_index_t(size());
for (auto i = 0u; i < values.size(); ++i) {
if ( (values[i] - value) % difference == 0 ) {
result.insert(i);
}
}

return result;
}

//' @title return bitset giving index of individuals whose value is in some range [a,b]
inline individual_index_t IntegerVariable::get_index_of_range(
const int a, const int b
Expand Down
13 changes: 13 additions & 0 deletions inst/include/IterableBitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class IterableBitset {
IterableBitset& operator&=(const IterableBitset&);
IterableBitset& operator|=(const IterableBitset&);
IterableBitset& operator^=(const IterableBitset&);
size_t count_and(const IterableBitset&) const;
IterableBitset& clear();
IterableBitset& inverse();
iterator begin();
Expand Down Expand Up @@ -293,6 +294,18 @@ inline IterableBitset<A>& IterableBitset<A>::operator &=(const IterableBitset<A>
return *this;
}

template<class A>
inline size_t IterableBitset<A>::count_and(const IterableBitset<A>& other) const {
if (max_size() != other.max_size()) {
Rcpp::stop("Incompatible bitmap sizes");
}
auto n = 0u;
for (auto i = 0u; i < bitmap.size(); ++i) {
n += popcount(bitmap[i] & other.bitmap[i]);
}
return n;
}

template<class A>
inline IterableBitset<A>& IterableBitset<A>::operator |=(const IterableBitset<A>& other) {
if (max_size() != other.max_size()) {
Expand Down
18 changes: 18 additions & 0 deletions man/CategoricalVariable.Rd

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

22 changes: 22 additions & 0 deletions man/IntegerVariable.Rd

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

18 changes: 18 additions & 0 deletions man/bitset_count_and.Rd

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

Loading
Loading