TorchSpatiotemporal / tsl

tsl: a PyTorch library for processing spatiotemporal data.
https://torch-spatiotemporal.readthedocs.io/
MIT License
236 stars 22 forks source link

`get_connectivity` returns different adjacencies depending on layout #35

Closed Abusagit closed 2 months ago

Abusagit commented 2 months ago

Hello!

Could you please clarify what is the reason for transposing adjacency matrix in get_connectivity method with layout="edge_index" option?

For directed graphs, this method returns reflected edges:

import tsl
import torch
import numpy as np
import pandas as pd
from tsl.datasets import LargeST

dataset = LargeST(root='./data/')

connectivity_dense = dataset.get_connectivity(layout="dense")

edges, weights = dataset.get_connectivity()

connectivity_sparse = np.zeros_like(connectivity_dense)
connectivity_sparse[edges[0], edges[1]] = weights

print(np.allclose(connectivity_sparse, connectivity_dense)) # returns False
print(np.allclose(connectivity_sparse, connectivity_dense.T)) # returns True

Also, connectivity_dense is equal to adjacency from the original work. This behaviour of layout="edge_index" seems misleading for me.

Looking forward to your answer, thank you!

marshka commented 2 months ago

Hi, the reason why we transpose the edges is that the standard representation of the adjacency matrix $\boldsymbol{A} \in \mathbb{R}^{N \times N}$ has nonzero entries in $a_{ij}$ if there is an edge going from $i$ to $j$. To receive messages from your incoming 1-hop neighbors, you thus have to compute $\boldsymbol{A}^{\top}\boldsymbol{X}$. The edge_index representation, instead, gives you the sparse COO format of $\boldsymbol{A}$.

By doing these operations, we ensure to always abide by the PyG's convention for message passing (See https://pytorch-geometric.readthedocs.io/en/latest/advanced/sparse_tensor.html).

Abusagit commented 2 months ago

Oh, Indeed, now it's clear for me, thank you! I feel like I forgot this convention in PyG :)

Abusagit commented 2 months ago

Then I close the issue, thank you for such fast reply!