def tester(): # G = nx.Graph() # g1 = nx.complete_graph(100) # g2 = nx.complete_graph(100) # A = nx.disjoint_union(g1,g2) # second_layer_clique_1 = r.sample(set(A.nodes),100) # second_layer_clique_2 = set(A.nodes).difference(second_layer_clique_1) # #second_layer_clique_1 = [0,1,2,3,4,5,6,7,29,28,27,26,25,24,23] # #second_layer_clique_2 = [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22] # for i in second_layer_clique_1: # for j in second_layer_clique_1: # if i != j: # A.add_edge(i,j) # for i in second_layer_clique_2: # for j in second_layer_clique_2: # if i != j: # A.add_edge(i,j) # G = A.copy() size = 500 num_comms = 5 comm_size = int(size / num_comms) comms = [int(i / (size / num_comms)) for i in range(size)] results = [] for b in range(0, 3): for a in range(1, 11): g1 = gen_sbm(size, num_comms, a / 10, b / 10, probabilities=True) g2 = gen_sbm(size, num_comms, a / 10, b / 10, probabilities=True) #g3 = gen_sbm(size,num_comms,1,0,probabilities=True) g_sbm.label_graph(g1.nxgraph, "A", comms) g_sbm.label_graph(g2.nxgraph, "B", comms) # g_sbm.draw_community_graph(g1.nxgraph,show=True) # g_sbm.draw_community_graph(g2.nxgraph,show=True) new_g = g_sbm.merge_graphs([g1.nxgraph, g2.nxgraph]) new_eg = eg.Experiment_Graph("merge_i", nxgraph=new_g) mixer, likely = model_recover(new_eg.nxgraph, layers=2, layer_width=5, runs=10, display=False, merge_graph=new_g) results.append( [a / 10, b / 10, recovery_NMI(mixer, new_g), likely]) print(results[-1]) print(results)
def gen_sbm(N, K, a, b, probabilities=False): am = np.random.uniform(size=(N, N)) C = int(N / K) if not probabilities: a = a / C b = b / (N - C) for r in range(K): for s in range(r + 1): slice = am[r * C:(r + 1) * C, s * C:(s + 1) * C] if r == s: am[r * C:(r + 1) * C, s * C:(s + 1) * C] = np.where(slice < a, 1.0, 0.0) else: am[r * C:(r + 1) * C, s * C:(s + 1) * C] = np.where(slice < b, 1.0, 0.0) am = np.tril(am, -1) + np.tril(am, -1).T return eg.Experiment_Graph("sbm", am=am)
def infomap_partitioner(graph, pmin, pmax, runs=5): structure = graph.get_structure("infomap", add=True) partition = graph.partition(structure, "infomap") # graph.draw_graph_communities(partition) # plt.show() unique = np.unique(partition) partitions = [[] for i in range(len(unique))] for i in range(graph.N): partitions[partition[i]].append(i) graph_partitions = [] ng = graph.nxgraph for part in partitions: subgraph = nx.convert_node_labels_to_integers(ng.subgraph(part)) graph_partitions.append(eg.Experiment_Graph("part", nxgraph=subgraph)) return graph_partitions, partitions
def sbm_partitioner(graph, pmin, pmax, runs=5): if "hybrid_partition" in graph.structures: structure = graph.structures["hybrid_partition"] partition = graph.partition(structure, "sbm") elif pmax == None and pmin == None and "sbm" in graph.structures: structure = graph.best_SBM()[0] partition = graph.partition(structure, "sbm") graph.structures["hybrid_partition"] = structure else: g = graph.gtgraph best = np.Inf for i in range(runs): structure = gt.minimize_blockmodel_dl(g, deg_corr=True, B_min=pmin, B_max=pmax) if structure.entropy() < best: partition = graph.partition(structure, "dc_sbm") for i in range(runs): structure = gt.minimize_blockmodel_dl(g, B_min=pmin, B_max=pmax) if structure.entropy() < best: partition = graph.partition(structure, "sbm") graph.structures["hybrid_partition"] = structure # graph.draw_graph_communities(partition) # plt.show() unique = np.unique(partition) partitions = [[] for i in range(len(unique))] for i in range(graph.N): partitions[partition[i]].append(i) graph_partitions = [] ng = graph.nxgraph for part in partitions: subgraph = nx.convert_node_labels_to_integers(ng.subgraph(part)) graph_partitions.append(eg.Experiment_Graph("part", nxgraph=subgraph)) return graph_partitions, partitions
def main(): print("Running Graph Greedy as main, tests follow:") graph = nx.barabasi_albert_graph(100, 4) test_graph = eg.Experiment_Graph("test", nxgraph=graph) def num_nbrs(group, graph): neighbors = [[i for i in graph.nxgraph.neighbors(ind)] for ind in group] total_unique = {nbr for nblist in neighbors for nbr in nblist} return len(total_unique) res = greedy(test_graph, num_nbrs, [3, 4, 5], submodular=False, progress=True) print("Supermodular", res) res = greedy(test_graph, num_nbrs, [3, 4, 5], submodular=True, progress=True) print("Submodular", res)
def main(): print("Running Connection Walker as main, tests follow:") bagraph = nx.barabasi_albert_graph(100, 3) ergraph = nx.erdos_renyi_graph(100, .03) dcsbm = gen.gen_dc_sbm(500, 5, 5, 1, 2, 2, 25, probabilities=False) graph = eg.Experiment_Graph("test", nxgraph=ergraph) graph = dcsbm def num_nbrs(group, graph): neighbors = [[i for i in graph.nxgraph.neighbors(ind)] for ind in group] total_unique = {nbr for nblist in neighbors for nbr in nblist} return len(total_unique) avg_deg = graph.M / graph.N c = min(.4 / avg_deg, 1.0) k = 7 IC = exp_inf_wrapper("IC", {"seed_set": None, "c": c}, trials=5) IC_res = exp_inf_wrapper("IC", {"seed_set": None, "c": c}, trials=100) degree_seq = [graph.nxgraph.degree(node) for node in range(graph.N)] med_deg = np.median(degree_seq) t_bottom = 1 / (2 * med_deg) t_top = .5 t1 = t_bottom + .5 * (t_top - t_bottom) t2 = t1 * 1.5 S_LT = exp_inf_wrapper("S_LT", { "seed_set": None, "t1": t1, "t2": t2, "type": "relative" }, trials=1) LT = exp_inf_wrapper("LT", { "seed_set": None, "r_threshold": t2, "type": "relative" }, trials=1) search_process = S_LT test_process = S_LT s = time.time() res = connection_builder(graph, search_process, [25, 50], params={ "sol_ratio": 1, "descent_rate": .75, "coverings": 1, "update_period": 20, "min_pop": 10 }, progress=False, animate=False) e = time.time() print(res) print("Connection Build:", test_process(res[0][-1], graph), "in", round(e - s, 1)) # Greedy s = time.time() res = gg.greedy(graph, search_process, [25, 50], submodular=False) e = time.time() print("Greedy:", test_process(res[0][-1], graph), "in", round(e - s, 1)) fig = plt.figure() plt.imshow(graph.adj_mat.toarray(), cmap="magma") plt.show() # Top Deg top = temp_grab(graph, "top" + str(k), top_K, [graph, deg, k]) print("Top deg:", round(test_process(top, graph), 1)) # Rand rand = r.sample(range(graph.N), k) print("Rand:", round(test_process(rand, graph), 1))