-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
215 lines (184 loc) · 6.82 KB
/
evaluation.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
from typing import Callable
import numpy as np
from epyt_flow.metrics import running_mse as epytflow_running_mse
from sklearn.metrics import root_mean_squared_error, r2_score, mean_absolute_error
def evaluate_transport_delay(y_pred: np.ndarray, y_true: np.ndarray,
metric: Callable[[np.ndarray, np.ndarray], float],
nodes_id: list[str], transport_delay_per_node: dict) -> dict:
"""
Evaluates the prediction for an entire network (i.e. every node in the network) under some
given evaluation metric and group the results by the transport delays of the nodes.
Parameters
----------
y_pred : `numpy.ndarray`
Predicted outputs -- must be a two-dimensional array where the first axis corresponds
to a node and the second axis encodes time.
y_true : `numpy.ndarray`
Ground truth outputs -- must be a two-dimensional array where the first axis corresponds
to a node and the second axis encodes time.
metric : `Callable[[np.ndarray, np.ndarray], float]`
A callable function that computes some metric for a single node prediction --
i.e. two 1d arrays are compared.
nodes_id : `list[str]`
List of nodes ID -- ordering must be the same as in 'y_pred' and 'y_true'.
transport_delay_per_node : `dict`
Dictionary mapping a node ID to the corresponding transport delay.
Returns
-------
`dict`
Maps transport delays to scores (i.e. results of the evaluation metric).
"""
r = {}
for idx, node_id in enumerate(nodes_id):
score = metric(y_pred[idx, :], y_true[idx, :])
t_delay = transport_delay_per_node[node_id]
if t_delay not in r:
r[t_delay] = []
r[t_delay].append(score)
return r
class Evaluator:
"""
Class for evaluating the predictions for a single node.
"""
@staticmethod
def evaluate_predictions(y_pred: np.ndarray, y_true: np.ndarray) -> dict:
"""
Computes and returns all evaluation metrics.
Parameters
----------
y_pred : `numpy.ndarray`
Predicted outputs.
y_true : `numpy.ndarray`
Ground truth outputs.
Returns
-------
`dict`
All metrics.
"""
return {"y_true": y_true, "y_pred": y_pred,
"MSE": Evaluator.mean_squared_error(y_pred, y_true),
"MAE": Evaluator.mean_absolute_error(y_pred, y_true),
"RunningMSE": Evaluator.running_mse(y_pred, y_true),
"RunningMAE": Evaluator.running_mae(y_pred, y_true)
}
@staticmethod
def mean_squared_error(y_pred: np.ndarray, y_true: np.ndarray) -> float:
"""
Computes and returns the mean-squared-error (MSE).
Parameters
----------
y_pred : `numpy.ndarray`
Predicted outputs.
y_true : `numpy.ndarray`
Ground truth outputs.
Returns
-------
`float`
The mean-squared-error.
"""
if len(y_true.shape) > 1:
return root_mean_squared_error(y_true, y_pred, multioutput='raw_values')**2
else:
return root_mean_squared_error(y_true, y_pred)**2
@staticmethod
def mean_absolute_error(y_pred: np.ndarray, y_true: np.ndarray) -> float:
"""
Computes and returns the Mean-Absolute error.
Parameters
----------
y_pred : `numpy.ndarray`
Predicted outputs.
y_true : `numpy.ndarray`
Ground truth outputs.
Returns
-------
`float`
The mean absolute error.
"""
if len(y_true.shape) > 1:
return mean_absolute_error(y_true, y_pred, multioutput="raw_values")
else:
return mean_absolute_error(y_true, y_pred)
@staticmethod
def running_mse_(y_pred: np.ndarray, y_true: np.ndarray) -> list[float]:
"""
Computes and returns the running mean-squared-error --
i.e. the MSE for every point in time.
Parameters
----------
y_pred : `numpy.ndarray`
Predicted outputs.
y_true : `numpy.ndarray`
Ground truth outputs.
Returns
-------
`list[float]`
The running mean-squared-error.
"""
if len(y_true.shape) > 1:
return [epytflow_running_mse(y_pred[i], y_true[i]) for i in range(y_true.shape[0])]
else:
return epytflow_running_mse(y_pred, y_true)
@staticmethod
def mean_absolued_error(y_pred: np.ndarray, y_true: np.ndarray) -> float:
"""
Computes and returns the Mean-Absolute error.
Parameters
----------
y_pred : `numpy.ndarray`
Predicted outputs.
y_true : `numpy.ndarray`
Ground truth outputs.
Returns
-------
`float`
The mean absolute error.
"""
if len(y_true.shape) > 1:
return mean_absolute_error(y_true, y_pred, multioutput="raw_values")
else:
return mean_absolute_error(y_true, y_pred)
@staticmethod
def running_mae_(y_pred: np.ndarray, y_true: np.ndarray) -> list[float]:
"""
Computes and returns the running mean-absolute-error --
i.e. the MAE for every point in time.
Parameters
----------
y_pred : `numpy.ndarray`
Predicted outputs.
y_true : `numpy.ndarray`
Ground truth outputs.
Returns
-------
`list[float]`
The running mean-absolute-error.
"""
def my_running_mae(y_pred_, y_) -> list[float]:
r = []
for t in range(2, len(y_pred_)):
r.append(mean_absolute_error(y_[:t], y_pred_[:t]))
return r
if len(y_true.shape) > 1:
return [my_running_mae(y_pred[i], y_true[i]) for i in range(y_true.shape[0])]
else:
return my_running_mae(y_pred, y_true)
'''
(Luca): Putting this here, running_average_metric is a fast version of any running metric
By default running_mae and running_mse use the last axis, i.e. shape should be [..., time],
or axis has to be something else.
'''
@staticmethod
def running_average_metric(values, axis=0):
return np.swapaxes(np.swapaxes(
np.add.accumulate(values, axis=axis), axis1=axis, axis2=-1)
/ np.arange(1, values.shape[axis] + 1), axis1=axis, axis2=-1
)
@staticmethod
def running_mae(y_pred, y_true, axis=-1):
values = np.abs(y_true - y_pred)
return Evaluator.running_average_metric(values, axis=axis)
@staticmethod
def running_mse(y_pred, y_true, axis=-1):
values = np.square(y_true - y_pred)
return Evaluator.running_average_metric(values, axis=axis)