From 700d4cbaa5b969c983063e694d4586a732f85d42 Mon Sep 17 00:00:00 2001 From: erliuu Date: Sun, 25 Oct 2026 00:51:34 -0700 Subject: [PATCH 1/2] fix([r] markers): Fix bias-matching k-neighbours bug --- R/markers.R | 4 +++- R/utils.R | 8 ++------ man/add_marker_features.Rd | 3 ++- tests/testthat/test-markers.R | 28 ++++++++++++++++++++++++++++ tests/testthat/test-utils.R | 13 +++++++++++++ 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/R/markers.R b/R/markers.R index bfa780f..5ea49d1 100644 --- a/R/markers.R +++ b/R/markers.R @@ -423,7 +423,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 diff --git a/R/utils.R b/R/utils.R index c4c510f..ceaa1b6 100644 --- a/R/utils.R +++ b/R/utils.R @@ -439,15 +439,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 diff --git a/man/add_marker_features.Rd b/man/add_marker_features.Rd index 20d7580..0433e77 100644 --- a/man/add_marker_features.Rd +++ b/man/add_marker_features.Rd @@ -77,7 +77,8 @@ backgrounds. Default 0.8.} \item{scale_to}{(numeric) Column-sum normalization target. Default 1e4.} -\item{knn_method}{(function) BPCells KNN method. Default \code{knn_hnsw()}.} +\item{knn_method}{(function) BPCells KNN method; its \code{k} is replaced by \code{k}. +Default \code{knn_hnsw()}.} \item{seed}{(integer) Random seed. Default 1L.} diff --git a/tests/testthat/test-markers.R b/tests/testthat/test-markers.R index 576570f..2eb09ac 100644 --- a/tests/testthat/test-markers.R +++ b/tests/testthat/test-markers.R @@ -691,3 +691,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)) +}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index fc5e1bc..66d33ac 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -6,3 +6,16 @@ test_that(".get_quantiles matches trunc(rank(v))/length(v) and handles ties", { expect_equal(.get_quantiles(c(1, 1, 2)), c(1 / 3, 1 / 3, 1)) 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) +}) From 2bfbdb5f55ef2d1389d93968af95e64e5aacb979 Mon Sep 17 00:00:00 2001 From: erliuu Date: Sun, 25 Oct 2026 00:53:00 -0700 Subject: [PATCH 2/2] fix([r] markers): Update roxygen documentation --- man/add_marker_features.Rd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/man/add_marker_features.Rd b/man/add_marker_features.Rd index 0433e77..20d7580 100644 --- a/man/add_marker_features.Rd +++ b/man/add_marker_features.Rd @@ -77,8 +77,7 @@ backgrounds. Default 0.8.} \item{scale_to}{(numeric) Column-sum normalization target. Default 1e4.} -\item{knn_method}{(function) BPCells KNN method; its \code{k} is replaced by \code{k}. -Default \code{knn_hnsw()}.} +\item{knn_method}{(function) BPCells KNN method. Default \code{knn_hnsw()}.} \item{seed}{(integer) Random seed. Default 1L.}