-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolbox_analysis.py
392 lines (341 loc) · 18.9 KB
/
toolbox_analysis.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import traceback
import sys
import os
import json
import datetime
from loguru import logger
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import statistics
from SALib.analyze import sobol, fast
from pandas import DataFrame
import benchmark_2_example.benchmark_multi_energy_sim as benchmark_sim
import toolbox_start as benchmark_sa
logger.remove()
logger.add("results.log", level="DEBUG")
logger.add(sys.stderr, level="DEBUG")
def plot_graphs(recipes, folder, format, dpi, scenario_name='test_1'):
def plot_and_save(store, parameter, title, keys, xlabel='simulation time', ylabel='Power'):
df_to_plot = DataFrame()
i = 0
for key in store.keys():
df_to_plot['ID_' + str(i)] = store[key][keys]
i += 1
df_to_plot.plot(title=title, legend=True, xlabel='simulation time', ylabel=ylabel)
plt.savefig(f'{folder}/{scenario_name}_{parameter}.{format}', dpi=dpi, format=format)
# Parameters to be plotted
parameters = [
{'parameter': 'heat_pump_power', 'title': 'Heat pump power',
'keys': ('HeatPumpSim-0.heatpump_0', 'P_effective'), 'ylabel': 'Power'},
{'parameter': 'voltage_bus1', 'title': 'Voltage Bus 1', 'keys': ('ElNetworkSim-0.Bus_1_0', 'vm_pu'),
'ylabel': 'Voltage'},
{'parameter': 'voltage_bus2', 'title': 'Voltage Bus 2', 'keys': ('ElNetworkSim-0.Bus_2_0', 'vm_pu'),
'ylabel': 'Voltage'},
{'parameter': 'noise_generator_output', 'title': 'Noise Generator Output',
'keys': ('NoiseGeneratorSim-0.ng__0', 'output'), 'ylabel': 'Voltage'},
{'parameter': 'volt_ctrl_setpoint', 'title': 'Vlt Ctrl Setpoint',
'keys': ('VoltageCtrlSim-0.VoltageController_0', 'hp_p_el_kw_setpoint'), 'ylabel': 'Setpoint'},
{'parameter': 'HEX_heat_Power', 'title': 'HEX Heat Power',
'keys': ('HeatExchangerSim-0.HEXConsumer_0', 'P_heat'), 'ylabel': 'Power'},
{'parameter': 'tank_average_temperature', 'title': 'Tank average temp',
'keys': ('StorageTankSim-0.StratifiedWaterStorageTank_0', 'T_avg'), 'ylabel': 'Temperature'}
]
# Plotting parameters
store = pd.HDFStore(benchmark_sim.get_store_filename(scenario_name, ''))
for param in parameters:
plot_and_save(store, param['parameter'], param['title'], param['keys'], xlabel='simulation time',
ylabel=param['ylabel'])
store.close()
def do_anova_analysis(results, variation_params, target_metrics, plots, plt_show, folder_figures, dpi, format):
logger.info('Do ANOVA analysis')
import itertools
from scipy import stats
anova_results = pd.DataFrame(columns=['factor', 'target_metric', 'F', 'p'])
for target_metric in target_metrics:
for factor in variation_params.keys():
factor_values = results.groupby(factor).first().index.values.tolist()
factor_min = min(factor_values)
factor_max = max(factor_values)
factor_min_results = list(results[(results[factor] == factor_min)][target_metric])
factor_max_results = list(results[(results[factor] == factor_max)][target_metric])
F, p = stats.f_oneway(factor_min_results, factor_max_results)
anova_results.loc[len(anova_results.index)] = [factor, target_metric, F, p]
if plots:
fig = plt.figure(figsize = (10, 5))
data = anova_results[anova_results['target_metric'] == target_metric]
# creating the bar plot
plt.bar(data['factor'], data['p'], color ='maroon', width = 0.4)
plt.axhline(y=0.05, color='r', linestyle='-')
plt.xlabel("Factors analysed")
plt.ylabel("p-Value")
plt.title(f"ANOVA for {target_metric}")
plt.savefig(f'{folder_figures}/ANOVA_{target_metric}.{format}', dpi=dpi, format=format)
if plt_show:
plt.show()
logger.info(f' (p < 0.05: Hypothesis 0 (same variance) is rejected -> different variances in sample -> '
f'change in parameter has effect.)\n{anova_results.to_markdown()}')
def do_manova_analysis(results, variation_params, target_metrics, plots, plt_show, folder_figures, dpi, format):
logger.info('Do MANOVA analysis')
from statsmodels.multivariate.manova import MANOVA
target_metric_observations = results[target_metrics]
factor_values = results[variation_params.keys()]
manova = MANOVA(endog=target_metric_observations, exog=factor_values)
manova_result = manova.mv_test()
# print(manova_result.summary())
# print(manova_result)
factors_list = list(variation_params.keys())
for i, key in enumerate(manova_result.results.keys()):
factor = factors_list[i]
factor_result = manova_result[key]['stat']
logger.info(f'MANOVA statistic for factor {factor}: \n {factor_result}')
def do_oat_analysis(results, variation_params, target_metric, folder_figures, scenario_name='test_1',
dpi=300, format='png', plt_show=False):
results_df = pd.DataFrame()
variances_df = pd.DataFrame()
for count, factor in enumerate(variation_params.keys()):
min = results[target_metric].iloc[2 * count + 1]
mean = results[target_metric].iloc[0]
max = results[target_metric].iloc[2 * count + 2]
# results_df = results_df.append({'factor': factor, 'min': min, 'mean': mean, 'max': max}, ignore_index=True)
results_df = results_df.append({'factor': factor, 'value': min}, ignore_index=True)
results_df = results_df.append({'factor': factor, 'value': mean}, ignore_index=True)
results_df = results_df.append({'factor': factor, 'value': max}, ignore_index=True)
# logger.info(f'Target metric: {target_metric} - Factor: {factor} - changed with min {(min / mean * 100):.2f}%')
# logger.info(f'Target metric: {target_metric} - Factor: {factor} - changed with max {(max / mean * 100):.2f}%')
variances_df = variances_df.append({'factor': factor, 'variance': statistics.variance([min, mean, max])},
ignore_index=True)
fig, ax = plt.subplots(figsize=(12, 6))
results_df.boxplot(by='factor', ax=ax)
ax.tick_params(axis='x', labelrotation=90)
ax.set_title(f'Target metric: {target_metric}')
if plt_show:
plt.show()
plt.savefig(f'{folder_figures}\\{scenario_name}_{target_metric}.{format}',
dpi=dpi,
format=format,
bbox_inches="tight")
results_df.to_csv(f'{folder_figures}\\{scenario_name}_{target_metric}.csv')
# logger.info(variances_df.sort_values(by=['variance']).to_markdown())
return variances_df
def do_sobol_analysis(results, variation_params, target_metric, folder_figures, scenario_name='test_1',
dpi=300, format='png', plt_show=False):
from matplotlib import pyplot as plt
if len(list(variation_params.keys())) == 1:
param = list(variation_params.keys())[0]
exog = results[[param]]
endog = results[[target_metric]]
all = pd.concat([exog, endog], axis=1).sort_values(by=[param])
fig = plt.figure()
ax = plt.axes()
all.plot(ax=ax, x=param, y=target_metric)
plt.savefig(f'{folder_figures}/{scenario_name}_sobol_1_factor_{target_metric}.{format}', dpi=dpi, format=format)
return
param1 = list(variation_params.keys())[0]
param2 = list(variation_params.keys())[1]
# Get the used treatments (battery size and power)
exog = results[[param1, param2]]
# Get the self consumption index as response for each treatment
endog = results[[target_metric]]
# We want to fit a metamodel to our system
# We chose Kriging (Gaussian Process) this time around. You can also choose other metamodels if you want
# (simplest would be linear interpolation)
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
# Kriging needs a kernel. This kernel parameterization should work, but you can also play around with it.
kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
gp.fit(exog.values, endog.values) # Fit the Kriging model to our samples and associated responses
# Next we need to evaluate our metamodel. Thus, we set up a 50x50 point evaluation grid:
var_item1 = variation_params[param1]
var_item2 = variation_params[param2]
x_vec, y_vec = np.meshgrid(np.linspace(var_item1['min'], var_item1['max'], 50),
np.linspace(var_item2['min'], var_item2['max'], 50))
# x_vec and y_vec is for plotting. We reshape them to get vectors for predicting:
evalgrid = np.array([x_vec.flatten(), y_vec.flatten()]).T
# Using our metamodel for predictions (Kriging also gives us a sense of uncertainty via the sigma):
scipred, sigma = gp.predict(evalgrid, return_std=True)
# For plotting we need to reshape our prediction array:
pltsci = np.reshape(scipred, (50, 50))
from mpl_toolkits.mplot3d import Axes3D
# matplotlib
# This gives us the figure in an extra window
fig = plt.figure()
ax = plt.axes(projection="3d")
# Labelling Axis
ax.set_xlabel(param1)
ax.set_ylabel(param2)
ax.set_zlabel(target_metric)
# ax = fig.gca(projection='3d') #Deprecated since Matplotlib 3.4
# Get 3D surface plot:
surf = ax.plot_surface(x_vec, y_vec, pltsci)
# Include our original sample points:
ax.plot(exog.values[:, 0], exog.values[:, 1], endog.values.ravel(), 'r*')
plt.savefig(f'{folder_figures}/{datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")}_{scenario_name}_{target_metric}.'
f'{format}', dpi=dpi, format=format)
if plt_show:
plt.show()
def analyze_results(recipes, variations_dict, basic_conf, folder=None, format='png', dpi=300, doe_type='anova',
plots=True, target_metrics=[], folder_figures='figures', plt_show=False, results_file=None):
if not folder:
folder = basic_conf['folder_temp_files_ANOVA']
scenario_name = basic_conf['scenario_name']
logger.info(f'Start analysis script for scenario {scenario_name}')
# if plots:
# plot_graphs(recipes, folder_figures, format, dpi)
if not results_file:
results_file = os.path.join(folder, f'{basic_conf["summary_filename"]}.h5')
logger.info(f'No results file was specified. Using filename from simulation parameter file: {results_file}')
if results_file.endswith('json'):
logger.info(f'Results are read in from the JSON file {results_file}')
with open(results_file) as data:
results = pd.DataFrame(json.load(data))
elif results_file.endswith('h5') or results_file.endswith('hdf5'):
logger.info(f'Results are read in from the HDF5 file {results_file}')
run_store = pd.HDFStore(results_file)
results = [run_store[k] for k in run_store.keys()]
run_store.close()
results = pd.concat(results, axis=0).set_index('ID')
elif results_file.endswith('csv'):
logger.info(f'Results are read in from the CSV file {results_file}')
results = pd.DataFrame(pd.read_csv(results_file))
# for analysis min and max values are needed
variation_params = {}
for entity, params in variations_dict.items():
for param, variation in params.items():
variation_params[f'{entity}.{param}'] = {}
if isinstance(variation, dict):
variation_params[f'{entity}.{param}']['min'] = variation['mean'] - variation['stdvs'] * 3
variation_params[f'{entity}.{param}']['max'] = variation['mean'] + variation['stdvs'] * 3
else:
variation_params[f'{entity}.{param}']['min'] = variation[0]
variation_params[f'{entity}.{param}']['max'] = variation[1]
if doe_type == 'sobol' or doe_type == 'LHS':
for target_metric in target_metrics:
do_sobol_analysis(results, variation_params, target_metric, folder_figures,
scenario_name=scenario_name, dpi=dpi, format=format, plt_show=False)
elif doe_type == 'extreme_points':
do_anova_analysis(results, variation_params, target_metrics, plots, plt_show, folder_figures, dpi, format)
if len(target_metrics) > 1:
do_manova_analysis(results, variation_params, target_metrics, plots, plt_show, folder_figures, dpi, format)
elif doe_type == 'OAT':
variances_df = None
ranking = {}
for i, target_metric in enumerate(target_metrics):
variances_res = do_oat_analysis(results, variation_params, target_metric, folder_figures,
scenario_name=scenario_name, dpi=dpi, format=format, plt_show=False)
if variances_df is None:
variances_df = pd.DataFrame(columns=variances_res.iloc[:, 0])
variance_sorted = variances_res.sort_values(by=['variance'], ascending=False)
for i, (index, row) in enumerate(variance_sorted.iterrows()):
if row['factor'] in ranking:
ranking[row['factor']] += i
else:
ranking[row['factor']] = i
variances_res = variances_res.transpose()
variances_res.columns = variances_res.iloc[0]
variances_df = variances_df.append(variances_res[1:], ignore_index=True)
# logger.info(variances_df.to_markdown())
variance_sum = variances_df.sum(axis=0)
# logger.info('Sum of variance over all target metrics')
# logger.info(variance_sum.sort_values(ascending=False))
fig, ax = plt.subplots(figsize=(12, 4))
variance_sum.plot.bar(ax=ax)
ax.tick_params(axis='x', labelrotation=90)
ax.set_title(f'Variance sum of factor over all targets')
ax.set_xlabel('Factors')
if plt_show:
plt.show()
plt.savefig(f'{folder_figures}\\{scenario_name}_factor_varianceSum.{format}',
dpi=dpi,
format=format,
bbox_inches="tight")
variance_sum.to_csv(f'{folder_figures}\\{scenario_name}_factor_varianceSum.csv')
fig, ax = plt.subplots(figsize=(12, 4))
ranking_df = pd.DataFrame.from_dict([ranking]).T
ranking_df.sort_values(by=0).plot.bar(ax=ax, legend=None)
ax.tick_params(axis='x', labelrotation=90)
ax.set_title(f'Ranking of factor over all targets (sum of rank)')
ax.set_xlabel('Factors')
if plt_show:
plt.show()
plt.savefig(f'{folder_figures}\\{scenario_name}_factor_ranking.{format}',
dpi=dpi,
format=format,
bbox_inches="tight")
variance_sum.to_csv(f'{folder_figures}\\{scenario_name}_factor_ranking.csv')
elif doe_type == 'sobol_indices' or doe_type == 'fast':
problem, discrete = benchmark_sa.create_problem(variations_dict)
# si_results = DataFrame()
for target_metric in target_metrics:
logger.info(f'Do sobol indices analysis for target metric {target_metric}:')
try:
results_array = results[[target_metric]].iloc[:, 0].to_numpy()
logger.info(f'Reults_array: {results_array}')
if doe_type == 'sobol_indices':
si = sobol.analyze(problem,
results_array,
calc_second_order=False,
conf_level=0.95,
print_to_console=False)
elif doe_type == 'fast':
si = fast.analyze(problem,
results_array,
conf_level=0.95,
print_to_console=False)
si_filter = {k: si[k] for k in ["ST", "ST_conf", "S1", "S1_conf"]}
si_df = pd.DataFrame(si_filter, index=problem["names"])
si_df.to_csv(f'{folder_figures}/{scenario_name}_si_analysis_{target_metric}.csv')
# pandas.concat([si_results, pd.DataFrame(si_filter, index=problem["names"])])
fig, ax = plt.subplots(1)
# ax.set_ylim([-1, 3])
indices = si_df[["S1", "ST"]]
err = si_df[["S1_conf", "ST_conf"]]
indices.plot.bar(yerr=err.values.T, ax=ax, rot=0)
ax.tick_params(axis='x', labelrotation=5)
ax.set_ylabel('Sobol Index')
ax.set_xlabel('Parameter')
plt.title(target_metric)
fig.set_size_inches(8, 4)
# plt.savefig(f'{folder_figures}/{scenario_name}_si_analysis_2_{target_metric}.{format}', dpi=dpi, format=format)
fig.savefig(f'{folder_figures}/{scenario_name}_si_analysis_{target_metric}.{format}', dpi=dpi, format=format)
except Exception as e:
logger.info(f'Exception for target metric: {target_metric}: {e}')
traceback.print_exception(*sys.exc_info())
# logger.info(si_results.to_markdown)
# si_results.to_csv('si_analysis.csv')
else:
logger.info('No analysis type defined, which is matching with the available types.')
if __name__ == "__main__":
import argparse
# Parse command line options.
parser = argparse.ArgumentParser()
parser.add_argument('--folder', default = os.path.join('output','temp_files_ls_hs_trial'), help = 'folder with configuration files')
parser.add_argument('--results', default = 'results_ANOVA.csv', help = 'file with values of target metrics for simulation runs')
args = parser.parse_args()
# Read configuration files from temp folder to make them available for analysis
temp_folder = args.folder
results = args.results
results_file = os.path.join(temp_folder, results)
with open(os.path.join(temp_folder, 'recipes.json')) as data:
recipes = json.load(data)
with open(os.path.join(temp_folder, 'variations_dict.json')) as data:
variations_dict = json.load(data)
with open(os.path.join(temp_folder, 'basic_conf.json')) as data:
basic_conf = json.load(data)
with open(os.path.join(temp_folder, 'target_metrics.json')) as data:
target_metrics = json.load(data)
with open(os.path.join(temp_folder, 'sim_parameters.json')) as data:
sim_parameters = json.load(data)
analyze_results(recipes=recipes,
variations_dict=variations_dict,
basic_conf=basic_conf,
folder=temp_folder,
format=sim_parameters['format'],
dpi=sim_parameters['dpi'],
doe_type=sim_parameters['doe_type'],
plots=sim_parameters['plots'],
target_metrics=target_metrics,
plt_show=sim_parameters['show_plots'],
folder_figures=sim_parameters['folder_figures'],
results_file=results_file)