-
Notifications
You must be signed in to change notification settings - Fork 11
/
metrics.py
171 lines (153 loc) · 4.57 KB
/
metrics.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
"""
Various metrics for evaluating models trained on SEVIR
"""
import tensorflow as tf
"""
Standard contingincy table-based metrics used in forecast verification
https://www.nws.noaa.gov/oh/rfcdev/docs/Glossary_Verification_Metrics.pdf
"""
def probability_of_detection(y_true,y_pred,threshold):
"""
Inputs:
-------
y_true: [N,L,L,D]
y_pred: [N,L,L,D]
threshold: [D,]
Outputs
-------
pod=hits/(hits+misses) averaged over the D channels
"""
return tf.reduce_mean(run_metric_over_channels(y_true,y_pred,threshold,_pod))
def success_rate(y_true,y_pred,threshold):
"""
a.k.a 1 - (false alarm rate)
Inputs:
-------
y_true: [N,L,L,D]
y_pred: [N,L,L,D]
threshold: [D,]
Outputs
-------
sucr=hits/(hits+false_alarms) averaged over the D channels
"""
return tf.reduce_mean(run_metric_over_channels(y_true,y_pred,threshold,_sucr))
def critical_success_index(y_true,y_pred,threshold):
"""
Inputs:
-------
y_true: [N,L,L,D]
y_pred: [N,L,L,D]
threshold: [D,]
Outputs
-------
pod=hits/(hits+misses+false_alarms) averaged over the D channels
"""
return tf.reduce_mean(run_metric_over_channels(y_true,y_pred,threshold,_csi))
def BIAS(y_true,y_pred,threshold):
"""
Computes the 2^( mean(log BIAS/log 2) )
Inputs:
-------
y_true: [N,L,L,D]
y_pred: [N,L,L,D]
threshold: [D,]
Outputs
-------
pod=(hits+false_alarms)/(hits+misses) pow(2)-log-averaged over the D channels
"""
logbias = tf.math.log(run_metric_over_channels(y_true,y_pred,threshold,_bias))/tf.math.log(2.0)
return tf.math.pow( 2.0, tf.reduce_mean(logbias))
def run_metric_over_channels(y_true,y_pred,threshold,metric):
"""
Inputs:
-------
y_true: [N,L,L,D]
y_pred: [N,L,L,D]
threshold: [D,]
Outputs
-------
[D,] tensor of metrics computed over each channel
"""
# permute channels to first dim to work with tf.map_fn
elems = (tf.transpose(y_true,(3,0,1,2)),
tf.transpose(y_pred,(3,0,1,2)),
threshold)
# Average over channels
return tf.map_fn(metric,elems,dtype=tf.float32)
def _pod(X):
"""
Single channel version of probability_of_detection
Inputs:
-------
tuple X = (y_true,y_pred,T) where
y_true: [N,L,L]
y_pred: [N,L,L]
T: [1,]
"""
y_true,y_pred,T=X
t,p=_threshold(y_true,y_pred,T)
hits = tf.reduce_sum(t*p)
misses = tf.reduce_sum( t*(1-p) )
return (hits+1e-6)/(hits+misses+1e-6)
def _sucr(X):
"""
Single channel version of success_rate
Inputs:
-------
tuple X = (y_true,y_pred,T) where
y_true: [N,L,L]
y_pred: [N,L,L]
T: [1,]
"""
y_true,y_pred,T=X
t,p=_threshold(y_true,y_pred,T)
hits = tf.reduce_sum(t*p)
fas = tf.reduce_sum( (1-t)*p )
return (hits+1e-6)/(hits+fas+1e-6)
def _csi(X):
"""
Single channel version of csi
Inputs:
-------
tuple X = (y_true,y_pred,T) where
y_true: [N,L,L]
y_pred: [N,L,L]
T: [1,]
"""
y_true,y_pred,T=X
t,p=_threshold(y_true,y_pred,T)
hits = tf.reduce_sum(t*p)
misses = tf.reduce_sum( t*(1-p) )
fas = tf.reduce_sum( (1-t)*p )
return (hits+1e-6)/(hits+misses+fas+1e-6)
def _bias(X):
"""
Single channel version of csi
Inputs:
-------
tuple X = (y_true,y_pred,T) where
y_true: [N,L,L]
y_pred: [N,L,L]
T: [1,]
"""
y_true,y_pred,T=X
t,p=_threshold(y_true,y_pred,T)
hits = tf.reduce_sum(t*p)
misses = tf.reduce_sum( t*(1-p) )
fas = tf.reduce_sum( (1-t)*p )
return (hits+fas+1e-6)/(hits+misses+1e-6)
def _threshold(X,Y,T):
"""
Returns binary tensors t,p the same shape as X & Y. t = 1 whereever
X > t. p =1 wherever Y > t. p and t are set to 0 whereever EITHER
t or p are nan. This is useful for counts that don't involve correct
rejections.
"""
t=tf.math.greater_equal(X, T)
t=tf.dtypes.cast(t, tf.float32)
p=tf.math.greater_equal(Y, T)
p=tf.dtypes.cast(p, tf.float32)
is_nan = tf.math.logical_or(tf.math.is_nan(X),tf.math.is_nan(Y))
t = tf.where(is_nan,tf.zeros_like(t,dtype=tf.float32),t)
p = tf.where(is_nan,tf.zeros_like(p,dtype=tf.float32),p)
return t,p