-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmetrics.py
128 lines (116 loc) · 3.29 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
# -*-Encoding: utf-8 -*-
################################################################################
#
# Copyright (c) 2022 Baidu.com, Inc. All Rights Reserved
#
################################################################################
"""
Description: Some useful metrics
Authors: Lu,Xinjiang (luxinjiang@baidu.com)
Date: 2022/03/10
"""
import numpy as np
import pandas as pd
def mae(pred, gt):
"""
Desc:
Mean Absolute Error
Args:
pred:
gt: ground truth vector
Returns:
MAE value
"""
_mae = 0.
if len(pred) > 0 and len(gt) > 0:
_mae = np.mean(np.abs(pred - gt))
return _mae
def mse(pred, gt):
"""
Desc:
Mean Square Error
Args:
pred:
gt: ground truth vector
Returns:
MSE value
"""
_mse = 0.
if len(pred) > 0 and len(gt) > 0:
_mse = np.mean((pred - gt) ** 2)
return _mse
def rmse(pred, gt):
"""
Desc:
Root Mean Square Error
Args:
pred:
gt: ground truth vector
Returns:
RMSE value
"""
return np.sqrt(mse(pred, gt))
def regressor_scores(prediction, gt):
"""
Desc:
Some common metrics for regression problems
Args:
prediction:
gt: ground truth vector
Returns:
A tuple of metrics
"""
_mae = mae(prediction, gt)
_rmse = rmse(prediction, gt)
return _mae, _rmse
def turbine_scores(pred, gt, raw_data, examine_len):
"""
Desc:
Calculate the MAE and RMSE of one turbine
Args:
pred: prediction for one turbine
gt: ground truth
raw_data: the DataFrame of one wind turbine
examine_len:
Returns:
The averaged MAE and RMSE
"""
nan_cond = pd.isna(raw_data).any(axis=1)
invalid_cond = (raw_data['Patv'] < 0) | \
((raw_data['Patv'] == 0) & (raw_data['Wspd'] > 2.5)) | \
((raw_data['Pab1'] > 89) | (raw_data['Pab2'] > 89) | (raw_data['Pab3'] > 89)) | \
((raw_data['Wdir'] < -180) | (raw_data['Wdir'] > 180) | (raw_data['Ndir'] < -720) |
(raw_data['Ndir'] > 720))
indices = np.where(~nan_cond & ~invalid_cond)
prediction = pred[indices]
targets = gt[indices]
# NOTE: Before calculating the metrics, the unit of the outcome (e.g. predicted or true) power
# should be converted from Kilo Watt to Mega Watt first.
_mae, _rmse = regressor_scores(prediction[-examine_len:] / 1000, targets[-examine_len:] / 1000)
return _mae, _rmse
def regressor_detailed_scores(predictions, gts, raw_df_lst, settings):
"""
Desc:
Some common metrics
Args:
predictions:
gts: ground truth vector
raw_df_lst:
settings:
Returns:
A tuple of metrics
"""
all_mae, all_rmse = [], []
for i in range(settings["capacity"]):
prediction = predictions[i]
gt = gts[i]
raw_df = raw_df_lst[i]
_mae, _rmse = turbine_scores(prediction, gt, raw_df, settings["output_len"])
# In case NaN is encountered
if _mae != _mae or _rmse != _rmse:
continue
all_mae.append(_mae)
all_rmse.append(_rmse)
total_mae = np.array(all_mae).sum()
total_rmse = np.array(all_rmse).sum()
return total_mae, total_rmse