Skip to content

Commit 5972f20

Browse files
authored
Add more testing (#315)
* update example h5ad * extend tests to improve testing coverage * update news * fix tests
1 parent bd2dce2 commit 5972f20

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(PR #309)
55
- Remove deprecated functions and arguments (PR #311, PR #313)
66
- Minor cleanups and improvements (PR #313).
7+
- Add more tests to increase coverage (PR #315)
78

89
# anndataR 0.2.0
910

inst/extdata/example.h5ad

-4.56 KB
Binary file not shown.

inst/scripts/example_h5ad.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# python v3.10.10
2-
import anndata # anndata v0.8.0
3-
import scanpy # scanpy v1.9.3
4-
import numpy # numpy v1.23.5
5-
import pandas # pandas v2.0.0
6-
import scipy.sparse # scipy v1.10.1
1+
# python v3.13.5
2+
import anndata # anndata v0.11.4
3+
import scanpy # scanpy v1.11.4
4+
import numpy # numpy v2.2.6
5+
import pandas # pandas v2.3.0
6+
import scipy.sparse # scipy v1.14.1
77

88
# This script uses Python to create an example H5AD file for testing
99
# interoperability between languages. It is designed to be a small but
@@ -22,6 +22,9 @@
2222
#
2323
# CHANGELOG
2424
#
25+
# v0.3.0 (2025-08-04)
26+
# - Add adata.varp["test_varp"] to test reading of varp
27+
# - Update package versions to latest stable versions
2528
# v0.2.0 (2023-05-11)
2629
# - Add 1D sparse matrix to `adata.uns["Sparse1D"]
2730
# - Reduce the size of `adata.uns["String2D"]` and add columns to values
@@ -83,5 +86,8 @@
8386
scanpy.tl.leiden(adata)
8487
scanpy.tl.rank_genes_groups(adata, "leiden")
8588

89+
# add varp to test reading of varp
90+
adata.varp["test_varp"] = numpy.random.rand(adata.n_vars, adata.n_vars)
91+
8692
# Write the H5AD file
87-
adata.write("example.h5ad")
93+
adata.write_h5ad("inst/extdata/example.h5ad", compression="gzip")

tests/testthat/test-HDF5AnnData.R

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ test_that("reading varm works", {
5050
)
5151
})
5252

53+
# trackstatus: class=HDF5AnnData, feature=test_get_obsp, status=done
54+
test_that("reading obsp works", {
55+
obsp <- adata$obsp
56+
expect_true(is.list(obsp), "list")
57+
expect_equal(
58+
names(obsp),
59+
c("connectivities", "distances")
60+
)
61+
})
62+
63+
# trackstatus: class=HDF5AnnData, feature=test_get_varp, status=done
64+
test_that("reading varp works", {
65+
varp <- adata$varp
66+
expect_true(is.list(varp), "list")
67+
expect_equal(
68+
names(varp),
69+
c("test_varp")
70+
)
71+
})
72+
5373
# trackstatus: class=HDF5AnnData, feature=test_get_obs, status=done
5474
test_that("reading obs works", {
5575
obs <- adata$obs
@@ -187,3 +207,71 @@ test_that("writing var names works", {
187207
h5ad$var_names <- LETTERS[1:20]
188208
expect_identical(h5ad$var_names, LETTERS[1:20])
189209
})
210+
211+
# trackstatus: class=HDF5AnnData, feature=test_set_obsm, status=done
212+
test_that("writing obsm works", {
213+
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
214+
obs <- data.frame(row.names = 1:10)
215+
var <- data.frame(row.names = 1:20)
216+
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)
217+
218+
obsm_x <- matrix(rnorm(10 * 5), nrow = 10, ncol = 5)
219+
h5ad$obsm <- list(X = obsm_x)
220+
expect_identical(h5ad$obsm$X, obsm_x)
221+
})
222+
223+
# trackstatus: class=HDF5AnnData, feature=test_set_varm, status=done
224+
test_that("writing varm works", {
225+
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
226+
obs <- data.frame(row.names = 1:10)
227+
var <- data.frame(row.names = 1:20)
228+
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)
229+
varm_x <- matrix(rnorm(20 * 5), nrow = 20, ncol = 5)
230+
h5ad$varm <- list(PCs = varm_x)
231+
expect_identical(h5ad$varm$PCs, varm_x)
232+
})
233+
234+
# trackstatus: class=HDF5AnnData, feature=test_set_obsp, status=done
235+
test_that("writing obsp works", {
236+
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
237+
obs <- data.frame(row.names = 1:10)
238+
var <- data.frame(row.names = 1:20)
239+
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)
240+
241+
obsp_x <- matrix(rnorm(10 * 10), nrow = 10, ncol = 10)
242+
h5ad$obsp <- list(connectivities = obsp_x)
243+
expect_identical(h5ad$obsp$connectivities, obsp_x)
244+
})
245+
246+
# trackstatus: class=HDF5AnnData, feature=test_set_varp, status=done
247+
test_that("writing varp works", {
248+
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
249+
obs <- data.frame(row.names = 1:10)
250+
var <- data.frame(row.names = 1:20)
251+
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)
252+
253+
varp_x <- matrix(rnorm(20 * 20), nrow = 20, ncol = 20)
254+
h5ad$varp <- list(connectivities = varp_x)
255+
expect_identical(h5ad$varp$connectivities, varp_x)
256+
})
257+
258+
# trackstatus: class=HDF5AnnData, feature=test_set_uns, status=done
259+
test_that("writing uns works", {
260+
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
261+
obs <- data.frame(row.names = 1:10)
262+
var <- data.frame(row.names = 1:20)
263+
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)
264+
265+
h5ad$uns <- list(
266+
foo = "bar",
267+
baz = c(1, 2, 3),
268+
nested = list(
269+
nested_foo = "nested_bar",
270+
nested_baz = c(4L, 5L, 6L)
271+
)
272+
)
273+
expect_identical(h5ad$uns$foo, "bar")
274+
expect_equal(h5ad$uns$baz, c(1, 2, 3), ignore_attr = TRUE)
275+
expect_identical(h5ad$uns$nested$nested_foo, "nested_bar")
276+
expect_equal(h5ad$uns$nested$nested_baz, c(4L, 5L, 6L), ignore_attr = TRUE)
277+
})

0 commit comments

Comments
 (0)