-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
182 lines (143 loc) · 4.94 KB
/
utils.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
import numpy as np
import sys
import time
def pdb_on_error():
import sys
def info(type, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthook__(type, value, tb)
else:
import traceback, pdb
# we are NOT in interactive mode, print the exception
traceback.print_exception(type, value, tb)
print
# then start the debugger in post-mortem mode.
# pdb.pm() # deprecated
pdb.post_mortem(tb) # more
sys.excepthook = info
class Timer:
def __init__(self):
self.timestamp = None
def start(self, duration):
self.timestamp = time.time()
self.duration = duration
def elapsed(self):
return self.timestamp + self.duration < time.time()
class P(object):
def __init__(self):
self.buff = []
self.len_so_far = 0
def print_out(self, what):
swhat = str(what)
self.buff.append(swhat)
self.len_so_far += len(swhat)
def tab(self, size):
if self.len_so_far < size:
self.print_out(' ' * (size - self.len_so_far))
def render(self):
return "".join(self.buff)
class ConfusionMatrix:
"""
source: lasagne toolkit
Simple confusion matrix class
row is the true class, column is the predicted class
"""
def __init__(self, n_classes):
self.n_classes = n_classes
self.mat = np.zeros((n_classes,n_classes),dtype='int')
def __str__(self):
return np.array_str(self.mat)
def batchAdd(self,y_true,y_pred):
assert len(y_true) == len(y_pred)
assert max(y_true) < self.n_classes
assert max(y_pred) < self.n_classes
for i in range(len(y_true)):
self.mat[y_true[i],y_pred[i]] += 1
def zero(self):
self.mat.fill(0)
def getErrors(self):
"""
Calculate differetn error types
:return: vetors of true postives (tp) false negatives (fn), false positives (fp) and true negatives (tn)
pos 0 is first class, pos 1 is second class etc.
"""
tp = np.asarray(np.diag(self.mat).flatten(),dtype='float')
fn = np.asarray(np.sum(self.mat, axis=1).flatten(),dtype='float') - tp
fp = np.asarray(np.sum(self.mat, axis=0).flatten(),dtype='float') - tp
tn = np.asarray(np.sum(self.mat)*np.ones(self.n_classes).flatten(),dtype='float') - tp - fn - fp
return tp,fn,fp,tn
def accuracy(self):
"""
Calculates global accuracy
:return: accuracy
:example: >>> conf = ConfusionMatrix(3)
>>> conf.batchAdd([0,0,1],[0,0,2])
>>> print conf.accuracy()
"""
tp, _, _, _ = self.getErrors()
n_samples = np.sum(self.mat)
return np.sum(tp) / n_samples
def sensitivity(self):
tp, tn, fp, fn = self.getErrors()
res = tp / (tp + fn)
res = res[~np.isnan(res)]
return res
def specificity(self):
tp, tn, fp, fn = self.getErrors()
res = tn / (tn + fp)
res = res[~np.isnan(res)]
return res
def positivePredictiveValue(self):
tp, tn, fp, fn = self.getErrors()
res = tp / (tp + fp)
res = res[~np.isnan(res)]
return res
def negativePredictiveValue(self):
tp, tn, fp, fn = self.getErrors()
res = tn / (tn + fn)
res = res[~np.isnan(res)]
return res
def falsePositiveRate(self):
tp, tn, fp, fn = self.getErrors()
res = fp / (fp + tn)
res = res[~np.isnan(res)]
return res
def falseDiscoveryRate(self):
tp, tn, fp, fn = self.getErrors()
res = fp / (tp + fp)
res = res[~np.isnan(res)]
return res
def F1(self):
tp, tn, fp, fn = self.getErrors()
res = (2*tp) / (2*tp + fp + fn)
res = res[~np.isnan(res)]
return res
def matthewsCorrelation(self):
tp, tn, fp, fn = self.getErrors()
numerator = tp*tn - fp*fn
denominator = np.sqrt((tp + fp)*(tp + fn)*(tn + fp)*(tn + fn))
res = numerator / denominator
res = res[~np.isnan(res)]
return res
def getMat(self):
return self.mat
def inline_print(string):
sys.stderr.write('\r\t%s' % (string))
sys.stderr.flush()
def init_logging(logger_name='XTrack'):
import logging
# Setup logging.
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
logging_format = '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
formatter = logging.Formatter(logging_format)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
logging.root = logger
def get_git_revision_hash():
import subprocess
return subprocess.check_output(['git', 'rev-parse', 'HEAD'])