Skip to content

Commit

Permalink
improve and test is_directed
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Mar 1, 2019
1 parent 3d373e6 commit be1ad87
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 2 additions & 6 deletions pygsp/graphs/graph.py
Expand Up @@ -395,7 +395,7 @@ def is_directed(self, recompute=False):
r"""Check if the graph has directed edges (cached).
In this framework, we consider that a graph is directed if and
only if its weight matrix is non symmetric.
only if its weight matrix is not symmetric.
Parameters
----------
Expand All @@ -407,10 +407,6 @@ def is_directed(self, recompute=False):
directed : bool
True if the graph is directed.
Notes
-----
Can also be used to check if a matrix is symmetrical
Examples
--------
Expand Down Expand Up @@ -440,7 +436,7 @@ def is_directed(self, recompute=False):
if hasattr(self, '_directed') and not recompute:
return self._directed

self._directed = np.abs(self.W - self.W.T).sum() != 0
self._directed = (self.W != self.W.T).nnz != 0
return self._directed

def extract_components(self):
Expand Down
16 changes: 16 additions & 0 deletions pygsp/tests/test_graphs.py
Expand Up @@ -85,6 +85,22 @@ def test_is_connected(self):
self.assertEqual(graph.is_directed(), True)
self.assertEqual(graph.is_connected(), True)

def test_is_directed(self):
graph = graphs.Graph([
[0, 3, 0, 0],
[3, 0, 4, 0],
[0, 4, 0, 2],
[0, 0, 2, 0],
])
assert graph.W.nnz == 6
self.assertEqual(graph.is_directed(), False)
graph.W[0, 1] = 0
assert graph.W.nnz == 6
self.assertEqual(graph.is_directed(recompute=True), True)
graph.W[1, 0] = 0
assert graph.W.nnz == 6
self.assertEqual(graph.is_directed(recompute=True), False)

def test_laplacian(self):

adjacency = np.array([
Expand Down

0 comments on commit be1ad87

Please sign in to comment.