Skip to content

fix: keep gene order in batched highly_variable_genes without n_top_genes - #4371

Open
haeganm wants to merge 2 commits into
scverse:mainfrom
haeganm:fix-hvg-batch-subset-order
Open

haeganm wants to merge 2 commits into
scverse:mainfrom
haeganm:fix-hvg-batch-subset-order

Conversation

@haeganm

@haeganm haeganm commented Sep 17, 2026

Copy link
Copy Markdown

With batch_key set, the per-batch results are combined with df.groupby(df.index), which sorts the genes by name. #3042 restored the original order, but only in the n_top_genes branch. With the default mean/dispersion cutoffs the frame stayed name-sorted, so subset=True applied the mask by position and kept the wrong genes (on pbmc3k with two batches only 269 of the 1857 kept genes were actual HVGs). inplace=False also returned the rows in sorted order.

The fix reindexes to adata.var_names once after both branches instead of only inside the if.

New test test_subset_consistency_cutoffs runs seurat and cell_ranger with n_top_genes=None, with and without batch_key, on data with unsorted var_names. The two batched cases fail on main and pass here, the unbatched ones pass on both.

…enes

The per-batch results are combined with a groupby, which sorts the
genes by name. The original order was only restored in the
n_top_genes branch, so with the default cutoffs subset=True applied
a name-sorted mask by position and kept the wrong genes. Restore the
order of adata.var_names for both branches.
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.

highly_variable_genes with batch_key and subset=True keeps the wrong genes when n_top_genes is not set

1 participant