Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/pathpyG/core/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading