-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathanimate_maps.Rmd
428 lines (313 loc) · 13.5 KB
/
animate_maps.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
---
title: "bupaR Docs | Animate Process Maps"
---
```{r echo = F, out.width="25%", fig.align = "right"}
knitr::include_graphics("images/icons/pm.PNG")
```
***
# Animate Process Maps
```{r echo = F}
knitr::opts_chunk$set(eval = T)
```
```{r include = F}
library(bupaverse)
library(processanimateR)
```
```{r eval = F}
library(bupaverse)
library(processanimateR)
```
Here, we use the `patients` event log provided by the `eventdataR` package.
A basic animation with static color and token size:
```{r warning = F}
animate_process(patients)
```
Default token color, size, or image can be changed as follows:
```{r}
animate_process(patients, mapping = token_aes(size = token_scale(12), shape = "rect"))
animate_process(patients, mapping = token_aes(color = token_scale("red")))
```
The example animation on the top of this site:
```{r}
animate_process(patients, mode = "relative", jitter = 10, legend = "color",
mapping = token_aes(color = token_scale("employee",
scale = "ordinal",
range = RColorBrewer::brewer.pal(7, "Paired"))))
```
Tokens can also be assigned images, for example:
```{r}
animate_process(patients,
mapping = token_aes(shape = "image",
size = token_scale(10),
image = token_scale("https://upload.wikimedia.org/wikipedia/en/5/5f/Pacman.gif")))
```
## Use external data
It is possible to use a secondary data frame to determine the aesthetics of tokens regardless of the timestamps at which activities occurred. This could be useful if some measurements were taken throughout a process, but the measurement event itself should not be included in the process map.
For example, the _lacticacid_ measurements of the `sepsis` data set could be used in that way:
```{r, message = F}
library(processanimateR)
library(dplyr)
library(bupaR)
# Extract only the lacticacid measurements
lactic <- sepsis %>%
mutate(lacticacid = as.numeric(lacticacid)) %>%
filter_activity(c("LacticAcid")) %>%
as.data.frame() %>%
select("case" = case_id,
"time" = timestamp,
value = lacticacid) # format needs to be 'case,time,value'
# Remove the measurement events from the sepsis log
sepsisBase <- sepsis %>%
filter_activity(c("LacticAcid", "CRP", "Leucocytes", "Return ER",
"IV Liquid", "IV Antibiotics"), reverse = T) %>%
filter_trace_frequency(percentage = 0.95)
# Animate with the secondary data frame `lactic`
animate_process(sepsisBase,
mode = "relative",
duration = 300,
legend = "color",
mapping = token_aes(color = token_scale(lactic,
scale = "linear",
range = c("#fff5eb","#7f2704"))))
```
`processanimateR` animation can be also used interactively as part of a (Shiny) web-application. Here, an example application that expects attributes are of an appropriate data type and automatically chooses appropriate color scales is given. We first define a function `ianimate_process()` that defines our Shiny application as follows:
## Shiny
```{r eval = F}
library(processanimateR)
library(shiny)
library(shinycssloaders)
ianimate_process <- function(eventlog, min.time = 30, max.time = 600, default.time = 60) {
ui <- function(request) {
fluidPage(
tags$head(tags$style("#process{height:90vh !important;}")),
titlePanel("Hello processanimateR!"),
sidebarLayout(
sidebarPanel(
width = 2,
sliderInput("duration", "Animation duration", min.time, max.time, default.time),
selectInput("type", "Animation type", c("relative", "absolute"), "relative"),
selectInput("sizeAttribute", "Size attribute", c("none", colnames(eventlog)), "none"),
selectInput("colorAttribute", "Color attribute", c("none", colnames(eventlog)), "none"),
selectInput("orientation", "Orientation", c("horizontal"="LR", "vertical"="TB"), "horizontal"),
h4("Selected cases"),
textOutput("token_selection"),
h4("Selected activities"),
textOutput("activity_selection")
),
mainPanel(
width = 10,
shinycssloaders::withSpinner(processanimaterOutput("process"))
)
)
)
}
server <- function(session, input, output) {
data <- reactive({
if (input$colorAttribute != "none") {
attr <- rlang::sym(input$colorAttribute)
val <- eventlog %>% pull(!!attr)
if (!(is.character(val) || is.factor(val))) {
warning("Trying to use a numeric attribute for the token color!")
}
}
if (input$sizeAttribute != "none") {
# This only works for numeric attributes
attr <- rlang::sym(input$sizeAttribute)
val <- eventlog %>% pull(!!attr)
if (!is.numeric(val)) {
warning("Trying to use a non-numeric attribute for the token size!")
}
}
eventlog
})
output$token_selection <- renderText({
paste0(input$process_tokens, ",")
})
output$activity_selection <- renderText({
paste0(input$process_activities, ",")
})
output$process <- renderProcessanimater(expr = {
graph <- processmapR::process_map(data(), render = F)
model <- DiagrammeR::add_global_graph_attrs(graph, attr = "rankdir", value = input$orientation, attr_type = "graph")
if (input$sizeAttribute != "none" && input$colorAttribute != "none") {
animate_process(data(), model,
mode = input$type,
legend = "color",
mapping = token_aes(color = token_scale(input$colorAttribute, scale = "ordinal",
range = RColorBrewer::brewer.pal(5, "YlOrBr")),
size = token_scale(input$sizeAttribute, scale = "linear", range = c(6,10))),
duration = input$duration)
} else if (input$sizeAttribute != "none") {
animate_process(data(), model,
mode = input$type,
legend = "size",
mapping = token_aes(size = token_scale(input$sizeAttribute, scale = "linear", range = c(6,10))),
duration = input$duration)
} else if (input$colorAttribute != "none") {
animate_process(data(), model,
mode = input$type,
legend = "color",
mapping = token_aes(color = token_scale(input$colorAttribute, scale = "ordinal", range = RColorBrewer::brewer.pal(5, "YlOrBr"))),
duration = input$duration)
} else {
animate_process(data(), model,
mode = input$type,
duration = input$duration)
}
})
}
shinyApp(ui, server, options = list(height = 500))
}
```
Then, the application can be, for example, launched by calling:
```{r eval = F}
library(eventdataR)
library(edeaR)
library(dplyr)
ianimate_process(sepsis %>%
filter_trace_frequency(percentage = 0.2) %>%
filter_activity(c("Return ER"), reverse = T) %>%
# we fix the datatype of some of the attributes to allow proper rendering of the token color
# the token size option currently only support numeric attributes
mutate_at(c("lacticacid", "leucocytes", "crp", "age"), as.numeric) %>%
mutate_at(c("disfuncorg", "sirscriteria2ormore", "infectionsuspected"), as.logical))
```
### Selections
Selections made in the `processanimateR` animation can be used as input by Shiny applications. Here a simple example in which a Shiny module is created that renders the case identifiers of selected tokens and the identifiers and names of selected activities:
```{r eval = F}
library(shiny)
library(processanimateR)
library(eventdataR)
library(jsonlite)
shinyAnimation <- function(eventlog, min.time = 30, max.time = 600, default.time = 60) {
# Define Shiny Module
animationUI <- function(id, title) {
ns <- NS(id)
tagList(
h2(title),
processanimaterOutput(ns("process")),
h4("Selected cases"),
textOutput(ns("token_selection")),
h4("Selected activities"),
textOutput(ns("activity_selection")),
h4("Current time"),
textOutput(ns("activity_time"))
)
}
animation <- function(input, output, session, ...) {
output$token_selection <- renderText({
if (is.null(input$process_tokens)) {
"None"
} else {
paste0(input$process_tokens, collapse = ",")
}
})
output$activity_selection <- renderText({
if (is.null(input$process_activities)) {
"None"
} else {
activities <- jsonlite::fromJSON(input$process_activities)
paste0("(", activities$id, ",", activities$activity, ")", collapse = ",")
}
})
output$activity_time <- renderText({
if (is.null(input$process_time)) {
"0"
} else {
input$process_time
}
})
output$process <- renderProcessanimater(expr = {
animate_process(eventlog, ...)
})
}
ui <- fluidPage(
animationUI("module1", "Relative"),
animationUI("module2", "Absolute")
)
# Two animations
server <- function(input, output, session) {
callModule(animation, "module1", mode = "relative")
callModule(animation, "module2")
}
shinyApp(ui, server, options = list(height = 500))
}
shinyAnimation(patients)
```
## User defined bins
_Contributed by [Dominic Rowney](https://github.com/DomRowney/). _
It is possible to change the aesthetics of tokens based on the timestamp of the animation.
For example, the number of days a ‘patient’ has been in the system.
```{r}
# Libraries ---------------------------------------------------------------
library(dplyr) ##pipes
library(tidyr) ##tidy data, partcularly the crossing() function
library(lubridate) ##date time manipulation
library(bupaR) ##buisness process analytics
library(processanimateR) ##animates process
# Create performance time flags ------------------------------------------------
my_flags <- data.frame(value = c(0,2,4,8,16)) %>%
mutate(day = days(value)) #convert numeric value into days
```
This will change the colour of the token at 0, 2, 4, 8, and 16 days.
`crossing()` joins the cases from `patients` to `my_flags` and creates all possible combinations.
```{r}
# Create timestamps of flags ----------------------------------------------
my_timeflags <- patients %>%
cases %>%
crossing(my_flags) %>% ##similar to a SQL outer join
mutate(time = start_timestamp + day) %>%
filter(time <= complete_timestamp) %>%
select("case" = patient,time,value) ##must be case, time, value
```
The data for the `token_scale()` must have the column headings ‘case, time, value’.
Without setting `domain = my_flags$value` the flags follow alphabetic order (e.g. 0, 16, 2, 4, 8) rather than the numeric order we want. See d3-legend for further information.
```{r}
# Animate process ---------------------------------------------------------
patients %>%
animate_process(mode ="absolute",
jitter=10,
legend = "color",
mapping = token_aes(
color = token_scale(my_timeflags
, scale = "ordinal"
, domain = my_flags$value
, range = rev(RColorBrewer::brewer.pal(5,"Spectral"))
)))
```
The colors can be adjusted by the `range` argument. In this case the scale is reversed with `rev()` to go from blue to red. See [RColorBrewer::brewer.pal.info](https://www.rdocumentation.org/packages/RColorBrewer/topics/ColorBrewer) for all options:
_**Acknowledgement**_
Thanks to [Dominic Rowney](https://github.com/DomRowney/) for this nice example of an advanced `processanimateR` usage. The original example code can be found [here](https://github.com/DomRowney/ProcessAnimateR_vignette).
## Token scales
Several aesthetics of tokens (`color`, `size`, `opacity`, `image`) can be dynamically determined based on event log attributes using D3 scales.
### Ordinal scales
```{r}
animate_process(patients,
legend = "color",
mapping = token_aes(color = token_scale("employee",
scale = "ordinal",
range = RColorBrewer::brewer.pal(8, "Paired"))))
```
### Linear scales
```{r eval = F}
animate_process(sample_n(traffic_fines, 1000) %>% filter_trace_frequency(percentage = 0.95),
mode = "relative",
legend = "color",
mapping = token_aes(color = token_scale("amount",
scale = "linear",
range = c("yellow","red"))))
```
### Time scales
```{r}
animate_process(patients,
mapping = token_aes(color = token_scale("time",
scale = "time",
range = c("blue","red"))))
```
Source: [https://bupaverse.github.io/processanimateR/](https://bupaverse.github.io/processanimateR/)
```{r footer, results = "asis", echo = F}
CURRENT_PAGE <- stringr::str_replace(knitr::current_input(), ".Rmd",".html")
res <- knitr::knit_expand("_button_footer.Rmd", quiet = TRUE)
res <- knitr::knit_child(text = unlist(res), quiet = TRUE)
cat(res, sep = '\n')
```