From 805439e045cd4276e784e6707d2fb62fd26211d7 Mon Sep 17 00:00:00 2001 From: Bernard Knueven Date: Thu, 27 May 2021 18:10:24 -0600 Subject: [PATCH 1/7] a bit more options cleanup --- prescient/simulator/master_options.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/prescient/simulator/master_options.py b/prescient/simulator/master_options.py index f6b78fd5..f4d00774 100644 --- a/prescient/simulator/master_options.py +++ b/prescient/simulator/master_options.py @@ -74,25 +74,15 @@ def outer_parse(args=None, values=None): def _construct_inner_options_parser(): parser = OptionParser() - guiOverride = {} # a dictionary of dictionaries to facilitate gui creation - # guiOverride can also indicate that an option is not intended for user input - directory_options = OptionGroup(parser, "Directory Options") - structure_options = OptionGroup(parser, "Structure Options") - solver_options = OptionGroup(parser, "Solver Options") populator_options = OptionGroup(parser, "Populator Options") - MSSimulator_options = OptionGroup(parser, "MMSimulator Options") input_simulation_options = OptionGroup(parser, "InputSimulation Options") solver_simulation_options = OptionGroup(parser, "SolverSimulation Options") output_simulation_options = OptionGroup(parser, "OutputSimulation Options") extension_options = OptionGroup(parser, "Extension Options") other_simulation_options = OptionGroup(parser, "OtherSimulation Options") - parser.add_option_group(directory_options) - parser.add_option_group(structure_options) - parser.add_option_group(solver_options) parser.add_option_group(populator_options) - parser.add_option_group(MSSimulator_options) parser.add_option_group(input_simulation_options) parser.add_option_group(solver_simulation_options) parser.add_option_group(output_simulation_options) @@ -367,11 +357,11 @@ def _construct_inner_options_parser(): dest='symbolic_solver_labels', default=False) - solver_options.add_option('--enable-quick-start-generator-commitment', - help="Allows quick start generators to be committed if load shedding occurs", - action="store_true", - dest="enable_quick_start_generator_commitment", - default=False) + solver_simulation_options.add_option('--enable-quick-start-generator-commitment', + help="Allows quick start generators to be committed if load shedding occurs", + action="store_true", + dest="enable_quick_start_generator_commitment", + default=False) solver_simulation_options.add_option('--day-ahead-pricing', help="Choose the pricing mechanism for the day-ahead market. Choices are " From 85a5af2980b6901541768a97994261482399051b Mon Sep 17 00:00:00 2001 From: Bernard Knueven Date: Thu, 27 May 2021 18:42:59 -0600 Subject: [PATCH 2/7] initial switch to argparse --- prescient/simulator/master_options.py | 155 ++++++++++++-------------- 1 file changed, 74 insertions(+), 81 deletions(-) diff --git a/prescient/simulator/master_options.py b/prescient/simulator/master_options.py index f4d00774..ce52e9b9 100644 --- a/prescient/simulator/master_options.py +++ b/prescient/simulator/master_options.py @@ -18,7 +18,7 @@ import os from typing import Tuple, Dict -from optparse import OptionParser, OptionGroup +from argparse import ArgumentParser import prescient.plugins def construct_options_parser() -> Tuple[OptionParser, Dict[str, Dict[str, bool]]]: @@ -73,48 +73,41 @@ def outer_parse(args=None, values=None): def _construct_inner_options_parser(): - parser = OptionParser() + parser = ArgumentParser() - populator_options = OptionGroup(parser, "Populator Options") - input_simulation_options = OptionGroup(parser, "InputSimulation Options") - solver_simulation_options = OptionGroup(parser, "SolverSimulation Options") - output_simulation_options = OptionGroup(parser, "OutputSimulation Options") - extension_options = OptionGroup(parser, "Extension Options") - other_simulation_options = OptionGroup(parser, "OtherSimulation Options") - - parser.add_option_group(populator_options) - parser.add_option_group(input_simulation_options) - parser.add_option_group(solver_simulation_options) - parser.add_option_group(output_simulation_options) - parser.add_option_group(extension_options) - parser.add_option_group(other_simulation_options) + populator_options = parser.add_argument_group("Populator Options") + input_simulation_options = parser.add_argument_group("InputSimulation Options") + solver_simulation_options = parser.add_argument_group("SolverSimulation Options") + output_simulation_options = parser.add_argument_group("OutputSimulation Options") + extension_options = parser.add_argument_group("Extension Options") + other_simulation_options = parser.add_argument_group("OtherSimulation Options") ########################## # CHAIN ONLY OPTIONS # ########################## - populator_options.add_option("--start-date", + populator_options.add_argument("--start-date", help="The start date for the simulation - specified in MM-DD-YYYY format. " "Defaults to 01-01-2011.", action="store", dest="start_date", - type="string", + type=str, default="01-01-2011") - populator_options.add_option("--num-days", + populator_options.add_argument("--num-days", help="The number of days for which to create scenearios", action="store", dest="num_days", - type="int", + type=int, default=7) - populator_options.add_option("--output-directory", + populator_options.add_argument("--output-directory", help="The root directory to which all of the generated simulation files and " "associated data are written.", action="store", dest="output_directory", - type="string", + type=str, default="outdir") ############################# @@ -123,35 +116,35 @@ def _construct_inner_options_parser(): # # PRESCIENT_INPUT_OPTIONS - input_simulation_options.add_option('--data-directory', + input_simulation_options.add_argument('--data-directory', help='Specifies the directory to pull data from for confcomp.', action='store', dest='data_directory', - type='string', + type=str, default="confcomp_data") - input_simulation_options.add_option('--run-deterministic-ruc', + input_simulation_options.add_argument('--run-deterministic-ruc', help='DEPRECATED, must be used. Invokes deterministic instead ' 'of stochastic reliability unit commitment during simulation.', action='store_true', dest='run_deterministic_ruc', default=True) - extension_options.add_option('--plugin', + extension_options.add_argument('--plugin', help='The name of a python module that extends prescient behavior', - type="string", + type=str, action='append', dest='plugin_modules', default=[]) - input_simulation_options.add_option('--simulator-plugin', + input_simulation_options.add_argument('--simulator-plugin', help='If the user has an alternative methods for the various simulator functions,' ' they should be specified here, e.g., prescient.plugin.my_special_plugin.', action='store', dest='simulator_plugin', default=None) - input_simulation_options.add_option('--deterministic-ruc-solver-plugin', + input_simulation_options.add_argument('--deterministic-ruc-solver-plugin', help='If the user has an alternative method to solve the deterministic RUCs,' ' it should be specified here, e.g., prescient.plugin.my_special_plugin.' 'NOTE: This option is ignored if --simulator-plugin is used.', @@ -159,14 +152,14 @@ def _construct_inner_options_parser(): dest='deterministic_ruc_solver_plugin', default=None) - input_simulation_options.add_option('--run-ruc-with-next-day-data', + input_simulation_options.add_argument('--run-ruc-with-next-day-data', help='When running the RUC, use the data for the next day ' 'for tailing hours.', action='store_true', dest='run_ruc_with_next_day_data', default=False) - input_simulation_options.add_option('--run-sced-with-persistent-forecast-errors', + input_simulation_options.add_argument('--run-sced-with-persistent-forecast-errors', help='Create all SCED instances assuming persistent forecast error, ' 'instead of the default prescience. ' 'Only applicable when running deterministic RUC.', @@ -174,7 +167,7 @@ def _construct_inner_options_parser(): dest='run_sced_with_persistent_forecast_errors', default=False) - input_simulation_options.add_option('--ruc-prescience-hour', + input_simulation_options.add_argument('--ruc-prescience-hour', help='Hour before which linear blending of forecast and actuals ' 'takes place when running deterministic ruc. A value of ' '0 indicates we always take the forecast. Default is 0.', @@ -183,49 +176,49 @@ def _construct_inner_options_parser(): type=int, default=0) - input_simulation_options.add_option('--ruc-execution-hour', + input_simulation_options.add_argument('--ruc-execution-hour', help="Specifies when the the RUC process is executed. " "Negative values indicate time before horizon, positive after.", action='store', dest='ruc_execution_hour', - type='int', + type=int, default=16) - input_simulation_options.add_option('--ruc-every-hours', + input_simulation_options.add_argument('--ruc-every-hours', help="Specifies at which hourly interval the RUC process is executed. " "Default is 24. Should be a divisor of 24.", action='store', dest='ruc_every_hours', - type='int', + type=int, default=24) - populator_options.add_option("--ruc-horizon", + populator_options.add_argument("--ruc-horizon", help="The number of hours for which the reliability unit commitment is executed. " "Must be <= 48 hours and >= --ruc-every-hours. " "Default is 48.", action="store", dest="ruc_horizon", - type="int", + type=int, default=48) - input_simulation_options.add_option('--sced-horizon', + input_simulation_options.add_argument('--sced-horizon', help="Specifies the number of time periods " "in the look-ahead horizon for each SCED. " "Must be at least 1.", action='store', dest='sced_horizon', - type='int', + type=int, default=1) - input_simulation_options.add_option('--sced-frequency-minutes', + input_simulation_options.add_argument('--sced-frequency-minutes', help="Specifies how often a SCED will be run, in minutes. " "Must divide evenly into 60, or be a multiple of 60.", action='store', dest='sced_frequency_minutes', - type='int', + type=int, default=60) - input_simulation_options.add_option("--enforce-sced-shutdown-ramprate", + input_simulation_options.add_argument("--enforce-sced-shutdown-ramprate", help="Enforces shutdown ramp-rate constraints in the SCED. " "Enabling this options requires a long SCED look-ahead " "(at least an hour) to ensure the shutdown ramp-rate " @@ -234,14 +227,14 @@ def _construct_inner_options_parser(): dest="enforce_sced_shutdown_ramprate", default=False) - input_simulation_options.add_option("--no-startup-shutdown-curves", + input_simulation_options.add_argument("--no-startup-shutdown-curves", help="For thermal generators, do not infer startup/shutdown " "ramping curves when starting-up and shutting-down.", action="store_true", dest="no_startup_shutdown_curves", default=False) - input_simulation_options.add_option("--simulate-out-of-sample", + input_simulation_options.add_argument("--simulate-out-of-sample", help="Execute the simulation using an out-of-sample scenario, " "specified in Scenario_actuals.dat files in the daily input directories. " "Defaults to False, " @@ -252,16 +245,16 @@ def _construct_inner_options_parser(): dest="simulate_out_of_sample", default=False) - input_simulation_options.add_option("--reserve-factor", + input_simulation_options.add_argument("--reserve-factor", help="The reserve factor, expressed as a constant fraction of demand, " "for spinning reserves at each time period of the simulation. " "Applies to both stochastic RUC and deterministic SCED models.", action="store", dest="reserve_factor", - type="float", + type=float, default=0.0) - input_simulation_options.add_option('--compute-market-settlements', + input_simulation_options.add_argument('--compute-market-settlements', help="Solves a day-ahead as well as real-time market and reports " "the daily profit for each generator based on the computed prices. " "Only compatible with deterministic RUC (for now). " @@ -270,100 +263,100 @@ def _construct_inner_options_parser(): dest='compute_market_settlements', default=False) - input_simulation_options.add_option('--price-threshold', + input_simulation_options.add_argument('--price-threshold', help="Maximum possible value the price can take " "If the price exceeds this value due to Load Mismatch, then " "it is set to this value.", action='store', - type='float', + type=float, dest='price_threshold', default=10000.) - input_simulation_options.add_option('--reserve-price-threshold', + input_simulation_options.add_argument('--reserve-price-threshold', help="Maximum possible value the reserve price can take " "If the reserve price exceeds this value, then " "it is set to this value.", action='store', - type='float', + type=float, dest='reserve_price_threshold', default=1000.) # # PRESCIENT_SOLVER_OPTIONS - solver_simulation_options.add_option('--sced-solver', + solver_simulation_options.add_argument('--sced-solver', help="Choose the desired solver type for SCEDs", action='store', dest='sced_solver_type', - type='string', + type=str, default='cbc') - solver_simulation_options.add_option('--deterministic-ruc-solver', + solver_simulation_options.add_argument('--deterministic-ruc-solver', help="Choose the desired solver type for deterministic RUCs", action='store', dest='deterministic_ruc_solver_type', - type='string', + type=str, default='cbc') - solver_simulation_options.add_option("--sced-solver-options", + solver_simulation_options.add_argument("--sced-solver-options", help="Solver options applied to all SCED solves", action="append", dest="sced_solver_options", - type="string", + type=str, default=[]) - solver_simulation_options.add_option("--deterministic-ruc-solver-options", + solver_simulation_options.add_argument("--deterministic-ruc-solver-options", help="Solver options applied to all deterministic RUC solves", action="append", dest="deterministic_ruc_solver_options", - type="string", + type=str, default=[]) - solver_simulation_options.add_option("--stochastic-ruc-ef-solver-options", + solver_simulation_options.add_argument("--stochastic-ruc-ef-solver-options", help="Solver options applied to all stochastic RUC EF solves", action="append", dest="stochastic_ruc_ef_solver_options", - type="string", + type=str, default=[]) - solver_simulation_options.add_option('--write-deterministic-ruc-instances', + solver_simulation_options.add_argument('--write-deterministic-ruc-instances', help='Write all individual SCED instances.', action='store_true', dest='write_deterministic_ruc_instances', default=False) - solver_simulation_options.add_option('--write-sced-instances', + solver_simulation_options.add_argument('--write-sced-instances', help='Write all individual SCED instances.', action='store_true', dest='write_sced_instances', default=False) - solver_simulation_options.add_option('--print-sced', + solver_simulation_options.add_argument('--print-sced', help='Print results from SCED solves.', action='store_true', dest='print_sced', default=False) - solver_simulation_options.add_option('--ruc-mipgap', + solver_simulation_options.add_argument('--ruc-mipgap', help="Specifies the mipgap for all deterministic RUC solves.", action='store', dest='ruc_mipgap', - type='float', + type=float, default=0.01) - solver_simulation_options.add_option('--symbolic-solver-labels', + solver_simulation_options.add_argument('--symbolic-solver-labels', help="When interfacing with the solver, " "use symbol names derived from the model.", action='store_true', dest='symbolic_solver_labels', default=False) - solver_simulation_options.add_option('--enable-quick-start-generator-commitment', + solver_simulation_options.add_argument('--enable-quick-start-generator-commitment', help="Allows quick start generators to be committed if load shedding occurs", action="store_true", dest="enable_quick_start_generator_commitment", default=False) - solver_simulation_options.add_option('--day-ahead-pricing', + solver_simulation_options.add_argument('--day-ahead-pricing', help="Choose the pricing mechanism for the day-ahead market. Choices are " "LMP -- locational marginal price, " "ELMP -- enhanced locational marginal price, and " @@ -371,52 +364,52 @@ def _construct_inner_options_parser(): "Default is aCHP.", action='store', dest='day_ahead_pricing', - type='string', + type=str, default='aCHP') # # PRESCIENT_OUTPUT_OPTIONS - output_simulation_options.add_option('--output-ruc-initial-conditions', + output_simulation_options.add_argument('--output-ruc-initial-conditions', help="Output ruc (deterministic or stochastic) initial conditions prior " "to each solve. Default is False.", action='store_true', dest='output_ruc_initial_conditions', default=False) - output_simulation_options.add_option('--output-ruc-solutions', + output_simulation_options.add_argument('--output-ruc-solutions', help="Output ruc (deterministic or stochastic) solutions following each solve." " Default is False.", action='store_true', dest='output_ruc_solutions', default=False) - output_simulation_options.add_option('--output-sced-initial-conditions', + output_simulation_options.add_argument('--output-sced-initial-conditions', help='Output sced initial conditions prior to each solve. Default is False.', action='store_true', dest='output_sced_initial_conditions', default=False) - output_simulation_options.add_option('--output-sced-demands', + output_simulation_options.add_argument('--output-sced-demands', help='Output sced demands prior to each solve. Default is False.', action='store_true', dest='output_sced_demands', default=False) - output_simulation_options.add_option('--output-solver-logs', + output_simulation_options.add_argument('--output-solver-logs', help="Output solver logs during execution.", action='store_true', dest='output_solver_logs', default=False) - output_simulation_options.add_option('--output-max-decimal-places', + output_simulation_options.add_argument('--output-max-decimal-places', help="When writing summary files, this rounds the output to the " "specified accuracy. Default is 6.", action='store', - type='int', + type=int, dest='output_max_decimal_places', default=6) - output_simulation_options.add_option('--disable-stackgraphs', + output_simulation_options.add_argument('--disable-stackgraphs', help="Disable stackgraph generation", action='store_true', dest='disable_stackgraphs', @@ -426,15 +419,15 @@ def _construct_inner_options_parser(): # # PRESCIENT_OTHER_OPTIONS - other_simulation_options.add_option('--profile', + other_simulation_options.add_argument('--profile', help='Enable profiling of Python code. ' 'The value of this option is the number of functions that are summarized.', action='store', - type='int', + type=int, dest='profile', default=0) - other_simulation_options.add_option('--traceback', + other_simulation_options.add_argument('--traceback', help='When an exception is thrown, show the entire call stack.' 'Ignored if profiling is enabled. Default is False.', action='store_true', From 6142c18f38971d44fffc79cef030ae54337ca1a9 Mon Sep 17 00:00:00 2001 From: Bernard Knueven Date: Tue, 1 Jun 2021 11:10:59 -0600 Subject: [PATCH 3/7] changes for argparse --- prescient/simulator/prescient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prescient/simulator/prescient.py b/prescient/simulator/prescient.py index aa51d36f..b13f15b9 100644 --- a/prescient/simulator/prescient.py +++ b/prescient/simulator/prescient.py @@ -118,14 +118,14 @@ def main_prescient(options: Options): def main(args=None): if args is None: - args = sys.argv + args = sys.argv[1:] # # Parse command-line options. # try: options_parser = MasterOptions.construct_options_parser() - (options, args) = options_parser.parse_args(args=args) + options = options_parser.parse_args(args=args) except SystemExit: # the parser throws a system exit if "-h" is specified - catch # it to exit gracefully. From 5808d5f7ddc76dfaa6463714c4d609ac7ecdd4bf Mon Sep 17 00:00:00 2001 From: Bernard Knueven Date: Tue, 1 Jun 2021 10:44:43 -0600 Subject: [PATCH 4/7] removing old options --- .../runners/simulate_with_network_deterministic.txt | 4 ---- .../runners/simulate_with_network_deterministic_year.txt | 4 ---- tests/simulator_tests/test_cases/simulate_deterministic.txt | 1 - .../test_cases/simulate_with_network_deterministic.txt | 1 - 4 files changed, 10 deletions(-) diff --git a/prescient/downloaders/rts_gmlc_prescient/runners/simulate_with_network_deterministic.txt b/prescient/downloaders/rts_gmlc_prescient/runners/simulate_with_network_deterministic.txt index c5b6450a..830fa43e 100644 --- a/prescient/downloaders/rts_gmlc_prescient/runners/simulate_with_network_deterministic.txt +++ b/prescient/downloaders/rts_gmlc_prescient/runners/simulate_with_network_deterministic.txt @@ -3,18 +3,14 @@ command/exec simulator.py --simulate-out-of-sample --run-sced-with-persistent-forecast-errors --output-directory=deterministic_with_network_simulation_output ---run-deterministic-ruc --start-date=07-10-2020 --num-days=7 --sced-horizon=4 --traceback ---random-seed=10 --output-sced-initial-conditions --output-sced-demands ---output-sced-solutions --output-ruc-initial-conditions --output-ruc-solutions ---output-ruc-dispatches --output-solver-logs --ruc-mipgap=0.01 --symbolic-solver-labels diff --git a/prescient/downloaders/rts_gmlc_prescient/runners/simulate_with_network_deterministic_year.txt b/prescient/downloaders/rts_gmlc_prescient/runners/simulate_with_network_deterministic_year.txt index b502d8db..9f617ded 100644 --- a/prescient/downloaders/rts_gmlc_prescient/runners/simulate_with_network_deterministic_year.txt +++ b/prescient/downloaders/rts_gmlc_prescient/runners/simulate_with_network_deterministic_year.txt @@ -3,18 +3,14 @@ command/exec simulator.py --simulate-out-of-sample --run-sced-with-persistent-forecast-errors --output-directory=deterministic_with_network_simulation_output_year ---run-deterministic-ruc --start-date=01-02-2020 --num-days=364 --sced-horizon=4 --traceback ---random-seed=10 --output-sced-initial-conditions --output-sced-demands ---output-sced-solutions --output-ruc-initial-conditions --output-ruc-solutions ---output-ruc-dispatches --output-solver-logs --ruc-mipgap=0.01 --symbolic-solver-labels diff --git a/tests/simulator_tests/test_cases/simulate_deterministic.txt b/tests/simulator_tests/test_cases/simulate_deterministic.txt index 41b0124e..300cdb1f 100644 --- a/tests/simulator_tests/test_cases/simulate_deterministic.txt +++ b/tests/simulator_tests/test_cases/simulate_deterministic.txt @@ -3,7 +3,6 @@ command/exec simulator.py --simulate-out-of-sample --run-sced-with-persistent-forecast-errors --output-directory=deterministic_simulation_output ---run-deterministic-ruc --start-date=07-10-2020 --num-days=7 --sced-horizon=4 diff --git a/tests/simulator_tests/test_cases/simulate_with_network_deterministic.txt b/tests/simulator_tests/test_cases/simulate_with_network_deterministic.txt index 06c4e832..2e464c83 100644 --- a/tests/simulator_tests/test_cases/simulate_with_network_deterministic.txt +++ b/tests/simulator_tests/test_cases/simulate_with_network_deterministic.txt @@ -3,7 +3,6 @@ command/exec simulator.py --simulate-out-of-sample --run-sced-with-persistent-forecast-errors --output-directory=deterministic_with_network_simulation_output ---run-deterministic-ruc --start-date=07-10-2020 --num-days=7 --sced-horizon=4 From 0070fd74c70ed1d3bd80fe2795794fe9a4c5cd63 Mon Sep 17 00:00:00 2001 From: Bernard Knueven Date: Wed, 2 Jun 2021 11:45:24 -0600 Subject: [PATCH 5/7] updating Options type --- prescient/simulator/options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prescient/simulator/options.py b/prescient/simulator/options.py index 5b5ae096..c4486e51 100644 --- a/prescient/simulator/options.py +++ b/prescient/simulator/options.py @@ -11,7 +11,7 @@ # If we ever change options parsers, we can change the options type once here instead of # changing it everywhere. -import optparse +import argparse -Options = optparse.Values +Options = argparse.Namespace From 9b5a598213b5f3a8559c4bd5582eb71c8d7541b7 Mon Sep 17 00:00:00 2001 From: Bernard Knueven Date: Wed, 2 Jun 2021 15:26:46 -0600 Subject: [PATCH 6/7] updating plugin registration for argparse --- prescient/plugins/plugin_registration.py | 9 +++++---- prescient/simulator/master_options.py | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/prescient/plugins/plugin_registration.py b/prescient/plugins/plugin_registration.py index a16b9a54..8f9a2193 100644 --- a/prescient/plugins/plugin_registration.py +++ b/prescient/plugins/plugin_registration.py @@ -1,7 +1,6 @@ from __future__ import annotations from typing import TYPE_CHECKING if TYPE_CHECKING: - from optparse import Option from typing import Callable from prescient.simulator.options import Options from prescient.stats import DailyStats, HourlyStats, OverallStats, OperationsStats @@ -9,16 +8,18 @@ from prescient.engine.abstract_types import OperationsModel, RucModel from . import get_active_plugin_manager -def add_custom_commandline_option(option: Option) -> None: +def add_custom_commandline_argument(*args, **kwargs) -> None: ''' To add custom command-line options to Prescient, create a file that calls this function and include that file as a plugin on the command line (--plugin=my_file). + + Arguments to this function are passed directly into the Prescient + instance of argparse.ArgumentParser's add_argument method ''' from .internal import active_parser - active_parser.add_option(option) + active_parser.add_argument(*args, **kwargs) - def register_for_hourly_stats(callback: Callable[[HourlyStats], None]) -> None: ''' Called during plugin registration to request a subscription to hourly stats updates. diff --git a/prescient/simulator/master_options.py b/prescient/simulator/master_options.py index ce52e9b9..d78c90d4 100644 --- a/prescient/simulator/master_options.py +++ b/prescient/simulator/master_options.py @@ -33,7 +33,7 @@ def construct_options_parser() -> Tuple[OptionParser, Dict[str, Dict[str, bool]] ''' # To support the ability to add new command line options to the line that - # is currently being parsed, we convert a standard OptionParser into a + # is currently being parsed, we convert a standard ArgumentParser into a # two-pass parser by replacing the parser's parse_args method with a # modified version. In the modified parse_args, a first pass through the # command line finds any --plugin arguments and allows the plugin to From 2e0028297592f7e86774292ee503adda3d664a7d Mon Sep 17 00:00:00 2001 From: Bernard Knueven Date: Wed, 2 Jun 2021 15:27:12 -0600 Subject: [PATCH 7/7] adding example/test plugin --- .../test_cases/simulate_deterministic.txt | 2 + .../simulator_tests/test_cases/test_plugin.py | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/simulator_tests/test_cases/test_plugin.py diff --git a/tests/simulator_tests/test_cases/simulate_deterministic.txt b/tests/simulator_tests/test_cases/simulate_deterministic.txt index 300cdb1f..fff74f1b 100644 --- a/tests/simulator_tests/test_cases/simulate_deterministic.txt +++ b/tests/simulator_tests/test_cases/simulate_deterministic.txt @@ -16,3 +16,5 @@ command/exec simulator.py --traceback --enforce-sced-shutdown-ramprate --no-startup-shutdown-curves +--plugin=test_plugin.py +--print-callback-message diff --git a/tests/simulator_tests/test_cases/test_plugin.py b/tests/simulator_tests/test_cases/test_plugin.py new file mode 100644 index 00000000..54e2c628 --- /dev/null +++ b/tests/simulator_tests/test_cases/test_plugin.py @@ -0,0 +1,57 @@ +# example test plugin + +import prescient.plugins as pplugins + +def msg(callback_name, options=None): + if options is None or options.print_callback_message: + print(f"Called plugin function {callback_name}") + +pplugins.add_custom_commandline_argument('--print-callback-message', + help='Print a message when callback is called', + action='store_true', + dest='print_callback_message', + default=False) + +def hourly_stats_callback(hourly_stats): + msg('hourly_stats_callback') +pplugins.register_for_hourly_stats(hourly_stats_callback) + +def daily_stats_callback(daily_stats): + msg('daily_stats_callback') +pplugins.register_for_daily_stats(daily_stats_callback) + +def overall_stats_callback(overall_stats): + msg('overall_stats_callback') +pplugins.register_for_overall_stats(overall_stats_callback) + +def options_preview_callback(options): + msg('options_preview_callback', options) +pplugins.register_options_preview_callback(options_preview_callback) + +def initialization_callback(options, simulator): + msg('initialization_callback', options) +pplugins.register_initialization_callback(initialization_callback) + +def before_ruc_solve_callback(options, simulator, ruc_model, uc_date, uc_hour): + msg('before_ruc_solve_callback', options) +pplugins.register_before_ruc_solve_callback(before_ruc_solve_callback) + +def after_ruc_generation_callback(options, simulator, ruc_plan, uc_date, uc_hour): + msg('after_ruc_generation_callback', options) +pplugins.register_after_ruc_generation_callback(after_ruc_generation_callback) + +def after_ruc_activation_callback(options, simulator): + msg('after_ruc_activation_callback', options) +pplugins.register_after_ruc_activation_callback(after_ruc_activation_callback) + +def before_operations_solve_callback(options, simulator, sced_model): + msg('before_operations_solve_callback', options) +pplugins.register_before_operations_solve_callback(before_operations_solve_callback) + +def after_operations_callback(options, simulator, sced_model): + msg('after_operations_callback', options) +pplugins.register_after_operations_callback(after_operations_callback) + +def update_operations_stats_callback(options, simulator, operations_stats): + msg('update_operations_stats_callback', options) +pplugins.register_update_operations_stats_callback(update_operations_stats_callback)