def print_evaluate(A_matrix,sampled_graph): stats = utils.compute_graph_statistics(sampled_graph) sps_truth = utils.sp(A_matrix) sps_gen = utils.sp(sampled_graph) stats['sp_emd']= utils.emd(sps_truth,sps_gen) s = utils.specGap(A_matrix) stats['spec_gap']=(s-utils.specGap(sampled_graph))**2 print(stats)
def gen_graphs(X_from_walks, target): X = genExpected_fromWalks(X_from_walks, target.sum()) print(X) sps_truth = utils.sp(target) sp_emds = [] sgl2s = [] for i in range(20): sampled_graph = gnp(X) sps_gen = utils.sp(sampled_graph) sp_emd = utils.emd(sps_truth, sps_gen) sgl2 = (utils.specGap(target) - utils.specGap(sampled_graph))**2 sp_emds.append(sp_emd) sgl2s.append(sgl2) return sp_emds, sgl2s
def global_comp(A, X, sps_truth, spec): sp_emds = [] specs = [] ccs = [] for i in range(100): sampled_graph = gnp(X) sps_gen = utils.sp(sampled_graph) sp_emds.append(utils.emd(sps_truth, sps_gen)) specs.append((spec - utils.specGap(sampled_graph))**2) ccs.append(3 * utils.statistics_triangle_count(sampled_graph) / utils.statistics_claw_count(sampled_graph)) stats = {} stats['shortest path emds'] = sp_emds stats['spec gaps'] = specs stats['clustering coefficients'] = ccs print('Shortest path') print(np.mean(sp_emds)) print(np.std(sp_emds)) print('Specs') print(np.mean(specs)) print(np.std(specs)) print('Clusterig Coefficient') print(np.mean(ccs)) print(np.std(ccs)) print('True CC') tc = 3 * utils.statistics_triangle_count( np.array(A)) / utils.statistics_claw_count(np.array(A)) print(tc) print('ABS diff') print(tc - np.mean(ccs)) return stats
def global_comp(A,X,sps_truth,spec): sp_emds = [] specs = [] for i in range(100): sampled_graph = gnp(X) sps_gen = utils.sp(sampled_graph) sp_emds.append(utils.emd(sps_truth,sps_gen)) specs.append((spec-utils.specGap(sampled_graph))**2) return np.mean(sp_emds), np.std(sp_emds), np.mean(specs), np.std(specs)
def spec_gap_largest_component(A): G = nx.from_numpy_matrix(A) Gc = max(nx.connected_component_subgraphs(G), key=len) A = np.asarray(nx.adjacency_matrix(Gc).todense()) return utils.specGap(A)
test = test_edges + test_nonEdges G = nx.from_numpy_matrix(A_full) L = nx.normalized_laplacian_matrix(G).todense() eig_vals, eig_vecs = linalg.eig(L) eig_list = zip(eig_vals, np.transpose(eig_vecs)) eig_list.sort(key=lambda x: x[0]) u = np.asarray([u_i.real for u_i in eig_list[-2][1]])[0][0] truth = utils.compute_graph_statistics(np.asarray(A_full)) f = open('plots/truth.txt', "w") f.write(str(truth)) f.close() truth_spec = utils.specGap(A_full) #train_spec = utils.specGap(A_matrix) spec_gaps_w = [] spec_gaps_e = [] spec_gaps_w_raw = [] spec_gaps_e_raw = [] spec_gaps_a_raw = [] spec_gaps_a = [] spec_gaps_w_std = [] spec_gaps_e_std = [] spec_gaps_a_std = [] spec_gaps_truth = [] spec_gaps_train = [] spec_gaps_w_emp = []
trueScores.append(A[int(u)][int(v)]) predScores.append(X[int(u)][int(v)]) auc = roc_auc_score(trueScores, predScores) ap = average_precision_score(trueScores, predScores) return auc, ap G = nx.read_gml('../data/football.gml') _A_obs = nx.adjacency_matrix(G) A_matrix = _A_obs.todense() valid_edges = np.loadtxt('plots/football_val_edges.txt').tolist() valid_nonEdges = np.loadtxt('plots/football_val_non_edges.txt').tolist() valid = valid_edges + valid_nonEdges sps_truth = utils.sp(A_matrix) spec_truth = utils.specGap(A_matrix) X = np.loadtxt( 'plots/football_walk_em/trainingIteration_1200_expectedGraph.txt') print('Random Walks Size 16') global_comp(A_matrix, X, sps_truth, spec_truth) print(auc_p(X, np.asarray(A_matrix), valid)) X = np.loadtxt( 'plots/football_edge_em_nbs/trainingIteration_3200_expectedGraph.txt') print('Random Walks Size 2 No BS adj') global_comp(A_matrix, X, sps_truth, spec_truth) print(auc_p(X, np.asarray(A_matrix), valid)) X = np.loadtxt( 'plots/football_edge_em_adj_bs/trainingIteration_1200_expectedGraph.txt') print('Random Walks Size 2 BS adj') global_comp(A_matrix, X, sps_truth, spec_truth)
def evaluate_expected(X, valid, test, A_full): spec = utils.specGap(X) auc_valid, ap_valid = auc_p(X, np.asarray(A_full), valid) auc_test, ap_test = auc_p(X, np.asarray(A_full), test) return auc_test, auc_valid, ap_test, ap_valid, spec
start = 1 train_path = 'plots/' + name + '_training.txt' target_path = 'plots/' + name + '.txt' A_matrix = np.loadtxt(train_path) print('verifying correct barbell edge should be one') print(A_matrix[3][99]) print(A_matrix[80][130]) A_full = np.loadtxt(target_path) N = A_full.shape[0] G = nx.from_numpy_matrix(A_full) truth_spec = utils.specGap(A_full) start = 1 step = 400 k = maxIter / step #initialize all params barbell_edge_density_1 = [] barbell_edge_density_2 = [] num_edges_crossing_1 = [] num_edges_crossing_2 = [] num_edges_crossing_3 = []
target_path = 'plots/'+name+'.txt' A_matrix = np.loadtxt(train_path) A_full = np.loadtxt(target_path) N = A_full.shape[0] valid_edges = np.loadtxt('plots/'+name+'_val_edges.txt').tolist() valid_nonEdges = np.loadtxt('plots/'+name+'_val_non_edges.txt').tolist() valid = valid_edges+valid_nonEdges test_edges = np.loadtxt('plots/'+name+'_test_edges.txt').tolist() test_nonEdges = np.loadtxt('plots/'+name+'_test_non_edges.txt').tolist() test = test_edges+test_nonEdges G = nx.from_numpy_matrix(A_full) truth_spec = utils.specGap(A_full) start = 1 step = 400 k=int(maxIter)/step max_patience = 5 patience_spec = max_patience patience_auc = max_patience patience_ep = max_patience patience_vc = max_patience best_spec = -10 best_auc = 0 best_ep = 0 best_vc = 0
valid = valid_edges + valid_nonEdges G = nx.from_numpy_matrix(A_matrix) L = nx.normalized_laplacian_matrix(G).todense() eig_vals, eig_vecs = linalg.eig(L) eig_list = zip(eig_vals, np.transpose(eig_vecs)) eig_list.sort(key=lambda x: x[0]) u = np.asarray([u_i.real for u_i in eig_list[-2][1]])[0][0] truth = utils.compute_graph_statistics(np.asarray(A_matrix)) f = open('plots/truth.txt', "w") f.write(str(truth)) f.close() truth_spec = utils.specGap(A_full) train_spec = utils.specGap(A_matrix) truth_cc = utils.cc(A_full) cc_emd_combo = [] cc_emd_reg = [] cc_emd_fmm = [] cc_emd_combo_std = [] cc_emd_reg_std = [] cc_emd_fmm_std = [] k = 11 for i in range(1, k): print(i) X_c = np.loadtxt( 'plots/barbell_sameDensity/barbell_combo_mixed/trainingIteration_{}_expectedGraph.txt'
A_full = np.loadtxt(target_path) N = A_full.shape[0] valid_edges = np.loadtxt('plots/' + name + '_val_edges.txt').tolist() valid_nonEdges = np.loadtxt('plots/' + name + '_val_non_edges.txt').tolist() valid = valid_edges + valid_nonEdges test_edges = np.loadtxt('plots/' + name + '_test_edges.txt').tolist() test_nonEdges = np.loadtxt('plots/' + name + '_test_non_edges.txt').tolist() test = test_edges + test_nonEdges G_true = nx.from_numpy_matrix(A_full) truth_spec = utils.spectrum(A_full) truth_spec_gap = utils.specGap(A_full) start = 5 step = 400 k = maxIter / step #initialize all params aucs_test_1 = [] aucs_valid_1 = [] aps_test_1 = [] aps_valid_1 = [] specs_1 = [] aucs_test_2 = []