-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathA0_initialization.py
More file actions
581 lines (475 loc) · 17.8 KB
/
Copy pathA0_initialization.py
File metadata and controls
581 lines (475 loc) · 17.8 KB
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
r"""
Module A0 - Initialization
==========================
Module A0_initialization defines functions to parse user inputs to the MVS simulation.
- Display welcome message with current version number
- Parse command line arguments and set default values for MVS parameters if not provided
- Check that all necessary files and folder exist
- Create output directory
- Define screen logging depth
Usage from root of repository:
.. code-block:: bash
python mvs_tool.py [-h] [-i [PATH_INPUT_FOLDER]] [-ext [{json,csv}]] [-o [PATH_OUTPUT_FOLDER]]
[-log [{debug,info,error,warning}]] [-f [OVERWRITE]] [-pdf [PDF_REPORT]] [-png [SAVE_PNG]]
Usage when multi-vector-simulator is installed as a package:
.. code-block:: bash
mvs_tool [-h] [-i [PATH_INPUT_FOLDER]] [-ext [{json,csv}]] [-o [PATH_OUTPUT_FOLDER]]
[-log [{debug,info,error,warning}]] [-f [OVERWRITE]] [-pdf [PDF_REPORT]] [-png [SAVE_PNG]]
Process MVS arguments
optional arguments:
-h, --help
show this help message and exit
-i [PATH_INPUT_FOLDER]
path to the input folder
-ext [{json,csv}]
type (json or csv) of the input files (default: 'json')
-o [PATH_OUTPUT_FOLDER]
path to the output folder for the simulation's results
-log [{debug,info,error,warning}]
level of logging in the console
-f [OVERWRITE]
overwrite the output folder if True (default: False)
-pdf [PDF_REPORT]
generate a pdf report of the simulation if True (default: False)
-png [SAVE_PNG]
generate png figures of the simulation in the output_folder if True (default: False)
"""
import argparse
import logging
import os
import shutil
from oemof.tools import logger
from multi_vector_simulator.utils.constants import (
REPO_PATH,
PACKAGE_DATA_PATH,
DEFAULT_INPUT_PATH,
DEFAULT_OUTPUT_PATH,
RUN_SENSITIVITY,
JSON_FNAME,
CSV_FNAME,
JSON_EXT,
CSV_EXT,
CSV_ELEMENTS,
INPUT_FOLDER,
INPUTS_COPY,
DEFAULT_MAIN_KWARGS,
PDF_REPORT,
SIMULATION_SETTINGS,
PATH_INPUT_FILE,
PATH_INPUT_FOLDER,
PATH_OUTPUT_FOLDER,
INPUT_TYPE,
OVERWRITE,
DISPLAY_OUTPUT,
SAVE_PNG,
LOGFILE,
REPORT_FOLDER,
OUTPUT_FOLDER,
PDF_REPORT,
JSON_WITH_RESULTS,
JSON_FILE_EXTENSION,
ARG_PDF,
ARG_REPORT_PATH,
ARG_PATH_SIM_OUTPUT,
ARG_DEBUG_REPORT,
)
from multi_vector_simulator.utils.constants_json_strings import LABEL
from multi_vector_simulator.version import version_num
def mvs_arg_parser():
"""Create a command line argument parser for MVS
Usage from root of repository:
.. code-block:: bash
python mvs_tool.py [-h] [-i [PATH_INPUT_FOLDER]] [-ext [{json,csv}]] [-o [PATH_OUTPUT_FOLDER]]
[-log [{debug,info,error,warning}]] [-f [OVERWRITE]] [-pdf [PDF_REPORT]] [-png [SAVE_PNG]]
[--version]
Usage when multi-vector-simulator is installed as a package:
.. code-block:: bash
mvs_tool [-h] [-i [PATH_INPUT_FOLDER]] [-ext [{json,csv}]] [-o [PATH_OUTPUT_FOLDER]]
[-log [{debug,info,error,warning}]] [-f [OVERWRITE]] [-pdf [PDF_REPORT]] [-png [SAVE_PNG]]
[--version]
Process MVS arguments
optional arguments:
-h, --help
show this help message and exit
-i [PATH_INPUT_FOLDER]
path to the input folder
-ext [{json,csv}]
type (json or csv) of the input files (default: 'json')
-o [PATH_OUTPUT_FOLDER]
path to the output folder for the simulation's results
-log [{debug,info,error,warning}]
level of logging in the console
-f [OVERWRITE]
overwrite the output folder if True (default: False)
-pdf [PDF_REPORT]
generate a pdf report of the simulation if True (default: False)
-png [SAVE_PNG]
generate png figures of the simulation in the output_folder if True (default: False)
--version
show program's version number and exit
:return: parser
"""
parser = argparse.ArgumentParser(
prog="python mvs_tool.py", description="Process MVS arguments"
)
parser.add_argument(
"-i",
dest=PATH_INPUT_FOLDER,
nargs="?",
type=str,
help="path to the input folder",
default=DEFAULT_INPUT_PATH,
)
parser.add_argument(
"-ext",
dest=INPUT_TYPE,
nargs="?",
type=str,
help="type (json or csv) of the input files (default: 'json'",
default=JSON_EXT,
const=JSON_EXT,
choices=[JSON_EXT, CSV_EXT],
)
parser.add_argument(
"-o",
dest=PATH_OUTPUT_FOLDER,
nargs="?",
type=str,
help="path to the output folder for the simulation's results",
default=DEFAULT_OUTPUT_PATH,
)
parser.add_argument(
"-log",
dest=DISPLAY_OUTPUT,
help="level of logging in the console",
nargs="?",
default="info",
const="info",
choices=["debug", "info", "error", "warning"],
)
parser.add_argument(
"-f",
dest=OVERWRITE,
help="overwrite the output folder if True (default: False)",
nargs="?",
const=True,
default=False,
type=bool,
)
parser.add_argument(
"-pdf",
dest="pdf_report",
help="generate a pdf report of the simulation if True (default: False)",
nargs="?",
const=True,
default=False,
type=bool,
)
parser.add_argument(
"-png",
dest=SAVE_PNG,
help="generate png figures of the simulation in the output_folder if True (default: False)",
nargs="?",
const=True,
default=False,
type=bool,
)
parser.add_argument("--version", action="version", version=version_num)
parser.add_argument(
"-s",
dest=RUN_SENSITIVITY,
nargs="?",
type=str,
help="path to the output folder for the simulation's results",
default=True,
)
return parser
def report_arg_parser():
"""Create a command line argument parser for MVS
Usage when multi-vector-simulator is installed as a package:
.. code-block:: bash
mvs_report [-h] [-i [PATH_SIM_OUTPUT]] [-o [REPORT_PATH]] [-pdf]
Process mvs report command line arguments
optional arguments:
-h, --help
show this help message and exit
-pdf [PRINT_REPORT]
print the report as pdf (default: False)
-i [OUTPUT_FOLDER]
path to the simulation result json file 'json_with_results.json'
-o [REPORT_PATH]
path to save the pdf report
:return: parser
"""
parser = argparse.ArgumentParser(
prog="mvs_report", description="Display the report of a MVS simulation",
)
parser.add_argument(
"-pdf",
dest=ARG_PDF,
help="print the report as pdf (default: False)",
nargs="?",
const=True,
default=False,
type=bool,
)
parser.add_argument(
"-i",
dest=ARG_PATH_SIM_OUTPUT,
nargs="?",
type=str,
help=f"path to the simulation result json file {JSON_WITH_RESULTS}.json'",
default=os.path.join(
REPO_PATH, OUTPUT_FOLDER, JSON_WITH_RESULTS + JSON_FILE_EXTENSION
),
)
parser.add_argument(
"-o",
dest=ARG_REPORT_PATH,
nargs="?",
type=str,
help="path to save the pdf report",
default="",
)
parser.add_argument(
"-d",
dest=ARG_DEBUG_REPORT,
nargs="?",
type=bool,
help="run the dash app in debug mode with hot-reload",
const=True,
default=False,
)
return parser
def check_input_folder(path_input_folder, input_type):
"""Enforces the rules for the input folder and files
There should be a single json file for config (described under JSON_FNAME) in case
input_type is equal to JSON_EXT.
There should be a folder with csv files (name of folder given by CSV_ELEMENTS) in case
input_type is equal to CSV_EXT.
:param path_input_folder: path to input folder
:param input_type: of of JSON_EXT or CSV_EXT
:return: the json filename which will be used as input of the simulation
"""
logging.debug("Checking for inputs files")
if input_type == JSON_EXT:
path_input_file = os.path.join(path_input_folder, JSON_FNAME)
if os.path.exists(path_input_file) is False:
raise (
FileNotFoundError(
"Missing input json file!\n"
"The input json file '{}' in path '{}' can not be found.\n"
"Operation terminated.".format(JSON_FNAME, path_input_folder)
)
)
json_files = [f for f in os.listdir(path_input_folder) if f.endswith(JSON_EXT)]
if len(json_files) > 1:
raise (
FileExistsError(
"Two many json files ({}) in input folder '{}'!\n"
"Only the input json file '{}' should be present.\n"
"Operation terminated.".format(
", ".join(json_files), path_input_folder, JSON_FNAME
)
)
)
elif input_type == CSV_EXT:
path_input_file = os.path.join(path_input_folder, CSV_ELEMENTS, CSV_FNAME)
if os.path.exists(os.path.join(path_input_folder, CSV_ELEMENTS)) is False:
raise (
FileNotFoundError(
"Missing folder for csv inputs! "
"The input csv folder '{}' can not be found in the input path '{}'.\n"
"Operation terminated.".format(CSV_ELEMENTS, path_input_folder)
)
)
return path_input_file
def check_output_folder(path_input_folder, path_output_folder, overwrite):
"""Enforces the rules for the output folder
An error is raised if the path_output_folder already exists, unless overwrite is set
to True. The path_output_folder is created if not existing and the content of
path_input_folder is copied in a folder named INPUTS_COPY.
:param path_input_folder: path to input folder
:param path_output_folder: path to output folder
:param overwrite: boolean indicating what to do if the output folder exists already
:return: the path to the folder stored in the output folder as copy of the input folder
"""
path_output_folder_inputs = os.path.join(path_output_folder, INPUTS_COPY)
logging.debug("Checking for output folder")
if os.path.exists(path_output_folder) is True:
if overwrite is False:
raise (
FileExistsError(
"Output folder {} already exists. "
"If you want to overwrite the folder, please choose the force option -f when executing the MVS. "
"Otherwise, provide a name to a new output folder with option -o.".format(
path_output_folder
)
)
)
else:
# Pre-existing folder is be deleted
logging.info("Removing existing output folder " + path_output_folder)
shutil.rmtree(path_output_folder, ignore_errors=True)
try:
# trying to create path_output_folder
os.makedirs(path_output_folder, exist_ok=True)
logging.info("Creating output folder " + path_output_folder)
except OSError as error:
# In case that path_output_folder already exists
logging.info(
"It was not possible to create the output folder " + path_output_folder
)
logging.info(f"Creating folder {INPUT_FOLDER} in output folder.")
shutil.copytree(path_input_folder, path_output_folder_inputs)
def process_user_arguments(
path_input_folder=None,
input_type=None,
path_output_folder=None,
overwrite=None,
pdf_report=None,
display_output=None,
save_png=None,
lp_file_output=False,
welcome_text=None,
sensitivity=None,
):
"""
Process user command from terminal inputs. If inputs provided as arguments of the function,
they will overwrite the command line arguments.
Parameters
----------
path_input_folder: str
Describes path to inputs folder (command line "-i")
input_type: str
Describes type of input to expect (command line "-ext")
path_output_folder: str
Describes path to folder to be used for terminal output (command line "-o")
Must not exist before
overwrite: str
(Optional) Can force tool to replace existing output folder (command line "-f")
pdf_report: str
(Optional) Can generate an automatic pdf report of the simulation's results (Command line "-pdf")
save_png: str
(Optional) Can generate png figures with the simulation's results (Command line "-png")
display_output: str
(Optional) Determines which messages are used for terminal output (command line "-log")
Allowed values are
"debug": All logging messages,
"info": All informative messages and warnings (default),
"warning": All warnings,
"error": Only errors,
lp_file_output: str
Save linear equation system generated as lp file
welcome_text: str
Text to be displayed
Returns
-------
user_input: dict
Dictionary with these arguments as keys (except welcome_text which is replaced by label)
Notes
-----
Tested with:
- A0.test_input_folder_is_copied_in_output_within_folder_named_input()
- A0.test_input_folder_not_existing_raise_filenotfound_error()
- A0.test_if_json_opt_and_no_json_file_in_input_folder_raise_filenotfound_error()
- A0.test_if_csv_opt_and_csv_elements_folder_not_in_input_folder_raise_filenotfound_error()
- A0.test_if_csv_opt_path_input_file_set_to_path_input_folder_mvs_csv_config_dot_json()
- A0.test_if_log_opt_display_output_is_set_with_correct_value()
- A0.test_if_path_output_folder_exists_raise_fileexists_error()
- A0.test_if_f_opt_preexisting_path_output_folder_should_be_replaced()
- A0.test_if_path_output_folder_recursive_create_full_path()
- A0.test_if_pdf_opt_the_key_path_pdf_report_exists_in_user_inputs()
- A0.test_if_no_pdf_opt_the_key_path_pdf_report_does_not_exist_in_user_inputs()
- A0.test_if_png_pdf_not_active()
- A0.test_if_both_pdf_and_png_opt_raises_no_error()
"""
logging.debug("Get user inputs from console")
# Parse the arguments from the command line
parser = mvs_arg_parser()
args = vars(parser.parse_args())
# Give priority from user input kwargs over command line arguments
# However the command line arguments have priority over default kwargs
if path_input_folder is None:
path_input_folder = args.get(
PATH_INPUT_FOLDER, DEFAULT_MAIN_KWARGS[PATH_INPUT_FOLDER]
)
if input_type is None:
input_type = args.get(INPUT_TYPE, DEFAULT_MAIN_KWARGS[INPUT_TYPE])
if path_output_folder is None:
path_output_folder = args.get(
PATH_OUTPUT_FOLDER, DEFAULT_MAIN_KWARGS[PATH_OUTPUT_FOLDER]
)
if overwrite is None:
overwrite = args.get(OVERWRITE, DEFAULT_MAIN_KWARGS[OVERWRITE])
if pdf_report is None:
pdf_report = args.get("pdf_report", DEFAULT_MAIN_KWARGS["pdf_report"])
if display_output is None:
display_output = args.get(DISPLAY_OUTPUT, DEFAULT_MAIN_KWARGS[DISPLAY_OUTPUT])
if save_png is None:
save_png = args.get(SAVE_PNG, DEFAULT_MAIN_KWARGS[SAVE_PNG])
# if the default input file does not exist, use package default input file
if (
path_input_folder == DEFAULT_INPUT_PATH
and os.path.exists(os.path.join(path_input_folder, JSON_FNAME)) is False
):
path_input_folder = os.path.join(PACKAGE_DATA_PATH, INPUT_FOLDER)
logging.info(
"No default input file found in your path, using example simulation input"
)
path_input_file = check_input_folder(path_input_folder, input_type)
check_output_folder(path_input_folder, path_output_folder, overwrite)
user_input = {
LABEL: SIMULATION_SETTINGS,
PATH_INPUT_FOLDER: path_input_folder,
INPUT_TYPE: input_type,
PATH_INPUT_FILE: path_input_file,
PATH_OUTPUT_FOLDER: path_output_folder,
OVERWRITE: overwrite,
DISPLAY_OUTPUT: display_output,
"lp_file_output": lp_file_output,
}
if pdf_report is True:
user_input.update(
{
"path_pdf_report": os.path.join(
path_output_folder, REPORT_FOLDER, PDF_REPORT
)
}
)
if save_png is True:
user_input.update({"path_png_figs": path_output_folder})
if display_output == "debug":
screen_level = logging.DEBUG
elif display_output == "info":
screen_level = logging.INFO
elif display_output == "warning":
screen_level = logging.WARNING
elif display_output == "error":
screen_level = logging.ERROR
else:
screen_level = logging.INFO
#if sensitivity is None:
# sensitivity = args.get(RUN_SENSITIVITY, DEFAULT_MAIN_KWARGS[RUN_SENSITIVITY])
# Define logging settings and path for saving log
logger.define_logging(
logpath=path_output_folder,
logfile=LOGFILE,
file_level=logging.DEBUG,
screen_level=screen_level,
)
# Disable log messages of external libraries saving into the log file, unless they are ERROR or CRITICAL level
for ext_lib_logger in (
"asyncio",
"asyncio.coroutines",
"websockets.server",
"websockets.protocol",
"websockets.client",
"urllib3.connectionpool",
"PIL.PngImagePlugin",
):
logger.getLogger(ext_lib_logger).setLevel(logging.ERROR)
if welcome_text is not None:
# display welcome text
logging.info(welcome_text)
return user_input