Skip to content

Instantly share code, notes, and snippets.

@mazieres
Created March 19, 2015 14:07
Show Gist options
  • Save mazieres/85419de470d993a4e3a6 to your computer and use it in GitHub Desktop.
Save mazieres/85419de470d993a4e3a6 to your computer and use it in GitHub Desktop.
import unittest
import networkx as nx
class TestExtract(unittest.TestCase):
def test_graph_from_adj_mat(self):
X = np.array([
[(0,), (1,), (2,)],
[(1,), (0,), (0,)],
[(2,), (0,), (0,)]], dtype=[('weight', '<i8')])
tested = graph_from_adj_mat(X, labels=['A','B','C'])
expected = {
'A': {'C': {'weight': 2}, 'B': {'weight': 1}},
'B': {'A': {'weight': 1}},
'C': {'A': {'weight': 2}}}
for k in tested:
msg = '\nExpected:\n{}\nGot:\n{}'.format(expected, tested)
self.assertEqual(expected[k], tested[k], msg=msg)
def graph_from_adj_mat(adj_mat, labels=None):
G = nx.from_numpy_matrix(adj_mat)
if labels:
nx.relabel_nodes(G, dict(enumerate(labels)), copy=False)
return G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment