-
Notifications
You must be signed in to change notification settings - Fork 0
/
timation,
144 lines (107 loc) · 5.41 KB
/
timation,
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
future_apply package:future.apply R Documentation
_A_p_p_l_y _F_u_n_c_t_i_o_n_s _O_v_e_r _A_r_r_a_y _M_a_r_g_i_n_s _v_i_a _F_u_t_u_r_e_s
_D_e_s_c_r_i_p_t_i_o_n:
‘future_apply()’ implements ‘base::apply()’ using future with
perfect replication of results, regardless of future backend used.
It returns a vector or array or list of values obtained by
applying a function to margins of an array or matrix.
_U_s_a_g_e:
future_apply(
X,
MARGIN,
FUN,
...,
simplify = TRUE,
future.envir = parent.frame(),
future.stdout = TRUE,
future.conditions = "condition",
future.globals = TRUE,
future.packages = NULL,
future.seed = FALSE,
future.scheduling = 1,
future.chunk.size = NULL,
future.label = "future_apply-%d"
)
_A_r_g_u_m_e_n_t_s:
X: an array, including a matrix.
MARGIN: A vector giving the subscripts which the function will be
applied over. For example, for a matrix ‘1’ indicates rows,
‘2’ indicates columns, ‘c(1, 2)’ indicates rows and columns.
Where ‘X’ has named dimnames, it can be a character vector
selecting dimension names.
FUN: A function taking at least one argument.
simplify: a logical indicating whether results should be simplified if
possible.
future.envir: An environment passed as argument ‘envir’ to
‘future::future()’ as-is.
future.stdout: If ‘TRUE’ (default), then the standard output of the
underlying futures is captured, and re-outputted as soon as
possible. If ‘FALSE’, any output is silenced (by sinking it
to the null device as it is outputted). If ‘NA’ (not
recommended), output is _not_ intercepted.
future.conditions: A character string of conditions classes to be
captured and relayed. The default is the same as the
‘condition’ argument of ‘future::Future()’. To not intercept
conditions, use ‘conditions = character(0L)’. Errors are
always relayed.
future.globals: A logical, a character vector, or a named list for
controlling how globals are handled. For details, see below
section.
future.packages: (optional) a character vector specifying packages to
be attached in the R environment evaluating the future.
future.seed: A logical or an integer (of length one or seven), or a
list of ‘length(X)’ with pre-generated random seeds. For
details, see below section.
future.scheduling: Average number of futures ("chunks") per worker. If
‘0.0’, then a single future is used to process all elements
of ‘X’. If ‘1.0’ or ‘TRUE’, then one future per worker is
used. If ‘2.0’, then each worker will process two futures (if
there are enough elements in ‘X’). If ‘Inf’ or ‘FALSE’, then
one future per element of ‘X’ is used. Only used if
‘future.chunk.size’ is ‘NULL’.
future.chunk.size: The average number of elements per future ("chunk").
If ‘Inf’, then all elements are processed in a single future.
If ‘NULL’, then argument ‘future.scheduling’ is used.
future.label: If a character string, then each future is assigned a
label ‘sprintf(future.label, chunk_idx)’. If TRUE, then the
same as ‘future.label = "future_lapply-%d"’. If FALSE, no
labels are assigned.
...: (optional) Additional arguments passed to ‘FUN()’, except
future.* arguments, which are passed on to ‘future_lapply()’
used internally.
_V_a_l_u_e:
Returns a vector or array or list of values obtained by applying a
function to margins of an array or matrix. See ‘base::apply()’ for
details.
_A_u_t_h_o_r(_s):
The implementations of ‘future_apply()’ is adopted from the source
code of the corresponding base R function, which is licensed under
GPL (>= 2) with 'The R Core Team' as the copyright holder.
_E_x_a_m_p_l_e_s:
## ---------------------------------------------------------
## apply()
## ---------------------------------------------------------
X <- matrix(c(1:4, 1, 6:8), nrow = 2L)
Y0 <- apply(X, MARGIN = 1L, FUN = table)
Y1 <- future_apply(X, MARGIN = 1L, FUN = table)
print(Y1)
stopifnot(all.equal(Y1, Y0, check.attributes = FALSE)) ## FIXME
Y0 <- apply(X, MARGIN = 1L, FUN = stats::quantile)
Y1 <- future_apply(X, MARGIN = 1L, FUN = stats::quantile)
print(Y1)
stopifnot(all.equal(Y1, Y0))
## ---------------------------------------------------------
## Parallel Random Number Generation
## ---------------------------------------------------------
## Regardless of the future plan, the number of workers, and
## where they are, the random numbers produced are identical
X <- matrix(c(1:4, 1, 6:8), nrow = 2L)
plan(multisession)
set.seed(0xBEEF)
Y1 <- future_apply(X, MARGIN = 1L, FUN = sample, future.seed = TRUE)
print(Y1)
plan(sequential)
set.seed(0xBEEF)
Y2 <- future_apply(X, MARGIN = 1L, FUN = sample, future.seed = TRUE)
print(Y2)
stopifnot(all.equal(Y1, Y2))