-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmap.R
More file actions
327 lines (313 loc) · 10.2 KB
/
map.R
File metadata and controls
327 lines (313 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# mirai map functions ----------------------------------------------------------
#' mirai Map
#'
#' Asynchronous parallel map of a function over a list or vector using
#' \pkg{mirai}, with optional \pkg{promises} integration. For matrix or
#' dataframe inputs, maps over rows.
#'
#' Sends each application of function `.f` on an element of `.x` (or row of
#' `.x`) for computation in a separate [mirai()] call. If `.x` is named, names
#' are preserved.
#'
#' Takes advantage of \pkg{mirai} scheduling to minimise overall execution
#' time.
#'
#' Facilitates recovery from partial failure by returning all 'miraiError' /
#' 'errorValue' as the case may be, thus allowing only failures to be re-run.
#'
#' This function requires daemons to have previously been set, and will error
#' otherwise.
#'
#' @param .x (list | vector | matrix | data.frame) input to map over. For matrix
#' or dataframe, maps over rows (see Multiple Map section).
#' @param .f (function) applied to each element of `.x`, or each row of a
#' matrix / dataframe.
#' @param ... (named arguments) objects referenced but not defined in `.f`.
#' @param .args (list) constant arguments passed to `.f`.
#' @param .promise (function | list) registers a promise against each mirai.
#' Either an `onFulfilled` function, or a list of (`onFulfilled`,
#' `onRejected`) functions for [promises::then()]. Requires the \pkg{promises}
#' package.
#' @inheritParams mirai
#'
#' @return A 'mirai_map' (list of 'mirai' objects).
#'
#' @section Collection Options:
#'
#' `x[]` collects the results of a 'mirai_map' `x` and returns a list. This will
#' wait for all asynchronous operations to complete if still in progress,
#' blocking but user-interruptible.
#'
#' `x[.flat]` collects and flattens map results to a vector, checking that
#' they are of the same type to avoid coercion. Note: errors if an 'errorValue'
#' has been returned or results are of differing type.
#'
#' `x[.progress]` collects map results whilst showing a progress bar from
#' the \pkg{cli} package, if installed, with completion percentage and ETA, or
#' else a simple text progress indicator. Note: if the map operation completes
#' too quickly then the progress bar may not show at all.
#'
#' `x[.stop]` collects map results applying early stopping, which stops at
#' the first failure and cancels remaining operations.
#'
#' The options above may be combined in the manner of: \cr
#' `x[.stop, .progress]` which applies early stopping together with a
#' progress indicator.
#'
#' @section Multiple Map:
#'
#' If `.x` is a matrix or dataframe (or other object with 'dim' attributes),
#' *multiple* map is performed over its **rows**. Character row names are
#' preserved as names of the output.
#'
#' This allows map over 2 or more arguments, and `.f` should accept at least as
#' many arguments as there are columns. If the dataframe has column names, or
#' the matrix has column dimnames, arguments are passed to `.f` by name.
#'
#' To map over **columns** instead, first wrap a dataframe in [as.list()], or
#' transpose a matrix using [t()].
#'
#' @section Nested Maps:
#'
#' To run maps within maps, the function provided to the outer map must include
#' a call to [daemons()] to set daemons for the inner map. To guard against
#' inadvertently spawning an excessive number of daemons on the same machine,
#' attempting to launch local daemons within a map using `daemons(n)` will
#' error.
#'
#' When the outer daemons run on remote machines and you want local daemons on
#' each, use 2 separate calls instead of `daemons(n)`:
#' `daemons(url = local_url()); launch_local(n)`. This is equivalent, and is
#' permitted from within a map.
#'
#' @examplesIf interactive()
#' daemons(4)
#'
#' # perform and collect mirai map
#' mm <- mirai_map(c(a = 1, b = 2, c = 3), rnorm)
#' mm
#' mm[]
#'
#' # map with constant args specified via '.args'
#' mirai_map(1:3, rnorm, .args = list(n = 5, sd = 2))[]
#'
#' # flatmap with helper function passed via '...'
#' mirai_map(
#' 10^(0:9),
#' function(x) rnorm(1L, valid(x)),
#' valid = function(x) min(max(x, 0L), 100L)
#' )[.flat]
#'
#' # unnamed matrix multiple map: arguments passed to function by position
#' (mat <- matrix(1:4, nrow = 2L))
#' mirai_map(mat, function(x = 10, y = 0, z = 0) x + y + z)[.flat]
#'
#' # named matrix multiple map: arguments passed to function by name
#' (mat <- matrix(1:4, nrow = 2L, dimnames = list(c("a", "b"), c("y", "z"))))
#' mirai_map(mat, function(x = 10, y = 0, z = 0) x + y + z)[.flat]
#'
#' # dataframe multiple map: using a function taking '...' arguments
#' df <- data.frame(a = c("Aa", "Bb"), b = c(1L, 4L))
#' mirai_map(df, function(...) sprintf("%s: %d", ...))[.flat]
#'
#' # indexed map over a vector (using a dataframe)
#' v <- c("egg", "got", "ten", "nap", "pie")
#' mirai_map(
#' data.frame(1:length(v), v),
#' sprintf,
#' .args = list(fmt = "%d_%s")
#' )[.flat]
#'
#' # return a 'mirai_map' object, check for resolution, collect later
#' mp <- mirai_map(2:4, function(x) runif(1L, x, x + 1))
#' unresolved(mp)
#' mp
#' mp[.flat]
#' unresolved(mp)
#'
#' # progress indicator counts up from 0 to 4 seconds
#' res <- mirai_map(1:4, Sys.sleep)[.progress]
#'
#' # stops early when second element returns an error
#' tryCatch(mirai_map(list(1, "a", 3), sum)[.stop], error = identity)
#'
#' daemons(0)
#'
#' @examplesIf interactive() && requireNamespace("promises", quietly = TRUE)
#' # promises example that outputs the results, including errors, to the console
#' daemons(1, dispatcher = FALSE)
#' ml <- mirai_map(
#' 1:30,
#' function(i) {Sys.sleep(0.1); if (i == 30) stop(i) else i},
#' .promise = list(
#' function(x) cat(paste(x, "")),
#' function(x) { cat(conditionMessage(x), "\n"); daemons(0) }
#' )
#' )
#'
#' @export
#'
mirai_map <- function(.x, .f, ..., .args = list(), .promise = NULL, .compute = NULL) {
require_daemons(.compute = .compute, call = environment())
is.function(.f) || stop(sprintf(._[["function_required"]], typeof(.f)))
if (is.null(.compute)) {
.compute <- .[["cp"]]
}
spn <- otel_map_span(.compute)
dx <- dim(.x)
vec <- if (is.null(dx)) {
`names<-`(
lapply(.x, function(x) {
mirai(
.expr = do.call(.f, c(list(.x), .args), quote = TRUE),
...,
.args = list(.f = .f, .x = x, .args = .args, .mirai_within_map = TRUE),
.compute = .compute
)
}),
names(.x)
)
} else if (is.data.frame(.x)) {
rn <- attr(.x, "row.names", exact = TRUE)
`names<-`(
lapply(seq_len(dx[1L]), function(i) {
mirai(
.expr = do.call(.f, c(.x, .args), quote = TRUE),
...,
.args = list(.f = .f, .x = lapply(.x, `[[`, i), .args = .args, .mirai_within_map = TRUE),
.compute = .compute
)
}),
if (is.character(rn)) rn
)
} else {
`names<-`(
lapply(seq_len(dx[1L]), function(i) {
mirai(
.expr = do.call(.f, c(as.list(.x), .args), quote = TRUE),
...,
.args = list(.f = .f, .x = .x[i, ], .args = .args, .mirai_within_map = TRUE),
.compute = .compute
)
}),
if (is.matrix(.x)) dimnames(.x)[[1L]]
)
}
if (length(.promise)) {
if (is.list(.promise)) {
lapply(vec, promises::then, .promise[[1L]], .promise[2L][[1L]])
} else {
lapply(vec, promises::then, .promise)
}
}
invisible(`class<-`(vec, "mirai_map"))
}
#' @export
#'
`[.mirai_map` <- function(x, ...) {
missing(..1) && return(collect_aio_(x))
dots <- eval(`[[<-`(substitute(alist(...)), 1L, quote(list)), envir = .opts)
mmap(x, dots, envir = parent.frame())
}
#' @export
#'
print.mirai_map <- function(x, ...) {
xlen <- length(x)
cat(sprintf("< mirai map [%d/%d] >\n", xlen - .unresolved(x), xlen), file = stdout())
invisible(x)
}
#' mirai Map Options
#'
#' Expressions to be provided to the `[]` method for 'mirai_map' objects.
#'
#' @inheritSection mirai_map Collection Options
#'
#' @keywords internal
#' @export
#'
.flat <- compiler::compile(quote(
if (i == 0L) {
xi <- TRUE
} else if (i == 1L) {
typ <<- typeof(xi)
} else {
is_error_value(xi) && stop_m(x, i, xi)
typeof(xi) != typ &&
{
stop_mirai(x)
cli_enabled ||
stop(
sprintf("Cannot flatten outputs of differing type: %s / %s", typ, typeof(xi)),
call. = FALSE
)
cli::cli_abort(
c(`!` = "cannot flatten outputs of differing type: {typ} / {typeof(xi)}"),
location = i,
name = names(x)[i],
call = quote(mirai_map())
)
}
}
))
#' @rdname dot-flat
#' @export
#'
.progress <- compiler::compile(quote(
if (cli_enabled) {
if (i == 0L) {
envir <<- new.env(parent = envir)
options <- .[["progress"]]
if (is.list(options)) {
do.call(
cli::cli_progress_bar,
c(list(total = xlen, auto_terminate = TRUE, .envir = envir), options)
)
} else {
cli::cli_progress_bar(
name = options,
type = NULL,
total = xlen,
auto_terminate = TRUE,
.envir = envir
)
}
`[[<-`(., "progress", NULL)
} else {
cli::cli_progress_update(.envir = envir)
}
} else {
cat(sprintf("\r[ %d / %d %s", i, xlen, if (i < xlen) ".... ]" else "done ]\n"), file = stderr())
}
))
#' @rdname dot-flat
#' @export
#'
.stop <- compiler::compile(quote(is_error_value(xi) && stop_m(x, i, xi)))
# internals --------------------------------------------------------------------
mmap <- function(x, dots, envir = parent.frame()) {
expr <- if (length(dots) > 1L) do.call(expression, dots) else dots[[1L]]
xlen <- length(x)
i <- 0L
typ <- xi <- FALSE
collect_map <- function(i) {
xi <- collect_aio_(x[[i]])
eval(expr)
xi
}
eval(expr)
out <- `names<-`(lapply(seq_len(xlen), collect_map), names(x))
xi && return(unlist(out, recursive = FALSE))
out
}
stop_m <- function(x, i, xi) {
stop_mirai(x)
cli_enabled || stop(sprintf("In index %d:\n%s", i, attr(xi, "message")), call. = FALSE)
name <- names(x)[i]
cli::cli_abort(
c(i = "In index: {i}.", i = if (length(name) && nzchar(name)) "With name: {name}."),
location = i,
name = name,
parent = `class<-`(attributes(xi), c("error", "condition")),
call = quote(mirai_map())
)
}