def graph_worker_oneshot(inputlist, queue, print_queue): for duty in inputlist: name = duty[0] G = duty[1] algo = duty[2] param = duty[3] A = nx.to_numpy_matrix(G) x, l = eg.eigen_centrality(A) eg.info("Setup completed") start_time = time.time() if algo == "spectre": m = float(param) n = nm.shape(A)[0] B = eg.sample_simm_matrix2(A, int(round(n * m))) H = None algo += str(m) elif algo == "traj": H = traj.random_graph(G) elif algo == "sah": dists = param.split(",") H = sah.random_graph(G, dists[0], dists[1]) elif algo == "modularity": m = float(param) n = nm.shape(A)[0] B = eg.modularity_clone_matrix(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B) while nx.is_isomorphic(G, H): B = eg.modularity_clone_matrix(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B) algo += str(m) elif algo == "25k": H = twok.gen_25k_graph(G) elif algo == "sbm": H = sbm.generate(G) eg.info("Graph Generated") gc.collect() stat = get_statistics1(G, H, time.time() - start_time, fraction=0.1) s = algo + "," + name + "," + str(len(G.nodes())) for el in stat: s += "," + str(el) print_queue.put(s) print_queue.put("\n") gc.collect()
def modularity_clone_graph(G, m=0.9): names = G.nodes() A = nx.to_numpy_matrix(G, nodelist=names) n = nm.shape(A)[0] B = modularity_clone_matrix(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B, names) return H
def graph_worker_oneshot(inputlist, queue, print_queue): for duty in inputlist: name = duty[0] G = duty[1] algo = duty[2] param = duty[3] names = G.nodes() names.sort(key=int) A = nx.to_numpy_matrix(G, nodelist=names) eg.info("Setup completed") start_time = time.time() if algo == "modularity": m = float(param) n = nm.shape(A)[0] B = eg.modularity_clone_matrix(A, int(round(n*m))) H = gu.simm_matrix_2_graph(B, names) while nx.is_isomorphic(G, H): B = eg.modularity_clone_matrix(A, int(round(n*m))) H = gu.simm_matrix_2_graph(B, names) algo += str(m) elif algo == "traj": H = traj.random_graph(G) trnames = H.nodes() mapping = {trnames[i]: names[i] for i in range(len(names))} H = nx.relabel_nodes(H, mapping) elif algo == "25k": H = twok.gen_25k_graph(G, names) elif algo == "sbm": H = sbm.generate(G, names) eg.info("Graph Generated " + name) gc.collect() stat = get_statistics(G, H, time.time()-start_time, fraction=0.1) s = name + "," + str(len(G.nodes())) + "," + algo for el in stat: s += "," + str(el) print_queue.put(s) print_queue.put("\n") gc.collect()
def modgraph(G, level): A = nx.to_numpy_matrix(G) n = nm.shape(A)[0] B = eg.modularity_clone_matrix(A, level) H = gu.simm_matrix_2_graph(B) # while nx.is_isomorphic(G, H): # B = eg.modularity_clone_matrix(A, level) # H = gu.simm_matrix_2_graph(B) return H
def get_statistics(G, H, A, B, x, l, duration): eg.info("Computing statistics...") if not H: eg.info("Building networkx graph...") H = gu.simm_matrix_2_graph(B) eg.info("Computing centrality...") y, m = (-1, -1) #eg.eigen_centrality(B, maxiter=100000) # nx.write_weighted_edgelist(H, "pgp_spectre0.9_" + # str(random.randint(0, 99999999)) + ".edges") eg.info("Computing centrality distance...") eigen_err = -1 #eg.vec_dist(x, y) eg.info("Computing clustering ratio...") clust_err = gu.average_clustering(A) / gu.average_clustering(B) # clust_err = nm.average(nx.clustering(G).values()) /\ # nm.average(nx.clustering(H).values()) eg.info("Computing lambda ratio...") lambda_err = -1 #abs(l / m) eg.info("Computing degree correlation...") degree_corr = gu.correlation(gu.get_degrees(A), gu.get_degrees(B)) eg.info("Check connectivity...") if nx.is_connected(H): conn = 1 else: conn = 0 eg.info("Distance distribution correlation...") distance_dist_corr = -1 #gu.correlate_dist_dict(gu.node_distance_dist(A), # gu.node_distance_dist(B)) eg.info("Degree betweenness correlation...") degree_bet_corr = -1 #gu.correlate_dist_dict(gu.get_degree_betweeness(A), # gu.get_degree_betweeness(B)) eg.info("K-coreness correlation...") kcore_corr = -1 #gu.correlate_dist_dict(gu.get_kcoreness(A), # gu.get_kcoreness(B)) eg.info("Common neighbourhood correlation...") common_neigh_corr = -1 #gu.correlate_dist_dict(gu.get_common_neigh_dist(A), # gu.get_common_neigh_dist(B)) eg.info("Modularity ratio...") Gm = gu.norm_modularity(G) Hm = gu.norm_modularity(H) modularity_ratio = Gm[0] / Hm[0] partition_ratio = Gm[1] / float(Hm[1]) eg.info("Avg neighbourhood degree correlation...") avg_neigh_deg_corr = -1 #gu.correlate_dist_dict(gu.get_avg_neighbour_degree(A), # gu.get_avg_neighbour_degree(B)) eg.info("Done with stats") return (eigen_err, degree_corr, clust_err, lambda_err, distance_dist_corr, degree_bet_corr, kcore_corr, common_neigh_corr, modularity_ratio, partition_ratio, avg_neigh_deg_corr, conn, duration)
def entropy_analysis(G): with open("entropy.data", "w") as doc: doc.write("graph,alpha,modratio,entropy,denanonratio\n") for e in G: name = e[0] g = e[1] # gu.connect_components(g) # WARNING for alpha in [0.1, 0.3, 0.5, 0.7, 0.9]: names = g.nodes() A = nx.to_numpy_matrix(g, nodelist=names) n = min(A.shape) for i in range(10): B = truncated_mod_approx(A, int(round(n*alpha))) H = gu.simm_matrix_2_graph(sample_mod_matrix(B), names) gu.connect_components(H) modratio = (community.modularity( community.best_partition(H), H) / community.modularity( community.best_partition(g), g)) entropy = mat_entropy(B) deanonratio = deanon_ratio(g, H) doc.write(name + "," + str(alpha) + "," + str(modratio) + "," + str(entropy) + "," + str(deanonratio) + "\n")
def get_statistics2(G, H, A, B, x, l): eg.info("Computing statistics...") if not H: eg.info("Building networkx graph...") H = gu.simm_matrix_2_graph(B) gu.connect_components(H) eg.info("Computing centrality...") y, m = eg.eigen_centrality(B, maxiter=100000) eg.info("Computing lambda ratio...") lambda_err = abs(l / m) eg.info("Computing centrality distance...") eigen_err = eg.vec_dist(x, y) inputs = [("avg_neigh_deg_corr", A, B), ("mod_ratio", G, H), ("comm_neigh_corr", A, B), ("kcore_corr", A, B), ("deg_bet_corr", A, B), ("dist_dist", A, B), ("degree_corr", G, H), ("clust_ratio", G, H)] mets = parallelism.launch_workers(inputs, stat_worker, inputs_per_worker=1, parallelism=4) res = {} for el in mets: res.update(el) eg.info("Check connectivity...") if nx.is_connected(H): conn = 1 else: conn = 0 eg.info("Done with stats") return (eigen_err, res['degree_corr'], res['clust_ratio'], lambda_err, res['dist_dist'], res['deg_bet_corr'], res['kcore_corr'], res['comm_neigh_corr'], res['mod_ratio'], res['avg_neigh_deg_corr'], conn)
def graph_worker_oneshot(inputlist, queue, print_queue): for duty in inputlist: name = duty[0] G = duty[1] algo = duty[2] param = duty[3] A = nx.to_numpy_matrix(G) # x, l = eg.eigen_centrality(A) eg.info("Setup completed") start_time = time.time() if algo == "1k": H = nx.random_degree_sequence_graph((nx.degree(G).values())) # B = nx.to_numpy_matrix(H) elif algo == "2k": joint_degrees = nx.algorithms.mixing.degree_mixing_dict(G) H = joint_degree_graph(joint_degrees) # B = nx.to_numpy_matrix(H) elif algo == "eig": precision = float(param) # B = eg.build_matrix(x, l, precision) B = eg.generate_matrix(x, l * x, precision, gu.get_degrees(A)) H = None algo += str(precision) elif algo == "modeig": precision = float(param) B = eg.synthetic_modularity_matrix(A, precision) H = None algo += str(precision) elif algo == "spectre": m = float(param) n = nm.shape(A)[0] B = eg.sample_simm_matrix2(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B) while nx.is_isomorphic(G, H): B = eg.sample_simm_matrix2(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B) algo += str(m) elif algo == "laplacian": m = float(param) n = nm.shape(A)[0] B = eg.laplacian_clone_matrix(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B) while nx.is_isomorphic(G, H): B = eg.sample_simm_matrix2(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B) algo += str(m) elif algo == "modspec": m = float(param) n = nm.shape(A)[0] B = eg.modspec_clone_matrix(A, int(round(n * m))) H = None algo += str(m) elif algo == "franky": m = float(param) n = nm.shape(A)[0] B = eg.franky_clone_matrix(A, int(round(n * m))) H = None algo += str(m) elif algo == "modularity": m = float(param) n = nm.shape(A)[0] B = eg.modularity_clone_matrix(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B) while nx.is_isomorphic(G, H): B = eg.modularity_clone_matrix(A, int(round(n * m))) H = gu.simm_matrix_2_graph(B) algo += str(m) elif algo == "25k": test25 = Estimation() gen25 = Generation() test25.load_graph("", graph=G) test25.calcfull_CCK() test25.calcfull_JDD() gen25.set_JDD(test25.get_JDD('full')) gen25.set_KTRI(test25.get_KTRI('full')) gen25.construct_triangles_2K() gen25.mcmc_improved_2_5_K(error_threshold=0.05) H = gen25.G # B = nx.to_numpy_matrix(H) eg.info("Graph Generated") stat = get_statistics1(G, H, time.time() - start_time) s = algo + "," + name + "," + str(len(G.nodes())) for el in stat: s += "," + str(el) print_queue.put(s) print_queue.put("\n") gc.collect()
partiton_ratio = [] d_pcc = [] for i in range(10): count += 1 if count <= 10: #G = nx.read_weighted_edgelist("D:/software/python/spectral_graph_forge-master/spectral_graph_forge-master/toy.edges") #G = nx.read_weighted_edgelist("C:/Users/Administrator/Desktop/lee/ego-Facebook/facebook/3980.edges") #G = nx.read_weighted_edgelist("C:/Users/Administrator/Desktop/lee/ego-Facebook/facebook/348.edges") G = nx.karate_club_graph() #G=nx.generators.davis_southern_women_graph() #G = nx.generators.florentine_families_graph() A = nx.to_numpy_matrix(G) B_matrix = modularity_clone_matrix(A, 6) #B_matrix =entropy_analysis.truncated_mod_approx(A, 6) #B_1=entropy_analysis.sample_mod_matrix(B_matrix) B = simm_matrix_2_graph(B_matrix, None) print("***************%dth running result***************" % (i + 1)) print "***************now is the modularity of the original Graph B" m_b = kl_modularity.maximum_modularity(B)[0] print "***************now is the modularity of the original Graph G" m_g = kl_modularity.maximum_modularity(G)[0] print "***************this is the modularity ratio of the original Graph G and new graph B" print float(m_b / m_g) m_bg_result.append(float(m_b / m_g)) print "##########now is the clustering########## " c_g = graph_utils.clustering(A) c_b = graph_utils.clustering(B_matrix) print "this is the clustering of the original Graph G and new graph B" print c_b print c_g