-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwevalidate.py
312 lines (232 loc) · 10.5 KB
/
wevalidate.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# This script runs the comparison between timeseries data,
# as specified in config.yaml. This is the main routine for i-validate.
#
# config_test.yaml contains erroneous data that are designed for testing
# this code.
#
# Joseph Lee <joseph.lee at pnnl.gov>
import yaml
import sys
import os
import pathlib
import numpy as np
import pandas as pd
from tools import eval_tools, cal_print_metrics
def compare(config=None):
config_dir = os.path.join((pathlib.Path(os.getcwd()).parent), 'config')
if config is None:
config_file = os.path.join(config_dir, 'config.yaml')
else:
config_file = os.path.join(config_dir, config)
sys.path.append('.')
conf = yaml.load(open(config_file), Loader=yaml.FullLoader)
base = conf['base']
comp = conf['comp']
p_curve = conf['power_curve']
print('validation start time:', conf['time']['window']['start'])
print('validation end time:', conf['time']['window']['end'])
print('location:', conf['location'])
print('baseline dataset:', base['name'])
print('variable:', conf['reference']['var'])
# Load modules
metrics = [eval_tools.get_module_class('metrics', m)()
for m in conf['metrics']]
crosscheck_ts = eval_tools.get_module_class('qc', 'crosscheck_ts')(conf)
plotting = eval_tools.get_module_class('plotting', 'plot_data')(conf)
# Data frame containing data at all heights
all_lev_df = pd.DataFrame()
all_lev_stat_df = pd.DataFrame()
all_ramp_ts_df = pd.DataFrame()
all_ramp_stat_df = pd.DataFrame()
for lev in conf['levels']['height_agl']:
# For data storage and metrics computation
results = []
print()
print('######################### height a.g.l.: '+str(lev)
+ ' '+conf['levels']['height_units']
+ ' #########################'
)
print()
print('********** for '+base['name']+': **********')
# Run __init__
base['input'] = eval_tools.get_module_class(
'inputs', base['function'])(base, conf)
base['data'] = base['input'].get_ts(lev)
# For each specified comparison dataset
for ind, c in enumerate(comp):
print()
print('********** for '+c['name']+': **********')
# Run __init__
c['input'] = eval_tools.get_module_class(
'inputs', c['function'])(c, conf)
c['data'] = c['input'].get_ts(lev)
results = eval_tools.append_results(results, base, c, conf)
# Crosscheck between datasets
combine_df = crosscheck_ts.align_time(base, c)
cal_print_metrics.run(
combine_df, metrics, results, ind, c, conf, base, lev
)
metricstat_dict = {key: results[ind][key]
for key in conf['metrics']}
metricstat_df = pd.DataFrame.from_dict(
metricstat_dict, orient='index', columns=[c['target_var']]
)
metricstat_df.columns = pd.MultiIndex.from_product(
[[lev], [c['name']], metricstat_df.columns]
)
if all_lev_stat_df.empty:
all_lev_stat_df = all_lev_stat_df.append(metricstat_df)
else:
all_lev_stat_df = pd.concat(
[all_lev_stat_df, metricstat_df], axis=1
)
plotting.plot_ts_line(combine_df, lev)
plotting.plot_histogram(combine_df, lev)
plotting.plot_pair_scatter(combine_df, lev)
if 'ramps' in conf:
ramp_data = cal_print_metrics.remove_na(
combine_df, ramp_txt=True
)
for ramps in conf['ramps']:
r = eval_tools.get_module_class(
'ramps', ramps['definition'])(
conf, c, ramp_data, ramps)
print()
print('@@@@@~~ calculating ramp skill scores at '+str(lev)
+ ' '+conf['levels']['height_units']
+ ' using definition: '
+ r.__class__.__name__+' ~~@@@@@')
ramp_df = r.get_rampdf()
process_ramp = eval_tools.get_module_class(
'ramps', 'process_ramp')(ramp_df)
ramp_df = process_ramp.add_contingency_table()
plot_ramp = eval_tools.get_module_class(
'plotting', 'plot_ramp')(
ramp_df, combine_df, conf, lev, ramps)
# Generating all the ramp texts and plots can take up memory space
if 'plotting' in ramps:
if ramps['plotting'] is True:
plot_ramp.plot_ts_contingency()
process_ramp.print_contingency_table()
# Print skill scores
# process_ramp.cal_print_scores()
ramp_summary_df = process_ramp.generate_ramp_summary_df()
ramp_summary_df.columns = pd.MultiIndex.from_product(
[[lev], [c['name']], [c['target_var']],
[r.ramp_nature], [r.get_ramp_method_name()]]
)
ramp_df.columns = pd.MultiIndex.from_product(
[[lev], [c['name']], [c['target_var']],
[r.ramp_nature], [r.get_ramp_method_name()], ramp_df.columns]
)
if all_ramp_stat_df.empty:
all_ramp_stat_df = all_ramp_stat_df.append(
ramp_summary_df
)
all_ramp_ts_df = all_ramp_ts_df.append(
ramp_df
)
else:
all_ramp_stat_df = pd.concat(
[all_ramp_stat_df, ramp_summary_df], axis=1
)
all_ramp_ts_df = pd.concat(
[all_ramp_ts_df, ramp_df], axis=1
)
combine_df.columns = pd.MultiIndex.from_product(
[[lev], [c['name']], combine_df.columns]
)
if all_lev_df.empty:
all_lev_df = all_lev_df.append(combine_df)
else:
all_lev_df = pd.concat([all_lev_df, combine_df], axis=1)
if 'output' in conf and conf['output']['writing'] is True:
output_path = os.path.join(
(pathlib.Path(os.getcwd()).parent), conf['output']['path']
)
if not os.path.exists(output_path):
os.makedirs(output_path)
if conf['output']['format'] == 'csv':
all_lev_df.to_csv(
os.path.join(output_path,
'ts_'+conf['output']['org']+'.csv')
)
all_lev_stat_df.to_csv(
os.path.join(output_path,
'metrics_'+conf['output']['org']+'.csv')
)
if 'ramps' in conf:
all_ramp_stat_df.to_csv(
os.path.join(output_path,
'ramp_'+conf['output']['org']+'.csv')
)
all_ramp_ts_df.to_csv(
os.path.join(output_path,
'ramp_ts_'+conf['output']['org']+'.csv')
)
pc_results = []
# For power curve
for ind, c in enumerate(comp):
# If both variables are wind speeds
# and hub height exists in user-defined validation levels
if (
base['nature'] == 'ws' and c['nature'] == 'ws'
and p_curve['hub_height'] in all_lev_df.columns.get_level_values(0)
):
print()
print('######################### deriving wind power at '
+ str(p_curve['hub_height'])
+ ' '+conf['levels']['height_units']
+ ' #########################')
print()
print('use power curve: '+p_curve['file'])
hh = p_curve['hub_height']
hhws_df = all_lev_df.xs([hh, c['name']], level=0, axis=1)
pc_csv = eval_tools.get_module_class(
'inputs', p_curve['function'])(
p_curve['path'], p_curve['file'], p_curve['ws'],
p_curve['power'], hhws_df, hh, conf
)
power_df = pc_csv.get_power()
pc_results = eval_tools.append_results(pc_results, base, c, conf)
cal_print_metrics.run(
power_df, metrics, pc_results, ind, c, conf, base, hh
)
# Plot simulated power curves, not extremely useful
# pc_csv.plot_pc()
pc_csv.plot_power_ts()
pc_csv.plot_power_scatter()
# Generate derived power output file
if ('output_path' in p_curve) and ('wt_num' in p_curve):
# Convert to MW for wind farm
power_df = power_df * p_curve['wt_num'] / 1e3
# print(power_df)
# print(c['name'])
# c should has 1 element
col = [s for s in power_df.columns if c['name'] in s][0]
# print(col)
# print(col)
new_col = 'power_'+str(p_curve['hub_height']).replace('.', '-')+conf['levels']['height_units']
# print(new_col)
# power_df[col]
# print(power_df.rename({col: new_col}, axis=1))
power_df.rename(columns={col: new_col}, inplace=True)
# 'power_78-25m'
power_df[new_col].to_csv(
os.path.join(output_path, p_curve['output_path'],
'derived_power_'+c['name']+'.csv')
)
# power_df[col].to_csv(
# os.path.join(output_path,
# 'test.csv')
# )
# power_df.columns = pd.MultiIndex.from_product(
# [[lev], [c['name']], combine_df.columns]
# )
else:
print()
print('not deriving power for '+c['name']+',')
print('either baseline and compare data are not wind speed,\n'
+ 'or hub height does not exist in validation data,\n'
+ 'hence power curve is not derived'
)