예제 #1
0
def generate_graph_by_gt(after=None, before=None):
    submissions = [
        s for s in Submission.objects.filter(
            id__in=[comment.submission_id for comment in Comment.objects.all()]).prefetch_related('author')]

    graph = gt.Graph()
    graph.vp['author'] = graph.new_vp('string')
    for submission in submissions:
        authors = set()

        if submission.created_at.replace(tzinfo=None) >= after and submission.created_at.replace(tzinfo=None) <= before:
            authors.add(submission.author.name)

        authors = authors | set(
            comment.author.name for comment in Comment.objects.filter(
                submission=submission, created_at__gte=after, created_at__lte=before).prefetch_related('author'))

        s_graph = complete_graph(len(authors))
        s_graph.vertex_properties['author'] = s_graph.new_vp('string')
        s_graph.vertex_properties['author_idx'] = s_graph.new_vp('int')

        authors = list(authors)
        for author_idx, v in enumerate(s_graph.get_vertices()):
            s_graph.vertex_properties['author'][v] = authors[author_idx]
            rv = find_vertex(graph, graph.vp['author'], s_graph.vp['author'][v])
            s_graph.vertex_properties['author_idx'][v] = graph.vertex_index[rv[0]] if rv else -1

        graph, props = graph_union(graph, s_graph, s_graph.vp['author_idx'], [(graph.vp['author'], s_graph.vp['author'])])
        graph.vp['author'] = props[0]

    return graph
예제 #2
0
def add_in_graph(gInit, n, graphGen, *args):
    '''
    Add into initial input graph -gInit-, -n- disconnected graphs that are produced by the graphGen(*args) function.
    Input:
        -gInit- initial graph. If equals to None, then the output Graph will be comprised by -n- disconnected components each produced by graphGen(*args).
        -n- the number of graphs that we will add to gInit.
        -graphGen- a function that is producing a specific type of graph, e.g. create_star_graph
        -*args- the arguments with witch graphGen will be called.
        
    Example:
        Calling add_in_graph(None, 10, generation.complete_graph, 5)
        produces a graph that is comprised by 10 complete graphs of size 5 each.        
    '''

    if n <= 1 and gInit != None:
        return generation.graph_union(graphGen(*args), gInit)
    elif n <= 1 and gInit == None:
        return graphGen(*args)
    else:
        return generation.graph_union(
            graphGen(*args), add_in_graph(gInit, n - 1, graphGen, *args))
예제 #3
0
def append_lattices(g, nLatte=20, latticeSize=[5, 5]):
    def isCorner(gLatte, node):
        if gLatte.vertex(node).out_degree() == 2: return True
        else: return False

    gLatte = add_in_graph(None, nLatte, generation.lattice, latticeSize)
    gLatte.vp["late"] = gLatte.new_vertex_property("short")
    gLatte.vp["late"].a = np.ones(gLatte.num_vertices())

    gFinal = generation.graph_union(g, gLatte, internal_props=True)
    comp, hist = topology.label_components(gFinal)
    numOfCC = max(comp.a)
    assert (numOfCC == nLatte)

    bbNodes = set(np.where(gFinal.vp["bb"].a == 1)[0])
    attached = set(np.where(gFinal.vp["attachments"].a == 1)[0])
    freeBBnodes = bbNodes - attached
    if freeBBnodes < nLatte:
        warnings.warn(
            "Not enough free nodes in the erdos graph to attach all the lattices."
        )
        return None

    freeBBnodes = list(freeBBnodes)
    np.random.shuffle(freeBBnodes)
    k = 0
    for cc in range(1, numOfCC + 1):
        atNode = np.where(comp.a == cc)[0][0]
        assert (isCorner(gFinal, atNode)
                )  # The first node of a generation.lattice graph is a corner.
        gFinal.add_edge(atNode, freeBBnodes[k])
        k += 1
        gFinal.vp["attachments"].a[atNode] = 1
        gFinal.vp["attachments"].a[freeBBnodes[k]] = 1
    assert (topology.label_components(gFinal)[1][0] == gFinal.num_vertices()
            )  # gFinal must be Fully Connected.
    return gFinal
예제 #4
0
 def merge_graphs(self, g1, g2):
     self._g = graph_union(g1._g, g2._g, internal_props=True)
예제 #5
0
def erdos_circles_cliques(bbSize,
                          nCircles,
                          circleSize,
                          nCliques,
                          cliqueSize,
                          save=True):
    '''
    Makes a random (Erdos-Renyi) graph connected with circles and cliques.
    Input:
        -bbSize[0]-   how many nodes the random Graph will have
        -bbSize[1]-   how many edges the random Graph will have
        -nCliques-    how many cliques to add of size -cliqueSize-
        -nCircles-    how many circles to add of size -circleSize-    
    '''

    backbone = erdos_renyi_graph(bbSize[0],
                                 bbSize[1],
                                 directed=False,
                                 gcc=True)
    print "The random backbone graph has %d vertices and %d edges." % (
        backbone.num_vertices(), backbone.num_edges())

    assert (np.sum(topology.label_largest_component(backbone).a) ==
            backbone.num_vertices())

    if backbone.num_vertices() < nCircles + nCliques:
        warnings.warn(
            "The erdos part of the graph is too small to add the requested circles/cliques."
        )

    backbone.vp["bb"] = backbone.new_vertex_property(
        "short")  # Mark all nodes belonging in the random backbone.
    backbone.vp["bb"].a = np.ones(backbone.num_vertices())

    gCircle = add_in_graph(None, nCircles, generation.circular_graph,
                           circleSize)
    gCircle.vp["circ"] = gCircle.new_vertex_property(
        "short")  # Mark all nodes belonging in the Circles.
    gCircle.vp["circ"].a = np.ones(gCircle.num_vertices())

    gCliq = add_in_graph(None, nCliques, generation.complete_graph, cliqueSize)
    gCliq.vp["cliq"] = gCliq.new_vertex_property(
        "short")  # Mark all nodes belonging in the Cliques.
    gCliq.vp["cliq"].a = np.ones(gCliq.num_vertices())

    concat1 = generation.graph_union(backbone, gCliq, internal_props=True)
    gFinal = generation.graph_union(concat1, gCircle, internal_props=True)

    assert (sum(gFinal.vp['cliq'].a == 1) == gCliq.num_vertices() and \
            sum(gFinal.vp['circ'].a == 1) == gCircle.num_vertices())

    comp, hist = topology.label_components(
        gFinal)  # Mark to which CC every node is.
    numOfCC = max(comp.a)
    assert (numOfCC == nCircles + nCliques)

    bbNodes = np.where(gFinal.vp["bb"].a == 1)[0]
    np.random.shuffle(bbNodes)
    k = 0

    gFinal.vp["attachments"] = gFinal.new_vertex_property(
        "short"
    )  # Bookkeeping which nodes of the backbone where used to connect with the circles/cliques.

    for cc in range(1, numOfCC + 1):
        atNode = np.where(comp.a == cc)[0][
            0]  # Since all nodes of the added graphs are equivalent we can pick the 1st to make the attachment.
        gFinal.add_edge(atNode, bbNodes[k])
        k += 1
        gFinal.vp["attachments"].a[atNode] = 1
        gFinal.vp["attachments"].a[bbNodes[k]] = 1

    assert (topology.label_components(gFinal)[1][0] == gFinal.num_vertices()
            )  # gFinal must be Fully Connected

    print "The graph with the cliques and circles has in total %d vertices and %d edges." % (
        gFinal.num_vertices(), gFinal.num_edges())

    return gFinal