diff --git a/R/markers.R b/R/markers.R index cc4c8df..a741153 100644 --- a/R/markers.R +++ b/R/markers.R @@ -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 diff --git a/R/utils.R b/R/utils.R index ae92727..919f34d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 diff --git a/tests/testthat/test-markers.R b/tests/testthat/test-markers.R index 7d335cb..021f2ba 100644 --- a/tests/testthat/test-markers.R +++ b/tests/testthat/test-markers.R @@ -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)) +}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 30ea630..ae2d48d 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -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"))