def test_transform_correct_types(self):
     lse = LaplacianSpectralEmbed(n_components=2)
     for graph in self.testgraphs.values():
         A, a = remove_vertices(graph, 1, return_removed=True)
         lse.fit(A)
         directed = lse.latent_right_ is not None
         w = lse.transform(a)
         if directed:
             self.assertIsInstance(w, tuple)
             self.assertIsInstance(w[0], np.ndarray)
             self.assertIsInstance(w[1], np.ndarray)
         elif not directed:
             self.assertIsInstance(w, np.ndarray)
             self.assertEqual(np.atleast_2d(w).shape[1], 2)
Beispiel #2
0
 def test_transform_correct_types(self):
     ase = AdjacencySpectralEmbed(n_components=2)
     for graph in self.testgraphs.values():
         A, a = remove_vertices(graph, 1, return_removed=True)
         ase.fit(A)
         directed = ase.latent_right_ is not None
         weighted = not np.array_equal(A, A.astype(bool))
         w = ase.transform(a)
         if directed:
             self.assertIsInstance(w, tuple)
             self.assertIsInstance(w[0], np.ndarray)
             self.assertIsInstance(w[1], np.ndarray)
         elif not directed:
             self.assertIsInstance(w, np.ndarray)
             self.assertEqual(np.atleast_2d(w).shape[1], 2)
Beispiel #3
0
    def test_exceptions(self):
        ase = clone(self.ase)

        with pytest.raises(Exception):
            ase.fit(self.testgraphs["Gwd"])
            ase.transform("9001")

        with pytest.raises(Exception):
            Guwd = self.testgraphs["Guwd"]
            ase.fit(Guwd)
            ase.transform(np.ones(len(Guwd)))

        with pytest.raises(ValueError):
            A, a = remove_vertices(self.testgraphs["Gw"], [0, 1], return_removed=True)
            a = a.T
            ase.fit(A)
            ase.transform(a)
    def test_embedding(self):
        epsilon = 0.1
        nodes_per_community = 100
        P = np.array([[0.8, 0.2], [0.2, 0.8]])
        undirected, labels_ = sbm(2 * [nodes_per_community],
                                  P,
                                  return_labels=True)

        oos_idx = 0
        A, a = remove_vertices(undirected,
                               indices=oos_idx,
                               return_removed=True)

        lse = LaplacianSpectralEmbed(n_components=2)
        X_hat = lse.fit_transform(A)
        w = lse.transform(a)
        self.assertTrue(
            X_hat[0][0] - epsilon < w[0][0] < X_hat[0][0] + epsilon)
        self.assertTrue(
            X_hat[0][1] - epsilon < w[0][1] < X_hat[0][1] + epsilon)
Beispiel #5
0
    def test_directed_correct_latent_positions(self):
        # setup
        ase = AdjacencySpectralEmbed(n_components=3)
        P = np.array([[0.9, 0.1, 0.1], [0.3, 0.6, 0.1], [0.1, 0.5, 0.6]])
        M, labels = sbm([200, 200, 200], P, directed=True, return_labels=True)

        # one node from each community
        oos_idx = np.nonzero(np.r_[1, np.diff(labels)[:-1]])[0]
        labels = list(labels)
        oos_labels = [labels.pop(i) for i in oos_idx]

        # Grab out-of-sample, fit, transform
        A, a = remove_vertices(M, indices=oos_idx, return_removed=True)
        latent_left, latent_right = ase.fit_transform(A)
        oos_left, oos_right = ase.transform(a)

        # separate into communities
        for i, latent in enumerate([latent_left, latent_right]):
            left = i == 0
            df = pd.DataFrame(
                {
                    "Type": labels,
                    "Dimension 1": latent[:, 0],
                    "Dimension 2": latent[:, 1],
                    "Dimension 3": latent[:, 2],
                }
            )
            # make sure that oos vertices are closer to their true community averages than other community averages
            means = df.groupby("Type").mean()
            if left:
                avg_dist_within = np.diag(pairwise_distances(means, oos_left))
                avg_dist_between = np.diag(pairwise_distances(means, oos_right))
                self.assertTrue(all(avg_dist_within < avg_dist_between))
            elif not left:
                avg_dist_within = np.diag(pairwise_distances(means, oos_right))
                avg_dist_between = np.diag(pairwise_distances(means, oos_left))
                self.assertTrue(all(avg_dist_within < avg_dist_between))
Beispiel #6
0
 def test_directed_vertex_direction(self):
     M = self.testgraphs["Guwd"]
     oos_idx = 0
     A, a = remove_vertices(M, indices=oos_idx, return_removed=True)
     assert_equal(np.delete(M[:, 0], oos_idx), a[0])