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

Mass rename of step to time in implementation #375

Merged
merged 4 commits into from Nov 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
@@ -1,8 +1,9 @@
Package: dust
Title: Iterate Multiple Realisations of Stochastic Models
Version: 0.11.35
Version: 0.12.0
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "rich.fitzjohn@gmail.com"),
person("Alex", "Hill", role = "aut"),
person("John", "Lees", role = "aut"),
person("Imperial College of Science, Technology and Medicine",
role = "cph"))
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
@@ -1,3 +1,9 @@
# dust 0.12.0

* Breaking change, with `step` (and so on with `step_end`) changing to `time` everywhere, in order to smooth the inclusion of continuous time models. This affects quite a few methods:
* `step()` and `set_step()` become `time()` and `set_time()`
* `update_state()`'s argument changes from `step` to `time`

# dust 0.11.24

* Add `min_log_likelihood` support to `$filter()` (#361)
Expand Down
2 changes: 1 addition & 1 deletion R/compile.R
Expand Up @@ -129,7 +129,7 @@ glue_whisker <- function(template, data) {
dust_template_data <- function(model, config, cuda, reload_data) {
methods <- function(target) {
nms <- c("alloc", "run", "simulate", "set_index", "n_state",
"update_state", "state", "step", "reorder", "resample",
"update_state", "state", "time", "reorder", "resample",
"rng_state", "set_rng_state", "set_n_threads",
"set_data", "compare_data", "filter")
m <- sprintf("%s = dust_%s_%s_%s", nms, target, config$name, nms)
Expand Down
168 changes: 84 additions & 84 deletions R/cpp11.R

Large diffs are not rendered by default.

60 changes: 33 additions & 27 deletions R/data.R
@@ -1,21 +1,26 @@
##' Prepare data for use with the `$set_data()` method. This is not
##' required for use but tries to simplify the most common use case
##' where you have a [data.frame] with some column indicating "model
##' step" (`name_step`), and other columns that might be use in your
##' `data_compare` function. Each row will be turned into a named R
##' list, which your `dust_data` function can then work with to get
##' where you have a [data.frame] with some column indicating "dust
##' time step" (`name_time`), and other columns that might be use in
##' your `data_compare` function. Each row will be turned into a named
##' R list, which your `dust_data` function can then work with to get
##' this time-steps values. See Details for use with multi-pars
##' objects.
##'
##' Note that here "dust time step" (`name_time`) refers to the *dust*
##' time step (which will be a non-negative integer) and not the
##' rescaled value of time that you probably use within the model. See
##' [dust_generator] for more information.
##'
##' The data object as accepted by `data_set` must be a [list] and
##' each element must itself be a list with two elements; the model
##' `step` at which the data applies and any R object that corresponds
##' each element must itself be a list with two elements; the dust
##' `time` at which the data applies and any R object that corresponds
##' to data at that point. We expect that most of the time this second
##' element will be a key-value list with scalar keys, but more
##' flexibility may be required.
##'
##' For multi-data objects, the final format is a bit more awkward;
##' each time step we have a list with elements `step`, `data_1`,
##' each time step we have a list with elements `time`, `data_1`,
##' `data_2`, ..., `data_n` for `n` parameters. There are two ways of
##' creating this that might be useful: *sharing* the data across all
##' parameters and using some column as a grouping value.
Expand All @@ -38,7 +43,7 @@
##'
##' @param object An object, at this point must be a [data.frame]
##'
##' @param name_step The name of the data column within `object`; this
##' @param name_time The name of the data column within `object`; this
##' column must be integer-like and every integer must be
##' nonnegative and unique
##'
Expand All @@ -52,28 +57,28 @@
##'
##' @export
##' @examples
##' d <- data.frame(step = seq(0, 50, by = 10), a = runif(6), b = runif(6))
##' d <- data.frame(time = seq(0, 50, by = 10), a = runif(6), b = runif(6))
##' dust::dust_data(d)
dust_data <- function(object, name_step = "step", multi = NULL) {
dust_data <- function(object, name_time = "time", multi = NULL) {
assert_is(object, "data.frame")
steps <- object[[name_step]]
if (is.null(steps)) {
times <- object[[name_time]]
if (is.null(times)) {
stop(sprintf("'%s' is not a column in %s",
name_step, deparse(substitute(object))))
name_time, deparse(substitute(object))))
}
isteps <- as.integer(round(steps))
if (any(isteps < 0)) {
stop(sprintf("All elements in column '%s' must be nonnegative", name_step))
itimes <- as.integer(round(times))
if (any(itimes < 0)) {
stop(sprintf("All elements in column '%s' must be nonnegative", name_time))
}
if (any(abs(steps - isteps) > sqrt(.Machine$double.eps))) {
stop(sprintf("All elements in column '%s' must be integer-like", name_step))
if (any(abs(times - itimes) > sqrt(.Machine$double.eps))) {
stop(sprintf("All elements in column '%s' must be integer-like", name_time))
}

rows <- lapply(seq_len(nrow(object)), function(i) as.list(object[i, ]))
if (is.null(multi)) {
ret <- Map(list, isteps, rows)
ret <- Map(list, itimes, rows)
} else if (is_integer_like(multi)) {
ret <- Map(function(i, d) c(list(i), rep(list(d), multi)), isteps, rows)
ret <- Map(function(i, d) c(list(i), rep(list(d), multi)), itimes, rows)
} else if (is.character(multi)) {
group <- object[[multi]]
if (is.null(group)) {
Expand All @@ -83,20 +88,21 @@ dust_data <- function(object, name_step = "step", multi = NULL) {
if (!is.factor(group)) {
stop(sprintf("Column '%s' must be a factor", multi))
}
isteps <- unname(split(isteps, group))
if (length(unique(isteps)) != 1L) {
itimes <- unname(split(itimes, group))
if (length(unique(itimes)) != 1L) {
stop("All groups must have the same time steps, in the same order")
}
isteps <- isteps[[1L]]
itimes <- itimes[[1L]]
rows_grouped <- unname(split(rows, group))
ret <- lapply(seq_along(isteps), function(i)
c(list(isteps[[i]]), lapply(rows_grouped, "[[", i)))
ret <- lapply(seq_along(itimes), function(i) {
c(list(itimes[[i]]), lapply(rows_grouped, "[[", i))
})
} else {
stop("Invalid option for 'multi'; must be NULL, integer or character")
}

if (any(duplicated(isteps))) {
stop(sprintf("All elements in column '%s' must be unique", name_step))
if (any(duplicated(itimes))) {
stop(sprintf("All elements in column '%s' must be unique", name_time))
}

ret
Expand Down