-
Notifications
You must be signed in to change notification settings - Fork 99
Fix for spatial_neighbors breaks when transform='spectral' #1028
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
base: main
Are you sure you want to change the base?
Changes from 15 commits
7e67cc1
dbdda0c
a3366e0
b3ddcb7
2fffcfd
eeab937
2ce6e85
651505a
303d5c4
8c881be
bcb5989
172080d
806273a
a3db2d0
ea457c3
6e60c6c
22acdad
017a763
126fd38
a24419a
4eae80c
4f6010b
8f178cc
c7c8f83
3d82ed5
6680ec2
0086dac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,8 @@ | |
| import pandas as pd | ||
| from anndata import AnnData | ||
| from anndata.utils import make_index_unique | ||
| from geopandas import GeoDataFrame | ||
| from numba import njit | ||
| from fast_array_utils import stats as fau_stats | ||
| from numba import njit, prange | ||
| from scanpy import logging as logg | ||
| from scipy.sparse import ( | ||
| SparseEfficiencyWarning, | ||
|
|
@@ -453,29 +453,67 @@ def _build_connectivity( | |
| return Adj | ||
|
|
||
|
|
||
| @njit | ||
| def outer(indices: NDArrayA, indptr: NDArrayA, degrees: NDArrayA) -> NDArrayA: | ||
| res = np.empty_like(indices, dtype=np.float64) | ||
| start = 0 | ||
| for i in range(len(indptr) - 1): | ||
| @njit(parallel=True) | ||
selmanozleyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def _csr_bilateral_diag_scale_helper( | ||
| data: NDArrayA, indices: NDArrayA, indptr: NDArrayA, degrees: NDArrayA | ||
selmanozleyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) -> NDArrayA: | ||
| """ | ||
| Return an array F aligned with CSR non-zeros such that | ||
| F[k] = d[i] * data[k] * d[j] for the k-th non-zero (i, j) in CSR order. | ||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
||
| data : array of float | ||
| CSR `data` (non-zero values). | ||
| indices : array of int | ||
| CSR `indices` (column indices). | ||
| indptr : array of int | ||
| CSR `indptr` (row pointer). | ||
| degrees : array of float, shape (n,) | ||
| Diagonal scaling vector. | ||
|
|
||
| Returns | ||
| ------- | ||
| array of float | ||
| Length equals len(data). Entry-wise factors d_i * d_j * data[k] | ||
| """ | ||
|
|
||
| res = np.empty_like(data, dtype=np.float64) | ||
|
||
| for i in prange(len(indptr) - 1): | ||
| ixs = indices[indptr[i] : indptr[i + 1]] | ||
| res[start : start + len(ixs)] = degrees[i] * degrees[ixs] | ||
| start += len(ixs) | ||
| res[indptr[i] : indptr[i + 1]] = degrees[i] * degrees[ixs] * data[indptr[i] : indptr[i + 1]] | ||
|
|
||
| return res | ||
|
|
||
|
|
||
| def symmetric_normalize_csr(adj: spmatrix) -> csr_matrix: | ||
| """ | ||
| Return D^{-1/2} * A * D^{-1/2}, where D = diag(degrees(A)) and A = adj. | ||
|
|
||
|
|
||
| Parameters | ||
| ---------- | ||
| adj : scipy.sparse.csr_matrix | ||
|
|
||
| Returns | ||
| ------- | ||
| scipy.sparse.csr_matrix | ||
| """ | ||
| degrees = np.squeeze(np.array(np.sqrt(1.0 / fau_stats.sum(adj, axis=0)))) | ||
| if adj.shape[0] != len(degrees): | ||
| raise ValueError("len(degrees) must equal number of rows of adj") | ||
| res_data = _csr_bilateral_diag_scale_helper(adj.data, adj.indices, adj.indptr, degrees) | ||
| return csr_matrix((res_data, adj.indices, adj.indptr), shape=adj.shape) | ||
|
|
||
|
|
||
| def _transform_a_spectral(a: spmatrix) -> spmatrix: | ||
| if not isspmatrix_csr(a): | ||
| a = a.tocsr() | ||
| if not a.nnz: | ||
| return a | ||
|
|
||
| degrees = np.squeeze(np.array(np.sqrt(1.0 / a.sum(axis=0)))) | ||
| a = a.multiply(outer(a.indices, a.indptr, degrees)) | ||
| a.eliminate_zeros() | ||
|
|
||
| return a | ||
| return symmetric_normalize_csr(a) | ||
|
|
||
|
|
||
| def _transform_a_cosine(a: spmatrix) -> spmatrix: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.