-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_increasing_edge_removal.py
127 lines (96 loc) · 5.39 KB
/
get_increasing_edge_removal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from pathlib import Path
import sys
import os
import numpy as np
from problem.spectral_subgraph_localization import find_voting_majority, edgelist_to_adjmatrix
from experiments_ssl_data_term import balanced_acc, f1, solution_graph, graph_edit_distance, use_graph_edit_distance_generator, enforce_cardinality_constraint_by_spectrum, spectrum_from_graph, spectrum_square_diff
from problem.dijkstra import DijkstraSolution
import torch
import networkx as nx
graph = 'football'
per = '0.1'
folder_suffix = '9'
experiments_to_make = 30
if len(sys.argv) >= 2:
graph = sys.argv[1]
if len(sys.argv) >= 3:
per = sys.argv[2]
if len(sys.argv) >= 4:
folder_suffix = sys.argv[3]
if len(sys.argv) >= 5:
experiments_to_make = int(sys.argv[4])
if __name__ == '__main__':
rootdir = f'experiments_final{folder_suffix}/{graph}/{per}'
directories = []
for file in os.listdir(rootdir):
d = os.path.join(rootdir, file)
if os.path.isdir(d):
directories.append(d)
directories.sort()
for folder in directories:
with open(f'{folder}/votes') as votes_file, \
open(f'{folder}/ground_truth') as gt_file:
*_, per, lr = folder.split('/')
per = float(per)
lr = float(lr)
ext = str(int(per*100))+"_"+str(int(lr))
edgefile = './data/'+graph+'/'+str(int(per*100))+'/1/'+graph+'_'+ext+'.txt'
A1=edgelist_to_adjmatrix(edgefile)
G=nx.from_numpy_array(A1)
A = torch.tensor(nx.to_numpy_array(G))
n = len(G.nodes())
gt_string = gt_file.read().replace('])','').replace('\n','')
gt = gt_string.split('][')[0]
gt = gt.replace('[','').replace(']','').replace('array(','').split(', ')
gt = [float(x) for x in gt]
gt_indices = [i for i, res in enumerate(gt) if res == 0]
_G = G.copy()
Q = _G.subgraph(gt_indices)
ref_spectrum = spectrum_from_graph(Q)
length_of_query = len(Q.nodes())
thresholds = [0.2, 0.3, 0.4]
votes_string = votes_file.read().replace('])', '').replace('\n', '')
votes_list = np.zeros(n)
for tensor in votes_string.split(']['):
tensor = tensor.replace('[','').replace(']','').replace('tensor(','')
tensor = tensor.split(', ')
tensor = [float(x) for x in tensor]
tensor = np.array(tensor)
votes_list += tensor
print(votes_list)
# Recompute solutions!
votes = torch.tensor(votes_list)
# Find solutions for standard voting
for threshold in thresholds:
v = find_voting_majority(votes, experiments_to_make, threshold)
v_balanced_accuracy = balanced_acc(gt, v.clone().detach().numpy())
v = enforce_cardinality_constraint_by_spectrum(G, v, ref_spectrum)
S = solution_graph(G, v)
cc_v_spectrum = spectrum_from_graph(S)
cc_v_spectrum_diff = spectrum_square_diff(ref_spectrum, cc_v_spectrum)
cc_v_balanced_accuracy = balanced_acc(gt, v)
Path(f"{folder}/increasing_edge_removal").mkdir(parents=True, exist_ok=True)
# Write it!
f = open(f"{folder}/increasing_edge_removal/balanced_accuracy_{threshold}.txt", "w")
f.write(str([v_balanced_accuracy]))
f = open(f"{folder}/increasing_edge_removal/cc_balanced_accuracy_{threshold}.txt", "w")
f.write(str([cc_v_balanced_accuracy]))
f = open(f"{folder}/increasing_edge_removal/cc_spectrum_diff_{threshold}.txt", "w")
f.write(str([cc_v_spectrum_diff]))
# Find solutions for neighborhood
for threshold in thresholds:
dijkstra = DijkstraSolution(A, votes, experiments_to_make, "cubic", threshold, "constant", length_of_query)
v = dijkstra.solution()
v_balanced_accuracy = balanced_acc(gt, v.clone().detach().numpy())
v = enforce_cardinality_constraint_by_spectrum(G, v, ref_spectrum)
S = solution_graph(G, v)
cc_v_spectrum = spectrum_from_graph(S)
cc_v_spectrum_diff = spectrum_square_diff(ref_spectrum, cc_v_spectrum)
cc_v_balanced_accuracy = balanced_acc(gt, v)
# Write it!
f = open(f"{folder}/increasing_edge_removal/n_balanced_accuracy_{threshold}.txt", "w")
f.write(str([v_balanced_accuracy]))
f = open(f"{folder}/increasing_edge_removal/cc_n_balanced_accuracy_{threshold}.txt", "w")
f.write(str([cc_v_balanced_accuracy]))
f = open(f"{folder}/increasing_edge_removal/cc_n_spectrum_diff_{threshold}.txt", "w")
f.write(str([cc_v_spectrum_diff]))