-
Notifications
You must be signed in to change notification settings - Fork 26
/
static.Rmd
596 lines (499 loc) · 17 KB
/
static.Rmd
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# Static branching {#static}
```{r, message = FALSE, warning = FALSE, echo = FALSE}
knitr::opts_knit$set(root.dir = fs::dir_create(tempfile()))
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
options(
drake_make_menu = FALSE,
drake_clean_menu = FALSE,
warnPartialMatchArgs = FALSE,
crayon.enabled = FALSE,
readr.show_progress = FALSE,
tidyverse.quiet = TRUE
)
```
```{r, message = FALSE, warning = FALSE, echo = FALSE}
library(drake)
library(glue)
library(purrr)
library(rlang)
library(tidyverse)
invisible(drake_example("main", overwrite = TRUE))
invisible(file.copy("main/raw_data.xlsx", ".", overwrite = TRUE))
invisible(file.copy("main/report.Rmd", ".", overwrite = TRUE))
tmp <- suppressWarnings(drake_plan(x = 1, y = 2))
```
## Why static branching?
Static branching helps us write large plans compactly. Instead of typing out every single target by hand, we use a special shorthand to declare entire batches of similar targets. To practice static branching in a controlled setting, try the interactive exercises at <https://wlandau.shinyapps.io/learndrakeplans> (from the workshop at <https://github.com/wlandau/learndrake>).
Without static branching, plans like this one become too cumbersome to type by hand.
```{r, eval = FALSE}
# Without static branching:
drake_plan(
data = get_data(),
analysis_fast_1_main = main(data, mean = 1, tuning = "fast"),
analysis_slow_1_main = main(data, mean = 1, tuning = "slow"),
analysis_fast_2_main = main(data, mean = 2, tuning = "fast"),
analysis_slow_2_main = main(data, mean = 2, tuning = "slow"),
analysis_fast_3_main = main(data, mean = 3, tuning = "fast"),
analysis_slow_3_main = main(data, mean = 3, tuning = "slow"),
analysis_fast_4_main = main(data, mean = 4, tuning = "fast"),
analysis_slow_4_main = main(data, mean = 4, tuning = "slow"),
analysis_fast_1_altv = altv(data, mean = 1, tuning = "fast"),
analysis_slow_1_altv = altv(data, mean = 1, tuning = "slow"),
analysis_fast_2_altv = altv(data, mean = 2, tuning = "fast"),
analysis_slow_2_altv = altv(data, mean = 2, tuning = "slow"),
analysis_fast_3_altv = altv(data, mean = 3, tuning = "fast"),
analysis_slow_3_altv = altv(data, mean = 3, tuning = "slow"),
analysis_fast_4_altv = altv(data, mean = 4, tuning = "fast"),
analysis_slow_4_altv = altv(data, mean = 4, tuning = "slow"),
summary_analysis_fast_1_main = summarize_model(analysis_fast_1_main),
summary_analysis_slow_1_main = summarize_model(analysis_slow_1_main),
summary_analysis_fast_2_main = summarize_model(analysis_fast_2_main),
summary_analysis_slow_2_main = summarize_model(analysis_slow_2_main),
summary_analysis_fast_3_main = summarize_model(analysis_fast_3_main),
summary_analysis_slow_3_main = summarize_model(analysis_slow_3_main),
summary_analysis_fast_4_main = summarize_model(analysis_fast_4_main),
summary_analysis_slow_4_main = summarize_model(analysis_slow_4_main),
summary_analysis_fast_1_altv = summarize_model(analysis_fast_1_altv),
summary_analysis_slow_1_altv = summarize_model(analysis_slow_1_altv),
summary_analysis_fast_2_altv = summarize_model(analysis_fast_2_altv),
summary_analysis_slow_2_altv = summarize_model(analysis_slow_2_altv),
summary_analysis_fast_3_altv = summarize_model(analysis_fast_3_altv),
summary_analysis_slow_3_altv = summarize_model(analysis_slow_3_altv),
summary_analysis_fast_4_altv = summarize_model(analysis_fast_4_altv),
summary_analysis_slow_4_altv = summarize_model(analysis_slow_4_altv),
model_summary_altv = dplyr::bind_rows(
summary_analysis_fast_1_altv,
summary_analysis_slow_1_altv,
summary_analysis_fast_2_altv,
summary_analysis_slow_2_altv,
summary_analysis_fast_3_altv,
summary_analysis_slow_3_altv,
summary_analysis_fast_4_altv,
summary_analysis_slow_4_altv
),
model_summary_main = dplyr::bind_rows(
summary_analysis_fast_1_main,
summary_analysis_slow_1_main,
summary_analysis_fast_2_main,
summary_analysis_slow_2_main,
summary_analysis_fast_3_main,
summary_analysis_slow_3_main,
summary_analysis_fast_4_main,
summary_analysis_slow_4_main
)
)
```
Static branching makes it easier to write and understand plans. To activate static branching, use the `transform` argument of `target()`.
```{r}
# With static branching:
model_functions <- rlang::syms(c("main", "altv")) # We need symbols.
model_functions # List of symbols.
plan <- drake_plan(
data = get_data(),
analysis = target(
model_function(data, mean = mean_value, tuning = tuning_setting),
# Define an analysis target for each combination of
# tuning_setting, mean_value, and model_function.
transform = cross(
tuning_setting = c("fast", "slow"),
mean_value = !!(1:4), # Why `!!`? See "Tidy Evaluation" below.
model_function = !!model_functions # Why `!!`? See "Tidy Evaluation" below.
)
),
# Define a new summary target for each analysis target defined previously.
summary = target(
summarize_model(analysis),
transform = map(analysis)
),
# Group together the summary targets by the corresponding value
# of model_function.
model_summary = target(
dplyr::bind_rows(summary),
transform = combine(summary, .by = model_function)
)
)
plan
```
*Always* check the graph to make sure the plan makes sense.
```{r}
plot(plan) # a quick and dirty alternative to vis_drake_graph()
```
If the graph is too complicated to look at or too slow to load, downsize the plan with `max_expand`. Then, when you are done debugging and testing, remove `max_expand` to scale back up to the full plan.
```{r}
model_functions <- rlang::syms(c("main", "altv"))
plan <- drake_plan(
max_expand = 2,
data = get_data(),
analysis = target(
model_function(data, mean = mean_value, tuning = tuning_setting),
transform = cross(
tuning_setting = c("fast", "slow"),
mean_value = !!(1:4), # Why `!!`? See "Tidy Evaluation" below.
model_function = !!model_functions # Why `!!`? See "Tidy Evaluation" below.
)
),
summary = target(
summarize_model(analysis),
transform = map(analysis)
),
model_summary = target(
dplyr::bind_rows(summary),
transform = combine(summary, .by = model_function) # defined in "analysis"
)
)
# Click and drag the nodes in the graph to improve the view.
plot(plan)
```
## Grouping variables
A *grouping variable* contains iterated values for a single instance of `map()` or `cross()`. `mean_value` and `tuning_par` are grouping variables below. Notice how they are defined inside `cross()`. Grouping variables are not targets, and they must be declared inside static transformations.
```{r}
drake_plan(
data = get_data(),
model = target(
fit_model(data, mean_value, tuning_par),
transform = cross(
mean_value = c(1, 2),
tuning_par = c("fast", "slow")
)
)
)
```
Each model has its own `mean_value` and `tuning_par`. To see this correspondence, set `trace = TRUE`.
```{r}
drake_plan(
trace = TRUE,
data = get_data(),
model = target(
fit_model(data, mean_value, tuning_par),
transform = cross(
mean_value = c(1, 2),
tuning_par = c("fast", "slow")
)
)
)
```
If we summarize those models, each *summary* has its own `mean_value` and `tuning_par`. In other words, grouping variables have a natural nesting, and they propagate forward so we can use them in downstream targets. Notice how `mean_value` and `tuning_par` appear in `summarize_model()` and `combine()` below.
```{r}
plan <- drake_plan(
trace = TRUE,
data = get_data(),
model = target(
fit_model(data, mean_value, tuning_par),
transform = cross(
mean_value = c(1, 2),
tuning_par = c("fast", "slow")
)
),
summary = target(
# mean_value and tuning_par are old grouping variables from the models
summarize_model(model, mean_value, tuning_par),
transform = map(model)
),
summary_by_tuning = target(
dplyr::bind_rows(summary),
# tuning_par is an old grouping variable from the models.
transform = combine(summary, .by = tuning_par)
)
)
plot(plan)
```
### Limitations of grouping variables
Each grouping variable should be defined only once. In the plan below, there are multiple conflicting definitions of `a1`, `a2`, and `a3` in the dependencies of `c1`, so `drake` does not know which definitions to use.
```{r, error = TRUE}
drake_plan(
b1 = target(1, transform = map(a1 = 1, a2 = 1, .id = FALSE)),
b2 = target(1, transform = map(a1 = 1, a3 = 1, .id = FALSE)),
b3 = target(1, transform = map(a2 = 1, a3 = 1, .id = FALSE)),
c1 = target(1, transform = map(a1, a2, a3, .id = FALSE)),
trace = TRUE
)
```
Other workarounds include `bind_plans()` (on separate sub-plans) and [dynamic branching](#dynamic). Always check your plans before you run them (`vis_drake_graph()` etc.).
## Tidy evaluation
In earlier plans, we used "bang-bang" operator `!!` from [tidy evaluation](https://tidyeval.tidyverse.org/), e.g. `model_function = !!model_functions` in `cross()`. But why? Why not just type `model_function = model_functions`? Consider the following incorrect plan.
```{r}
model_functions <- rlang::syms(c("main", "altv"))
plan <- drake_plan(
data = get_data(),
analysis = target(
model_function(data, mean = mean_value, tuning = tuning_setting),
transform = cross(
tuning_setting = c("fast", "slow"),
mean_value = 1:4, # without !!
model_function = model_functions # without !!
)
)
)
drake_plan_source(plan)
```
Because we omit `!!`, we create two problems:
1. The commands use `model_functions()` instead of the desired `main()` and `altv()`.
2. We are missing the targets with `mean = 2` and `mean = 3`.
Why? To make static branching work properly, `drake` does not actually evaluate the arguments to `cross()`. It just uses the raw symbols and expressions. To force `drake` to use the *values* instead, we need `!!`.
```{r}
model_functions <- rlang::syms(c("main", "altv"))
plan <- drake_plan(
data = get_data(),
analysis = target(
model_function(data, mean = mean_value, tuning = tuning_setting),
transform = cross(
tuning_setting = c("fast", "slow"),
mean_value = !!(1:4), # with !!
model_function = !!model_functions # with !!
)
)
)
drake_plan_source(plan)
```
## Static transformations
There are four transformations in static branching: `map()`, `cross()`, `split()`, and `combine()`. They are not actual functions, just special language to supply to the `transform` argument of `target()` in `drake_plan()`. Each transformation is similar to a function from the [Tidyverse](https://www.tidyverse.org/).
| `drake` | Tidyverse analogue |
|-------------|-----------------------------|
| `map()` | `pmap()` from `purrr` |
| `cross()` | `crossing()` from `tidyr` |
| `split()` | `group_map()` from `dplyr` |
| `combine()` | `summarize()` from `dplyr` |
### `map()`
`map()` creates a new target for each row in a grid.
```{r}
drake_plan(
x = target(
simulate_data(center, scale),
transform = map(center = c(2, 1, 0), scale = c(3, 2, 1))
)
)
```
You can supply the grid directly with the `.data` argument. Note the use of `!!` below. (See the tidy evaluation section.)
```{r}
my_grid <- tibble(
sim_function = c("rnorm", "rt", "rcauchy"),
title = c("Normal", "Student t", "Cauchy")
)
my_grid$sim_function <- rlang::syms(my_grid$sim_function)
drake_plan(
x = target(
simulate_data(sim_function, title, center, scale),
transform = map(
center = c(2, 1, 0),
scale = c(3, 2, 1),
.data = !!my_grid,
# In `.id`, you can select one or more grouping variables
# for pretty target names.
# Set to FALSE to use short numeric suffixes.
.id = sim_function # Try `.id = c(sim_function, center)` yourself.
)
)
)
```
### `cross()`
`cross()` creates a new target for each combination of argument values.
```{r}
drake_plan(
x = target(
simulate_data(nrow, ncol),
transform = cross(nrow = c(1, 2, 3), ncol = c(4, 5))
)
)
```
### `split()`
The `split()` transformation distributes a dataset as uniformly as possible across multiple targets.
```{r, split1}
plan <- drake_plan(
large_data = get_data(),
slice_analysis = target(
large_data %>%
analyze(),
transform = split(large_data, slices = 4)
),
results = target(
dplyr::bind_rows(slice_analysis),
transform = combine(slice_analysis)
)
)
plan
```
```{r}
plot(plan)
```
At runtime, `drake_slice()` takes a single subset of the data. It supports data frames, matrices, and arbitrary arrays.
```{r}
drake_slice(mtcars, slices = 32, index = 1)
drake_slice(mtcars, slices = 32, index = 2)
```
### `combine()`
`combine()` aggregates targets. The closest comparison is the unquote-splice operator `!!!` from tidy evaluation.
```{r}
plan <- drake_plan(
data_group1 = target(
sim_data(mean = x, sd = y),
transform = map(x = c(1, 2), y = c(3, 4))
),
data_group2 = target(
pull_data(url),
transform = map(url = c("example1.com", "example2.com"))
),
larger = target(
bind_rows(data_group1, data_group2, .id = "id") %>%
arrange(sd) %>%
head(n = 400),
transform = combine(data_group1, data_group2)
)
)
drake_plan_source(plan)
```
To create multiple combined groups, use the `.by` argument.
```{r}
plan <- drake_plan(
data = target(
sim_data(mean = x, sd = y, skew = z),
transform = cross(x = c(1, 2), y = c(3, 4), z = c(5, 6))
),
combined = target(
bind_rows(data, .id = "id") %>%
arrange(sd) %>%
head(n = 400),
transform = combine(data, .by = c(x, y))
)
)
drake_plan_source(plan)
```
## Target names
`drake` releases after 7.12.0 let you define your own custom names with the optional `.names` argument of transformations.
```{r}
analysis_names <- c("experimental", "thorough", "minimal", "naive")
plan <- drake_plan(
dataset = target(
get_dataset(data_index),
transform = map(data_index = !!seq_len(2), .names = c("new", "old"))
),
analysis = target(
apply_method(method_name, dataset),
transform = cross(
method_name = c("method1", "method2"),
dataset,
.names = !!analysis_names
)
),
summary = target(
summarize(analysis),
transform = combine(analysis, .by = dataset, .names = c("table1", "table2"))
)
)
plan
plot(plan)
```
The disadvantage of `.names` is you need to know in advance the number of targets a transformation will generate. As an alternative, all transformations have an optional `.id` argument to control the names of targets. Use it to select the grouping variables that go into the names, as well as the order they appear in the suffixes.
```{r}
drake_plan(
data = target(
get_data(param1, param2),
transform = map(
param1 = c(123, 456),
param2 = c(7, 9),
param2 = c("abc", "xyz"),
.id = param2
)
)
)
```
```{r}
drake_plan(
data = target(
get_data(param1, param2),
transform = map(
param1 = c(123, 456),
param2 = c(7, 9),
param2 = c("abc", "xyz"),
.id = c(param2, param1)
)
)
)
```
```{r}
drake_plan(
data = target(
get_data(param1, param2),
transform = map(
param1 = c(123, 456),
param2 = c(7, 9),
param2 = c("abc", "xyz"),
.id = c(param1, param2)
)
)
)
```
Set `.id` to `FALSE` to ignore the grouping variables altogether.
```{r}
drake_plan(
data = target(
get_data(param1, param2),
transform = map(
param1 = c(123, 456),
param2 = c(7, 9),
param2 = c("abc", "xyz"),
.id = FALSE
)
)
)
```
Finally, `drake` supports a special `.id_chr` symbol in commands to let you refer to the name of the current target as a character string.
```{r}
as_chr <- function(x) {
deparse(substitute(x))
}
plan <- drake_plan(
data = target(
get_data(param),
transform = map(param = c(123, 456))
),
keras_model = target(
save_model_hdf5(fit_model(data), file_out(!!sprintf("%s.h5", .id_chr))),
transform = map(data, .id = param)
),
result = target(
predict(load_model_hdf5(file_in(!!sprintf("%s.h5", as_chr(keras_model))))),
transform = map(keras_model, .id = param)
)
)
plan
```
```{r}
drake_plan_source(plan)
```
## Tags
A tag is a custom grouping variable for a transformation. There are two kinds of tags:
1. In-tags, which contain the target name you start with, and
2. Out-tags, which contain the target names generated by the transformations.
```{r}
drake_plan(
x = target(
command,
transform = map(y = c(1, 2), .tag_in = from, .tag_out = c(to, out))
),
trace = TRUE
)
```
Subsequent transformations can use tags as grouping variables and add to existing tags.
```{r}
plan <- drake_plan(
prep_work = do_prep_work(),
local = target(
get_local_data(n, prep_work),
transform = map(n = c(1, 2), .tag_in = data_source, .tag_out = data)
),
online = target(
get_online_data(n, prep_work, port = "8080"),
transform = map(n = c(1, 2), .tag_in = data_source, .tag_out = data)
),
summary = target(
summarize(bind_rows(data, .id = "data")),
transform = combine(data, .by = data_source)
),
munged = target(
munge(bind_rows(data, .id = "data")),
transform = combine(data, .by = n)
)
)
drake_plan_source(plan)
plot(plan)
```