-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_metrics.py
241 lines (206 loc) · 5.03 KB
/
error_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 20 15:36:37 2011
@ author: Sat Kumar Tomer
@ author's webpage: http://civil.iisc.ernet.in/~satkumar/
@ author's email id: [email protected]
@ author's website: www.ambhas.com
A libray with Python functions for calculations of
micrometeorological parameters and some miscellaneous
utilities.
functions:
pc_bias : percentage bias
apb : absolute percent bias
rmse : root mean square error
mae : mean absolute error
bias : bias
NS : Nash-Sutcliffe Coefficient
L: likelihood estimation
correlation: correlation
"""
# import required modules
import numpy as np
from random import randrange
import matplotlib.pyplot as plt
from scipy.stats import kendalltau
def filter_nan(s, o):
"""
this functions removed the data from simulated and observed data
whereever the observed data contains nan
this is used by all other functions, otherwise they will produce nan as
output
"""
if np.sum(~np.isnan(s * o)) >= 1:
data = np.array([s.flatten(), o.flatten()])
data = np.transpose(data)
data = data[~np.isnan(data).any(1)]
s = data[:, 0]
o = data[:, 1]
return s, o
def kendalltau_nan(s, o):
"""
kendall's tau
input:
s: simulated
o: observed
output:
kendall's tau
"""
s, o = filter_nan(s, o)
return kendalltau(s, o)[0]
def pc_bias(s, o):
"""
Percent Bias
input:
s: simulated
o: observed
output:
pc_bias: percent bias
"""
s, o = filter_nan(s, o)
return 100.0 * sum(s - o) / sum(o)
def apb(s, o):
"""
Absolute Percent Bias
input:
s: simulated
o: observed
output:
apb_bias: absolute percent bias
"""
s, o = filter_nan(s, o)
return 100.0 * sum(abs(s - o)) / sum(o)
def rmse(s, o):
"""
Root Mean Squared Error
input:
s: simulated
o: observed
output:
rmses: root mean squared error
"""
s, o = filter_nan(s, o)
return np.sqrt(np.mean((s - o) ** 2))
def mae(s, o):
"""
Mean Absolute Error
input:
s: simulated
o: observed
output:
maes: mean absolute error
"""
s, o = filter_nan(s, o)
return np.mean(abs(s - o))
def bias(s, o):
"""
Bias
input:
s: simulated
o: observed
output:
bias: bias
"""
s, o = filter_nan(s, o)
return np.mean(s - o)
def NS(s, o):
"""
Nash Sutcliffe efficiency coefficient
input:
s: simulated
o: observed
output:
ns: Nash Sutcliffe efficient coefficient
"""
s, o = filter_nan(s, o)
return 1 - sum((s - o) ** 2) / sum((o - np.mean(o)) ** 2)
def L(s, o, N=5):
"""
Likelihood
input:
s: simulated
o: observed
output:
L: likelihood
"""
s, o = filter_nan(s, o)
return np.exp(-N * sum((s - o) ** 2) / sum((o - np.mean(o)) ** 2))
def correlation(s, o):
"""
correlation coefficient
input:
s: simulated
o: observed
output:
correlation: correlation coefficient
"""
s, o = filter_nan(s, o)
if s.size == 0:
corr = np.NaN
else:
corr = np.corrcoef(o, s)[0, 1]
return corr
def index_agreement(s, o):
"""
index of agreement
Willmott (1981, 1982)
input:
s: simulated
o: observed
output:
ia: index of agreement
"""
s, o = filter_nan(s, o)
ia = 1 - (np.sum((o - s) ** 2)) / (np.sum(
(np.abs(s - np.mean(o)) + np.abs(o - np.mean(o))) ** 2))
return ia
def agreement_coefficient(s, o):
"""
agreement coefficient
An Agreement Coefficient for Image Comparison by Lei Ji and Kevin Gallo
input:
s: simulated
o: observed
output:
ac: agreement coefficient
"""
s, o = filter_nan(s, o)
sbar = np.mean(s)
obar = np.mean(o)
ac = 1 - (np.sum((s - o) ** 2)) / np.sum(
(np.abs(sbar - obar) + np.abs(s - sbar)) * (
np.abs(sbar - obar) + np.abs(o - obar)))
return ac
def KGE(s, o):
"""
Kling-Gupta Efficiency
input:
s: simulated
o: observed
output:
kge: Kling-Gupta Efficiency
cc: correlation
alpha: ratio of the standard deviation
beta: ratio of the mean
"""
s, o = filter_nan(s, o)
cc = correlation(s, o)
alpha = np.std(s) / np.std(o)
beta = np.sum(s) / np.sum(o)
kge = 1 - np.sqrt((cc - 1) ** 2 + (alpha - 1) ** 2 + (beta - 1) ** 2)
return kge, cc, alpha, beta
def assimilation_eff(assimilated, simulated, observed):
"""
Assimilation efficiency (Aubert et al., 2003)
Input:
assimilated: assimilated flow
simulated: simulated flow
observed: observed flow
Output:
Eff
"""
s, o = filter_nan(simulated, observed)
a, o = filter_nan(assimilated, observed)
Eff = 100 * (1 - np.sum((a - o) ** 2) / np.sum((s - o) ** 2))
return Eff