def test_lcc_numpy(self): expected_lcc_matrix = np.array([ [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], ]) expected_nodelist = np.array([0, 1, 2, 3, 5]) g = nx.DiGraph() [g.add_node(i) for i in range(1, 7)] g.add_edge(1, 2) g.add_edge(1, 3) g.add_edge(3, 4) g.add_edge(3, 4) g.add_edge(3, 6) g.add_edge(6, 3) g.add_edge(4, 2) g = nx.to_numpy_array(g) lcc_matrix, nodelist = gus.largest_connected_component( g, return_inds=True) np.testing.assert_array_equal(lcc_matrix, expected_lcc_matrix) np.testing.assert_array_equal(nodelist, expected_nodelist) lcc_matrix = gus.largest_connected_component(g) np.testing.assert_array_equal(lcc_matrix, expected_lcc_matrix)
def test_lcc_scipy(self): expected_lcc_matrix = np.array([ [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], ]) expected_nodelist = np.array([0, 1, 2, 3, 5]) adjacency = np.array([ [0, 1, 1, 0, 0, 0, 0], # connected [0, 0, 0, 0, 0, 0, 0], # connected [0, 0, 0, 1, 0, 1, 0], # connected [0, 1, 0, 0, 0, 0, 0], # connected [0, 0, 0, 0, 0, 0, 0], # not connected [0, 0, 1, 0, 0, 0, 0], # connected [0, 0, 0, 0, 0, 0, 0], # not connected ]) sparse_adjacency = csr_matrix(adjacency) lcc_matrix, nodelist = gus.largest_connected_component( sparse_adjacency, return_inds=True) np.testing.assert_array_equal(lcc_matrix.toarray(), expected_lcc_matrix) np.testing.assert_array_equal(nodelist, expected_nodelist)
def test_lcc_scipy_empty(self): adjacency = np.array([[0, 1], [1, 0]]) adjacency = csr_matrix(adjacency) # remove the actual connecting edges. this is now a disconnected graph # with two nodes. however, scipy still stores the entry that now has a 0 in it # as having a 'nonempty' value, which is used in the lcc calculation adjacency[0, 1] = 0 adjacency[1, 0] = 0 lcc_adjacency = gus.largest_connected_component(adjacency) assert lcc_adjacency.shape[0] == 1
def test_lcc_bad_matrix(self): A = np.array([0, 1]) with self.assertRaises(ValueError): gus.largest_connected_component(A)