-
Notifications
You must be signed in to change notification settings - Fork 113
spatial_neigbours extensibility features and clarification #1147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
selmanozleyen
wants to merge
64
commits into
scverse:main
Choose a base branch
from
selmanozleyen:feat/spatial_neighbours
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
5c08081
init
selmanozleyen 5847d84
doc clarifications
selmanozleyen 179ba06
don't expose the builders
selmanozleyen e187aba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cdb5699
deduplicate
selmanozleyen 847a46b
Merge branch 'feat/spatial_neighbours' of https://github.com/selmanoz…
selmanozleyen 69cda1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fddc027
arrange builders
selmanozleyen a30988a
Merge branch 'feat/spatial_neighbours' of https://github.com/selmanoz…
selmanozleyen f194b10
better behaviour
selmanozleyen ae1eba1
move classes
selmanozleyen a86572b
Merge branch 'main' into feat/spatial_neighbours
selmanozleyen 039b852
cleanup
selmanozleyen 4699993
Merge branch 'feat/spatial_neighbours' of https://github.com/selmanoz…
selmanozleyen b5dcf9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4c11adf
add docs
selmanozleyen 2b937e9
Merge branch 'feat/spatial_neighbours' of https://github.com/selmanoz…
selmanozleyen ea1ce93
remove leftover
selmanozleyen ebb7fd5
resolution in reolve func
selmanozleyen 69a9394
reduce dup code
selmanozleyen 77755ed
deprecate invalid n_neighs
selmanozleyen d8a8145
expose new functions and deprecate the flat one
selmanozleyen 2e75b3f
move branches to classes
selmanozleyen 8c1c2ef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 268d30c
remove radius abstraction
selmanozleyen 3f41243
Merge branch 'feat/spatial_neighbours' of https://github.com/selmanoz…
selmanozleyen aa9734a
add extensibility page
selmanozleyen 67bad43
unprivate methods and add extensibility page
selmanozleyen 65fa245
give a better example
selmanozleyen 85bfdeb
apply signature suggestion
selmanozleyen 79f584f
reorganize abstractions
selmanozleyen 43cdbb6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e632f74
remove unnecessary abstraction
selmanozleyen 1f9da0e
Merge branch 'feat/spatial_neighbours' of https://github.com/selmanoz…
selmanozleyen c026860
put the warning in resolve graph builder code
selmanozleyen 10cc2f0
remove n_neighs from RadiusBuilder
selmanozleyen 6477f18
definition of Adj, Dst and rename to adj, dst
selmanozleyen 516b3e0
Generalize the base class for extensibility
selmanozleyen 141fdb6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1fe316b
update docs
selmanozleyen bc6746a
Merge branch 'feat/spatial_neighbours' of https://github.com/selmanoz…
selmanozleyen bb1ec50
clarify delauney vs grid
selmanozleyen 84cbdc4
dtype rec
selmanozleyen 6d942ec
be a bit more verbose on funcitons
selmanozleyen ae9a4e6
spatial_neighbours_from_builder
selmanozleyen cd3f6fd
update docs to refer to each other
selmanozleyen 95012dc
refer to the classes properly
selmanozleyen 5a02b59
mark as TODO's
selmanozleyen 3af98cc
rename sugg
selmanozleyen e8a19d7
add combine into API
selmanozleyen ac682a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0f3329c
update docs
selmanozleyen 00ec2b0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 042f749
[pre-commit.ci] pre-commit autoupdate (#1149)
pre-commit-ci[bot] 1a87841
Functions to QC histopathology images (#1036)
timtreis 1cb6fde
remove coord_type abstraction
selmanozleyen 8afcd03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3c4dffb
Merge branch 'main' into feat/spatial_neighbours
selmanozleyen 21f02c3
make builder positional consistently
selmanozleyen 744c757
make more abstractions
selmanozleyen dbc7f0d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d7e5e5f
graphmatrixT exposure
selmanozleyen 13768e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ea3f3ca
Merge branch 'main' into feat/spatial_neighbours
selmanozleyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # Extensibility | ||
|
|
||
| ## Custom graph builders | ||
|
|
||
| The `squidpy.gr.neighbors` module exposes two builder base classes: | ||
|
|
||
| - {class}`~squidpy.gr.neighbors.GraphBuilder` is the generic builder pipeline. | ||
| Use it when you want to plug in a custom coordinate type or sparse-matrix backend. | ||
| - {class}`~squidpy.gr.neighbors.GraphBuilderCSR` is the CSR-specialized builder used | ||
| by the built-in graph construction strategies. Use it when your builder returns | ||
| {class}`~scipy.sparse.csr_matrix` objects and should reuse Squidpy's CSR-specific | ||
| postprocessors, sparse warning suppression, and multi-library combination. | ||
| - Reusable postprocessors such as | ||
| {class}`~squidpy.gr.neighbors.DistanceIntervalPostprocessor`, | ||
| {class}`~squidpy.gr.neighbors.PercentilePostprocessor`, and | ||
| {class}`~squidpy.gr.neighbors.TransformPostprocessor` are also exposed for | ||
| custom builder composition. | ||
|
|
||
| ### What to override | ||
|
|
||
| | Base class | Method / property | Required | Purpose | | ||
| |---|---|---|---| | ||
| | {class}`~squidpy.gr.neighbors.GraphBuilder` | {meth}`~squidpy.gr.neighbors.GraphBuilder.build_graph` | yes | Construct and return ``(adj, dst)`` using the coordinate and matrix types of your custom backend. | | ||
| | {class}`~squidpy.gr.neighbors.GraphBuilder` | {meth}`~squidpy.gr.neighbors.GraphBuilder.postprocessors` | no | Return post-build processing steps for ``(adj, dst)``. You can either override this or pass ``postprocessors=...`` to ``super().__init__()``. | | ||
| | {class}`~squidpy.gr.neighbors.GraphBuilder` | {meth}`~squidpy.gr.neighbors.GraphBuilder.combine` | no | Combine per-library results when using ``library_key``. If you do not need ``library_key`` support, leaving this unimplemented is fine. | | ||
|
|
||
|
|
||
| The generic builder only defines the pipeline. The CSR-specialized builder adds | ||
| multi-library ``library_key`` combination and | ||
| {class}`~scipy.sparse.SparseEfficiencyWarning` suppression, while built-in and | ||
| custom CSR builders can compose the public reusable postprocessors for | ||
| distance-interval pruning, percentile filtering, and adjacency transforms. | ||
|
|
||
| Here ``adj`` and ``dst`` are square sparse matrices of shape ``(n_obs, n_obs)`` | ||
| with matching sparsity structure: | ||
|
|
||
| - ``adj`` is the connectivity / adjacency matrix. Non-zero entries mark edges in | ||
| the graph, and built-in builders typically use ``1.0`` for present edges. | ||
| - ``dst`` is the distance matrix for those same edges. For generic graphs this is | ||
| usually the Euclidean edge length. For grid builders it may instead encode | ||
| graph-distance semantics such as ring number. | ||
| - When subclassing {class}`~squidpy.gr.neighbors.GraphBuilderCSR`, both should be | ||
| returned as {class}`~scipy.sparse.csr_matrix`. | ||
| - For CSR-based builders, ``adj`` often behaves like a boolean or indicator | ||
| matrix describing whether an edge is present, even if it is stored with a | ||
| numeric dtype such as ``float32``. ``dst`` stores edge-associated values such | ||
| as distances and will often use a floating-point dtype. The exact dtype choice | ||
| is left to the builder implementation and may depend on performance, memory, | ||
| and numerical accuracy requirements. | ||
| - By convention, ``dst`` should have a zero diagonal, and ``adj`` should only | ||
| have a non-zero diagonal when ``set_diag=True``. | ||
|
|
||
| ### Example: fast radius search with SNN | ||
|
|
||
| The built-in {class}`~squidpy.gr.neighbors.RadiusBuilder` uses scikit-learn's | ||
| ``NearestNeighbors``. The [snnpy](https://github.com/nla-group/snn) library | ||
| provides a faster exact fixed-radius search based on PCA-based pruning. The | ||
| example below swaps the backend while keeping full compatibility with the rest | ||
| of the Squidpy graph pipeline: | ||
|
|
||
| ```python | ||
| import numpy as np | ||
| from scipy.sparse import csr_matrix | ||
| from snnpy import build_snn_model | ||
|
|
||
| from squidpy.gr.neighbors import GraphBuilderCSR | ||
|
|
||
|
|
||
| class SNNRadiusBuilder(GraphBuilderCSR): | ||
| """Radius graph using the SNN fixed-radius search backend.""" | ||
|
|
||
| def __init__(self, radius: float, **kwargs): | ||
| super().__init__(**kwargs) | ||
| self.radius = radius | ||
|
|
||
| def build_graph(self, coords): | ||
| N = coords.shape[0] | ||
| model = build_snn_model(coords, verbose=0) | ||
| indices, dists = model.batch_query_radius( | ||
| coords, self.radius, return_distance=True, | ||
| ) | ||
|
|
||
| row = np.repeat(np.arange(N), [len(idx) for idx in indices]) | ||
| col = np.concatenate(indices) | ||
| d = np.concatenate(dists).astype(np.float64) | ||
|
|
||
| adj = csr_matrix( | ||
| (np.ones(len(row), dtype=np.float32), (row, col)), | ||
| shape=(N, N), | ||
| ) | ||
| dst = csr_matrix((d, (row, col)), shape=(N, N)) | ||
|
|
||
| adj.setdiag(1.0 if self.set_diag else adj.diagonal()) | ||
| dst.setdiag(0.0) | ||
| return adj, dst | ||
| ``` | ||
|
|
||
| Use it like any other builder: | ||
|
|
||
| ```python | ||
| import squidpy as sq | ||
|
|
||
| sq.gr.spatial_neighbors_from_builder(adata, SNNRadiusBuilder(radius=100.0)) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.