def testFromIntAttribute(self):
     cl = VertexClustering.FromAttribute(self.graph, "int")
     self.assertTrue(cl.membership == list(range(10)))
     cl = VertexClustering.FromAttribute(self.graph, "int", 15)
     self.assertTrue(cl.membership == [0, 1, 0, 0, 2, 1, 3, 0, 4, 0])
     cl = VertexClustering.FromAttribute(self.graph, "int", [10, 20, 30])
     self.assertTrue(cl.membership == [0, 1, 2, 2, 1, 1, 3, 2, 1, 0])
def init_clusters(graph1, graph2, partition, rand_percentage=0.0):
    if rand_percentage >= 1 or len(partition) == 0:
        return VertexClustering(graph1, list(range(len(graph1.vs))))

    for idx, cluster in enumerate(partition):
        if partition.size(idx) > 1:
            vertices = graph2.vs(cluster)["name"]
            graph1.vs(name_in=vertices)["cluster_seed"] = idx

    cluster_length = len(partition)

    for vertex in graph1.vs(cluster_seed=None):
        vertex["cluster_seed"] = cluster_length
        cluster_length += 1

    if rand_percentage > 0:
        vertices = graph1.vs(cluster_seed_lt=len(partition))
        random_vertices = round((len(vertices) - 1) * rand_percentage)
        for v in sample(range(0, len(vertices)), random_vertices):
            vertices[v]["cluster_seed"] = cluster_length
            cluster_length += 1
    return VertexClustering.FromAttribute(graph1, attribute="cluster_seed")
 def testFromStringAttribute(self):
     cl = VertexClustering.FromAttribute(self.graph, "string")
     self.assertTrue(cl.membership == [0, 0, 0, 1, 1, 2, 2, 2, 0, 1])