-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroc2txt.py
185 lines (142 loc) · 5.31 KB
/
roc2txt.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" TODO: docstring """
#from future import __division__
import optparse
import sys
import os
from os import path
import warnings
import shutil
import itertools
import scipy as sp
from scipy import io
import numpy as np
import pylab as pl
# ------------------------------------------------------------------------------
DEFAULT_OVERWRITE = False
DEFAULT_SHOW = False
# ------------------------------------------------------------------------------
def roc2txt(output_filename,
input_filenames,
# --
show = DEFAULT_SHOW,
overwrite = DEFAULT_OVERWRITE,
):
if path.exists(output_filename) and not overwrite:
warnings.warn("not allowed to overwrite %s" % output_filename)
return
final_test_fnames = sp.empty((0,2), dtype=str)
final_test_y = []
final_test_pred = []
sets = set([])
# -- extract data from svm outputs
print "Collecting data ..."
for fname in input_filenames:
print fname
data = io.loadmat(fname)
# test_y
test_y = data['test_y']
# for now only accept binary classifiers
if test_y.shape[1] != 2:
raise NotImplementedError("test_y.shape[1] != 2, "
"binary classifiers only!!!")
# test_fnames
test_fnames = data['test_fnames']
if len(set(test_fnames) & sets) != 0:
raise ValueError("Duplicate input detected!")
sets = sets or set(test_fnames)
test_fnames.shape = len(test_y), -1
# for now only accept same / different protocol
assert test_fnames.shape[1] == 2
assert test_y.shape[0] == len(test_fnames)
test_y = test_y[:,1]
# test_predictions
test_predictions = data['test_predictions']
# assume same / different binary protocol
assert test_predictions.shape[0] == 1
test_predictions.shape = -1
# flip sign?
person1 = path.split(test_fnames[0,0])[0]
person2 = path.split(test_fnames[0,1])[0]
if person1 == person2:
if test_y[0] > 0:
flip_sign = 1.
else:
flip_sign = -1.
elif person1 != person2:
if test_y[0] > 0:
flip_sign = -1.
else:
flip_sign = 1.
final_test_fnames = sp.concatenate((final_test_fnames, test_fnames))
final_test_y = sp.concatenate((final_test_y, flip_sign*test_y))
final_test_pred = sp.concatenate((final_test_pred, flip_sign*test_predictions))
print final_test_fnames.shape, final_test_pred.shape
# -- verify data (based on same/different protocol a-la-LFW)
print "Verifying data ..."
for y, fnames in zip(final_test_y, final_test_fnames):
names = [path.split(fname)[0] for fname in fnames]
if y > 0:
assert names[0] == names[1]
else:
assert names[0] != names[1]
# -- true and false positive rates
pos = final_test_y>0
neg = final_test_y<=0
assert final_test_y.shape == final_test_pred.shape
pmin, pmax = final_test_pred.min(), final_test_pred.max()
print final_test_pred.shape
print pmin, pmax
thresholds = sp.unique(final_test_pred[final_test_pred.argsort()])
tpr_l = []
fpr_l = []
for i, th in enumerate(thresholds[::-1]):
tpr = (final_test_pred[pos]>th).mean()
fpr = (final_test_pred[neg]>th).mean()
# correct roc curve (make it convex)
if i != 0 and tpr_l[i-1] > tpr: tpr = tpr_l[i-1]
tpr_l += [tpr]
fpr_l += [fpr]
print "npoints=", len(tpr_l)
print "Saving", output_filename
sp.savetxt(output_filename, sp.array([tpr_l, fpr_l]).T, fmt='%1.10f')
if show:
#v1 = sp.loadtxt('funneled-v1-like-roc.txt')
#pl.plot(v1[:,1], v1[:,0])
#wolf = sp.loadtxt('accv09-wolf-hassner-taigman-roc.txt')
#pl.plot(wolf[:,1], wolf[:,0])
this = sp.loadtxt(output_filename)
print this.shape
pl.plot(this[:,1], this[:,0])
pl.show()
# ------------------------------------------------------------------------------
def main():
""" TODO: docstring """
usage = ("usage: %prog [options] "
"<output_filename> "
"<input_filename1> [<input_filename2>, ... ]")
parser = optparse.OptionParser(usage=usage)
parser.add_option("--overwrite",
default=DEFAULT_OVERWRITE,
action="store_true",
help="overwrite existing file [default=%default]")
parser.add_option("--show", "-s",
default=DEFAULT_SHOW,
action="store_true",
help="plot the roc curve and show it [default=%default]")
opts, args = parser.parse_args()
if len(args) < 2:
parser.print_help()
else:
output_filename = args[0]
input_filenames = args[1:]
roc2txt(output_filename,
input_filenames,
# --
show = opts.show,
overwrite = opts.overwrite,
)
# ------------------------------------------------------------------------------
if __name__ == "__main__":
main()