Repository navigation
Make neighbors work based on KNeighborsTransformer-compatible APIs - #2536
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #2536 +/- ##
==========================================
+ Coverage 72.01% 72.17% +0.15%
==========================================
Files 105 108 +3
Lines 11844 11894 +50
==========================================
+ Hits 8529 8584 +55
+ Misses 3315 3310 -5
|
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Looks pretty good. Just two remaining points from last review:
- Can we add an example of using this to the documentation?
- Could be under the "Usage" section? Maybe use a ball tree on low dim data?
- What do you think about using being able to pass in an instance of a class, rather than the class + kwargs?
- Just looking for a reasoning here.
And then:
- Changelog entry
|
What “Usage” section? We have a “Usage principles” page and functions/classes can have an “Examples” section. I’m pretty sure there was a very good reason for not passing an instance, but this was all so long ago that I forgot. I’ll see if it’s possible to maintain backwards compat while doing that! |
Yes, examples
Maybe it was about passing in arguments to the function? But I do think it would be quite useful to pass in a class that gets |
|
I think it was just so convoluted that it seemed too hard to do while keeping backwards compatibility, but I managed it! |
|
all done |
|
@ivirshup you can approve PRs where the only change missing is fixing checks. |
sklearn.neighbors.KNeighborsTransformeris a good abstraction for KNN calculationsWe could follow the suggestion here, then finish this PR scikit-learn-contrib/sklearn-ann#8 (comment)
For reviewing
scanpy/scanpy/neighbors/__init__.py
Line 471 in 73915b4
scanpy/scanpy/neighbors/__init__.py
Lines 522 to 523 in 73915b4
Should we change that logic for more predictability at the expense of backwards compatibility? Especially the “euclidean” condition makes not much sense IMHO
knn=False? It would make sense for e.g. the scipy knn transformer withalgorithm='kd_tree'…scanpy/scanpy/neighbors/__init__.py
Lines 614 to 617 in 73915b4
methodto only mean “connectivity method”.transformer_clsalone now determines how to calculate distancesTODO
_more_tagsmethods areMaybe in another PR?
UMAP(precomputed_knn=...)instead ofcompute_connectivities?Implementation
The way it used to be
Our default use case was basically a thin wrapper around umap’s
nearest_neighborsfunction, which in turn is a thin wrapper around PyNNDescent. We did some special casing around euclidean distance and small data sizes. That special casing reduced our test coverage:precomputedpath for small data)precomputedcode does some weird things to its knnindicesarray, which we don’t test for:The logic was:
pairwise_distances_get_indices_distances_from_dense_matrixand converting that into a sparse oneumap.nearest_neighbors.pairwise_distanceslike above and runumap.nearest_neighborswithmetric='precomputed'. its internal logic then does the same as_get_indices_distances_from_dense_matrixumap.nearest_neighborsdirectly, which simply runs PyNNDescent withn_treesandn_itersset with a heuristic.now the logic is
pairwise_distances, sparsify on demand. (this covers both our previous optimization and the umap'precomputed'code path)'umap'method was chosen, pick the same parameters mentioned aboveto thePyNNDescenttransformerFixes #2519