scverse / scverse/scanpy

Set n_pcs when use_rep is specified in sc.pp.neighbors

Open
#1,281 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.6k
Forks
779
Avg merge
1d 4h
Merged PRs (30d)
27

Description

  • Additional function parameters / changed functionality / changed defaults?

Sorry to file two issues in a row! I've been using Scanpy + scVI for my analysis recently, and am really enjoying it!

Currently, if use_rep is set in sc.pp.neighbors, then n_pcs is ignored. These seems like sane default behaviour to me - if we aren't using 'X_pca', then n_pcs doesn't really make sense.

This can actually be limiting, as I recently discovered. If you calculate a latent representation with scVI with n_latent = 50, you can't then do...

sc.pp.neighbors(adata, use_rep='X_scvi', n_pcs = 25)

...as the neighborhood graph is calculated on all 50 latent variables. If I want to use only 25 - say, as a point of comparison to see which best represents my data - I have to recalculate the latent representation with scVI with n_latent = 25. Basically, if you aren't using PCA, you have to calculate a full reduction for every number of dimensions you are interested in.

I don't think the fix would be that major. The source seems to be in the _choose_representation function, with the directly relevant snippet below:

https://github.com/theislab/scanpy/blob/5bc37a2b10f40463f1d90ea1d61dc599bbea2cd0/scanpy/tools/_utils.py#L48-L58

I think the only change needed would be to have the n_pcs is not None catch in all cases, not just when use_rep = 'X_pca'. Might also generalise the variable name to make it more clear it refers to any reduction, not just PCA. Admittedly, I only just started exploring the code base, so I'm not sure where else _choose_representation is called, or what other impacts this could have.

I have some blue sky time tomorrow, so I'll fork it then and see if I can whip up a pull request. Thanks for the excellent product!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in scanpy/tools/_utils.py at _choose_representation and inspect the other places that call it. Check how use_rep and n_pcs are currently handled, then verify that selecting fewer dimensions from a supplied representation works without changing existing PCA behavior. Done means the example using X_scvi and n_pcs=25 uses only 25 latent variables.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
bioinformatics
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.