コード例 #1
0
ファイル: test_graphs.py プロジェクト: andromeda0505/pygsp
 def test_erdosreny(self):
     graphs.ErdosRenyi(N=100, connected=False, directed=False)
     graphs.ErdosRenyi(N=100, connected=False, directed=True)
     graphs.ErdosRenyi(N=100, connected=True, directed=False, seed=42)
     graphs.ErdosRenyi(N=100, connected=True, directed=True, seed=42)
     G = graphs.ErdosRenyi(N=100, p=1, self_loops=True)
     self.assertEqual(G.W.nnz, 100**2)
コード例 #2
0
ファイル: test_graphs.py プロジェクト: andromeda0505/pygsp
    def test_differential_operator(self, n_vertices=98):
        r"""The Laplacian must always be the divergence of the gradient,
        whether the Laplacian is combinatorial or normalized, and whether the
        graph is directed or weighted."""
        def test_incidence_nx(graph):
            r"""Test that the incidence matrix corresponds to NetworkX."""
            incidence_pg = np.sign(graph.D.toarray())
            G = nx.OrderedDiGraph if graph.is_directed() else nx.OrderedGraph
            graph_nx = nx.from_scipy_sparse_matrix(graph.W, create_using=G)
            incidence_nx = nx.incidence_matrix(graph_nx, oriented=True)
            np.testing.assert_equal(incidence_pg, incidence_nx.toarray())

        for graph in [
                graphs.Graph(np.zeros((n_vertices, n_vertices))),
                graphs.Graph(np.identity(n_vertices)),
                graphs.Graph([[0, 0.8], [0.8, 0]]),
                graphs.Graph([[1.3, 0], [0.4, 0.5]]),
                graphs.ErdosRenyi(n_vertices, directed=False, seed=42),
                graphs.ErdosRenyi(n_vertices, directed=True, seed=42)
        ]:
            for lap_type in ['combinatorial', 'normalized']:
                graph.compute_laplacian(lap_type)
                graph.compute_differential_operator()
                L = graph.D.dot(graph.D.T)
                np.testing.assert_allclose(L.toarray(), graph.L.toarray())
                test_incidence_nx(graph)
コード例 #3
0
ファイル: test_graphs.py プロジェクト: scottgigante/pygsp
    def test_laplacian(self):

        adjacency = np.array([
            [0, 3, 0, 1],
            [3, 0, 1, 0],
            [0, 1, 0, 3],
            [1, 0, 3, 0],
        ])
        laplacian = np.array([
            [+4, -3, +0, -1],
            [-3, +4, -1, +0],
            [+0, -1, +4, -3],
            [-1, +0, -3, +4],
        ])
        G = graphs.Graph(adjacency)
        self.assertFalse(G.is_directed())
        G.compute_laplacian('combinatorial')
        np.testing.assert_allclose(G.L.toarray(), laplacian)
        G.compute_laplacian('normalized')
        np.testing.assert_allclose(G.L.toarray(), laplacian / 4)

        adjacency = np.array([
            [0, 6, 0, 1],
            [0, 0, 0, 0],
            [0, 2, 0, 3],
            [1, 0, 3, 0],
        ])
        G = graphs.Graph(adjacency)
        self.assertTrue(G.is_directed())
        G.compute_laplacian('combinatorial')
        np.testing.assert_allclose(G.L.toarray(), laplacian)
        G.compute_laplacian('normalized')
        np.testing.assert_allclose(G.L.toarray(), laplacian / 4)

        def test_combinatorial(G):
            np.testing.assert_equal(G.L.toarray(), G.L.T.toarray())
            np.testing.assert_equal(G.L.sum(axis=0), 0)
            np.testing.assert_equal(G.L.sum(axis=1), 0)
            np.testing.assert_equal(G.L.diagonal(), G.dw)

        def test_normalized(G):
            np.testing.assert_equal(G.L.toarray(), G.L.T.toarray())
            np.testing.assert_equal(G.L.diagonal(), 1)

        G = graphs.ErdosRenyi(100, directed=False)
        self.assertFalse(G.is_directed())
        G.compute_laplacian(lap_type='combinatorial')
        test_combinatorial(G)
        G.compute_laplacian(lap_type='normalized')
        test_normalized(G)

        G = graphs.ErdosRenyi(100, directed=True)
        self.assertTrue(G.is_directed())
        G.compute_laplacian(lap_type='combinatorial')
        test_combinatorial(G)
        G.compute_laplacian(lap_type='normalized')
        test_normalized(G)
コード例 #4
0
def main(_):
    # Initialize tempdir
    if tf.gfile.Exists(FLAGS.log_dir):
        tf.gfile.DeleteRecursively(FLAGS.log_dir)
    tf.gfile.MakeDirs(FLAGS.log_dir)

    # Initialize data
    G = graphs.ErdosRenyi(FLAGS.num_vertices, 0.1, seed=42)
    G.compute_laplacian("normalized")
    L = initialize_laplacian_tensor(G.W)
    W = (G.W).astype(np.float32)

    train_data, train_labels = generate_wave_samples(FLAGS.num_train,
                                                     W,
                                                     T=FLAGS.num_frames,
                                                     sigma=FLAGS.sigma)
    train_mb_source = MinibatchSource(train_data, train_labels)

    test_data, test_labels = generate_wave_samples(FLAGS.num_test,
                                                   W,
                                                   T=FLAGS.num_frames,
                                                   sigma=FLAGS.sigma)

    # Run training and evaluation loop
    run_training(train_mb_source, L, test_data, test_labels)
コード例 #5
0
ファイル: test_graphs.py プロジェクト: andromeda0505/pygsp
 def test_edge_list(self):
     for directed in [False, True]:
         G = graphs.ErdosRenyi(100, directed=directed)
         sources, targets, weights = G.get_edge_list()
         if not directed:
             self.assertTrue(np.all(sources <= targets))
         edges = np.arange(G.n_edges)
         np.testing.assert_equal(G.W[sources[edges], targets[edges]],
                                 weights[edges][np.newaxis, :])
コード例 #6
0
    def __init__(self,
                 n_nodes,
                 ux,
                 spectral_profile,
                 type_graph="erdos",
                 type_noise="gaussian",
                 save_history=True,
                 seed=None,
                 **kargs):

        ####### The algorithm asks the user a spectral_profile for the Graph filter that will be applied
        ### to white noise so the output signal is stationary with respect to the graph structure.

        #### This section initialize the structure of the graph signal

        ### Input
        # n_nodes=number of nodes
        # ux= mean of the signal
        # spectral_profile= a function generating the power spectral density of the sygnal.
        # type_graph=available  graph model (erdos,euclidean,barabasi_albert)
        # type_noise = distribution of the noise (gaussian,uniform) that will be used to ge

        self.n_nodes = n_nodes
        self.type_graph = type_graph
        self.type_noise = type_noise
        self.ux = ux
        self.spectral_profile = spectral_profile
        self.seed = seed

        if type_graph == "erdos":

            self.G = graphs.ErdosRenyi(self.n_nodes,
                                       kargs['p'],
                                       seed=self.seed)
            self.G.set_coordinates()

        if type_graph == "barabasi_albert":
            self.G = graphs.BarabasiAlbert(self.n_nodes,
                                           m=kargs['m'],
                                           m0=kargs['m'],
                                           seed=self.seed)
            self.G.set_coordinates()

        if type_graph == "Minnesota":
            self.G = graphs.Minnesota()

        self.generate_fourier()
        self.generate_filter()