Skip to content

Commit

Permalink
Merge pull request #17 from special-uor/dev
Browse files Browse the repository at this point in the history
CRAN v0.0.6
  • Loading branch information
villegar authored Apr 24, 2021
2 parents 6e943dc + 274870a commit d494a12
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 90 deletions.
6 changes: 2 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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/
Expand All @@ -50,12 +49,11 @@ Imports:
ggplot2,
MASS,
parallel,
progress,
progressr
Suggests:
magrittr,
progress,
scales,
testthat,
tictoc
Depends:
R (>= 3.6)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +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).
Expand Down
File renamed without changes.
File renamed without changes.
85 changes: 85 additions & 0 deletions R.ignore/utils-benchmark.R
Original file line number Diff line number Diff line change
@@ -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
}
}
86 changes: 0 additions & 86 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit d494a12

Please sign in to comment.