Skip to content

Commit

Permalink
Return epinow()'s "timing" output as <difftime> (#688)
Browse files Browse the repository at this point in the history
* Print units of model run time

* Add NEWS item

* Fix tests

* Make "timing" output a difftime object

* Use purrr::map_vec to preserve difftime class

* Test for the class instead of the type

* Improve NEWS item

* Import map_vec

* Add reviewer

Co-authored-by: Sebastian Funk <[email protected]>

---------

Co-authored-by: Sebastian Funk <[email protected]>
  • Loading branch information
jamesmbaazam and sbfnk authored Jun 7, 2024
1 parent 4915906 commit 916a988
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ importFrom(purrr,map2_dbl)
importFrom(purrr,map_chr)
importFrom(purrr,map_dbl)
importFrom(purrr,map_dfc)
importFrom(purrr,map_vec)
importFrom(purrr,pmap_dbl)
importFrom(purrr,quietly)
importFrom(purrr,reduce)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# EpiNow2 (development version)

## Model changes

- `epinow()` now returns the "timing" output in a "time difference"" format that is easier to understand and work with. By @jamesmbaazam in #688 and reviewed by @sbfnk.

# EpiNow2 1.5.2

A patch release to further fix an issue with the date in the package citation. This has now been addressed by removing `inst/CITATION`.
Expand Down
2 changes: 1 addition & 1 deletion R/epinow.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ epinow <- function(data,

# log timing if specified
if (output["timing"]) {
out$timing <- round(as.numeric(end_time - start_time), 1)
out$timing <- difftime(end_time, start_time)
if (!is.null(target_folder)) {
saveRDS(out$timing, file.path(target_folder, "runtime.rds"))
}
Expand Down
5 changes: 3 additions & 2 deletions R/summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ summarise_key_measures <- function(regional_results = NULL,
#' @return A data.table of region run times
#' @export
#' @importFrom data.table data.table fwrite
#' @importFrom purrr map safely
#' @importFrom purrr map safely map_vec
#' @keywords internal
#' @examples
#' regional_out <- readRDS(system.file(
Expand All @@ -530,7 +530,8 @@ regional_runtimes <- function(regional_output = NULL,
if (!is.null(regional_output)) {
timings <- data.table::data.table(
region = names(regional_output),
time = unlist(purrr::map(regional_output, ~ .$timing))
# purrr::map_vec will preserve the difftime class
time = unlist(purrr::map_vec(regional_output, ~ .$timing))
)
} else {
if (is.null(target_date)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-regional_epinow.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test_that("regional_epinow produces expected output when run with default settin
"summarised_measures", "reported_cases", "high_plots", "plots"
))
expect_equal(names(out$regional$realland), c("estimates", "estimated_reported_cases", "summary", "plots", "timing"))
expect_type(out$regional$realland$timing, "double")
expect_s3_class(out$regional$realland$timing, "difftime")
df_non_zero(out$regional$realland$estimates$samples)
df_non_zero(out$regional$realland$estimates$summarised)
df_non_zero(out$regional$realland$estimated_reported_cases$samples)
Expand Down Expand Up @@ -101,7 +101,7 @@ test_that("regional_epinow produces expected output when run with region specifi
"summarised_measures", "reported_cases", "high_plots", "plots"
))
expect_equal(names(out$regional$realland), c("estimates", "estimated_reported_cases", "summary", "plots", "timing"))
expect_type(out$regional$realland$timing, "double")
expect_s3_class(out$regional$realland$timing, "difftime")
df_non_zero(out$regional$realland$estimates$samples)
df_non_zero(out$regional$realland$estimates$summarised)
df_non_zero(out$regional$realland$estimated_reported_cases$samples)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-regional_runtimes.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ test_that("regional_runtimes produces expected output when with input", {
runtimes <- regional_runtimes(out$regional)
expect_equal(names(runtimes), c("region", "time"))
df_non_zero(runtimes)
expect_type(runtimes$time, "double")
expect_s3_class(runtimes$time, "difftime")
})

0 comments on commit 916a988

Please sign in to comment.