-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbench_convert.R
More file actions
158 lines (137 loc) · 4.1 KB
/
bench_convert.R
File metadata and controls
158 lines (137 loc) · 4.1 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
158
# =============================================================================
# Benchmark suite: conversions
# =============================================================================
# Benchmarks backend-to-backend conversions (HDF5↔InMemory) and
# format conversions (InMemory↔SCE, InMemory↔Seurat).
# =============================================================================
bench_convert <- function(h5ad_paths, iterations, x_types, zarr_paths) {
results <- list()
# --- Backend conversions (per X type) ---
for (xt in x_types) {
path <- h5ad_paths[[xt]]
# HDF5 → InMemory
env <- new.env(parent = globalenv())
env$.ad <- read_h5ad(path, as = "HDF5AnnData")
results <- c(
results,
run_one_benchmark(
name = paste0("convert_HDF5_to_InMemory_", xt),
expr = quote(.ad$as_InMemoryAnnData()),
iterations = iterations,
env = env
)
)
env$.ad$close()
# InMemory → HDF5
env2 <- new.env(parent = globalenv())
env2$.ad <- read_h5ad(path, as = "InMemoryAnnData")
results <- c(
results,
run_one_benchmark(
name = paste0("convert_InMemory_to_HDF5_", xt),
expr = quote({
.tmp <- tempfile(fileext = ".h5ad")
.result <- .ad$as_HDF5AnnData(.tmp)
.result$close()
unlink(.tmp)
}),
iterations = iterations,
env = env2
)
)
}
# --- Zarr ↔ InMemory conversions (per X type) ---
for (xt in x_types) {
zarr_path <- zarr_paths[[xt]]
# Zarr → InMemory
env <- new.env(parent = globalenv())
env$.ad <- read_zarr(zarr_path, as = "ZarrAnnData")
results <- c(
results,
run_one_benchmark(
name = paste0("convert_Zarr_to_InMemory_", xt),
expr = quote(.ad$as_InMemoryAnnData()),
iterations = iterations,
env = env
)
)
# InMemory → Zarr
env2 <- new.env(parent = globalenv())
env2$.ad <- read_zarr(zarr_path, as = "InMemoryAnnData")
results <- c(
results,
run_one_benchmark(
name = paste0("convert_InMemory_to_Zarr_", xt),
expr = quote({
.tmp <- tempfile()
.result <- .ad$as_ZarrAnnData(.tmp)
unlink(.tmp, recursive = TRUE)
}),
iterations = iterations,
env = env2
)
)
}
# --- Format conversions (using float_csparse as representative) ---
path <- h5ad_paths[["float_csparse"]]
ad <- read_h5ad(path, as = "InMemoryAnnData")
# InMemory → SingleCellExperiment
if (requireNamespace("SingleCellExperiment", quietly = TRUE)) {
env_sce <- new.env(parent = globalenv())
env_sce$.ad <- ad
results <- c(
results,
run_one_benchmark(
name = "convert_InMemory_to_SCE",
expr = quote(as_SingleCellExperiment(.ad)),
iterations = iterations,
env = env_sce
)
)
# SCE → InMemory
sce <- as_SingleCellExperiment(ad)
env_sce2 <- new.env(parent = globalenv())
env_sce2$.sce <- sce
results <- c(
results,
run_one_benchmark(
name = "convert_SCE_to_InMemory",
expr = quote(as_AnnData(.sce)),
iterations = iterations,
env = env_sce2
)
)
} else {
message(" [SKIP] SCE conversion: SingleCellExperiment not installed")
}
# InMemory → Seurat
if (requireNamespace("SeuratObject", quietly = TRUE)) {
env_seu <- new.env(parent = globalenv())
env_seu$.ad <- ad
results <- c(
results,
run_one_benchmark(
name = "convert_InMemory_to_Seurat",
expr = quote(suppressWarnings(as_Seurat(.ad))),
iterations = iterations,
env = env_seu
)
)
# Seurat → InMemory
seurat <- suppressWarnings(as_Seurat(ad))
env_seu2 <- new.env(parent = globalenv())
env_seu2$.seurat <- seurat
results <- c(
results,
run_one_benchmark(
name = "convert_Seurat_to_InMemory",
expr = quote(as_AnnData(.seurat)),
iterations = iterations,
env = env_seu2
)
)
} else {
message(" [SKIP] Seurat conversion: SeuratObject not installed")
}
results
}