-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalcroc.py
140 lines (115 loc) · 3.9 KB
/
calcroc.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
import glob
import os.path
import argparse
import subprocess
import csv
import math
import platform
import os.path
import extractfts
import datetime
from pathlib import Path
import numpy as np
import matplotlib.pyplot as pp
import sklearn.metrics as metrics
from sklearn.metrics import accuracy_score,precision_score, recall_score
def perf_measure(y_actual, y_hat):
TP = 0
FP = 0
TN = 0
FN = 0
for i in range(len(y_hat)):
if y_actual[i]==y_hat[i]==1:
TP += 1
if y_hat[i]==1 and y_actual[i]!=y_hat[i]:
FP += 1
if y_actual[i]==y_hat[i]==0:
TN += 1
if y_hat[i]==0 and y_actual[i]!=y_hat[i]:
FN += 1
return(TP, FP, TN, FN)
def main():
parser = argparse.ArgumentParser(description='psnr calculation parser.')
parser.add_argument('--infile', type=str, help='select directory', default='psnrresult_total.csv')
parser.add_argument('--feature', type=str, help='select directory', default='psnr')
args = parser.parse_args()
infile = args.infile
feature = args.feature
if infile == None:
print("select psnr result csv file")
return
scores = []
lables = []
if infile != None:
with open(infile) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
#print(row['psnr'], row['target'])
scores.append( float(row[feature]))
lables.append( 1- int(row['target']))
smax = float(max(scores))
#ascores = np.array(scores) / smax
ascores = np.array(scores)
alable = np.array(lables)
afpr, atpr, thresholds = metrics.roc_curve(alable, ascores)
arocauc = metrics.auc(afpr, atpr)
pp.title("Receiver Operating Characteristic")
pp.xlabel("False Positive Rate(1 - Specificity)")
pp.ylabel("True Positive Rate(Sensitivity)")
pp.xscale("log")
pp.plot(afpr, atpr, "b", label="(AUC = %0.2f)" % arocauc)
pp.xlim([0.0, 0.001])
#pp.plot([0, 0.1], [0, 1], "y--")
#pp.plot([0, 1], [0, 1], "r--")
pp.legend(loc="lower right")
pp.show()
pp.figure()
pp.plot(1.0 - atpr, thresholds, marker='*', label='tpr')
pp.plot(afpr, thresholds, marker='o', label='fpr')
pp.legend()
pp.xlim([0, 1])
pp.ylim([0, 1])
pp.xlabel('thresh')
pp.ylabel('far/fpr')
pp.title(' thresh - far/fpr')
pp.show()
for threval in np.arange(0.0001, 0.1, 0.0001):
predictval = (ascores > threval)
TP, FP, TN, FN = perf_measure(alable, predictval)
FACC = (TP + TN) / float(len(alable))
TPR = TP / float(TP + FN)
FPR = FP / float(FP + TN)
print("Threshold: %.5f Accuracy :%.5f TPR:%.5f FPR:%.5f" % (threval, FACC, TPR, FPR))
'''
FACC = accuracy_score(alable, predictval)
FFAR = precision_score(alable, predictval)
FTPR = recall_score(alable, predictval)
#ftar = precision_score(alable, predictval)
#ffar = recall_score(alable, predictval)
#print("Threshold: %.5f Accuracy : %.5f FAR: %.5f FRR: %.5f" %(threval ,FACC, FFAR,FFRR))
'''
print("task complete!")
'''
import argparse
import csv
from sklearn.metrics import fbeta_score, classification_report, confusion_matrix
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--infile", default="accuracy0211034802.csv", help="csv file to calculate confusion matrix.")
args = parser.parse_args()
infile = args.infile
label = []
ypred = []
with open(infile, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
#label.append(0)
label.append(int(row['target']))
ypred.append(int(row['infertarget']))
print(f'classification report:')
print(classification_report(label, ypred))
print(f'confusion matrix:')
print(confusion_matrix(label, ypred))
'''
if __name__== "__main__":
main()