Ejemplo n.º 1
0
def test_node_indices():
    #            e1
    # graph:  n1 -> n2
    #      e2  |  /
    #          v /   (e4 (n2->n3))
    #         n3 -> n4
    #            e3

    n1 = Node(1.0)
    n2 = Node(2.0)
    n3 = Node(3.0)
    n4 = Node(4.0)

    e1 = Edge([n1, n2])
    e2 = Edge([n1, n3])
    e3 = Edge([n3, n4])
    e4 = Edge([n2, n3])

    g1 = Graph([n1, n2, n3, n4], [e1, e2, e3, e4])

    assert g1.next_node_index == 4

    n1, n2, n3, n4 = g1.get_node_list()
    assert n1.get_attribute("node_index") == 0
    assert n2.get_attribute("node_index") == 1
    assert n3.get_attribute("node_index") == 2
    assert n4.get_attribute("node_index") == 3
Ejemplo n.º 2
0
def test_degree_undirected_graph():
    #            e1
    # graph:  n1 -- n2
    #      e2  |  /
    #          | /
    #         n3 -- n4
    #            e3

    n1 = Node(1.0)
    n2 = Node(2.0)
    n3 = Node(3.0)
    n4 = Node(4.0)

    e1 = Edge([n1, n2], weight=4.0, directed=False)
    e2 = Edge([n1, n3], weight=5.0, directed=False)
    e3 = Edge([n3, n4], weight=6.0, directed=False)
    e4 = Edge([n2, n3], weight=7.0, directed=False)

    g1 = Graph([n1, n2, n3, n4], [e1, e2, e3, e4], directed=False)
    D1 = g1.get_D()

    adjacency_matrix = np.array([
        [0.0, 4.0, 5.0, 0.0],
        [4.0, 0.0, 7.0, 0.0],
        [5.0, 7.0, 0.0, 6.0],
        [0.0, 0.0, 6.0, 0.0],
    ])

    np.testing.assert_allclose(D1, np.diag(adjacency_matrix.sum(1)))
    np.testing.assert_allclose(D1, np.diag(adjacency_matrix.sum(0)))
Ejemplo n.º 3
0
def test_adjacency_directed_graph():
    #            e1
    # graph:  n1 -> n2
    #      e2  |  /
    #          v /   (e4 (n2->n3))
    #         n3 -> n4
    #            e3

    n1 = Node(1.0)
    n2 = Node(2.0)
    n3 = Node(3.0)
    n4 = Node(4.0)

    e1 = Edge([n1, n2], weight=4.0)
    e2 = Edge([n1, n3], weight=5.0)
    e3 = Edge([n3, n4], weight=6.0)
    e4 = Edge([n2, n3], weight=7.0)

    g1 = Graph([n1, n2, n3, n4], [e1, e2, e3, e4])
    W1 = g1.get_W()

    adjacency_matrix = np.array([
        [0.0, 4.0, 5.0, 0.0],
        [0.0, 0.0, 7.0, 0.0],
        [0.0, 0.0, 0.0, 6.0],
        [0.0, 0.0, 0.0, 0.0],
    ])

    np.testing.assert_allclose(W1, adjacency_matrix)
Ejemplo n.º 4
0
def test_simple_undirected_graph_connections():
    #            e1
    # graph:  n1 -- n2
    #      e2  |  /
    #          | /
    #         n3 -- n4
    #            e3

    n1 = Node(1.0)
    n2 = Node(2.0)
    n3 = Node(3.0)
    n4 = Node(4.0)

    e1 = Edge([n1, n2], directed=False)
    e2 = Edge([n1, n3], directed=False)
    e3 = Edge([n3, n4], directed=False)
    e4 = Edge([n2, n3], directed=False)

    assert n1.get_attribute("in_degree") is None
    assert n1.get_attribute("out_degree") is None
    assert n1.get_attribute("degree") == 2

    assert n2.get_attribute("in_degree") is None
    assert n2.get_attribute("out_degree") is None
    assert n2.get_attribute("degree") == 2

    assert n3.get_attribute("in_degree") is None
    assert n3.get_attribute("out_degree") is None
    assert n3.get_attribute("degree") == 3

    assert n4.get_attribute("in_degree") is None
    assert n4.get_attribute("out_degree") is None
    assert n4.get_attribute("degree") == 1

    g1 = Graph([n1, n2, n3, n4], [e1, e2, e3, e4], directed=False)
    n1, n2, n3, n4 = g1.get_node_list()

    assert n1.get_attribute("in_degree") is None
    assert n1.get_attribute("out_degree") is None
    assert n1.get_attribute("degree") == 2

    assert n2.get_attribute("in_degree") is None
    assert n2.get_attribute("out_degree") is None
    assert n2.get_attribute("degree") == 2

    assert n3.get_attribute("in_degree") is None
    assert n3.get_attribute("out_degree") is None
    assert n3.get_attribute("degree") == 3

    assert n4.get_attribute("in_degree") is None
    assert n4.get_attribute("out_degree") is None
    assert n4.get_attribute("degree") == 1
Ejemplo n.º 5
0
def test_simple_directed_graph_connections():
    #            e1
    # graph:  n1 -> n2
    #      e2  |  /
    #          v /   (e4 (n2->n3))
    #         n3 -> n4
    #            e3

    n1 = Node(1.0)
    n2 = Node(2.0)
    n3 = Node(3.0)
    n4 = Node(4.0)

    e1 = Edge([n1, n2], directed=True)
    e2 = Edge([n1, n3], directed=True)
    e3 = Edge([n3, n4], directed=True)
    e4 = Edge([n2, n3], directed=True)

    assert n1.get_attribute("in_degree") == 0
    assert n1.get_attribute("out_degree") == 2
    assert n1.get_attribute("degree") == 2

    assert n2.get_attribute("in_degree") == 1
    assert n2.get_attribute("out_degree") == 1
    assert n2.get_attribute("degree") == 2

    assert n3.get_attribute("in_degree") == 2
    assert n3.get_attribute("out_degree") == 1
    assert n3.get_attribute("degree") == 3

    assert n4.get_attribute("in_degree") == 1
    assert n4.get_attribute("out_degree") == 0
    assert n4.get_attribute("degree") == 1

    g1 = Graph([n1, n2, n3, n4], [e1, e2, e3, e4])
    n1, n2, n3, n4 = g1.get_node_list()

    assert n1.get_attribute("in_degree") == 0
    assert n1.get_attribute("out_degree") == 2
    assert n1.get_attribute("degree") == 2

    assert n2.get_attribute("in_degree") == 1
    assert n2.get_attribute("out_degree") == 1
    assert n2.get_attribute("degree") == 2

    assert n3.get_attribute("in_degree") == 2
    assert n3.get_attribute("out_degree") == 1
    assert n3.get_attribute("degree") == 3

    assert n4.get_attribute("in_degree") == 1
    assert n4.get_attribute("out_degree") == 0
    assert n4.get_attribute("degree") == 1
Ejemplo n.º 6
0
def test_undirected_graph_raise_error_for_directed_edge():
    n1 = Node(1.0)
    n2 = Node(2.0)

    e1 = Edge([n1, n2], directed=True)

    with pytest.raises(RuntimeError):
        Graph(nodes=[n1, n2], edges=[e1], directed=False)
Ejemplo n.º 7
0
    def nextTask(self):
        id = -1
        while True:
            if len(self.localJobQueue) > 0 and random() < 0.7:
                id = self.localJobQueue.pop()

            elif len(self.IDQueue) == 0:
                self.IDQueue.append(
                    id=self.taskClient.getRedis(self.taskID).spop(100))
                if len(self.IDQueue) != 0:
                    id = self.IDQueue.get()

            if not (id in self.recentProcessedIDs):
                break
            else:
                id = -1
        if id == -1:
            return [id, Graph()]
        return [id, self.graphCache.egoNetwork(id)]
Ejemplo n.º 8
0
 def egoNetwork(self, nodeID):
     nodeID = strToUnicode(nodeID)
     rtnGraph = Graph()
     edgeID = 0
     if self.existNode(nodeID):
         neighbours = self.loadNeighbours(nodeID)
         for neighbour in neighbours:
             neighbour = strToUnicode(neighbour)
             rtnGraph.addEdge(edgeID, nodeID, neighbour, 1.0)
             edgeID += 1
             cNeighbours = self.loadNeighbours(neighbour)
             for cNeighbour in cNeighbours:
                 if cNeighbour in neighbours:
                     rtnGraph.addEdge(edgeID, neighbour, cNeighbour, 1.0)
                     edgeID += 1
     
     return rtnGraph
Ejemplo n.º 9
0
def test_simple_undirected_graph_neighbors():
    #            e1
    # graph:  n1 -- n2
    #      e2  |  /
    #          | /
    #         n3 -- n4
    #            e3

    n1 = Node(1.0)
    n2 = Node(2.0)
    n3 = Node(3.0)
    n4 = Node(4.0)

    e1 = Edge([n1, n2], directed=False)
    e2 = Edge([n1, n3], directed=False)
    e3 = Edge([n3, n4], directed=False)
    e4 = Edge([n2, n3], directed=False)

    g1 = Graph([n1, n2, n3, n4], [e1, e2, e3, e4], directed=False)

    neighbors = g1.get_neighbors(n1)
    assert n1 not in neighbors
    assert n2 in neighbors
    assert n3 in neighbors
    assert n4 not in neighbors

    neighbors = g1.get_neighbors(n2)
    assert n1 in neighbors
    assert n2 not in neighbors
    assert n3 in neighbors
    assert n4 not in neighbors

    neighbors = g1.get_neighbors(n3)
    assert n1 in neighbors
    assert n2 in neighbors
    assert n3 not in neighbors
    assert n4 in neighbors

    neighbors = g1.get_neighbors(n4)
    assert n1 not in neighbors
    assert n2 not in neighbors
    assert n3 in neighbors
    assert n4 not in neighbors
Ejemplo n.º 10
0
def test_simple_directed_graph_neighbors():
    #            e1
    # graph:  n1 -> n2
    #      e2  |  /
    #          v /   (e4 (n2->n3))
    #         n3 -> n4
    #            e3

    n1 = Node(1.0)
    n2 = Node(2.0)
    n3 = Node(3.0)
    n4 = Node(4.0)

    e1 = Edge([n1, n2])
    e2 = Edge([n1, n3])
    e3 = Edge([n3, n4])
    e4 = Edge([n2, n3])

    g1 = Graph([n1, n2, n3, n4], [e1, e2, e3, e4])

    neighbors = g1.get_neighbors(n1)
    assert n1 not in neighbors
    assert n2 in neighbors
    assert n3 in neighbors
    assert n4 not in neighbors

    neighbors = g1.get_neighbors(n2)
    assert n1 not in neighbors
    assert n2 not in neighbors
    assert n3 in neighbors
    assert n4 not in neighbors

    neighbors = g1.get_neighbors(n3)
    assert n1 not in neighbors
    assert n2 not in neighbors
    assert n3 not in neighbors
    assert n4 in neighbors

    neighbors = g1.get_neighbors(n4)
    assert n1 not in neighbors
    assert n2 not in neighbors
    assert n3 not in neighbors
    assert n4 not in neighbors
Ejemplo n.º 11
0
def visualize_graph_2d(
    points: np.array, graph: Graph, ax, title: str = '', annotate: bool = False
):
    """
    visualize a 2D graph in a matplotlib-axis

    :param points: points that are scattered
    :param graph: graph that is plotted
    :param ax: a matplotlib axis the data is plotted on
    :param title: a title for the figure
    :param annotate: whether to annotate the nodes with their corresponding index
    :return: matplotlib axis

    Example
    ::

        from cluster.utils import visualize_graph_2d
        from cluster.cluster import SpectralClustering

        # cluster the dataset
        spectral_clustering = SpectralClustering(k=2, graph_param=5)
        spectral_clustering.cluster(points=points)
        labels = spectral_clustering.get_labels()

        # visualize the graph
        fig, ax = plt.subplots()
        visualize_graph_2d(points, spectral_clustering.get_graph(), ax, 'kNN graph with 5 neighbors')
        plt.show()

    .. plot::

        import numpy as np
        from cluster.utils import visualize_graph_2d
        from cluster.cluster import SpectralClustering

        np.random.seed(42)

        N1 = 10
        N2 = 10
        r1 = 1 / 4
        r2 = 3 / 4

        x1 = 2 * np.pi * np.random.rand(N1)
        x2 = 2 * np.pi * np.random.rand(N2)
        r1 = r1 + np.random.rand(N1) * 0.1
        r2 = r2 + np.random.rand(N2) * 0.1

        points = np.vstack([np.concatenate([r1 * np.cos(x1), r2 * np.cos(x2)]),
                            np.concatenate([r1 * np.sin(x1), r2 * np.sin(x2)])]).T

        # cluster the dataset
        spectral_clustering = SpectralClustering(k=2, graph_param=5)
        spectral_clustering.cluster(points=points)
        labels = spectral_clustering.get_labels()

        # visualize the graph
        fig, ax = plt.subplots()
        visualize_graph_2d(points, spectral_clustering.get_graph(), ax, 'kNN graph with 5 neighbors')
        plt.show()
    """
    for i in range(len(graph.get_edge_list())):
        edge = graph.get_edge_list()[i]
        left = points[edge.get_from_node().get_attribute("node_index")]
        right = points[edge.get_to_node().get_attribute("node_index")]

        ax.plot([left[0], right[0]], [left[1], right[1]], color="lightblue")

    ax.scatter(
        points[:, 0],
        points[:, 1],
        s=5,
        color="blue",
        zorder=10 * len(graph.get_edge_list()),
    )

    if annotate:
        for node in graph.get_node_list():
            idx = node.get_attribute("node_index")
            ax.annotate(idx, (points[idx, 0] + 1e-2, points[idx, 1] + 1e-2))

    ax.set_title(title)

    return ax