Feedback on new niche implementation
Open
@selmanozleyen is already working on this.
Since Sep 10, 2026.
- Dominant language
- Python
- Stars
- 598
- Forks
- 121
- Avg merge
- 3d 11h
- Merged PRs (30d)
- 3
Description
Hi @shashkat and @selmanozleyen,
thanks for refactoring the niche stuff! I wasn't available to comment on https://github.com/scverse/squidpy/pull/1245 in time, so I'll leave some implementation-specific comments in this issue, to be addressed in a follow-up PR.
General
- please use kwargs-only for basically all arguments except
datafor all public functions - in general, the docstrings of the different niche methods are a bit light on details. I think they should provide a bit more details on the methodology, how they compare one to another, and reference the original publications. The docstrings should also make it clear to the user e.g. when to prefer UTAG over Neighborhood.
- You sometimes use
logg.warning, and sometimeswarnings.warn. Is that intentional?
calculate_niche_neighborhood
- docstring
calculate_niche_neighborhood-> specify what happens to the output if multipleresolutionsare specified.
Neighborhood embedder
- if extending n_hop_weights, I think this would most likely not intentional. I'd at least emit a warning, not debug, but probably just fail.
_calculate_neighborhood_profile. I think most of the calculation could be simplified into pd.dummies + matrix multiplication (
(pd.get_dummies(adata.obs[self.groups]).T.values @ adata.obsp[self.spatial_connectivities_key].T).T)- I wouldn't prematurely convert the profile to a pandas df as long as you still perform arithmetic operations on it.
UTAG
- it should be possible to specify an AnnData layer, do not blindly use
X. - Utag embedder: sc.tl.pca can take a sparse matrix directly and return the result matrix. No need to create an intermediate AnnData object.
Cellcharter
- I would move
use_repoutside the_CellcharterEmbedder, i.e. intantiate the embedder only ifuse_rep is None. The embedder could be renamed to something more generic, such asNHopPCAEmbedder. - why the
_setdiagwith.tolil()->.tocsr()roundtrip? CSR matrices have a native.setdiagfunction. - not exactly sure what cellcharter's intention with the "visited" filter is. If a k-hop space should strictly exclude nodes that are already included in a (k-1)-hop-space , this doesn't work since it is not accounted for node degrees. The comparison adj_hop = adj_hop > adj_visitied would need to use boolean matrices instead.
_postprocess_niche_results
- I'd convert the result columns back to categoricals after assigning the prefixes.
_calculate_niche_custom
Not entirely sure about this, but could it make sense, for performance reasons, to move the embedder out of the loop? The spatial neighbohood graph should already account for libraries. The embedding could then be stored globally in adata.obsm and be inspected or potentially be reused for other purposes.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.