From 9fd83156be2c0870d3e0fab03111f5e698514bef Mon Sep 17 00:00:00 2001 From: Roberto Villegas-Diaz Date: Sun, 18 Apr 2021 11:38:20 +0100 Subject: [PATCH 1/7] Move scales and tictoc to the Imports section --- DESCRIPTION | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 29225f5..27e25de 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -51,12 +51,12 @@ Imports: MASS, parallel, progress, - progressr -Suggests: - magrittr, + progressr, scales, - testthat, tictoc +Suggests: + magrittr, + testthat Depends: R (>= 3.6) Roxygen: list(markdown = TRUE) From 9daf94d4c71d5f9c89919ab1f8cfcbdfad8e0ca3 Mon Sep 17 00:00:00 2001 From: Roberto Villegas-Diaz Date: Sun, 18 Apr 2021 11:42:13 +0100 Subject: [PATCH 2/7] Increment version number --- DESCRIPTION | 2 +- NEWS.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 27e25de..1958946 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: fxTWAPLS Title: An Improved Version of WA-PLS -Version: 0.0.5 +Version: 0.0.6 Authors@R: c( person(given = "Mengmeng", family = "Liu", diff --git a/NEWS.md b/NEWS.md index b9221e7..c821a5d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,5 @@ +# fxTWAPLS 0.0.6 + # fxTWAPLS 0.0.5 * Added support for the `progressr` API (display a progress bar). From 5660848f1aee0f818d5d6a1df98c5d5aa551fbb9 Mon Sep 17 00:00:00 2001 From: Roberto Villegas-Diaz Date: Sun, 18 Apr 2021 12:00:36 +0100 Subject: [PATCH 3/7] Remove LazyData tag --- DESCRIPTION | 1 - 1 file changed, 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1958946..823f1e8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -37,7 +37,6 @@ Description: The goal of this package is to provide an improved version of pseudo-replication. License: GPL-3 Encoding: UTF-8 -LazyData: true URL: https://github.com/special-uor/fxTWAPLS/, https://special-uor.github.io/fxTWAPLS/, https://research.reading.ac.uk/palaeoclimate/ From 254c2d327dee504aa5dbb567571f34c1a0f2e3c4 Mon Sep 17 00:00:00 2001 From: Roberto Villegas-Diaz Date: Fri, 23 Apr 2021 21:03:10 +0100 Subject: [PATCH 4/7] Remove unused tests --- tests/testthat.R | 4 ---- tests/testthat/test-utils.R | 20 -------------------- 2 files changed, 24 deletions(-) delete mode 100644 tests/testthat.R delete mode 100644 tests/testthat/test-utils.R diff --git a/tests/testthat.R b/tests/testthat.R deleted file mode 100644 index 0a2a303..0000000 --- a/tests/testthat.R +++ /dev/null @@ -1,4 +0,0 @@ -library(testthat) -library(fxTWAPLS) - -test_check("fxTWAPLS") diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R deleted file mode 100644 index 5690022..0000000 --- a/tests/testthat/test-utils.R +++ /dev/null @@ -1,20 +0,0 @@ -test_that("parallel benchmark works", { - # Define toy function that sleeps for (60/cpus) seconds - a <- function(cpus) {Sys.sleep(2/cpus)} - times_df <- par_benchmark(c(1, 2), a, quiet = TRUE) - expect_equal(length(times_df$times), 2) - expect_output(par_benchmark(c(2), a, quiet = FALSE)) - expect_output(par_benchmark(c(2), a, quiet = FALSE, plot = TRUE)) -}) - -test_that("combine with progress bar works", { - # Load binary operator for backend - `%do%` <- foreach::`%do%` - N <- 5 - out <- foreach::foreach(i = 1:N, - .combine = comb_pb(N)) %do% { - Sys.sleep(1) - i - } - expect_equal(c(unname(out)), 1:N) -}) \ No newline at end of file From 1b72d20b1503f2687e922273e81e14379538deef Mon Sep 17 00:00:00 2001 From: Roberto Villegas-Diaz Date: Fri, 23 Apr 2021 21:04:05 +0100 Subject: [PATCH 5/7] Migrate old tests and internal benchmarking functions --- R.ignore/tests/testthat.R | 4 ++ R.ignore/tests/testthat/test-utils.R | 20 +++++++ R.ignore/utils-benchmark.R | 85 +++++++++++++++++++++++++++ R/utils.R | 86 ---------------------------- 4 files changed, 109 insertions(+), 86 deletions(-) create mode 100644 R.ignore/tests/testthat.R create mode 100644 R.ignore/tests/testthat/test-utils.R create mode 100644 R.ignore/utils-benchmark.R diff --git a/R.ignore/tests/testthat.R b/R.ignore/tests/testthat.R new file mode 100644 index 0000000..0a2a303 --- /dev/null +++ b/R.ignore/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(fxTWAPLS) + +test_check("fxTWAPLS") diff --git a/R.ignore/tests/testthat/test-utils.R b/R.ignore/tests/testthat/test-utils.R new file mode 100644 index 0000000..5690022 --- /dev/null +++ b/R.ignore/tests/testthat/test-utils.R @@ -0,0 +1,20 @@ +test_that("parallel benchmark works", { + # Define toy function that sleeps for (60/cpus) seconds + a <- function(cpus) {Sys.sleep(2/cpus)} + times_df <- par_benchmark(c(1, 2), a, quiet = TRUE) + expect_equal(length(times_df$times), 2) + expect_output(par_benchmark(c(2), a, quiet = FALSE)) + expect_output(par_benchmark(c(2), a, quiet = FALSE, plot = TRUE)) +}) + +test_that("combine with progress bar works", { + # Load binary operator for backend + `%do%` <- foreach::`%do%` + N <- 5 + out <- foreach::foreach(i = 1:N, + .combine = comb_pb(N)) %do% { + Sys.sleep(1) + i + } + expect_equal(c(unname(out)), 1:N) +}) \ No newline at end of file diff --git a/R.ignore/utils-benchmark.R b/R.ignore/utils-benchmark.R new file mode 100644 index 0000000..16e61fd --- /dev/null +++ b/R.ignore/utils-benchmark.R @@ -0,0 +1,85 @@ +#' Perform parallel benchmarks +#' +#' Perform parallel benchmarks on a function and generate a plot with execution +#' times vs CPU count. +#' +#' @param CPUS Vector with the number of CPUs. +#' @param FUN Parallel function, MUST have a parameter called \code{cpus}. +#' @param plot Boolean flag to request a plot for the results. +#' @param quiet Boolean flag to print results of each execution. +#' @param ... Optional arguments for \code{FUN}, must be named; e.g. +#' \code{x = test_df}. +#' +#' @examples +#' # Define toy function that sleeps for (2/cpus) seconds +#' a <- function(cpus) {Sys.sleep(2/cpus)} +#' fxTWAPLS:::par_benchmark(c(1, 2), a) +#' \donttest{ +#' fxTWAPLS:::par_benchmark(c(1, 2), a, plot = TRUE) +#' } +#' +#' @keywords internal +#' @noRd +par_benchmark <- function(CPUS, FUN, plot = FALSE, quiet = FALSE, ...) { + cpus <- NULL # Local binding + tictoc::tic.clearlog() + for (c in CPUS) { + tictoc::tic(paste0("Using ", c, " CPUs")) + out <- FUN(..., cpus = c) + tictoc::toc(log = TRUE, quiet = quiet) + } + times <- unlist(tictoc::tic.log(format = TRUE)) + times <- gsub(" sec elapsed", "", unlist(times)) + times <- gsub(".*: ", "", unlist(times)) + times <- as.numeric(times) + times_df <- data.frame(cpus = CPUS, times = times) + + if(plot) { + print(ggplot2::qplot(cpus, times, data = times_df) + + ggplot2::geom_area(alpha = 0.5) + + ggplot2::geom_line() + + ggplot2::labs(x = "CPUs", y = "Execution time [seconds]") + + ggplot2::scale_x_continuous(breaks = 1:max(CPUS)) + + ggplot2::scale_y_continuous(breaks = scales::pretty_breaks(10)) + ) + } + return(times_df) +} + +#' Combine results with progress bar +#' +#' Combine results with progress bar, to be used in combination with +#' \code{\link[foreach::foreach]{foreach::foreach}}. +#' +#' @importFrom utils flush.console +#' @importFrom utils setTxtProgressBar +#' @importFrom utils txtProgressBar +#' +#' @param iterator Number of iterations. +#' @param FUN Function to combine the results (default: \code{rbind}). +#' @param ... Optional parameters. +#' +#' @examples +#' \donttest{ +#' # Load binary operator for backend +#' `%do%` <- foreach::`%do%` +#' N <- 5 +#' out <- foreach::foreach(i = 1:N, +#' .combine = fxTWAPLS:::comb_pb(N)) %do% { +#' Sys.sleep(1) +#' i +#' } +#' } +#' +#' @noRd +#' @keywords internal +comb_pb <- function(iterator, FUN = rbind, ...) { + pb <- txtProgressBar(min = 1, max = iterator - 1, style = 3) + count <- 0 + function(...) { + count <- count + length(list(...)) - 1 + setTxtProgressBar(pb, count) + flush.console() + FUN(...) # this can feed into .combine option of foreach + } +} \ No newline at end of file diff --git a/R/utils.R b/R/utils.R index d137fce..3c0e76b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,89 +1,3 @@ -#' Perform parallel benchmarks -#' -#' Perform parallel benchmarks on a function and generate a plot with execution -#' times vs CPU count. -#' -#' @param CPUS Vector with the number of CPUs. -#' @param FUN Parallel function, MUST have a parameter called \code{cpus}. -#' @param plot Boolean flag to request a plot for the results. -#' @param quiet Boolean flag to print results of each execution. -#' @param ... Optional arguments for \code{FUN}, must be named; e.g. -#' \code{x = test_df}. -#' -#' @examples -#' # Define toy function that sleeps for (2/cpus) seconds -#' a <- function(cpus) {Sys.sleep(2/cpus)} -#' fxTWAPLS:::par_benchmark(c(1, 2), a) -#' \donttest{ -#' fxTWAPLS:::par_benchmark(c(1, 2), a, plot = TRUE) -#' } -#' -#' @keywords internal -#' @noRd -par_benchmark <- function(CPUS, FUN, plot = FALSE, quiet = FALSE, ...) { - cpus <- NULL # Local binding - tictoc::tic.clearlog() - for (c in CPUS) { - tictoc::tic(paste0("Using ", c, " CPUs")) - out <- FUN(..., cpus = c) - tictoc::toc(log = TRUE, quiet = quiet) - } - times <- unlist(tictoc::tic.log(format = TRUE)) - times <- gsub(" sec elapsed", "", unlist(times)) - times <- gsub(".*: ", "", unlist(times)) - times <- as.numeric(times) - times_df <- data.frame(cpus = CPUS, times = times) - - if(plot) { - print(ggplot2::qplot(cpus, times, data = times_df) + - ggplot2::geom_area(alpha = 0.5) + - ggplot2::geom_line() + - ggplot2::labs(x = "CPUs", y = "Execution time [seconds]") + - ggplot2::scale_x_continuous(breaks = 1:max(CPUS)) + - ggplot2::scale_y_continuous(breaks = scales::pretty_breaks(10)) - ) - } - return(times_df) -} - -#' Combine results with progress bar -#' -#' Combine results with progress bar, to be used in combination with -#' \code{\link[foreach::foreach]{foreach::foreach}}. -#' -#' @importFrom utils flush.console -#' @importFrom utils setTxtProgressBar -#' @importFrom utils txtProgressBar -#' -#' @param iterator Number of iterations. -#' @param FUN Function to combine the results (default: \code{rbind}). -#' @param ... Optional parameters. -#' -#' @examples -#' \donttest{ -#' # Load binary operator for backend -#' `%do%` <- foreach::`%do%` -#' N <- 5 -#' out <- foreach::foreach(i = 1:N, -#' .combine = fxTWAPLS:::comb_pb(N)) %do% { -#' Sys.sleep(1) -#' i -#' } -#' } -#' -#' @noRd -#' @keywords internal -comb_pb <- function(iterator, FUN = rbind, ...) { - pb <- txtProgressBar(min = 1, max = iterator - 1, style = 3) - count <- 0 - function(...) { - count <- count + length(list(...)) - 1 - setTxtProgressBar(pb, count) - flush.console() - FUN(...) # this can feed into .combine option of foreach - } -} - #' Show progress bar #' #' @param expr R expression. From 5f20d055b9f8c81135bc22cc438e7e3cbeb4ab79 Mon Sep 17 00:00:00 2001 From: Roberto Villegas-Diaz Date: Fri, 23 Apr 2021 21:04:59 +0100 Subject: [PATCH 6/7] Move the package progress to the list of suggested packages, instead of imports (this is used by progressr) --- DESCRIPTION | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 823f1e8..e9cc8e3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -49,13 +49,12 @@ Imports: ggplot2, MASS, parallel, + progressr +Suggests: + magrittr, progress, - progressr, scales, tictoc -Suggests: - magrittr, - testthat Depends: R (>= 3.6) Roxygen: list(markdown = TRUE) From 274870ab3d432c2f6920b0cfe6d775344a4dfabb Mon Sep 17 00:00:00 2001 From: Roberto Villegas-Diaz Date: Sat, 24 Apr 2021 18:21:19 +0100 Subject: [PATCH 7/7] Update NEWS --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index c821a5d..9bd7147 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # fxTWAPLS 0.0.6 +* Removed unused benchmark functions and test. +* Simplified the list of suggested packages. + # fxTWAPLS 0.0.5 * Added support for the `progressr` API (display a progress bar).