Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R/markers.R
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ add_marker_features.MarchRProject <- function(

bg_bias <- bias_mat_q[bg_names_pool, , drop = FALSE]
fg_bias <- bias_mat_q[fg_names, , drop = FALSE]
knn_idx <- knn_method(data = bg_bias, query = fg_bias)$idx
knn_idx <- partial_apply(knn_method, k = k_use, .missing_args_error = FALSE)(
data = bg_bias, query = fg_bias
)$idx

# Walk foreground cells in random order, picking one bias-matched
# background cell per foreground cell while maintaining group proportions
Expand Down
8 changes: 2 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,11 @@ partial_apply <- function(
base_fn <- f
function_name <- NULL

if (inherits(f, "bpcells_s3_partial")) {
if (inherits(f, "bpcells_s3_partial") && !is.null(attr(f, "generic_name", exact = TRUE))) {
prev_args <- attr(f, "args")
gen <- attr(f, "generic_name", exact = TRUE)
env <- environment(f)
if (
!is.null(gen) &&
!is.null(env) &&
exists(gen, envir = env, inherits = FALSE)
) {
if (!is.null(env) && exists(gen, envir = env, inherits = FALSE)) {
base_fn <- get(gen, envir = env, inherits = FALSE)
}
function_name <- gen
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-markers.R
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,31 @@ test_that("add_marker_features.default with bias matching works directly", {
expect_true(nrow(result$results) > 0)
expect_true(all(c("A", "B", "C") %in% result$results$foreground))
})

test_that(".match_bias_cell_groups searches k background neighbors, capped at the pool size", {
skip_if_not_installed("RcppHNSW")
set.seed(1)
cells <- paste0("c", 1:60)
cell_metadata <- data.frame(depth = runif(60), tss = runif(60), row.names = cells)
groups <- stats::setNames(rep(c("A", "B", "C"), each = 20), cells)
seen_k <- integer(0)
spy_knn <- function(data, query, k = 10L) {
seen_k <<- c(seen_k, as.integer(k))
knn_hnsw(data, query, k = k, verbose = FALSE)
}

MarchR:::.match_bias_cell_groups(cell_metadata, groups, bias = c("depth", "tss"),
k = 25L, knn_method = spy_knn)
expect_identical(seen_k, rep(25L, 3))

seen_k <- integer(0)
MarchR:::.match_bias_cell_groups(cell_metadata, groups, bias = c("depth", "tss"),
k = 100L, knn_method = spy_knn)
expect_identical(seen_k, rep(40L, 3))

seen_k <- integer(0)
wrapped <- partial_apply(spy_knn, verbose = TRUE, .missing_args_error = FALSE)
MarchR:::.match_bias_cell_groups(cell_metadata, groups, bias = c("depth", "tss"),
k = 25L, knn_method = wrapped)
expect_identical(seen_k, rep(25L, 3))
})
11 changes: 11 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ test_that(".get_quantiles matches trunc(rank(v))/length(v) and handles ties", {
expect_equal(.get_quantiles(numeric(0)), numeric(0))
})

test_that("partial_apply keeps new arguments when re-wrapping its own partial", {
f <- function(a, b = 1, c = 2) c(a = a, b = b, c = c)
p1 <- partial_apply(f, b = 10)
p2 <- partial_apply(p1, c = 20)
expect_identical(p2(a = 1), c(a = 1, b = 10, c = 20))
p3 <- partial_apply(p2, b = 5)
expect_identical(p3(a = 1), c(a = 1, b = 5, c = 20))

s3 <- partial_apply(LSI(n_dimensions = 5L), threads = 2L, .missing_args_error = FALSE)
expect_identical(attr(s3, "fn"), LSI)
expect_identical(attr(s3, "args")$n_dimensions, 5L)
test_that("log_progress prints a timestamp and the message", {
expect_null(.log_clock$start)
msgs <- capture_messages(log_progress("Computing LSI"))
Expand Down
Loading