Skip to content

weight_matrix with n_nearest_neighbors and n_neighbors #5

Open

Description

@wyq977

Hi, I am wondering why there are two parameters for nearest neighbours in weight matrix calculation.

def weight_matrix(adata, l, cutoff=None, n_neighbors=None, n_nearest_neighbors=6, single_cell=False):

It seems to me that there is no need for both or am I missing sth?

Best

Activity

  1. leeyoyohku commented on Jan 11, 2023

    @leeyoyohku
    Collaborator

    n_neighbors is for secreted (long-distance) signaling, while n_nearest_neighbors is for adjacent signaling (ECM or cell-cell contact).

  2. ACSoupir commented on Jul 23, 2025

    @ACSoupir

    Is it expected to not apply the cut-off to both weight matrices? rbf_d has it applied but not rbf_d0:

    nonzero_mask = np.array(rbf_d[rbf_d.nonzero()] < cutoff)[0]
    rows = rbf_d.nonzero()[0][nonzero_mask]
    cols = rbf_d.nonzero()[1][nonzero_mask]
    rbf_d[rows, cols] = 0

    Or am I missing a piece?

  3. ACSoupir commented on Jul 24, 2025

    @ACSoupir

    There also appears to be an implementation deviation from what is mentioned in the manuscript associated with weights:

    .. Spatial weight matrix is computed based on Radial Basis Function (RBF) kernel with an element-size normalisation,

    $$w_{ij}^{(0)} = exp \left \{ - \frac{d_{ij}^2}{2l^2} \right \}; w_{ij} = \frac{n}{W} w_{ij}^{(0)},$$

    where $d_{ij}$ is the geographical distance between spot $i$ and $j$ (i.e., Euclidean distance on spatial coordinates), $W$ is the sum of $w_{ij}^{(0)}$, and $n$ is the number of spots.

    This indicates like the full distance matrix is calculated, Gaussian kernel is applied, then values are filtered based on the cut-off.

    However, in the implementation with sklearn to calculate Nearest Neighbors, the output distance matrix isn't symmetric for low n_neighbor_layers (which is logical for ligand -> receptor directed interaction). But, when n_neighbors is large and l produces a kernel surface where n_neighbors captures points beyond the cut-off values, the matrix is symmetric and remains symmetric after applying the cut-off.

    The difference arises in the standardization step due to the summation of W being less in the first instance ("ECM/cell-cell contact" mentioned above). Can you comment on:

    1. whether or not it's believed this deviation matters (it produces different Global Bivariate Moran's I values), and
    2. why the full $w_{ij}^{(0)}$ matrix is summed and divided from $n$ rather than row standardization which seems to be the common theme in other implementations of Moran's I?

    Edit: Through working, it seems like if there are 6 nearest neighbors being requested for the ECM/direct cell-cell interaction and there are ties in distance between the 6th and 7th nearest neighbor, the current implementation can cause ambiguous selection of the nearest neighbor for which to calculate 'communication' between.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions