Initial framework of the niche refactor - #1245
Conversation
…late_niche() so that writing test case for sdata is possible
for more information, see https://pre-commit.ci
…ew implementation has differently named column
for more information, see https://pre-commit.ci
Tests in test_niche.py involving dummy_adata fail with scanpy<1.12.1 because of a change in handling of small edge weights by sc.pp.neighbors (scverse#4031). Updated the values in tests to match the expected values with scanpy>=1.12.1
for more information, see https://pre-commit.ci
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1245 +/- ##
==========================================
+ Coverage 77.08% 78.49% +1.40%
==========================================
Files 63 63
Lines 9436 9505 +69
Branches 1585 1586 +1
==========================================
+ Hits 7274 7461 +187
+ Misses 1561 1453 -108
+ Partials 601 591 -10
🚀 New features to boost your workflow:
|
…late_niche_spatialleiden
…patialleiden with library_key not None
for more information, see https://pre-commit.ci
|
This is ready for review @selmanozleyen @grst Some important points:
|
|
I will clean up the docstrings to have a dropdown per flavour so we get rid of this mess and will add the AutoK function in a separate PR. But still wanted to ask for a review from you @Intron7 |
|
@shashkat , thanks a lot for the PR I hope you don't mind my commits directly without asking but wdyt? also you mentioned spatial_neighbours didn't do something as it should like reusing the |
|
Hi @selmanozleyen , your welcome! Yeah no problem at all. I went through your changes and they look good to me. Just one point: Thanks a lot for reviewing the code! |
|
thanks, does this PR include #1229? |
|
Since we are going to deprecate the main functions, I think it's fine to not touch the |
Intron7
left a comment
There was a problem hiding this comment.
In general I'm a big fan of the new split approach to niche detection. I'm more than happy to adapt the four-function design in rapids-singlecell and deprecate the current calculate_niche — that function has grown unwieldy, and most of _validate_niche_args / _check_unnecessary_args should be deletable once the flavors have their own signatures.
My one ask: the class-based abstraction for custom niche workflows shouldn't be made public in this PR. Keep the classes as the implementation, just private — prefix the seven class names, drop them and calculate_niche_custom from the _niche import block and all in gr/init.py, and drop the matching docs API entries. No logic changes, no re-testing. If we want a discussion about a public class-based custom niche API, that's a fine discussion to have separately, but I'd keep this PR focused on splitting the single function into several.
Separately, and disclosing that both are mine: #1151 and scverse/scanpy#4108 add backend dispatch via scverse-backends, and #1151 dispatches calculate_niche and touches _niche.py, so it will conflict with this. The resolution is that this PR lands first and I rebase — four stable signatures are much better adapter targets than one flavor-branching entry point. But I don't like the class-based choose-your-own-adventure style with dispatch in play. Dispatch attaches to functions the library owns, so a subclass in a third-party package can't be a dispatch target, and get_embedding(adata) -> NDArrayA pins numpy into the contract — which forces materialization back to host at exactly the embed→cluster boundary where a fused implementation would keep things in place. If the ABCs do go public later, that return type should be widened to an array-API protocol first.
| gr.calculate_niche_utag | ||
| gr.calculate_niche_cellcharter | ||
| gr.calculate_niche_spatialleiden | ||
| gr.calculate_niche_custom |
There was a problem hiding this comment.
I don't think we should make this costum function public.
|
Yeah on the exposure of these custom functions I was doubting myself. I think it's not worth giving these public guarantees for now. I think the compatibility is a bit better than it was with rsc but compatibility on public layer imo is always going to cause duplicate code and be error prone on long term. I am not saying it should be through the extensibility api's but there should be a layer we give in only array api components as you said. This is the proper solution to it. |

Description
Niche refactor improving the current implementation giving the user facing functions a more explicit API, more flexibility through embedder and clusterer classes and possibility of using combinations of them using
calculate_niche_custom. Postprocessor classes allow more organized and flexible postprocessing actions like masking and min_niche_size filtering of niches.The core idea is similar to the spatial_neighbors refactor in #1147 and builds on top of the relatively smaller fix in #1229.
The below graph demonstrates the code flow planned (read children of a node from left to right). This allows usage of different combinations of embeddings and clusterings to obtain niches, and also exposing easy to use and explicit user facing functions for the different flavors. Brown nodes are user-facing functions.

How has this been tested?
Keeping the API of function
calculate_nichesame, for i) backward compatibility and ii) being able to validate results being same before and after refactor for some test cases that I wrote for the purpose of this refactor, for the different flavors. Though later on, I thinkcalculate_nicheshould be deprecated as its not clear how arguments of it interact with different values offlavorargument.Closes
Closes #1192, where this was initially discussed