-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults_analysis.py
239 lines (218 loc) · 13.6 KB
/
results_analysis.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#### Preanalysis the node degree results
import json
import torch
import math
def node_degree_results(edge_dict, results, test_hits, test_mrr, split_edge, node):
if str(node) in edge_dict:
if edge_dict[str(node)] not in results:
results[edge_dict[str(node)]] = {}
results[edge_dict[str(node)]]["hits"] = [test_hits]
results[edge_dict[str(node)]]["mrr"] = [test_mrr]
results[edge_dict[str(node)]]["number"] = 1
results[edge_dict[str(node)]]["hit_num"] = [split_edge['test']['new'][node]["positive"].size(1)]
else:
results[edge_dict[str(node)]]["hits"].append(test_hits)
results[edge_dict[str(node)]]["mrr"].append(test_mrr)
results[edge_dict[str(node)]]["number"] += 1
results[edge_dict[str(node)]]["hit_num"].append(split_edge['test']['new'][node]["positive"].size(1))
else:
if 0 not in results:
results[0] = {}
results[0]["hits"] = [test_hits]
results[0]["mrr"] = [test_mrr]
results[0]["number"] = 1
results[0]["hit_num"] = [split_edge['test']['new'][node]["positive"].size(1)]
else:
results[0]["hits"].append(test_hits)
results[0]["mrr"].append(test_mrr)
results[0]["number"] += 1
results[0]["hit_num"].append(split_edge['test']['new'][node]["positive"].size(1))
return results
def result_coldwarm(results, split_num):
log_results = {}
hits_list = [f'hits@{K}' for K in [10,20,30,50]]
for this_count in results:
if int(this_count) == 0:
if '0' not in log_results:
log_results['0'] = {}
for hits_name in hits_list:
log_results['0'][hits_name] = 0.0
# log_results['0']["hits"] = 0.0
log_results['0']["mrr"] = 0.0
log_results['0']["auc"] = 0.0
log_results['0']["edge_num"] = 0
log_results['0']["node_num"] = results[this_count]["number"]
for i in range(len(results[this_count]["mrr"])):
for hits_name in hits_list:
log_results['0'][hits_name] += results[this_count][hits_name][i] * results[this_count]["hit_num"][i]
# log_results['0']["hits"] += results[this_count]["hits"][i] * results[this_count]["hit_num"][i]
log_results['0']["mrr"] += results[this_count]["mrr"][i] * results[this_count]["hit_num"][i]
log_results['0']["auc"] += results[this_count]["auc"][i]
log_results['0']["edge_num"] += results[this_count]["hit_num"][i]
elif int(this_count) > 0 and int(this_count) < (split_num+1):
if f'1-{split_num}' not in log_results:
log_results[f'1-{split_num}'] = {}
# log_results[f'1-{split_num}']["hits"] = 0.0
for hits_name in hits_list:
log_results[f'1-{split_num}'][hits_name] = 0.0
log_results[f'1-{split_num}']["mrr"] = 0.0
log_results[f'1-{split_num}']["auc"] = 0.0
log_results[f'1-{split_num}']["edge_num"] = 0
log_results[f'1-{split_num}']["node_num"] = results[this_count]["number"]
else:
log_results[f'1-{split_num}']["node_num"] += results[this_count]["number"]
for i in range(len(results[this_count]["mrr"])):
for hits_name in hits_list:
log_results[f'1-{split_num}'][hits_name] += results[this_count][hits_name][i] * results[this_count]["hit_num"][i]
# log_results[f'1-{split_num}']["hits"] += results[this_count]["hits"][i] * results[this_count]["hit_num"][i]
log_results[f'1-{split_num}']["mrr"] += results[this_count]["mrr"][i] * results[this_count]["hit_num"][i]
log_results[f'1-{split_num}']["auc"] += results[this_count]["auc"][i]
log_results[f'1-{split_num}']["edge_num"] += results[this_count]["hit_num"][i]
else:
if f'>{split_num}' not in log_results:
log_results[f'>{split_num}'] = {}
for hits_name in hits_list:
log_results[f'>{split_num}'][hits_name] = 0.0
# log_results[f'>{split_num}']["hits"] = 0.0
log_results[f'>{split_num}']["mrr"] = 0.0
log_results[f'>{split_num}']["auc"] = 0.0
log_results[f'>{split_num}']["edge_num"] = 0
log_results[f'>{split_num}']["node_num"] = results[this_count]["number"]
else:
log_results[f'>{split_num}']["node_num"] += results[this_count]["number"]
for i in range(len(results[this_count]["mrr"])):
for hits_name in hits_list:
log_results[f'>{split_num}'][hits_name] += results[this_count][hits_name][i] * results[this_count]["hit_num"][i]
# log_results[f'>{split_num}']["hits"] += results[this_count]["hits"][i] * results[this_count]["hit_num"][i]
log_results[f'>{split_num}']["mrr"] += results[this_count]["mrr"][i] * results[this_count]["hit_num"][i]
log_results[f'>{split_num}']["auc"] += results[this_count]["auc"][i]
log_results[f'>{split_num}']["edge_num"] += results[this_count]["hit_num"][i]
if "overall" not in log_results:
log_results["overall"] = {}
# log_results["overall"]["hits"] = 0.0
for hits_name in hits_list:
log_results["overall"][hits_name] = 0.0
log_results["overall"]["mrr"] = 0.0
log_results["overall"]["auc"] = 0.0
log_results["overall"]["edge_num"] = 0
log_results["overall"]["node_num"] = results[this_count]["number"]
else:
log_results["overall"]["node_num"] += results[this_count]["number"]
for i in range(len(results[this_count]["mrr"])):
for hits_name in hits_list:
log_results["overall"][hits_name] += results[this_count][hits_name][i] * results[this_count]["hit_num"][i]
# log_results["overall"]["hits"] += results[this_count]["hits"][i] * results[this_count]["hit_num"][i]
log_results["overall"]["mrr"] += results[this_count]["mrr"][i] * results[this_count]["hit_num"][i]
log_results["overall"]["auc"] += results[this_count]["auc"][i]
log_results["overall"]["edge_num"] += results[this_count]["hit_num"][i]
return log_results
def result_log(results):
log_results = {}
hits_list = [f'hits@{K}' for K in [10,20,30,50]]
for this_count in results:
if str(int(math.log2(int(this_count)+1))) not in log_results:
log_results[str(int(math.log2(int(this_count)+1)))] = {}
for hits_name in hits_list:
log_results[str(int(math.log2(int(this_count)+1)))][hits_name] = 0.0
# log_results[str(int(math.log2(int(this_count)+1)))]["hits"] = 0.0
log_results[str(int(math.log2(int(this_count)+1)))]["mrr"] = 0.0
log_results[str(int(math.log2(int(this_count)+1)))]["auc"] = 0.0
log_results[str(int(math.log2(int(this_count)+1)))]["edge_num"] = 0
log_results[str(int(math.log2(int(this_count)+1)))]["node_num"] = results[this_count]["number"]
else:
log_results[str(int(math.log2(int(this_count)+1)))]["node_num"] += results[this_count]["number"]
for i in range(len(results[this_count]["mrr"])):
for hits_name in hits_list:
log_results[str(int(math.log2(int(this_count)+1)))][hits_name] += results[this_count][hits_name][i] * results[this_count]["hit_num"][i]
# log_results[str(int(math.log2(int(this_count)+1)))]["hits"] += results[this_count]["hits"][i] * results[this_count]["hit_num"][i]
log_results[str(int(math.log2(int(this_count)+1)))]["mrr"] += results[this_count]["mrr"][i] * results[this_count]["hit_num"][i]
log_results[str(int(math.log2(int(this_count)+1)))]["auc"] += results[this_count]["auc"][i]
log_results[str(int(math.log2(int(this_count)+1)))]["edge_num"] += results[this_count]["hit_num"][i]
if "overall" not in log_results:
log_results["overall"] = {}
for hits_name in hits_list:
log_results["overall"][hits_name] = 0.0
# log_results["overall"]["hits"] = 0.0
log_results["overall"]["mrr"] = 0.0
log_results["overall"]["auc"] = 0.0
log_results["overall"]["edge_num"] = 0
log_results["overall"]["node_num"] = results[this_count]["number"]
else:
log_results["overall"]["node_num"] += results[this_count]["number"]
for i in range(len(results[this_count]["mrr"])):
for hits_name in hits_list:
log_results["overall"][hits_name] += results[this_count][hits_name][i] * results[this_count]["hit_num"][i]
# log_results["overall"]["hits"] += results[this_count]["hits"][i] * results[this_count]["hit_num"][i]
log_results["overall"]["mrr"] += results[this_count]["mrr"][i] * results[this_count]["hit_num"][i]
log_results["overall"]["auc"] += results[this_count]["auc"][i]
log_results["overall"]["edge_num"] += results[this_count]["hit_num"][i]
return log_results
def merge_results(log_results, all_results, overall_results):
hits_list = [f'hits@{K}' for K in [10,20,30,50]]
for key in log_results.keys():
if key != "overall":
if key not in all_results:
all_results[key] = {}
all_results[key]["node_num"] = int(log_results[key]["node_num"])
all_results[key]["edge_num"] = int(log_results[key]["edge_num"])
for hits_name in hits_list:
all_results[key][hits_name] = [log_results[key][hits_name]/ log_results[key]["edge_num"]]
# all_results[key]['hits'] = [log_results[key]["hits"]/ log_results[key]["edge_num"]]
all_results[key]['mrr'] = [log_results[key]["mrr"]/ log_results[key]["edge_num"]]
all_results[key]['auc'] = [log_results[key]["auc"]/ log_results[key]["node_num"]]
else:
for hits_name in hits_list:
all_results[key][hits_name].append(log_results[key][hits_name]/ log_results[key]["edge_num"])
all_results[key]['auc'].append(log_results[key]["auc"]/ log_results[key]["node_num"])
all_results[key]['mrr'].append(log_results[key]["mrr"]/ log_results[key]["edge_num"])
else:
if "mrr" not in overall_results:
overall_results['node_num'] = int(log_results[key]["node_num"])
overall_results["edge_num"] = int(log_results[key]["edge_num"])
for hits_name in hits_list:
overall_results[hits_name] = [log_results[key][hits_name]/ log_results[key]["edge_num"]]
# all_results[key]['hits'] = [log_results[key]["hits"]/ log_results[key]["edge_num"]]
overall_results['mrr'] = [log_results[key]["mrr"]/ log_results[key]["edge_num"]]
overall_results['auc'] = [log_results[key]["auc"]/ log_results[key]["node_num"]]
else:
for hits_name in hits_list:
overall_results[hits_name].append(log_results[key][hits_name]/ log_results[key]["edge_num"])
overall_results['auc'].append(log_results[key]["auc"]/ log_results[key]["node_num"])
overall_results['mrr'].append(log_results[key]["mrr"]/ log_results[key]["edge_num"])
return all_results, overall_results
def sp_results_coldwarm(these_results, split_num):
all_results = {}
overall_results = {}
for results in these_results:
log_results = result_coldwarm(results, split_num)
all_results, overall_results = merge_results(log_results, all_results, overall_results)
return all_results, overall_results
def sp_results_log(these_results):
all_results = {}
overall_results = {}
for results in these_results:
log_results = result_log(results)
all_results, overall_results = merge_results(log_results, all_results, overall_results)
return all_results, overall_results
def save_results(file, all_results, overall_results):
for key in sorted(all_results.keys()):
print_out_str = str(key) + ", " + str(all_results[key]["node_num"]) + ", " + str(all_results[key]["edge_num"])
for this_K in [10,20,30,50]:
this_res_hits = torch.tensor(all_results[key][f'hits@{this_K}'])
print_out_str += f', hits@{this_K}: ' + f'{this_res_hits.mean():.2f} ± {this_res_hits.std():.2f}'
this_res_mrr = torch.tensor(all_results[key]['mrr'])
print_out_str += ", mrr: " + f'{this_res_mrr.mean():.2f} ± {this_res_mrr.std():.2f}'
this_res_auc = torch.tensor(all_results[key]['auc'])
print_out_str += ", auc: " + f'{this_res_auc.mean():.2f} ± {this_res_auc.std():.2f}'
print(print_out_str+"\n")
file.write(print_out_str+"\n")
print_out_str = "Overall, " + str(overall_results["node_num"]) + ", " + str(overall_results["edge_num"])
for this_K in [10,20,30,50]:
this_res_hits = torch.tensor(overall_results[f'hits@{this_K}'])
print_out_str += f', hits@{this_K}: ' + f'{this_res_hits.mean():.2f} ± {this_res_hits.std():.2f}'
this_res_mrr = torch.tensor(overall_results['mrr'])
print_out_str += ", mrr: " + f'{this_res_mrr.mean():.2f} ± {this_res_mrr.std():.2f}'
this_res_auc = torch.tensor(overall_results['auc'])
print_out_str += ", auc: " + f'{this_res_auc.mean():.2f} ± {this_res_auc.std():.2f}'
print(print_out_str+"\n")
file.write(print_out_str+"\n")