Skip to content

Commit

Permalink
fix bug when getting edge list of empty graph
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Dec 3, 2018
1 parent 92505c6 commit 2b71204
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pygsp/graphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,14 @@ def get_edge_list(self):
"""

W = self.W if self.is_directed() else sparse.tril(self.W)
if self.is_directed():
W = self.W.tocoo()
else:
W = sparse.tril(self.W, format='coo')

sources, targets = W.nonzero()
weights = self.W[sources, targets]
weights = np.asarray(weights).squeeze()
sources = W.row
targets = W.col
weights = W.data

assert self.n_edges == sources.size == targets.size == weights.size
return sources, targets, weights
Expand Down
15 changes: 15 additions & 0 deletions pygsp/tests/test_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ def test_difference(self):
self.assertEqual(len(Ls), G.n_vertices)
np.testing.assert_allclose(Ls, G.L.dot(self._signal))

def test_empty_graph(self, n_vertices=11):
graph = graphs.Graph(np.zeros((n_vertices, n_vertices)))
self.assertEqual(graph.n_vertices, n_vertices)
self.assertEqual(graph.n_edges, 0)
self.assertEqual(graph.W.nnz, 0)
self.assertEqual(graph.L.nnz, 0)
sources, targets, weights = graph.get_edge_list()
self.assertEqual(len(sources), 0)
self.assertEqual(len(targets), 0)
self.assertEqual(len(weights), 0)
graph.compute_differential_operator()
self.assertEqual(graph.D.nnz, 0)
graph.compute_fourier_basis()
np.testing.assert_allclose(graph.U, np.identity(n_vertices))

def test_set_coordinates(self):
G = graphs.FullConnected()
coords = self._rs.uniform(size=(G.N, 2))
Expand Down

0 comments on commit 2b71204

Please sign in to comment.