Skip to content

Ask for as many components as the data has - #10482

Merged
anashen merged 2 commits into
satijalab:mainfrom
BenjaminDEMAILLE:fix/leveragescore-small-objects
Oct 19, 2026
Merged

anashen merged 2 commits into
satijalab:mainfrom
BenjaminDEMAILLE:fix/leveragescore-small-objects

Conversation

@BenjaminDEMAILLE

Copy link
Copy Markdown
Contributor

SketchData() fails on any small object, at every size of sketch:

> SketchData(pbmc_small, ncells = 40, method = "LeverageScore")
Error: max(nu, nv) must be strictly less than min(nrow(A), ncol(A))

Cause

LeverageScore.default() sizes its decomposition to the object, and then does not use the value:

nv <- ifelse(nrow(x = object) < 50, nrow(x = object) - 1, 50)
...
Z <- irlba(A = object, nv = 50, nu = 0, verbose = FALSE)$v

nv is computed and dropped; 50 components are always requested. Anything with fewer than 50 features or cells on the side being decomposed therefore stops inside irlba, and since SketchData() is how LeverageScore() is usually reached, that is where it surfaces.

Fix

Use nv, bounded by both dimensions, and say so when there is nothing to decompose at all:

Cannot compute leverage scores from 1 features and 1 cells: at least two of each are needed

Behaviour is unchanged for objects with 50 or more of each, which is where the constant and the computed value agree.

Tests

tests/testthat/test_leverage_score_small.R: leverage scores on pbmc_small, sketching to 40 cells, asking for more cells than exist (all of them are kept), and the message for a matrix with nothing to decompose.

Four assertions fail without the change. Full suite NOT_CRAN=true: 1187 passing, the only 2 failures being the pre-existing Read10X_Image ones fixed in #10454.

🤖 Generated with Claude Code

LeverageScore() sizes its decomposition to the object:

  nv <- ifelse(nrow(x = object) < 50, nrow(x = object) - 1, 50)

and then does not use it:

  Z <- irlba(A = object, nv = 50, nu = 0, verbose = FALSE)$v

so 50 components are always requested, and anything with fewer than 50
features or cells stops with

  Error: max(nu, nv) must be strictly less than min(nrow(A), ncol(A))

That takes SketchData() with it, which is how it is usually reached:
SketchData() on pbmc_small fails at every size of sketch.

Use nv, bounded by both dimensions, and say so when there is nothing to
decompose at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@BenjaminDEMAILLE

Copy link
Copy Markdown
Contributor Author

Superseded by #10492, which groups five small independent fixes into one review. The commit is carried over unchanged; closing here to keep the queue down rather than because anything was wrong with it.

@anashen anashen reopened this Oct 19, 2026

@anashen anashen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch!

@anashen
anashen merged commit dc3d33a into satijalab:main Oct 19, 2026
@BenjaminDEMAILLE
BenjaminDEMAILLE deleted the fix/leveragescore-small-objects branch October 19, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants