def get_edge_list(): cn = barbell_graph(GC.barbell_m1, GC.barbell_m2) out = GC.nx2favites(cn, 'u') f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir), 'wb', 9) f.write('\n'.join(out).encode()) f.write(b'\n') f.close() GC.cn_communities = [ {i for i in range(GC.barbell_m1)}, { i for i in range(GC.barbell_m1 + GC.barbell_m2, 2 * GC.barbell_m1 + GC.barbell_m2) } ] # only left and right communities, not the path f = gopen( expanduser("%s/contact_network_partitions.txt.gz" % GC.out_dir), 'wb', 9) f.write(str(GC.cn_communities).encode()) f.write(b'\n') f.close() GC.cn_communities = [{str(i) for i in c} for c in GC.cn_communities] return out
def get_edge_list(): cn = complete_graph(GC.num_cn_nodes) out = GC.nx2favites(cn, 'u') f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir), 'wb', 9) f.write('\n'.join(out).encode()) f.write(b'\n') f.close() return out
def get_edge_list(): cn = barabasi_albert_graph(GC.num_cn_nodes, GC.num_edges_from_new, seed=GC.random_number_seed) if GC.random_number_seed is not None: GC.random_number_seed += 1 out = GC.nx2favites(cn, 'u') f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir), 'wb', 9) f.write('\n'.join(out).encode()) f.write(b'\n') f.close() return out
def get_edge_list(): cn = newman_watts_strogatz_graph(GC.num_cn_nodes, GC.nws_k, GC.nws_prob, seed=GC.random_number_seed) if GC.random_number_seed is not None: GC.random_number_seed += 1 out = GC.nx2favites(cn, 'u') f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir), 'wb', 9) f.write('\n'.join(out).encode()) f.write(b'\n') f.close() return out
def get_edge_list(): du = GC.d_or_u == 'd' cn = random_partition_graph(GC.rpg_sizes, GC.rpg_p_in, GC.rpg_p_out, directed=du, seed=GC.random_number_seed) if GC.random_number_seed is not None: GC.random_number_seed += 1 out = GC.nx2favites(cn, GC.d_or_u) f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir),'wb',9) f.write('\n'.join(out).encode()); f.write(b'\n') f.close() f = gopen(expanduser("%s/contact_network_partitions.txt.gz" % GC.out_dir),'wb',9) f.write(str(cn.graph['partition']).encode()); f.write(b'\n') f.close() GC.cn_communities = [{str(n) for n in c} for c in cn.graph['partition']] return out
def get_edge_list(): cn = relaxed_caveman_graph(GC.cave_num_cliques, GC.cave_clique_size, GC.cave_prob, seed=GC.random_number_seed) if GC.random_number_seed is not None: GC.random_number_seed += 1 out = GC.nx2favites(cn, 'u') f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir),'wb',9) f.write('\n'.join(out).encode()); f.write(b'\n') f.close() GC.cn_communities = [{c*GC.cave_clique_size+i for i in range(GC.cave_clique_size)} for c in range(GC.cave_num_cliques)] f = gopen(expanduser("%s/contact_network_partitions.txt.gz" % GC.out_dir),'wb',9) f.write(str(GC.cn_communities).encode()); f.write(b'\n') f.close() GC.cn_communities = [{str(i) for i in c} for c in GC.cn_communities] return out
def get_edge_list(): du = GC.d_or_u == 'd' cn = fast_gnp_random_graph(GC.num_cn_nodes, GC.er_prob, directed=du, seed=GC.random_number_seed) if GC.random_number_seed is not None: GC.random_number_seed += 1 out = GC.nx2favites(cn, GC.d_or_u) f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir), 'wb', 9) f.write('\n'.join(out).encode()) f.write(b'\n') f.close() return out
def get_edge_list(): try: cn = random_degree_sequence_graph(GC.cn_degree_sequence, tries=GC.cn_tries, seed=GC.random_number_seed) except NetworkXUnfeasible: from os import chdir chdir(GC.START_DIR) assert False, "Contact network degree sequence is not graphical" except NetworkXError: from os import chdir chdir(GC.START_DIR) assert False, "NetworkX failed to produce graph after %d tries" % GC.cn_tries if GC.random_number_seed is not None: GC.random_number_seed += 1 out = GC.nx2favites(cn, 'u') f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir), 'wb', 9) f.write('\n'.join(out).encode()) f.write(b'\n') f.close() return out
def get_edge_list(): # set things up a = (GC.cng_m + 1) / (2 * GC.cng_m) probs = [0] + [ 2 * a / (i * (i + 1)) for i in range(1, GC.cng_m + 1) ] # prepend 0 to make non-zero probabilities be indices 1 through M M = [ m for m, n in enumerate( multinomial(len(GC.cng_N), probs, size=1)[0]) for _ in range(n) ] com = list() for i in range(len(M)): com.append( GC.nx2favites( barabasi_albert_graph(GC.cng_N[i], M[i], seed=GC.random_number_seed), 'u')) if GC.random_number_seed is not None: GC.random_number_seed += 1 # process disconnected BA graphs (one per community) nodes = list() node_lines = list() edge_lines = list() for i, g in enumerate(com): nodes.append(list()) node_prefix = 'COM%d' % i for l in g: if len(l) == 0 or l[0] == '#': continue parts = l.split() assert parts[0] in {'NODE', 'EDGE' }, "Invalid FAVITES edge list encountered" if parts[0] == 'NODE': name = "%s-%s" % (node_prefix, parts[1]) nodes[-1].append(name) node_lines.append("NODE\t%s\t%s" % (name, parts[2])) else: u = "%s-%s" % (node_prefix, parts[1]) v = "%s-%s" % (node_prefix, parts[2]) edge_lines.append("EDGE\t%s\t%s\t%s\t%s" % (u, v, parts[3], parts[4])) # add edges between communities possible_num_er_edges = sum(len(c) for c in nodes)**2 - sum( len(c)**2 for c in nodes) if len(nodes) == 1: num_er_edges = 0 # only 1 community else: num_er_edges = 2 * binomial( possible_num_er_edges, GC.cng_p) # multiply by 2 for bidirectionality er_edges = set() er_choice_indices = list(range(len(nodes))) while len(er_edges) != 2 * num_er_edges: i = choice(er_choice_indices) j = choice(er_choice_indices) while i == j: j = choice(er_choice_indices) u = choice(nodes[i]) v = choice(nodes[j]) er_edges.add((u, v)) er_edges.add((v, u)) edge_lines.append("EDGE\t%s\t%s\t.\tu" % (u, v)) # output final graph out = node_lines + edge_lines f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir), 'wb', 9) f.write('\n'.join(out).encode()) f.write(b'\n') f.close() return out ''' edgelists = [] for cng_i, cng in enumerate(local_cn_generators): MF.modules['ContactNetworkGenerator'] = MF.module_implementations['ContactNetworkGenerator'][cng['ContactNetworkGenerator']]['class'] for param in MF.module_implementations['ContactNetworkGenerator'][cng['ContactNetworkGenerator']]['req']: setattr(GC,param,cng[param]) MF.modules['ContactNetworkGenerator'].init() edgelists.append(MF.modules['ContactNetworkGenerator'].get_edge_list()) g = {} # g[node]['attributes' or 'edges'] GC.cn_communities = [] total_num_nodes = 0 out = [] for edgelist_i,edgelist in enumerate(edgelists): GC.cn_communities.append([]) node_prefix = "CNG%d-COM%d"%(GC.cng_community_num,edgelist_i) for line in edgelist: if len(line) == 0 or line[0] == '#': continue parts = line.split() assert parts[0] in {'NODE','EDGE'}, "Invalid FAVITES edge list encountered" if parts[0] == 'NODE': name = "%s-%s" % (node_prefix,parts[1]) assert name not in g, "Duplicate node name encountered" g[name] = {'edges':[], 'attributes':parts[2]} out.append("NODE\t%s\t%s" % (name,parts[2])) GC.cn_communities[-1].append(name) else: u = "%s-%s" % (node_prefix,parts[1]) assert u in g, "Encountered non-existant node name" v = "%s-%s" % (node_prefix,parts[2]) assert v in g, "Encountered non-existant node name" g[u]['edges'].append((v,parts[3],parts[4])) assert len(GC.cn_communities[-1]) != 0, "Encountered empty community" total_num_nodes += len(GC.cn_communities[-1]) for u in g: for v,attr,du in g[u]['edges']: out.append("EDGE\t%s\t%s\t%s\t%s" % (u,v,attr,du)) possible_across_edges = 0 for i in range(len(GC.cn_communities)-1): for j in range(i+1,len(GC.cn_communities)): possible_across_edges += len(GC.cn_communities[i])*len(GC.cn_communities[j]) num_across_edges = binomial(possible_across_edges,GC.cn_p_across) done = set() for _ in range(num_across_edges): i,j = sample(range(len(GC.cn_communities)),2) u,v = choice(GC.cn_communities[i]),choice(GC.cn_communities[j]) while (u,v) in done: i,j = sample(range(len(GC.cn_communities)),2) u,v = choice(GC.cn_communities[i]),choice(GC.cn_communities[j]) done.add((u,v)) out.append("EDGE\t%s\t%s\t.\tu" % (u,v)) f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir),'wb',9) f.write('\n'.join(out).encode()); f.write(b'\n') f.close() f = gopen(expanduser("%s/contact_network_partitions.txt.gz" % GC.out_dir),'wb',9) f.write(str(GC.cn_communities).encode()); f.write(b'\n') f.close() return out''' exit()
def get_edge_list(): # set things up a = (GC.cng_m + 1) / (2 * GC.cng_m) probs = [0] + [ 2 * a / (i * (i + 1)) for i in range(1, GC.cng_m + 1) ] # prepend 0 to make non-zero probabilities be indices 1 through M M = [ m for m, n in enumerate( multinomial(len(GC.cng_N), probs, size=1)[0]) for _ in range(n) ] com = list() for i in range(len(M)): com.append( GC.nx2favites( barabasi_albert_graph(GC.cng_N[i], M[i], seed=GC.random_number_seed), 'u')) if GC.random_number_seed is not None: GC.random_number_seed += 1 # process disconnected BA graphs (one per community) nodes = list() node_lines = list() edge_lines = list() GC.cn_communities = list() for i, g in enumerate(com): nodes.append(list()) GC.cn_communities.append(set()) node_prefix = 'COM%d' % i for l in g: if len(l) == 0 or l[0] == '#': continue parts = l.split() assert parts[0] in {'NODE', 'EDGE' }, "Invalid FAVITES edge list encountered" if parts[0] == 'NODE': name = "%s-%s" % (node_prefix, parts[1]) nodes[-1].append(name) GC.cn_communities[-1].add(name) node_lines.append("NODE\t%s\t%s" % (name, parts[2])) else: u = "%s-%s" % (node_prefix, parts[1]) v = "%s-%s" % (node_prefix, parts[2]) edge_lines.append("EDGE\t%s\t%s\t%s\t%s" % (u, v, parts[3], parts[4])) # add edges between communities possible_num_er_edges = sum(len(c) for c in nodes)**2 - sum( len(c)**2 for c in nodes) if len(nodes) == 1: num_er_edges = 0 # only 1 community else: num_er_edges = 2 * binomial( possible_num_er_edges, GC.cng_p) # multiply by 2 for bidirectionality er_edges = set() er_choice_indices = list(range(len(nodes))) while len(er_edges) != 2 * num_er_edges: i = choice(er_choice_indices) j = choice(er_choice_indices) while i == j: j = choice(er_choice_indices) u = choice(nodes[i]) v = choice(nodes[j]) er_edges.add((u, v)) er_edges.add((v, u)) edge_lines.append("EDGE\t%s\t%s\t.\tu" % (u, v)) # output final graph out = node_lines + edge_lines f = gopen(expanduser("%s/contact_network.txt.gz" % GC.out_dir), 'wb', 9) f.write('\n'.join(out).encode()) f.write(b'\n') f.close() f = gopen( expanduser("%s/contact_network_partitions.txt.gz" % GC.out_dir), 'wb', 9) f.write(str(GC.cn_communities).encode()) f.write(b'\n') f.close() return out