forked from ibpsa/project1-boptest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcase.py
1349 lines (1195 loc) · 50 KB
/
testcase.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
This module defines the API to the test case used by the REST requests to
perform functions such as advancing the simulation, retrieving test case
information, and calculating and reporting results.
"""
from pyfmi import load_fmu
import numpy as np
import copy
import time
from data.data_manager import Data_Manager
from forecast.forecaster import Forecaster
from kpis.kpi_calculator import KPI_Calculator
import requests
import traceback
import logging
import pytz
from datetime import datetime
import uuid
import os
import json
import array as a
class TestCase(object):
'''Class that implements the test case.
'''
def __init__(self, fmupath='models/wrapped.fmu'):
'''Constructor.
Parameters
----------
fmupath : str, optional
Path to the test case fmu.
Default is assuming a particular directory structure.
'''
# Set BOPTEST version number
with open('version.txt', 'r') as f:
self.version = f.read()
# Set test case fmu path and check if path exists and throw execption
self.fmupath = fmupath
if not os.path.exists(fmupath) or not os.path.isfile(fmupath):
raise Exception("The test case FMU cannot be found. Check TESTCASE name entered correctly.")
# Instantiate a data manager for this test case
self.data_manager = Data_Manager(testcase=self)
# Load data and the kpis_json for the test case
self.data_manager.load_data_and_jsons()
# Instantiate a forecaster for this test case
self.forecaster = Forecaster(testcase=self)
# Define name
self.name = self.config_json['name']
# Load fmu
self.fmu = load_fmu(self.fmupath)
self.fmu.set_log_level(7)
# Configure the log, log file, and console output
name = 'boptest_{0}'.format(self.name)
fmt = '%(asctime)s UTC\t%(name)-20s%(levelname)s\t%(message)s'
datefmt = '%m/%d/%Y %I:%M:%S %p'
formatter = logging.Formatter(fmt,datefmt)
logging.basicConfig(filename='{0}.log'.format(name), filemode='w', level=10, format=fmt, datefmt=datefmt)
logger = logging.getLogger()
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
# Get version and check is 2.0
self.fmu_version = self.fmu.get_version()
if self.fmu_version != '2.0':
raise ValueError('FMU must be version 2.0.')
# Get building area
self.area = self.config_json['area']
# Get available control inputs and outputs
self.input_names = list(self.fmu.get_model_variables(causality = 2).keys())
self.output_names = list(self.fmu.get_model_variables(causality = 3).keys())
self.forecast_names = list(self.data.keys())
# Set default communication step
self.set_step(self.config_json['step'])
# Initialize simulation data arrays
self.__initilize_data()
# Set default fmu simulation options
self.options = self.fmu.simulate_options()
self.options['filter'] = self.output_names + self.input_names
# Instantiate a KPI calculator for the test case
self.cal = KPI_Calculator(testcase=self)
# Initialize test case
self.initialize(self.config_json['start_time'], self.config_json['warmup_period'])
# Set default scenario
self.set_scenario(self.config_json['scenario'])
def __initilize_data(self):
'''Initializes objects for simulation data storage.
Uses self.output_names and self.input_names to create
self.y, self.y_store, self.u, self.u_store,
self.inputs_metadata, self.outputs_metadata.
Parameters
----------
None
Returns
-------
None
'''
# Get input and output and forecast meta-data
self.inputs_metadata = self._get_var_metadata(self.fmu, self.input_names, inputs=True)
self.outputs_metadata = self._get_var_metadata(self.fmu, self.output_names)
self.forecasts_metadata = self.data_manager.get_data_metadata()
# Outputs data
self.y = {'time': a.array('d',[])}
for key in self.output_names:
# Do not store outputs that are current values of control inputs
flag = False
for key_u in self.input_names:
if key[:-2] == key_u[:-2]:
flag = True
break
if flag:
# Remove outputs that are current values of control inputs
# from outputs metadata dictionary
self.outputs_metadata.pop(key)
else:
self.y[key] = a.array('d',[])
self.y_store = copy.deepcopy(self.y)
# Inputs data
self.u = {'time':a.array('d',[])}
for key in self.input_names:
self.u[key] = a.array('d',[])
self.u_store = copy.deepcopy(self.u)
def __simulation(self,start_time,end_time,input_object=None):
'''Simulates the FMU using the pyfmi fmu.simulate function.
Parameters
----------
start_time: int
Start time of simulation in seconds.
final_time: int
Final time of simulation in seconds.
input_object: pyfmi input_object, optional
Input object for simulation
Default is None
Returns
-------
res: pyfmi results object
Results of the fmu simulation.
'''
# Set fmu initialization option
self.options['initialize'] = self.initialize_fmu
# Set sample rate
step = end_time - start_time
if step >= 30:
self.options['ncp'] = int((end_time-start_time)/30)
elif step == 0:
pass
elif (step < 30) and (step > 0):
self.options['ncp'] = int((end_time-start_time)/step)
# Simulate fmu
try:
res = self.fmu.simulate(start_time=start_time,
final_time=end_time,
options=self.options,
input=input_object)
except:
return traceback.format_exc()
# Set internal fmu initialization
self.initialize_fmu = False
return res
def __get_results(self, res, store=True, store_initial=False):
'''Get results at the end of a simulation and throughout the
simulation period for storage. This method assigns these results
to `self.y` and, if `store=True`, also to `self.y_store` and
to `self.u_store`.
This method is used by `initialize()` and `advance()` to retrieve
results. `initialize()` does not store results whereas `advance()`
does.
Parameters
----------
res: pyfmi results object
Results of the fmu simulation.
store: boolean
Set to true if desired to store results in `self.y_store` and
`self.u_store`
store_initial: boolean
Set to true if desired to store the initial point.
'''
# Determine if store initial point
if store_initial:
i = 0
else:
i = 1
# Store measurements
for key in self.y.keys():
self.y[key] = res[key][-1]
if store:
# Handle initialization of cs fmu generating multiple points for the same time
if res['time'][0] == res['time'][-1]:
self.y_store[key].append(res[key][-1])
else:
for x in res[key][i:]:
self.y_store[key].append(x)
# Store control signals (will be baseline if not activated, test controller input if activated)
for key in self.u.keys():
# Replace '_u' and '_y' for key used to collect data and don't overwrite time
if key[-2:] == '_u':
key_data = key[:-2]+'_y'
elif key == 'time':
key_data = 'time'
else:
key_data = key
self.u[key] = res[key_data][-1]
if store:
# Handle initialization of cs fmu generating multiple points for the same time
if res['time'][0] == res['time'][-1]:
self.u_store[key].append(res[key_data][-1])
else:
for x in res[key_data][i:]:
self.u_store[key].append(x)
def advance(self, u):
'''Advances the test case model simulation forward one step.
Parameters
----------
u : dict
Defines the control input data to be used for the step.
{<input_name>_activate : bool, int, float, or str convertable to 1 or 0
<input_name>_u : int or float, or str convertable to float}
Returns
-------
status: int
Indicates whether an advance request has been completed.
If 200, simulation advance was completed.
If 400, invalid inputs (non-numeric) were identified.
If 500, a simulation error occurred.
message: str
Includes the debug information
payload: dict
Contains the full state of measurement and input data at the end
of the step.
{<point_name> : <point_value>}
If empty, simulation end time has been reached.
If None, a simulation error has occurred.
'''
status = 200
# Calculate and store the elapsed time
if hasattr(self, 'tic_time'):
self.tac_time = time.time()
self.elapsed_control_time_ratio = np.append(self.elapsed_control_time_ratio, (self.tac_time-self.tic_time)/self.step)
# Set final time
self.final_time = self.start_time + self.step
alert_message = ''
# Set control inputs if they exist and are written
# Check if possible to overwrite
if u.keys():
# If there are overwriting keys available
# Check that any are overwritten
written = False
for key in u.keys():
if u[key]:
written = True
break
# If there are, create input object
if written:
u_list = []
u_trajectory = self.start_time
for key in u.keys():
if (key not in self.input_names):
payload = None
status = 400
message = "Unexpected input variable: {}.".format(key)
logging.error(message)
return status, message, payload
if (key != 'time' and (u[key] != None)):
if '_activate' in key:
try:
if float(u[key]) == 1:
checked_value = 1
elif float(u[key]) == 0:
checked_value = 0
else:
payload = None
status = 400
message = "Invalid value {} and/or type {} for input {}. Input must be a boolean, float, integer, string, or unicode able to be converted to a 1 or 0 (or 'T[t]rue' or 'F[f]alse').".format(u[key], type(u[key]), key)
logging.error(message)
return status, message, payload
except ValueError:
if (u[key] == 'True') or (u[key] == 'true'):
checked_value = 1
elif (u[key] == 'False') or (u[key] == 'false'):
checked_value = 0
else:
payload = None
status = 400
message = "Invalid value {} and/or type {} for input {}. Input must be a boolean, float, integer, string, or unicode able to be converted to a 1 or 0 (or 'T[t]rue' or 'F[f]alse').".format(u[key], type(u[key]), key)
logging.error(message)
return status, message, payload
else:
try:
value = float(u[key])
except:
payload = None
status = 400
message = "Invalid value {} for input {}. Value must be a float, integer, or string able to be converted to a float, but is {}.".format(u[key], key, type(u[key]))
logging.error(message)
return status, message, payload
# Check min/max if not activation input
checked_value, message = self._check_value_min_max(key, value)
if message is not None:
logging.warning(message)
alert_message = message + ';' + alert_message
u_list.append(key)
u_trajectory = np.vstack((u_trajectory, checked_value))
input_object = (u_list, np.transpose(u_trajectory))
# Otherwise, input object is None
else:
input_object = None
# Otherwise, input object is None
else:
input_object = None
# Simulate if not end of test
if self.start_time < self.end_time:
# Make sure stop at end of test
if self.final_time > self.end_time:
self.final_time = self.end_time
res = self.__simulation(self.start_time, self.final_time, input_object)
# Process results
if not isinstance(res, str):
# Get result and store measurement and control inputs
self.__get_results(res, store=True, store_initial=False)
# Raise the flag to compute time lapse
self.tic_time = time.time()
# Get full current state
payload = self._get_full_current_state()
# Write any messages
if alert_message == '':
message = "Advanced simulation successfully from {0}s to {1}s.".format(self.start_time, self.final_time)
else:
message = alert_message
# Advance start time
self.start_time = self.final_time
# Check if scenario is over
if self.start_time >= self.end_time:
self.scenario_end = True
# Log and return
logging.info(message)
return status, message, payload
else:
# Errors in the simulation
status = 500
message = "Failed to advance simulation: {}.".format(res)
payload = res
logging.error(message)
return status, message, payload
else:
# Simulation at end time
payload = dict()
message = "End of test case scenario time period reached."
logging.info(message)
return status, message, payload
def initialize(self, start_time, warmup_period, end_time=np.inf):
'''Initialize the test simulation.
Parameters
----------
start_time: int or float
Start time of simulation to initialize to in seconds.
warmup_period: int or float
Length of time before start_time to simulate for warmup in seconds.
end_time: int or float, optional
Specifies a finite end time to allow the simulation to continue
Default value is infinite.
Returns
-------
status: int
Indicates whether an initialization request has been completed.
If 200, initialization was completed successfully.
If 400, an invalid start time or warmup period (non-numeric) was identified.
If 500, an error occurred during the initialization simulation.
message: str
Includes detailed debugging information.
payload: dict
Contains the full state of measurement and input data at the end
of the initialization period.
{<point_name> : <point_value>}.
If None, an error occurred during the initialization simulation.
'''
status = 200
payload = None
# Reset fmu
self.fmu.reset()
# Reset simulation data storage
self.__initilize_data()
self.elapsed_control_time_ratio = np.array([])
# Check if the inputs are valid
try:
start_time = float(start_time)
except:
payload = None
status = 400
message = "Invalid value {} for parameter start_time. Value must be a float, integer, or string able to be converted to a float, but is {}.".format(start_time, type(start_time))
logging.error(message)
return status, message, payload
try:
warmup_period = float(warmup_period)
except:
payload = None
status = 400
message = "Invalid value {} for parameter warmup_period. Value must be a float, integer, or string able to be converted to a float, but is {}.".format(warmup_period, type(warmup_period))
logging.error(message)
return status, message, payload
if start_time < 0:
payload = None
status = 400
message = "Invalid value {} for parameter start_time. Value must not be negative.".format(start_time)
logging.error(message)
return status, message, payload
if warmup_period < 0:
payload = None
status = 400
message = "Invalid value {} for parameter warmup_period. Value must not be negative.".format(warmup_period)
logging.error(message)
return status, message, payload
# Record initial testing time
self.initial_time = start_time
# Record end testing time
self.end_time = end_time
# Set fmu intitialization
self.initialize_fmu = True
# Simulate fmu for warmup period.
# Do not allow negative starting time to avoid confusions
res = self.__simulation(max(start_time-warmup_period, 0), start_time)
# Process result
if not isinstance(res, str):
# Get result
self.__get_results(res, store=True, store_initial=True)
# Set internal start time to start_time
self.start_time = start_time
# Initialize KPI Calculator
self.cal.initialize()
# Set scenario end flag to false
self.scenario_end = False
# Get full current state
payload = self._get_full_current_state()
message = "Test simulation initialized successfully to {0}s with warmup period of {1}s.".format(self.start_time, warmup_period)
logging.info(message)
return status, message, payload
else:
payload = None
status = 500
message = "Failed to initialize test simulation: {}.".format(res)
logging.error(message)
return status, message, payload
def get_step(self):
'''Returns the current control step in seconds.
Parameters
----------
None
Returns
-------
status: int
Indicates whether a request for querying the control step has been completed.
If 200, the step was successfully queried.
If 500, an internal error occurred.
message: str
Includes detailed debugging information.
payload: int
The current control step.
None if error during query.
'''
status = 200
message = "Queried the control step successfully."
payload = None
try:
payload = self.step
logging.info(message)
except:
status = 500
message = "Failed to query the simulation step: {}".format(traceback.format_exc())
logging.error(message)
return status, message, payload
def set_step(self, step):
'''Sets the control step in seconds.
Parameters
----------
step: int or float
Control step in seconds.
Returns
-------
status: int
Indicates whether a request for setting the control step has been completed.
If 200, the step was successfully set.
If 400, an invalid simulation step (non-numeric) was identified.
If 500, an internal error occurred.
message: str
Includes detailed debugging information.
payload:
None
'''
status = 200
message = "Control step set successfully."
payload = None
try:
step = float(step)
except:
payload = None
status = 400
message = "Invalid value {} for parameter step. Value must be a float, integer, or string able to be converted to a float, but is {}.".format(step, type(step))
logging.error(message)
return status, message, payload
if step < 0:
payload = None
status = 400
message = "Invalid value {} for parameter step. Value must not be negative.".format(step)
logging.error(message)
return status, message, payload
try:
self.step = step
except:
payload = None
status = 500
message = "Failed to set the control step: {}".format(traceback.format_exc())
logging.error(message)
return status, message, payload
payload={'step':self.step}
logging.info(message)
return status, message, payload
def get_inputs(self):
'''Returns a dictionary of control inputs and their meta-data.
Parameters
----------
None
Returns
-------
status: int
Indicates whether a request for querying the inputs has been completed.
If 200, the inputs were successfully queried.
If 500, an internal error occurred.
message: str
Includes detailed debugging information.
payload: dict
Dictionary of control inputs and their meta-data.
Returns None if error in getting inputs and meta-data.
'''
status = 200
message = "Queried the inputs successfully."
payload = None
try:
payload = self.inputs_metadata
logging.info(message)
except:
status = 500
message = "Failed to query the input list: {}".format(traceback.format_exc())
logging.error(message)
return status, message, payload
def get_measurements(self):
'''Returns a dictionary of measurements and their meta-data.
Parameters
----------
None
Returns
-------
status: int
Indicates whether a request for querying the outputs has been completed.
If 200, the outputs were successfully queried.
If 500, an internal error occurred.
message: str
Includes detailed debugging information.
payload : dict
Dictionary of measurements and their meta-data.
Returns None if error in getting measurements and meta-data.
'''
status = 200
message = "Queried the measurements successfully."
payload = None
try:
payload = self.outputs_metadata
logging.info(message)
except:
status = 500
message = "Failed to query the measurement list: {}".format(traceback.format_exc())
logging.error(message)
return status, message, payload
def get_results(self, point_names, start_time, final_time):
'''Returns measurement and control input trajectories.
Parameters
----------
point_names: list
Variable names.
start_time : int or float
Start time of data to return in seconds.
final_time : int or float
Start time of data to return in seconds.
Returns
-------
status: int
Indicates whether a request for querying the results has been completed.
If 200, the results were successfully queried.
If 400, invalid start time and/or invalid final time (non-numeric) were identified.
If 500, an internal error occured.
message: str
Includes detailed debugging information.
payload : dict
Dictionary of variable trajectories with time as lists.
{'time':[<time_data>],
'var':[<var_data>]
}
Returns None if no variable can be found or a simulation error occurs.
'''
status = 200
try:
start_time = float(start_time)
except:
payload = None
status = 400
message = "Invalid value {} for parameter start_time. Value must be a float, integer, or string able to be converted to a float, but is {}.".format(start_time, type(start_time))
logging.error(message)
return status, message, payload
try:
final_time = float(final_time)
except:
payload = None
status = 400
message = "Invalid value {} for parameter final_time. Value must be a float, integer, or string able to be converted to a float, but is {}.".format(final_time, type(final_time))
logging.error(message)
return status, message, payload
payload = {}
try:
for point_name in point_names:
# Get correct points
if point_name in self.y_store.keys():
payload[point_name] = self.y_store[point_name]
elif point_name in self.u_store.keys():
payload[point_name] = self.u_store[point_name]
else:
status = 400
message = "Invalid point name {} in parameter point_names. Check lists of available inputs and measurements.".format(point_name)
logging.error(message)
return status, message, None
if any(item in point_names for item in self.y_store.keys()):
payload['time'] = self.y_store['time']
elif any(item in point_names for item in self.u_store.keys()):
payload['time'] = self.u_store['time']
# Get correct time
if payload and 'time' in payload:
# Find min and max time
min_t = min(payload['time'])
max_t = max(payload['time'])
# If min time is before start time
if min_t < start_time:
# Check if start time in time array
if start_time in payload['time']:
t1 = start_time
# Otherwise, find first time in time array after start time
else:
np_t = np.array(payload['time'])
t1 = np_t[np_t>=start_time][0]
# Otherwise, first time is min time
else:
t1 = min_t
# If max time is after final time
if max_t > final_time:
# Check if final time in time array
if final_time in payload['time']:
t2 = final_time
# Otherwise, find last time in time array before final time
else:
np_t = np.array(payload['time'])
t2 = np_t[np_t<=final_time][-1]
# Otherwise, last time is max time
else:
t2 = max_t
# Use found first and last time to find corresponding indecies
i1 = payload['time'].index(t1)
i2 = payload['time'].index(t2)+1
for key in (point_names +['time']):
payload[key] = payload[key][i1:i2]
except:
status = 500
message = "Failed to query simulation results: {}".format(traceback.format_exc())
logging.error(message)
return status, message, None
if not isinstance(payload, (list, type(None))):
for key in payload:
payload[key] = payload[key].tolist()
message = "Queried results data successfully for point names {}.".format(point_names)
logging.info(message)
return status, message, payload
def get_kpis(self):
'''Returns KPI data.
Requires standard sensor signals.
Parameters
----------
None
Returns
-------
status: int
Indicates whether a request for querying the KPIs has been completed.
If 200, the KPIs were successfully queried.
If 500, an internal error occured.
message: str
Includes detailed debugging information
payload : dict
Dictionary containing KPI names and values.
{<kpi_name>:<kpi_value>}.
Returns None if error during calculation.
'''
status = 200
message = "Queried KPIs successfully."
try:
# Set correct price scenario for cost
if self.scenario['electricity_price'] == 'constant':
price_scenario = 'Constant'
elif self.scenario['electricity_price'] == 'dynamic':
price_scenario = 'Dynamic'
elif self.scenario['electricity_price'] == 'highly_dynamic':
price_scenario = 'HighlyDynamic'
# Calculate the core kpis
payload = self.cal.get_core_kpis(price_scenario=price_scenario)
except:
payload = None
status = 500
message = "Failed to query KPIs: {}".format(traceback.format_exc())
logging.error(message)
logging.info(message)
return status, message, payload
def get_forecast_points(self):
'''Returns a dictionary of available forecast points and their meta-data.
Parameters
----------
None
Returns
-------
status: int
Indicates whether a request for querying the forecast points has been completed.
If 200, the outputs were successfully queried.
If 500, an internal error occurred.
message: str
Includes detailed debugging information.
payload : dict
Dictionary of forecast points and their meta-data.
Returns None if error in getting forecast points and meta-data.
'''
# Get the forecast
status = 200
message = "Queried the forecast points and their meta-data successfully."
try:
payload = self.forecasts_metadata
except:
status = 500
message = "Failed to query the test case forecast points and their meta-data: {}".format(traceback.format_exc())
payload = None
logging.error(message)
logging.info(message)
return status, message, payload
def get_forecast(self, point_names, horizon, interval):
'''Returns the test case data forecast
Parameters
----------
point_names : list of str
List of forecast point names for which to get data.
horizon: int or float
Forecast horizon in seconds.
interval: int or float
Forecast interval in seconds.
Returns
-------
status: int
Indicates whether a request for querying the forecast has been successfully completed.
If 200, the forecast was successfully queried.
If 500, an internal error occurred.
message: str
Includes detailed debugging information
payload: dict
Dictionary with the requested forecast data
{<variable_name>:<variable_forecast_trajectory>}
where <variable_name> is a string with the variable
key and <variable_forecast_trajectory> is a list with
the forecasted values. 'time' is included as a variable.
'''
# Get the forecast
status = 200
message = "Queried the forecast data successfully."
# Check inputs
try:
horizon = float(horizon)
except:
payload = None
status = 400
message = "Invalid value {} for parameter horizon. Value must be a float, integer, or string able to be converted to a float, but is {}.".format(horizon, type(horizon))
logging.error(message)
return status, message, payload
try:
interval = float(interval)
except:
payload = None
status = 400
message = "Invalid value {} for parameter interval. Value must be a float, integer, or string able to be converted to a float, but is {}.".format(interval, type(interval))
logging.error(message)
return status, message, payload
if horizon < 0:
payload = None
status = 400
message = "Invalid value {} for parameter horizon. Value must not be negative.".format(horizon)
logging.error(message)
return status, message, payload
if interval <= 0:
payload = None
status = 400
message = "Invalid value {} for parameter interval. Value must be positive.".format(interval)
logging.error(message)
return status, message, payload
wrong_points = []
for point in point_names:
if point not in self.forecast_names:
wrong_points.append(str(point))
if wrong_points:
payload = None
status = 400
message = "Invalid point name(s) {} in parameter point_names. Check list of available forecast points.".format(wrong_points)
logging.error(message)
return status, message, payload
try:
payload = self.forecaster.get_forecast(point_names,
horizon=horizon,
interval=interval)
except:
status = 500
message = "Failed to query the test case forecast data: {}".format(traceback.format_exc())
payload = None
logging.error(message)
logging.info(message)
return status, message, payload
def set_scenario(self, scenario):
'''Sets the test case scenario.
Parameters
----------
scenario : dict
{'electricity_price': <'constant' or 'dynamic' or 'highly_dynamic'>,
'time_period': see available <str> keys for test case
}
If any value is None, it will not change existing.
Returns
-------
status: int
Indicates whether a request for setting the scenario has been completed
If 200, the scenario was successfully set.
If 400, invalid electricity_price and/or time_period (non-numeric) were identified.
If 500, an internal error occurred.
message: str
Includes the detailed debug information
payload: dict
{'electricity_price': if succeeded in changing then True, else None,
'time_period': if succeeded then initial measurements, else None
}
'''
status = 200
message = "Test case scenario was set successfully."
payload = {
'electricity_price': None,
'time_period': None
}
if not hasattr(self, 'scenario'):
self.scenario = {}
try:
# Handle electricity price
if scenario['electricity_price']:
if scenario['electricity_price'] not in ['constant', 'dynamic', 'highly_dynamic']:
status = 400
message = "Scenario parameter electricy_price is {}, " \
"but should be 'constant', 'dynamic', or 'highly_dynamic'.". \
format(scenario['electricity_price'])
logging.error(message)
return status, message, payload
self.scenario['electricity_price'] = scenario['electricity_price']
payload['electricity_price'] = self.scenario['electricity_price']
# Handle timeperiod
if scenario['time_period']:
if scenario['time_period'] not in self.days_json:
status = 400
message = "Scenario parameter time_period is {}, but " \
"should be one of the following: {}.". \
format(scenario['time_period'], list(self.days_json.keys()))
logging.error(message)
return status, message, payload
self.scenario['time_period'] = scenario['time_period']
warmup_period = 7*24*3600.
key = self.scenario['time_period']
start_time = self.days_json[key]*24*3600.-7*24*3600.
end_time = start_time + 14*24*3600.
except:
status = 400
message = "Invalid values for the scenario parameters: {}".format(traceback.format_exc())
logging.error(message)
return status, message, payload
try:
if scenario['time_period']:
initialize_payload = self.initialize(start_time, warmup_period, end_time=end_time)
if initialize_payload[0] != 200:
status = 500
message = initialize_payload[1]
logging.error(message)
return status, message, payload
payload['time_period'] = initialize_payload[2]
# It's needed to reset KPI Calculator when scenario is changed
self.cal.initialize()
except:
status = 500
message = "Failed to set the test case scenario: {}".format(traceback.format_exc())
payload = None
logging.error(message)
logging.info(message)
return status, message, payload
def get_scenario(self):
'''Returns the current case scenario.
Parameters
----------