-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelayedMatrixStats_BioC_2017.Rmd
245 lines (177 loc) · 6.02 KB
/
DelayedMatrixStats_BioC_2017.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
---
title: "DelayedMatrixStats"
subtitle: "Porting the matrixStats API to work with DelayedMatrix objects"
author: "Peter Hickey (@PeteHaitch)"
date: "2017-07-26"
output:
xaringan::moon_reader:
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
ratio: 4:3
self_contained: false
editor_options:
chunk_output_type: console
---
```{r setup, include = FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(comment = "#>", message = FALSE, warning = FALSE,
collapse = TRUE)
library(rlang)
library(microbenchmark)
library(profmem)
library(Matrix)
library(HDF5Array)
library(matterArray) # GitHub only package
library(DelayedMatrixStats)
# NOTE: This uses some pretty crude, example-specific rounding
benchmark <- function(...) {
quos <- quos(...)
bm <- eval_tidy(quo(microbenchmark(!!!quos)))
mem <- sapply(quos[-length(quos)], function(x) {
total(eval_tidy(quo(profmem(!!!x))))
})
s <- summary(bm)
# NOTE: Hack to remove leading ~
s$expr <- gsub("~", "", s$expr)
timing_cols <- c("min", "lq", "median", "uq", "max")
unit <- attr(s, "unit")
unit <- ifelse(unit == "milliseconds", "ms", unit)
unit <- ifelse(unit == "microseconds", "μs", unit)
unit <- ifelse(unit == "seconds", "s", unit)
s$median <- signif(s$median, 3)
val <- cbind(s[, c("expr", "median")],
data.frame("MB" = round(mem / 10 ^ 6, 1)))
colnames(val) <- c("expr", paste0("Median time (", unit, ")"),
"Mem alloc (MB)")
print(val, row.names = FALSE)
}
file_size <- function(x) {
pryr:::show_bytes(structure(file.size(x), class = "object_size"))
}
```
# Why **matrixStats**?
[**matrixStats**](https://cran.r-project.org/package=matrixStats) by Henrik Bengtsson and co. on CRAN since 2009
--
Lots of useful col/row summary functions
```{r}
grep("^col", getNamespaceExports("matrixStats"), value = TRUE)
```
---
## Optimised row/column operations on _matrix_ objects
```{r}
# Simulate some zero-inflated count data
matrix <- matrix(sample(0:100, 20000 * 10000, replace = TRUE),
nrow = 20000,
ncol = 10000)
matrix[sample(length(matrix), length(matrix) * 0.6)] <- 0L
library(matrixStats)
benchmark(apply(matrix, 2, median),
colMedians(matrix),
times = 10)
```
---
# Why **matrixStats**?
## Optimised row/column operations on _matrix_ objects
```{r}
j <- c(2001:3000, 5001:5500)
benchmark(colSums(matrix[, j]),
colSums2(matrix, cols = j),
times = 10)
```
---
# Big data blues
- You've got matrix-like data but too large for in-memory _matrix_ :(
--
## _DelayedMatrix_!
- A wrapper around a matrix-like object
- Data can be in memory or on disk
- _DelayedMatrix_ works as an assay in a _SummarizedExperiment_
- _DelayedMatrix_ supports the standard & familiar _matrix_ API<sup>*</sup>
- `[`
- `dim()`
- `dimnames()`
- `t()`
- `log()`
- **`colSums()`**
- ...
.footnote[[*] But not subassignment]
---
# _DelayedMatrix_ backends
## In-memory backends
```{r}
DelayedMatrix <- DelayedArray::DelayedArray(matrix)
pryr::object_size(DelayedMatrix)
DelayeddgCMatrix <- DelayedArray(as(matrix, "dgCMatrix"))
pryr::object_size(DelayeddgCMatrix) # Larger than dense version!
RleMatrix <- RleArray(Rle(matrix), dim = dim(matrix))
pryr::object_size(RleMatrix) # Low RLE compressibility
TricksyRleMatrix <- as(matrix, "RleMatrix") # Uses tricksy tricks
pryr::object_size(TricksyRleMatrix) # Tricksy tricks in play
```
---
# _DelayedMatrix_ backends
## On-disk backends
```{r}
HDF5Matrix <- HDF5Array::writeHDF5Array(matrix)
pryr::object_size(HDF5Matrix)
file_size(HDF5Matrix@seed@file)
matterMatrix <- matterArray::writeMatterArray(matrix)
pryr::object_size(matterMatrix)
file_size(matterMatrix@seed@matter@paths)
```
---
class: center, inverse
# Why **DelayedMatrixStats**?
--
![https://cdn.meme.am/cache/instances/folder347/500x/80002347/the-one-ring-one-api-to-rule-them-all.jpg](https://cdn.meme.am/cache/instances/folder347/500x/80002347/the-one-ring-one-api-to-rule-them-all.jpg)
---
# Why **DelayedMatrixStats**?
- Support **matrixStats** API for _DelayedMatrix_ and derived classes
- Reduce friction between using _matrix_ or _DelayedMatrix_
--
## Initial release aim
General 'block-processing' method to work for _DelayedMatrix_ and arbitrary
derived classes
--
## Subsequent releases
'Backend-aware' optimised methods
---
# Why **DelayedMatrixStats**?
## Yay, same syntax works regardless of backend!
```{r, error = TRUE}
benchmark(colMedians(matrix),
colMedians(DelayedMatrix),
colMedians(DelayeddgCMatrix),
colMedians(RleMatrix),
colMedians(TricksyRleMatrix),
colMedians(HDF5Matrix),
colMedians(matterMatrix),
times = 10)
# Aside: apply(DelayedMatrix, 2, median) currently doesn't work
```
---
# Why **DelayedMatrixStats**?
## Backend-aware methods can improve performance
```{r}
CS <- function(x, j) colSums(x[, j]) # DelayedArray
CS2 <- function(x, j) colSums2(x, cols = j) # DelayedMatrixStats
j <- c(2001:3000, 5001:5500)
benchmark(CS(DelayedMatrix, j), # Block-processing
CS2(DelayedMatrix, j), # Backend-aware
CS(DelayeddgCMatrix, j), # Block-processing
CS2(DelayeddgCMatrix, j), # Backend-aware
CS(RleMatrix, j), # Block-processing
CS2(RleMatrix, j), # Backend-aware
times = 10)
```
---
class: center, middle
# For more
**DelayedMatrixStats:** [https://github.com/PeteHaitch/DelayedMatrixStats](https://github.com/PeteHaitch/DelayedMatrixStats)
**matter:** Developed by Kylie A. Bemis [https://bioconductor.org/packages/matter/](https://bioconductor.org/packages/matter/)
**matterArray:** [https://github.com/PeteHaitch/matterArray](https://github.com/PeteHaitch/matterArray)
**Slides:** [http://peterhickey.org/presentations/](http://peterhickey.org/presentations/)
**GitHub & Twitter:** [@PeteHaitch](https://twitter.com/PeteHaitch)