-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculate_scores.py
63 lines (59 loc) · 2.74 KB
/
calculate_scores.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
from sklearn.metrics import roc_auc_score, average_precision_score
import numpy as np
from itertools import product
def calculate_auc_aupr(y_pred, y_true):
y_pred = np.array(y_pred, dtype=float)
y_true = np.array(y_true, dtype=int)
auc = roc_auc_score(y_true=y_true, y_score=y_pred)
aupr = average_precision_score(y_true=y_true, y_score=y_pred)
return auc, aupr
partition = False
rewired = False
multiple_runs = True
if partition:
result_file = open("partitions/all_results.tsv", "w")
result_file.write("Dataset\tTrain\tTest\tAUC\tAUPR\n")
for dataset in ["du","guo","huang", "richoux", "pan", "dscript"]:
print(f"########## Dataset: {dataset} ##########")
for train in ["both", "0"]:
for test in ["0", "1"]:
if train == "0" and test == "0":
continue
y_pred = []
y_true = []
with open(f"partitions/{dataset}_train_{train}_test_{test}.txt", "r") as f:
for line in f:
y_pred.append(line.strip().split(" ")[0])
y_true.append(line.strip().split(" ")[1])
print(f"******* Train: {train}, test: {test} *******")
auc, aupr = calculate_auc_aupr(y_pred, y_true)
print(f"AUC: {round(auc, 4)}, AUPR: {round(aupr, 4)}")
result_file.write(f'{dataset}\t{train}\t{test}\t{round(auc, 4)}\t{round(aupr, 4)}\n')
result_file.close()
else:
if rewired:
folder = 'rewired'
datasets = ["du", "guo", "huang", "pan", "richoux_regular", "richoux_strict", "dscript"]
elif multiple_runs:
folder = 'multiple_runs'
datasets = ["guo", "huang"]
settings = ["original", "rewired"]
seeds = ["17612", "29715", "30940", "31191", "42446", "50495", "60688", "7413", "75212", "81645"]
datasets = [f"{setting}_{dataset}_{seed}" for dataset, seed, setting in product(datasets, seeds, settings)]
else:
folder = 'original'
datasets = ["du", "guo", "huang", "pan", "richoux_regular", "richoux_strict", "gold_standard", "dscript"]
result_file = open(f"{folder}/all_results.tsv", "w")
result_file.write("Dataset\tAUC\tAUPR\n")
for dataset in datasets:
print(f"########## Dataset: {dataset} ##########")
y_pred = []
y_true = []
with open(f"{folder}/{dataset}_results.txt", "r") as f:
for line in f:
y_pred.append(line.strip().split(" ")[0])
y_true.append(line.strip().split(" ")[1])
auc, aupr = calculate_auc_aupr(y_pred, y_true)
print(f"AUC: {round(auc, 4)}, AUPR: {round(aupr, 4)}")
result_file.write(f'{dataset}\t{round(auc, 4)}\t{round(aupr, 4)}\n')
result_file.close()