From a86d1b2d795d0bd5fd861274a19a87d7af644d1a Mon Sep 17 00:00:00 2001 From: Moritz Lampert Date: Fri, 20 Dec 2024 13:48:01 +0000 Subject: [PATCH] fix --- src/pathpyG/core/graph.py | 2 +- tests/core/test_graph.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pathpyG/core/graph.py b/src/pathpyG/core/graph.py index 6806607c..2aae5246 100644 --- a/src/pathpyG/core/graph.py +++ b/src/pathpyG/core/graph.py @@ -390,7 +390,7 @@ def sparse_adj_matrix(self, edge_attr: Any = None) -> Any: scipy.sparse.coo_matrix: sparse adjacency matrix representation of graph """ if edge_attr is None: - return torch_geometric.utils.to_scipy_sparse_matrix(self.data.edge_index.as_tensor()) + return torch_geometric.utils.to_scipy_sparse_matrix(self.data.edge_index.as_tensor(), num_nodes=self.n) else: return torch_geometric.utils.to_scipy_sparse_matrix( self.data.edge_index.as_tensor(), edge_attr=self.data[edge_attr], num_nodes=self.n diff --git a/tests/core/test_graph.py b/tests/core/test_graph.py index 52cdc474..7d6a7ebc 100644 --- a/tests/core/test_graph.py +++ b/tests/core/test_graph.py @@ -204,6 +204,16 @@ def test_sparse_adj_matrix(simple_graph): assert weighted_adj.data[1] == 1 assert weighted_adj.data[2] == 2 + g = Graph.from_edge_index(torch.tensor([[0], [1]]), num_nodes=5) + adj = g.sparse_adj_matrix() + assert adj.shape == (5, 5) + assert adj.nnz == 1 + + g.data.edge_attr = torch.tensor([[1]]) + adj = g.sparse_adj_matrix("edge_attr") + assert adj.shape == (5, 5) + assert adj.nnz == 1 + def test_degrees(simple_graph): in_degrees = simple_graph.degrees("in")