Skip to content

Commit

Permalink
feat: add parallel processing
Browse files Browse the repository at this point in the history
- adds `n.core`
- adds {mirai} as Suggested

fixes #71
  • Loading branch information
jack-davison committed Nov 25, 2024
1 parent dd9f5e4 commit 4ab2191
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 53 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: openairmaps
Title: Create Maps of Air Pollution Data
Version: 0.9.1.9000
Version: 0.9.1.9001
Authors@R: c(
person("Jack", "Davison", , "jack.davison@ricardo.com", role = c("cre", "aut"),
comment = c(ORCID = "0000-0003-2653-6615")),
Expand Down Expand Up @@ -43,6 +43,7 @@ Suggests:
httr,
jsonlite,
knitr,
mirai,
rmarkdown,
shiny,
worldmet
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# openairmaps (development version)

## New features

* Polar marker functions have gained the `n.core` argument, which allows for the use of `{mirai}` for parallel plot creation. `{mirai}` is not imported by `{openairmaps}` by default, but users will be prompted to download it if they set `n.core` to be greater than `1L`.

* The progress bar shown when `progress = TRUE` now better reflects the actual time until function completion.

# openairmaps 0.9.1

## New features
Expand Down
3 changes: 2 additions & 1 deletion R/addPolarMarkers.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ addPolarMarkers <-
d.fig = d.fig,
popup = popup,
label = label,
dropcol = pollutant
dropcol = pollutant,
ncores = 1L
)

# work out width/height
Expand Down
4 changes: 3 additions & 1 deletion R/polar_annulusMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ annulusMap <- function(data,
static = FALSE,
static.nrow = NULL,
progress = TRUE,
n.core = 1L,
...,
control = NULL) {
# check basemap providers are valid
Expand Down Expand Up @@ -197,7 +198,8 @@ annulusMap <- function(data,
d.fig = d.fig,
popup = popup,
label = label,
progress = progress
progress = progress,
ncores = n.core
)

if (static) {
Expand Down
3 changes: 2 additions & 1 deletion R/polar_diffMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#' The colours used for plotting, passed to [openair::openColours()]. It is
#' recommended to use a "diverging" colour palette (along with a symmetrical
#' `limit` scale) for effective visualisation.
#'
#'
#' @inheritDotParams openair::polarPlot -mydata -pollutant -x -limits -type
#' -cols -key -key.footer -key.header -key.position -units -angle.scale -alpha
#' -plot
Expand Down Expand Up @@ -82,6 +82,7 @@ diffMap <- function(before,
static = FALSE,
static.nrow = NULL,
progress = TRUE,
n.core = 1L,
...,
control = NULL) {
# check basemap providers are valid
Expand Down
4 changes: 3 additions & 1 deletion R/polar_freqMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ freqMap <- function(data,
static = FALSE,
static.nrow = NULL,
progress = TRUE,
n.core = 1L,
...,
control = NULL) {
# check basemap providers are valid
Expand Down Expand Up @@ -216,7 +217,8 @@ freqMap <- function(data,
d.fig = d.fig,
popup = popup,
label = label,
progress = progress
progress = progress,
ncores = n.core
)

if (static) {
Expand Down
4 changes: 3 additions & 1 deletion R/polar_percentileMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ percentileMap <- function(data,
static = FALSE,
static.nrow = NULL,
progress = TRUE,
n.core = 1L,
...,
control = NULL) {
# check basemap providers are valid
Expand Down Expand Up @@ -189,7 +190,8 @@ percentileMap <- function(data,
d.fig = d.fig,
popup = popup,
label = label,
progress = progress
progress = progress,
ncores = n.core
)

if (static) {
Expand Down
13 changes: 12 additions & 1 deletion R/polar_polarMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@
#' creating individual polar markers. This option allows this to be turned
#' off, if desired.
#'
#' @param n.core *Number of cores to use in parallel processing.*
#'
#' *default:* `1L` | *scope:* dynamic & static
#'
#' By default, each polar marker is drawn and saved sequentially. For big maps
#' with a lot of markers, this can be slow. Adjusting `n.core` to a number
#' greater than `1` will use [mirai][mirai::mirai-package] to create markers
#' in parallel.
#'
#' @param control **Deprecated.** Please use `type`.
#'
#' @inheritDotParams openair::polarPlot -mydata -pollutant -x -limits -type
Expand Down Expand Up @@ -325,6 +334,7 @@ polarMap <- function(data,
static = FALSE,
static.nrow = NULL,
progress = TRUE,
n.core = 1L,
...,
control = NULL) {
# list pairwise statistics
Expand Down Expand Up @@ -482,7 +492,8 @@ polarMap <- function(data,
popup = popup,
label = label,
dropcol = funpoll,
progress = progress
progress = progress,
ncores = n.core
)

if (!static) {
Expand Down
4 changes: 3 additions & 1 deletion R/polar_pollroseMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pollroseMap <- function(data,
static = FALSE,
static.nrow = NULL,
progress = TRUE,
n.core = 1L,
...,
control = NULL) {
# check basemap providers are valid
Expand Down Expand Up @@ -169,7 +170,8 @@ pollroseMap <- function(data,
d.fig = d.fig,
popup = popup,
label = label,
progress = progress
progress = progress,
ncores = n.core
)

if (static) {
Expand Down
4 changes: 3 additions & 1 deletion R/polar_windroseMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ windroseMap <- function(data,
static = FALSE,
static.nrow = NULL,
progress = TRUE,
n.core = 1L,
...,
control = NULL) {
# check basemap providers are valid
Expand Down Expand Up @@ -175,7 +176,8 @@ windroseMap <- function(data,
d.fig = d.fig,
popup = popup,
label = label,
progress = progress
progress = progress,
ncores = n.core
)

if (static) {
Expand Down
110 changes: 66 additions & 44 deletions R/utils-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ checkMapPrep <-
if (wd %in% Names & is.numeric(mydata[, wd])) {
## check for wd <0 or > 360
if (any(sign(mydata[[wd]][!is.na(mydata[[wd]])]) == -1 |
mydata[[wd]][!is.na(mydata[[wd]])] > 360)) {
mydata[[wd]][!is.na(mydata[[wd]])] > 360)) {
warning("Wind direction < 0 or > 360; removing these data")
mydata[[wd]][mydata[[wd]] < 0] <- NA
mydata[[wd]][mydata[[wd]] > 360] <- NA
Expand Down Expand Up @@ -234,7 +234,7 @@ assume_latlon <- function(data, latitude, longitude) {
len <- length(out)
if (len > 1) {
cli::cli_abort("Cannot identify {name}: Multiple possible matches ({out})",
call = NULL
call = NULL
)
return(NULL)
} else if (len == 0) {
Expand Down Expand Up @@ -325,8 +325,8 @@ make_leaflet_map <-
}
for (i in seq_along(provider)) {
map <- leaflet::addProviderTiles(map,
provider[[i]],
group = names(provider)[[i]]
provider[[i]],
group = names(provider)[[i]]
)
}

Expand Down Expand Up @@ -424,7 +424,8 @@ create_polar_markers <-
label = NULL,
d.fig,
dropcol = "conc",
progress = TRUE) {
progress = TRUE,
ncores) {
# make temp directory
dir <- tempdir()

Expand Down Expand Up @@ -467,22 +468,19 @@ create_polar_markers <-

# create plots
plots_df <-
nested_df %>%
dplyr::mutate(
plot = purrr::map(data, fun, .progress = ifelse(progress, "Creating Polar Markers", FALSE)),
url = paste0(
dir,
"/",
.data[[latitude]],
"_",
.data[[longitude]],
"_",
rm_illegal_chars(.data[[split_col]]),
"_",
id,
".png"
)
)
dplyr::mutate(nested_df,
url = paste0(
dir,
"/",
.data[[latitude]],
"_",
.data[[longitude]],
"_",
rm_illegal_chars(.data[[split_col]]),
"_",
id,
".png"
))

# work out w/h
if (length(d.fig) == 1) {
Expand All @@ -493,29 +491,53 @@ create_polar_markers <-
height <- d.fig[[2]]
}

purrr::pwalk(
list(
plots_df[[latitude]],
plots_df[[longitude]],
rm_illegal_chars(plots_df[[split_col]]),
plots_df$plot
),
.f = ~ {
grDevices::png(
filename = paste0(dir, "/", ..1, "_", ..2, "_", ..3, "_", id, ".png"),
width = width * 300,
height = height * 300,
res = 300,
bg = "transparent",
type = "cairo",
antialias = "none"
)
save_plot <- function(data, url) {
grDevices::pdf(NULL)

plot <- fun(data)

grDevices::png(
filename = url,
width = width * 300,
height = height * 300,
res = 300,
bg = "transparent",
type = "cairo",
antialias = "none"
)

plot(..4)
print(plot)

grDevices::dev.off()
grDevices::dev.off()

return(plot)
}

if (ncores == 1L) {
plots_df$plot <- purrr::pmap(
.l = dplyr::select(plots_df, "data", "url"),
.f = save_plot,
.progress = progress
)
} else {
rlang::check_installed("mirai")
if (progress) {
plots_df$plot <-
with(mirai::daemons(ncores),
mirai::mirai_map(
.x = dplyr::select(plots_df, "data", "url"),
.f = save_plot
)[mirai::.progress_cli, mirai::.stop])

} else {
plots_df$plot <-
with(mirai::daemons(ncores),
mirai::mirai_map(
.x = dplyr::select(plots_df, "data", "url"),
.f = save_plot
)[mirai::.stop])
}
)
}

return(plots_df)
}
Expand Down Expand Up @@ -598,7 +620,7 @@ create_static_map <-
ggplot2::labs(x = NULL, y = NULL)

if (length(pollutant) > 1 |
!is.null(facet)) {
!is.null(facet)) {
plt <-
plt + ggplot2::facet_wrap(ggplot2::vars(.data[[split_col]]), nrow = facet.nrow) +
ggplot2::theme(strip.text = ggtext::element_markdown())
Expand Down Expand Up @@ -735,8 +757,8 @@ check_legendposition <- function(position, static) {
} else {
position <- position %||% "topright"
rlang::arg_match(position,
c("topright", "topleft", "bottomright", "bottomleft"),
multiple = TRUE
c("topright", "topleft", "bottomright", "bottomleft"),
multiple = TRUE
)
}
return(position)
Expand Down
10 changes: 10 additions & 0 deletions man/annulusMap.Rd

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

10 changes: 10 additions & 0 deletions man/diffMap.Rd

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

Loading

0 comments on commit 4ab2191

Please sign in to comment.