From 9f8ab5387310b4dfa2dac7eb70b107e2983e7761 Mon Sep 17 00:00:00 2001 From: "Cano-Muniz, Santiago" Date: Fri, 27 Nov 2026 19:59:03 +0100 Subject: [PATCH] fix(gset): stop globaltest hanging on strong contrasts When Imhof is too coarse (p below ~1e-8), globaltest falls back to the generalised F. Its series terms can underflow to exactly 0, leaving the remainder stuck just above the tolerance, so the loop never ends. A lipidomics upload with 317 samples hung on its first contrast until the compute timeout killed it. Upstream globaltest has the same loop. The series now stops after 50 consecutive zero terms. P-values are unchanged wherever the old code finished, and the loop no longer copies its vectors on every term. --- R/gset-globaltest.r | 31 +++++++++++++----- tests/testthat/fixtures/gt-underflow-lams.rds | Bin 0 -> 445 bytes tests/testthat/test-gset-globaltest.R | 9 +++++ 3 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 tests/testthat/fixtures/gt-underflow-lams.rds diff --git a/R/gset-globaltest.r b/R/gset-globaltest.r index af4e8aa7..b52c73e9 100644 --- a/R/gset-globaltest.r +++ b/R/gset-globaltest.r @@ -186,27 +186,42 @@ gset.globaltest <- function(X, y, gmt, standardize = FALSE) { ## Ruben series coefficients; NULL if not a valid (non-negative) mixture. .gt.getC <- function(lams, beta, eps = 1e-10) { lams <- sort(lams) - cc <- prod(sqrt(beta / lams)) - if (cc == 0 || cc > 1) { + c0 <- prod(sqrt(beta / lams)) + if (c0 == 0 || c0 > 1) { return(NULL) } - rest <- 1 - cc - mx <- cc + rest <- 1 - c0 + mx <- c0 db <- 1 - beta / lams - d <- NULL + ## grown in chunks: c() per term made this loop quadratic in copies + N <- 1024 + cc <- numeric(N + 1) + d <- numeric(N) + cc[1] <- c0 ix <- 1 + zeros <- 0 repeat { - d <- c(d, 0.5 * sum(db^ix)) - cc <- c(cc, mean(cc[1:ix] * d[ix:1])) + if (ix > N) { + cc <- c(cc, numeric(N)) + d <- c(d, numeric(N)) + N <- 2 * N + } + d[ix] <- 0.5 * sum(db^ix) + ## mean.default skips S3 dispatch; same bits as mean() + cc[ix + 1] <- mean.default(cc[1:ix] * d[ix:1]) rest <- if (rest > 100 * .Machine$double.neg.eps) { rest - cc[ix + 1] } else { rest * lams[1] / lams[length(lams)] } mx <- max(mx, cc[ix + 1]) - if (rest / mx < eps || cc[ix + 1] < 0) break + ## Once the terms underflow to 0, rest stops shrinking and can sit just + ## above eps forever (upstream hangs too); a long run of zeros ends it. + zeros <- if (cc[ix + 1] == 0) zeros + 1 else 0 + if (rest / mx < eps || cc[ix + 1] < 0 || zeros >= 50) break ix <- ix + 1 } + cc <- cc[seq_len(ix + 1)] if (rest > 0) cc <- c(cc, rest) if (any(cc < 0)) NULL else cc } diff --git a/tests/testthat/fixtures/gt-underflow-lams.rds b/tests/testthat/fixtures/gt-underflow-lams.rds new file mode 100644 index 0000000000000000000000000000000000000000..7c8d1eb79167d7a4bb347b6ad11be17284875e7c GIT binary patch literal 445 zcmV;u0Yd&CiwFP!000001B>8dU|?WkU}0urU}6R`8Tc3&7;PQqI@xWWXjA8q`#Wb* zbLc*Y%sunP<62D}($y!}Jj&nXkm``yxQ4UAA<5ME$gYbY91;t1n%BtGIVAM2)!yt8 z>=1t<<^A>_5)P3yiES^>pK}P^|M8^YM-GRe>um=WL`ocd4_@1wk)!3{q2%*MMlj96 z)k1lWb9thJ6T>>unG@GJ*v7xm%*iiwFuxyle_BVVgOTjMrB+YO9TX!b{qTsi+kZrP z-`i{9l?TMSpEykw_qgC_!?QyV_=vr> zQfxFn;Cud5u=I540|DE)rb@7!JrKyN8ND%S^??vyfj5;CQV)ckSUy!RaLs}6pZBug zcJ4h8efF8}^(Br6Vx+^ivpAX^i1FZQIUpc=Abz6eA4_(Z0|}+}Hks!h9Z0m>X8LyX zy#tB&A8YLO+t%BMI(luBQO`3B0 nK;az^#uT&tqx2{}N{`Z`^e8<_kJ7ZJ8K3|FD=R4-{s{m80ifLF literal 0 HcmV?d00001 diff --git a/tests/testthat/test-gset-globaltest.R b/tests/testthat/test-gset-globaltest.R index c59500ab..b6e474ea 100644 --- a/tests/testthat/test-gset-globaltest.R +++ b/tests/testthat/test-gset-globaltest.R @@ -37,3 +37,12 @@ test_that("gset.globaltest output and edge cases", { expect_equal(out$SIZE, c(5, 14, 30, 1, 1, 2, NA, 1, 5)) expect_equal(nrow(playbase::gset.globaltest(X, y, list())), 0) }) + +test_that("generalised F terminates when the series underflows", { + ## Spectrum from a strong lipidomics contrast (n = 317): Imhof is too coarse, + ## genF's series underflows and rest sat just above eps forever. + lams <- readRDS(test_path("fixtures", "gt-underflow-lams.rds")) + setTimeLimit(elapsed = 120, transient = TRUE) + p <- tryCatch(playbase:::.gt.getP(lams), finally = setTimeLimit(elapsed = Inf)) + expect_equal(p, 5.2377304088156344e-10, tolerance = 1e-8) +})