-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbench_subset.R
More file actions
157 lines (139 loc) · 4.03 KB
/
bench_subset.R
File metadata and controls
157 lines (139 loc) · 4.03 KB
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
# =============================================================================
# Benchmark suite: subsetting & view materialization
# =============================================================================
# Benchmarks AnnDataView creation with different index types and
# materialization back to concrete implementations.
# =============================================================================
bench_subset <- function(h5ad_paths, iterations, zarr_paths) {
results <- list()
path <- h5ad_paths[["float_csparse"]]
for (backend in c("InMemoryAnnData", "HDF5AnnData", "ZarrAnnData")) {
short <- switch(
backend,
InMemoryAnnData = "InMemory",
HDF5AnnData = "HDF5",
ZarrAnnData = "Zarr"
)
ad <- if (backend == "ZarrAnnData") {
read_zarr(zarr_paths[["float_csparse"]], as = "ZarrAnnData")
} else {
read_h5ad(path, as = backend)
}
n_obs <- ad$n_obs()
n_vars <- ad$n_vars()
# Pre-compute indices
obs_int <- sort(sample.int(n_obs, as.integer(n_obs * 0.5)))
var_int <- sort(sample.int(n_vars, as.integer(n_vars * 0.5)))
obs_char <- ad$obs_names[obs_int]
var_char <- ad$var_names[var_int]
obs_logical <- seq_len(n_obs) %in% obs_int
# --- View creation: integer indices ---
env <- new.env(parent = globalenv())
env$.ad <- ad
env$.obs_int <- obs_int
env$.var_int <- var_int
# Subset obs only
results <- c(
results,
run_one_benchmark(
name = paste0("subset_obs_int_", short),
expr = quote(.ad[.obs_int, ]),
iterations = iterations,
env = env
)
)
# Subset both
results <- c(
results,
run_one_benchmark(
name = paste0("subset_both_int_", short),
expr = quote(.ad[.obs_int, .var_int]),
iterations = iterations,
env = env
)
)
# --- View creation: character indices ---
env2 <- new.env(parent = globalenv())
env2$.ad <- ad
env2$.obs_char <- obs_char
env2$.var_char <- var_char
results <- c(
results,
run_one_benchmark(
name = paste0("subset_obs_char_", short),
expr = quote(.ad[.obs_char, ]),
iterations = iterations,
env = env2
)
)
results <- c(
results,
run_one_benchmark(
name = paste0("subset_both_char_", short),
expr = quote(.ad[.obs_char, .var_char]),
iterations = iterations,
env = env2
)
)
# --- View creation: logical indices ---
env3 <- new.env(parent = globalenv())
env3$.ad <- ad
env3$.obs_logical <- obs_logical
results <- c(
results,
run_one_benchmark(
name = paste0("subset_obs_logical_", short),
expr = quote(.ad[.obs_logical, ]),
iterations = iterations,
env = env3
)
)
# --- Materialize view → InMemory ---
view <- ad[obs_int, var_int]
env4 <- new.env(parent = globalenv())
env4$.view <- view
results <- c(
results,
run_one_benchmark(
name = paste0("materialize_to_InMemory_", short),
expr = quote(.view$as_InMemoryAnnData()),
iterations = iterations,
env = env4
)
)
# --- Materialize view → HDF5 ---
results <- c(
results,
run_one_benchmark(
name = paste0("materialize_to_HDF5_", short),
expr = quote({
.tmp <- tempfile(fileext = ".h5ad")
.result <- .view$as_HDF5AnnData(.tmp)
.result$close()
unlink(.tmp)
}),
iterations = iterations,
env = env4
)
)
# --- Materialize view → Zarr ---
results <- c(
results,
run_one_benchmark(
name = paste0("materialize_to_Zarr_", short),
expr = quote({
.tmp <- tempfile()
.result <- .view$as_ZarrAnnData(.tmp)
unlink(.tmp, recursive = TRUE)
}),
iterations = iterations,
env = env4
)
)
# Clean up (ZarrAnnData holds no persistent file handles)
if (backend == "HDF5AnnData") {
ad$close()
}
}
results
}