Example #1
0
def test_invalid_tau2():
    with pytest.raises(nx.NetworkXError):
        n = 100
        tau1 = 1
        tau2 = 2
        mu = 0.1
        nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
Example #2
0
def test_neither_degrees_none():
    with pytest.raises(nx.NetworkXError):
        n = 100
        tau1 = 2
        tau2 = 2
        mu = -1
        nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2, average_degree=5)
Example #3
0
def test_mu_too_small():
    with pytest.raises(nx.NetworkXError):
        n = 100
        tau1 = 2
        tau2 = 2
        mu = -1
        nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
def safe_generate_lfr(num_nodes, avg_degree, min_communities, mu, tau1, tau2,
                      attempt, max_attempts):
    print('attempt {}'.format(attempt))
    if attempt >= max_attempts:
        raise Exception(
            'Maximum attempts to generate a connected LFR graph with the given settings exceeded'
        )
    G = None
    try:
        G = nx.LFR_benchmark_graph(n=num_nodes,
                                   tau1=tau1,
                                   tau2=tau2,
                                   mu=mu,
                                   average_degree=avg_degree,
                                   min_degree=None,
                                   max_degree=None,
                                   min_community=min_communities,
                                   max_community=None,
                                   tol=1e-07,
                                   max_iters=500,
                                   seed=None)
    except:
        print('generating the graph failed, trying again')
    if G is None:
        return safe_generate_lfr(num_nodes, avg_degree, min_communities, mu,
                                 tau1, tau2, attempt + 1, max_attempts)
    if nx.is_connected(G):
        return G
    else:
        print('generated graph is disconnected, trying again')
        return safe_generate_lfr(num_nodes, avg_degree, min_communities, mu,
                                 tau1, tau2, attempt + 1, max_attempts)
def generate_lfr_graph(n):
    p = 0.1
    connected = False
    tau1 = 2
    tau2 = 1.5
    mu = 0.3
    avg_degree = 4  #p * (n - 1) / 2
    max_degree = 10  #n
    while not connected:
        print("generating LFR graph failed, trying again")
        g = None
        try:
            g = nx.LFR_benchmark_graph(n=n,
                                       tau1=tau1,
                                       tau2=tau2,
                                       mu=mu,
                                       average_degree=avg_degree,
                                       min_degree=None,
                                       max_degree=max_degree,
                                       min_community=None,
                                       max_community=None,
                                       tol=1e-07,
                                       max_iters=1000,
                                       seed=None)
        except:
            print("LFR generation failed")
        if not g is None:
            connected = nx.is_connected(g)
    return g
Example #6
0
def test_both_degrees_none():
    with pytest.raises(nx.NetworkXError):
        n = 100
        tau1 = 2
        tau2 = 2
        mu = -1
        nx.LFR_benchmark_graph(n, tau1, tau2, mu)
Example #7
0
def test_generator():
    n = 250
    tau1 = 3
    tau2 = 1.5
    mu = 0.1
    G = nx.LFR_benchmark_graph(n, tau1, tau2, mu, average_degree=5,
                               min_community=20, seed=10)
    assert len(G) == 250
    C = {frozenset(G.nodes[v]['community']) for v in G}
    assert nx.community.is_partition(G.nodes(), C)
Example #8
0
def read_graph_from_file(path):
    if path.endswith(".csv"):
        edges = pd.read_csv(path)
        graph = nx.from_edgelist(edges.values.tolist())
    elif path.endswith(".gml"):
        graph = nx.read_gml(path, label=None)
    else:
        graph = nx.Graph(
            nx.LFR_benchmark_graph(1000,
                                   2,
                                   2,
                                   0.2,
                                   average_degree=10,
                                   max_degree=50))
    return graph
Example #9
0
def load_LFR(nb_nodes, tau1, tau2, mu, min_community, average_degree, seed):

    A = np.zeros((nb_nodes, nb_nodes), dtype=bool)
    ys = np.zeros(nb_nodes, dtype=int)
    G = nx.LFR_benchmark_graph(nb_nodes,
                               tau1,
                               tau2,
                               mu,
                               min_community=min_community,
                               average_degree=average_degree,
                               seed=seed)

    for node, ad in G.adjacency():
        A[node, list(ad.keys())] = True

    partitions = {frozenset(G.nodes[v]['community']) for v in G}
    for cls, points in enumerate(partitions):
        ys[list(points)] = cls

    return A, ys, G
def generate_the_network(num_nodes, tau1, tau2, mu, min_degree, max_degree):
    i = 0
    done = False
    while i < num_of_tries and done == False:
        print('i =', i)
        try:
            G = nx.LFR_benchmark_graph(num_nodes,
                                       tau1,
                                       tau2,
                                       mu,
                                       min_degree=min_degree,
                                       max_degree=max_degree)
            done = True
        except Exception as e:
            pass
        i += 1

    if done == False:
        return None

    return G
def generate_graph(num_nodes, tau1=2, tau2=5, mu=0.1, max_degree=15, average_degree=5,
                   min_community=6, max_community=30):
    """
    A function which generates a LFR benchmark graph with communities
    :param num_nodes: Number of nodes in the graph
    :param tau1: Tau1 parameter from LFR
    :param tau2: Tau2 parameter from LFR
    :param mu: Mu parameter from LFR
    :param max_degree: Maximum degree of a node
    :param average_degree: Average degree of a node
    :param min_community: Minimum number of nodes in a community
    :param max_community: Maxmimum number of nodes in a community
    :return: The graph G
    """
    G = nx.LFR_benchmark_graph(num_nodes, tau1, tau2, mu, average_degree, max_degree=max_degree,
                               min_community=min_community, max_community=max_community, seed=42)
    # Adding needed attributes used for graph model
    nx.set_node_attributes(G, 0, "status")
    nx.set_node_attributes(G, -1, "can_infect_again")
    nx.set_node_attributes(G, -1, "asymp")
    nx.set_node_attributes(G, 0, "time_in_state")
    return G
Example #12
0
def generate_the_network(n, tau1, tau2, mu, min_degree, max_degree):
    """generates the network with the given parameter using the LFR benchmark 
	method from the networkx package. This function tries 10 times to generate
	the network, if it cannot it returns None.

	Args:
		n ([int]): [number of nodes in the network]
		tau1 ([float]): [power-law exponent for degree distribution]
		tau2 ([float]): [power-law exponent for community size distribution]
		mu ([float]): [fraction of intra-community edges to each node]
		min_degree ([int]): [minimum degree of a node in the network]
		max_degree ([int]): [maximum degree of a node in the network]

	Returns:
		[nx.Graph]: [synthetic network]
	"""
    num_of_tries = 10
    successfully_built = False
    while num_of_tries > 0 and successfully_built == False:
        # print('i =', i)   # for debug purposes only!
        try:
            G = nx.LFR_benchmark_graph(n,
                                       tau1,
                                       tau2,
                                       mu,
                                       min_degree=min_degree,
                                       max_degree=max_degree)
            successfully_built = True
        except Exception as e:
            pass
        num_of_tries -= 1

    if successfully_built == False:
        return None

    return G
Example #13
0
def test_mu_too_small():
    n = 100
    tau1 = 2
    tau2 = 2
    mu = -1
    nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
def generate_lfr(mixing_param):
    return nx.LFR_benchmark_graph(n=N, tau1=3, tau2=1.1, mu=mixing_param,
                                  average_degree=10, max_degree=50, min_community=10, max_community=50, seed=10)
Example #15
0
import networkx as nx
import json
import math

for size in [100, 500, 1000, 5000, 10000, 50000, 100000, 500000, 1000000]:
    print(size)
    mu = 0.5

    for i in range(0, 30):
        try:
            G = nx.LFR_benchmark_graph(size,
                                       2.5,
                                       1.1,
                                       mu=mu,
                                       average_degree=20,
                                       max_degree=50,
                                       max_iters=5000)
        except:
            continue

        components = nx.connected_components(G)
        GC = G.subgraph(max(components, key=len))
        removed = list(filter(lambda x: not x in GC.nodes, range(0, size)))

        for node in GC.nodes:
            for r in removed:
                GC.nodes[node]["community"].discard(r)

        name = f'LFR_N{size}_ad5_mc50_mu{mu}_i{i}'

        # save G to disk
Example #16
0
# %% [markdown]
# #

from cdlib import algorithms, viz
import networkx as nx
import matplotlib.pyplot as plt
from graspy.plot import heatmap

g = nx.LFR_benchmark_graph(1000, 3, 1.5, 0.7, min_community=20, average_degree=5)
coms = algorithms.leiden(g)


def nc_to_label(coms):
    nodelist = []
    comlist = []
    com_map = coms.to_node_community_map()
    for node, assignment in com_map.items():
        assignment = assignment[0]
        nodelist.append(node)
        comlist.append(assignment)
    return nodelist, comlist


nodelist, labels = nc_to_label(coms)
adj = nx.to_numpy_array(g, nodelist=nodelist)

heatmap(adj, inner_hier_labels=labels)
Example #17
0
if args.seed is None:
    np.random.seed()
    random.seed()
else:
    np.random.seed(args.seed)
    random.seed(args.seed)

cluster_number = 1

# LFR Benchmark Graph
if args.generation[0] == 'lfr-benchmark':
    G = nx.LFR_benchmark_graph(args.n,
                               args.t1,
                               args.t2,
                               args.mu,
                               min_degree=args.min_degree,
                               max_degree=args.max_degree,
                               average_degree=args.avg_degree,
                               min_community=args.min_community,
                               max_community=args.max_community,
                               seed=args.seed)

    H = G.__class__()
    H.add_nodes_from(G)
    H.add_edges_from(G.edges)

    cluster_labels = None
    nx.set_node_attributes(H, cluster_labels, 'cluster_label')

    for (node, nodedata) in G.nodes.items():
        if H.nodes[node]['cluster_label'] is None:
            H.nodes[node]['cluster_label'] = cluster_number
Example #18
0
tau2 = 1.5  # parameter for community size distribution: must be strictly greater than 1
mu = 0.1  # faction of links between communities: must be in [0, 0.5]
avg_degree = 10  # average degree of nodes: must be in [0, num_nodes]
max_degree = 20  # maximum degree of a node in the graph: must be less than num_ndoes
pdf = np.random.uniform  # the probability distribution function to generate probability values
prob_links_ratio = 0.1  # ratio of new probabilistic links which we add to the network
swap_ratio = 0.1  # ratio of number of low probability values that are swapped with high probability values to all probability values

# create graph G via LFR, then determine its ground-truth communities
done = False
num_of_tries = 0
while not done and num_of_tries < 10:
    try:
        G = nx.LFR_benchmark_graph(num_nodes,
                                   tau1,
                                   tau2,
                                   mu,
                                   avg_degree,
                                   max_degree=max_degree)
        done = True
    except:
        num_of_tries += 1

if num_of_tries == 10:
    print("You need to change the parameters, this doesn't work.")
    exit(0)

comms = {frozenset(G.nodes[v]['community']) for v in G}
com2nodes = dict()
node2com = dict()
n = 0
for com in comms:
Example #19
0
import networkx as nx
import matplotlib.pyplot as plt
import networkx.algorithms.community as nx_comm
from sklearn.metrics.cluster import normalized_mutual_info_score

#Set up LFR graph
n = 100
tau1 = 3
tau2 = 1.5
mu = 0.1
G = nx.LFR_benchmark_graph(n,
                           tau1,
                           tau2,
                           mu,
                           min_degree=5,
                           min_community=25,
                           seed=10)

#Extract community labels for NMI score
set_comm = {frozenset(G.nodes[v]["community"]) for v in G}
comm_list = [node_set for node_set in set_comm]
color_map = list(range(0, 100))

for i, comm in enumerate(comm_list):
    for node_comm in comm:
        color_map[node_comm] = i

ground_labels = color_map

#Extract nodes list to align with community labels
node_list = []
Example #20
0
def test_invalid_tau2():
    n = 100
    tau1 = 1
    tau2 = 2
    mu = 0.1
    nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
Example #21
0
def test_mu_too_large():
    n = 100
    tau1 = 2
    tau2 = 2
    mu = 1.1
    nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
Example #22
0
def test_neither_degrees_none():
    n = 100
    tau1 = 2
    tau2 = 2
    mu = -1
    nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2, average_degree=5)
Example #23
0
def test_both_degrees_none():
    n = 100
    tau1 = 2
    tau2 = 2
    mu = -1
    nx.LFR_benchmark_graph(n, tau1, tau2, mu)