Skip to content

Commit

Permalink
Using argparse for simulator (#102)
Browse files Browse the repository at this point in the history
* changes for argparse
* remove old options
* update plugin registration for argparse
* add example/test plugin
  • Loading branch information
bknueven authored Jun 3, 2021
1 parent cd0f019 commit b8f7ec0
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions prescient/plugins/plugin_registration.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
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
from prescient.simulator.simulator import Simulator
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.
Expand Down
175 changes: 79 additions & 96 deletions prescient/simulator/master_options.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions prescient/simulator/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 2 additions & 2 deletions prescient/simulator/prescient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion tests/simulator_tests/test_cases/simulate_deterministic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,3 +16,5 @@ command/exec simulator.py
--traceback
--enforce-sced-shutdown-ramprate
--no-startup-shutdown-curves
--plugin=test_plugin.py
--print-callback-message
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions tests/simulator_tests/test_cases/test_plugin.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b8f7ec0

Please sign in to comment.