Example #1
0
def sbm(U, q, n, p, weighted=None, edges_constructor=add_edges_exact_sparse, pOut=None):
    U = set(U)
    g = networkx.Graph()

    # Pick our communities from the universe.
    communities = makeCommunities(U, q, n)

    # Add all nodes from the (new) universe, then add all of their edges.
    g.add_nodes_from(U)
    for nodes in communities:
        #add_edges(g, nodes, p, weighted=weighted)
        #add_edges_cm(g, nodes, p, weighted=weighted)
        #add_edges_exact(g, nodes, p, weighted=weighted)
        edges_constructor(g, nodes, p, weighted=weighted)

    if weighted:
        for a, b in g.edges_iter():
            newweight = 1. - product(1.-x for x in g.edge[a][b]['weights'])
            g.edge[a][b]['weight'] = newweight
    # Set the planted communities:
    nxutil.cmtyInit(g)
    for c, nodes in enumerate(communities):
        nxutil.cmtyAddFromList(g, c, nodes)
    # external edges?
    if pOut:
        #add_edges_out(g=g, p=pOut, g_layers=(g,), weighted=weighted,
        #              edges_constructor=edges_constructor)
        add_edges_out_sparse(g=g, p=pOut, g_layers=(g,), weighted=weighted,
                      edges_constructor=edges_constructor)
    return g
Example #2
0
def overlap_communities(g, n_min=1):
    """Return graph with communities set to all pairwise overlap of
    given graph communities.

    n_min = minimum size of overlap to be returned."""
    g_new = g.copy()
    nxutil.cmtyInit(g_new)

    cmtys = nxutil.communities(g)
    cmtys_list = list(cmtys)

    overlap_cmtys = { }
    overlap_cmty_i = 0
    print cmtys_list
    for i, c1 in enumerate(cmtys_list):
        c1nodes = cmtys[c1]
        for j, c2 in enumerate(cmtys_list[i+1:]):
            c2nodes = cmtys[c2]
            newCmtyNodes = c1nodes & c2nodes
            print c1, c2, len(c1nodes), len(c2nodes), len(newCmtyNodes)
            if len(newCmtyNodes) > n_min:
                overlap_cmtys[overlap_cmty_i] = newCmtyNodes
                overlap_cmty_i += 1
                nxutil.cmtyAddFromList(g_new, overlap_cmty_i, newCmtyNodes)
    return g_new
Example #3
0
def overlap_communities(g, n_min=1):
    """Return graph with communities set to all pairwise overlap of
    given graph communities.

    n_min = minimum size of overlap to be returned."""
    g_new = g.copy()
    nxutil.cmtyInit(g_new)

    cmtys = nxutil.communities(g)
    cmtys_list = list(cmtys)

    overlap_cmtys = {}
    overlap_cmty_i = 0
    print cmtys_list
    for i, c1 in enumerate(cmtys_list):
        c1nodes = cmtys[c1]
        for j, c2 in enumerate(cmtys_list[i + 1:]):
            c2nodes = cmtys[c2]
            newCmtyNodes = c1nodes & c2nodes
            print c1, c2, len(c1nodes), len(c2nodes), len(newCmtyNodes)
            if len(newCmtyNodes) > n_min:
                overlap_cmtys[overlap_cmty_i] = newCmtyNodes
                overlap_cmty_i += 1
                nxutil.cmtyAddFromList(g_new, overlap_cmty_i, newCmtyNodes)
    return g_new
Example #4
0
def make_overlap_test(N_U, q1, n1, p1, q2, n2, p2, pU):
    """

    N_U: number of nodes in the universe.
    q1, q2: sizes of level1 and level2 communities.
    n1, n2: number of nodes in level1 and level2 communities.
    p1, p2: edge densities
    pU: background connection probability.
    """
    U = set(range(N_U))
    g = networkx.Graph()

    #n1 = 25
    #q1 = 40
    #p1 = .95
    level1 = [ random.sample(U, n1) for _ in range(q1) ]
    U = set().union(*level1)
    print sum(len(x) for x in level1), [ len(x) for x in level1 ]
    print len(U)

    g.add_nodes_from(U)

    for points in level1:
        add_edges(g, points, p1)

    #level2 = None
    #n2 = 100
    #q2 = 10
    #p2 = .65
    #level2 = [ random.sample(U, size2) for _ in range(q2) ]
    level2 = partition_nodes(U, q2, n2)

    for points in level2:
        add_edges(g, points, p2)

    add_edges(g, list(g.nodes()), pU)

    g1 = g.copy()
    nxutil.cmtyInit(g1)
    for c, nodes in enumerate(level1):
        nxutil.cmtyAddFromList(g1, c, nodes)


    g2 = g.copy()
    nxutil.cmtyInit(g2)
    for c, nodes in enumerate(level2):
        nxutil.cmtyAddFromList(g2, c, nodes)

    return g, g1, g2
Example #5
0
def make_overlap_test(N_U, q1, n1, p1, q2, n2, p2, pU):
    """

    N_U: number of nodes in the universe.
    q1, q2: sizes of level1 and level2 communities.
    n1, n2: number of nodes in level1 and level2 communities.
    p1, p2: edge densities
    pU: background connection probability.
    """
    U = set(range(N_U))
    g = networkx.Graph()

    #n1 = 25
    #q1 = 40
    #p1 = .95
    level1 = [random.sample(U, n1) for _ in range(q1)]
    U = set().union(*level1)
    print sum(len(x) for x in level1), [len(x) for x in level1]
    print len(U)

    g.add_nodes_from(U)

    for points in level1:
        add_edges(g, points, p1)

    #level2 = None
    #n2 = 100
    #q2 = 10
    #p2 = .65
    #level2 = [ random.sample(U, size2) for _ in range(q2) ]
    level2 = partition_nodes(U, q2, n2)

    for points in level2:
        add_edges(g, points, p2)

    add_edges(g, list(g.nodes()), pU)

    g1 = g.copy()
    nxutil.cmtyInit(g1)
    for c, nodes in enumerate(level1):
        nxutil.cmtyAddFromList(g1, c, nodes)

    g2 = g.copy()
    nxutil.cmtyInit(g2)
    for c, nodes in enumerate(level2):
        nxutil.cmtyAddFromList(g2, c, nodes)

    return g, g1, g2
Example #6
0
def sbm(U,
        q,
        n,
        p,
        weighted=None,
        edges_constructor=add_edges_exact_sparse,
        pOut=None):
    U = set(U)
    g = networkx.Graph()

    # Pick our communities from the universe.
    communities = makeCommunities(U, q, n)

    # Add all nodes from the (new) universe, then add all of their edges.
    g.add_nodes_from(U)
    for nodes in communities:
        #add_edges(g, nodes, p, weighted=weighted)
        #add_edges_cm(g, nodes, p, weighted=weighted)
        #add_edges_exact(g, nodes, p, weighted=weighted)
        edges_constructor(g, nodes, p, weighted=weighted)

    if weighted:
        for a, b in g.edges_iter():
            newweight = 1. - product(1. - x for x in g.edge[a][b]['weights'])
            g.edge[a][b]['weight'] = newweight
    # Set the planted communities:
    nxutil.cmtyInit(g)
    for c, nodes in enumerate(communities):
        nxutil.cmtyAddFromList(g, c, nodes)
    # external edges?
    if pOut:
        #add_edges_out(g=g, p=pOut, g_layers=(g,), weighted=weighted,
        #              edges_constructor=edges_constructor)
        add_edges_out_sparse(g=g,
                             p=pOut,
                             g_layers=(g, ),
                             weighted=weighted,
                             edges_constructor=edges_constructor)
    return g
Example #7
0
def sbm_incomplete(U, q, n, p, weighted=None,
                   edges_constructor=None):
    """Create a SBM graph from a universe of nodes, possibly
    excluding some of the nodes."""
    U = set(U)
    g = networkx.Graph()

    # Pick our communities from the universe.  Note that the size of
    # the universe decreases.
    # is n constant, or does it change for all communities?
    if isinstance(n, (int, float)):
        communities = [ random.sample(U, n) for _ in range(q) ]
    else:
        communities = [ random.sample(U, int(round(_))) for _ in n ]
    U = set().union(*communities)
    #print sum(len(x) for x in communities), [ len(x) for x in communities ]
    #print len(U)

    # Add all nodes from the (new) universe, then add all of their edges.
    g.add_nodes_from(U)
    # is p constant for all communities, or per-community?
    if isinstance(p, (int, float)):
        for nodes in communities:
            edges_constructor(g, nodes, p, weighted=weighted)
    else:
        for nodes, p in zip(communities, p):
            edges_constructor(g, nodes, p, weighted=weighted)

    if weighted:
        for a, b in g.edges_iter():
            newweight = 1. - product(1.-x for x in g.edge[a][b]['weights'])
            g.edge[a][b]['weight'] = newweight

    # Set the planted communities:
    nxutil.cmtyInit(g)
    for c, nodes in enumerate(communities):
        nxutil.cmtyAddFromList(g, c, nodes)
    return g
Example #8
0
def sbm_incomplete(U, q, n, p, weighted=None, edges_constructor=None):
    """Create a SBM graph from a universe of nodes, possibly
    excluding some of the nodes."""
    U = set(U)
    g = networkx.Graph()

    # Pick our communities from the universe.  Note that the size of
    # the universe decreases.
    # is n constant, or does it change for all communities?
    if isinstance(n, (int, float)):
        communities = [random.sample(U, n) for _ in range(q)]
    else:
        communities = [random.sample(U, int(round(_))) for _ in n]
    U = set().union(*communities)
    #print sum(len(x) for x in communities), [ len(x) for x in communities ]
    #print len(U)

    # Add all nodes from the (new) universe, then add all of their edges.
    g.add_nodes_from(U)
    # is p constant for all communities, or per-community?
    if isinstance(p, (int, float)):
        for nodes in communities:
            edges_constructor(g, nodes, p, weighted=weighted)
    else:
        for nodes, p in zip(communities, p):
            edges_constructor(g, nodes, p, weighted=weighted)

    if weighted:
        for a, b in g.edges_iter():
            newweight = 1. - product(1. - x for x in g.edge[a][b]['weights'])
            g.edge[a][b]['weight'] = newweight

    # Set the planted communities:
    nxutil.cmtyInit(g)
    for c, nodes in enumerate(communities):
        nxutil.cmtyAddFromList(g, c, nodes)
    return g