def test_basic(self): methods = [PageRank(), Diffusion(), Closeness(), HITS(), Harmonic()] for adjacency in [test_graph(), test_digraph()]: n = adjacency.shape[0] for method in methods: score = method.fit_transform(adjacency) self.assertEqual(score.shape, (n, )) self.assertTrue(score.min() >= 0)
def test_options(self): ward = Ward() ward_options = Ward(embedding_method=Spectral(3), co_cluster=True) for algo in [ward, ward_options]: for input_matrix in [test_graph(), test_digraph(), test_bigraph()]: dendrogram = algo.fit_transform(input_matrix) self.assertEqual(dendrogram.shape, (input_matrix.shape[0] - 1, 4)) if algo.co_cluster: self.assertEqual(algo.dendrogram_full_.shape, (sum(input_matrix.shape) - 1, 4))
def test_random_projection(self): for algo in [RandomProjection(), RandomProjection(random_walk=True)]: adjacency = test_graph() embedding = algo.fit_transform(adjacency) self.assertEqual(embedding.shape[1], 2) adjacency = test_digraph() embedding = algo.fit_transform(adjacency) self.assertEqual(embedding.shape[1], 2) adjacency = test_graph_disconnect() embedding = algo.fit_transform(adjacency) self.assertEqual(embedding.shape[1], 2)
def test_louvain_hierarchy(self): louvain = LouvainNE() for adjacency in [ test_graph(), test_graph_disconnect(), test_digraph() ]: self.assertTupleEqual( louvain.fit_transform(adjacency).shape, (10, 2)) louvain.fit(test_bigraph()) self.assertTupleEqual(louvain.embedding_.shape, (6, 2))
def test_laplacian(self): # regular Laplacian spectral = LaplacianEmbedding(self.k, normalized=False) embedding = spectral.fit_transform(self.adjacency) self.assertAlmostEqual(np.linalg.norm(embedding.mean(axis=0)), 0) spectral = LaplacianEmbedding(self.k, regularization=0) with self.assertRaises(ValueError): spectral.fit(test_bigraph()) with self.assertRaises(ValueError): spectral.fit(test_digraph()) with self.assertRaises(ValueError): spectral.fit(test_graph_disconnect()) with self.assertWarns(Warning): n = self.k - 1 spectral.fit_transform(np.ones((n, n)))
def test_options(self): for adjacency in [test_graph(), test_digraph()]: n = adjacency.shape[0] force_atlas = ForceAtlas() layout = force_atlas.fit_transform(adjacency) self.assertEqual((n, 2), layout.shape) force_atlas = ForceAtlas(lin_log=True) layout = force_atlas.fit_transform(adjacency) self.assertEqual((n, 2), layout.shape) force_atlas = ForceAtlas(approx_radius=1.) layout = force_atlas.fit_transform(adjacency) self.assertEqual((n, 2), layout.shape) force_atlas.fit(adjacency, pos_init=layout, n_iter=1)
def test_regular(self): # regular Laplacian spectral = Spectral(self.k, normalized_laplacian=False, barycenter=False, normalized=False) embedding = spectral.fit_transform(self.adjacency) self.assertAlmostEqual(np.linalg.norm(embedding.mean(axis=0)), 0) error = np.abs(spectral.predict(self.adjacency[1]) - embedding[1]).sum() self.assertAlmostEqual(error, 0) spectral = Spectral(self.k, normalized_laplacian=False, regularization=0, equalize=True) with self.assertRaises(ValueError): spectral.fit(test_bigraph()) with self.assertRaises(ValueError): spectral.fit(test_digraph()) with self.assertRaises(ValueError): spectral.fit(test_graph_disconnect()) with self.assertWarns(Warning): n = self.k - 1 spectral.fit_transform(np.ones((n, n)))