-
Notifications
You must be signed in to change notification settings - Fork 1
/
fst_slides.Rmd
272 lines (181 loc) · 5.2 KB
/
fst_slides.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
---
title: "fst package for dealing with big data"
subtitle: "༼ʘ̚ل͜ʘ̚༽"
author: ""
institute: ""
date: ""
output:
xaringan::moon_reader:
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
# Links
- Slides: https://quinnasena.github.io/fst/fst_slides.html#1
- fst ducumentation: https://cran.r-project.org/web/packages/fst/fst.pdf
- tidyfst ducmentation: https://cran.r-project.org/web/packages/tidyfst/tidyfst.pdf
- Detailed info: https://www.r-bloggers.com/lightning-fast-serialization-of-data-frames-using-the-fst-package/
---
# The problem
.pull-left[
- 10 treatments applied to origin dataframe
- Second level of another 10 treatments = 100 treatments
- Third level of 10 treatments = 1000 (total 1111 dataframes)
- Each dataframe is 4750 rows (total = 5277250 rows)
- Multiplied by 100 replicates...
- All of the treatments need to be analysed
]
.pull-right[
```{r dendro, echo=FALSE, message=FALSE, fig.width=6, fig.height=6,fig.show='hold'}
library(ggraph)
library(igraph)
library(microbenchmark)
library(tidyverse)
library(fst)
library(tidyverse)
library(tidyfst)
# create an edge list data frame giving the hierarchical structure of your individuals
d1 <- data.frame(from="origin", to=paste("group", seq(1,10), sep=""))
d2 <- data.frame(from=rep(d1$to, each=10), to=paste("subgroup", seq(1,100), sep="_"))
#d3 <- data.frame(from=rep(d2$to, each=100), to=paste("sub_subgroup", seq(1,1000), sep="_"))
edges <- rbind(d1, d2)
# Create a graph object
mygraph <- graph_from_data_frame( edges )
# Basic tree
ggraph(mygraph, layout = 'dendrogram', circular = FALSE) +
geom_edge_diagonal() +
geom_node_point() +
theme_void()
```
]
---
# The problem
- Origin dataframe (with all treatments) = 18 Gb
- Analysis of model = 45 Gb output
- Further analysis needs to read in 45 Gb data and do further computations
- in R subsetting dataframes creates a copy of the original dataframe, both of which are stored in RAM.
- Impossible on my personal laptop and an inefficient use of resources from NeSI
---
# Hello fst
```{r}
### Create something big(ish) 30000 rows, 200 variables plus an alphabetical id column
a_lot_of_data <- as.data.frame(
matrix(
rnorm(3000000),
nrow = 30000,
ncol = 200
)
) %>%
mutate(id = rep(LETTERS[1:20], each=1500))
### How big?
print(object.size(a_lot_of_data), units = "Mb")
### Save as RDS and as FST
saveRDS(a_lot_of_data, "a_lot_of_data.Rds")
fst::write.fst(a_lot_of_data, "a_lot_of_data.fst")
```
---
# Write speed
.pull-left[
## Rds
```{r}
write_speed_rds <- microbenchmark(
saveRDS(a_lot_of_data, "a_lot_of_data.rds"),
times = 1
)
# speed in GB/s
as.numeric(object.size(a_lot_of_data)) / write_speed_rds$time
# speed in seconds
write_speed_rds$time/1e9
```
]
.pull-right[
## fst
```{r}
write_speed_fst <- microbenchmark(
write_fst(a_lot_of_data, "a_lot_of_data.fst"),
times = 1
)
# speed in GB/s
as.numeric(object.size(a_lot_of_data)) / write_speed_fst$time
# speed in seconds
write_speed_fst$time/1e9
```
]
---
# Size matters, the smaller the better...
```{r}
### Read in both formats
a_lot_of_data_rds <- readRDS("a_lot_of_data.rds")
a_lot_of_data_fst <- fst("a_lot_of_data.fst")
```
--
```{r}
### How Big are they now?
## Rds, same as original
print(object.size(a_lot_of_data_rds), units = "Mb")
## fst, a lot smaller than original
print(object.size(a_lot_of_data_fst), units = "Kb")
```
---
# Subsetting Rds
```{r}
### With Rds format we read in the entire dataframe and subset
subset_rds <- a_lot_of_data_rds[a_lot_of_data_rds$id == "E", c("V98", "id")]
print(object.size(subset_rds), units = "Kb")
head(subset_rds)
## Size total = 30.2 Kb + 46 Mb
```
---
# Subsetting fst
```{r}
### fst subsets *without* reading entire dataframe into memory
subset_fst <- a_lot_of_data_fst[a_lot_of_data_fst$id == "E", c("V98", "id")]
head(subset_fst)
print(object.size(subset_fst), units = "Kb")
## Size total = 16.3 Kb + 24.3 Kb
```
---
# Read fst
```{r}
### fst can read-in part of a dataset but I think it only takes row numbers as an argument
read_subset_fst <- read_fst("a_lot_of_data.fst", c("id","V98"), 1000, 2000)
head(read_subset_fst)
print(object.size(read_subset_fst), units = "Kb")
## Size total = 16.6 Kb
```
---
# tidyfst
- fst does not handle like a regular dataframe (although can be converted after subsetting)
```{r}
### Subsetting can also be done with tidyfst package that replicates some tidyverse functions
subset_tidyfst <- filter_dt(a_lot_of_data_fst, id == "C") %>%
select_dt(V98, id) #the tidyfst way of filtering fst data tables
head(subset_tidyfst)
```
---
# Pros and cons
.pull-left[
## Rds
- Capable of saving a list of outputs as one object (fst seems to only save a single object)
- Saved output read back in same format, i.e., class (df, matrix...)
\----------
<br>
- Slower to read/write
]
.pull-right[
## fst
- Can save huge amounts of memory!
- read/write is much much faster than any other package
<br>
\----------
<br>
- Reads back as fst format which handles like a data.table rather than data.frame
- I still save Rds as well as fst.
]
---
# (ง°ل͜°)ง
```{r, echo=FALSE, fig.align='center'}
knitr::include_graphics("multithreading.png")
```