Skip to content

Commit

Permalink
Merge pull request #137 from rl-institut/fix/tests
Browse files Browse the repository at this point in the history
Fix tests, add propagate_mode_errors option
  • Loading branch information
stefansc1 authored Aug 10, 2023
2 parents 9dba82d + 6b95231 commit d7fae9c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
3 changes: 3 additions & 0 deletions simba/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def modes_simulation(schedule, scenario, args):
:type args: Namespace
:return: final schedule and scenario
:rtype: tuple
:raises Exception: if args.propagate_mode_errors is set, re-raises error instead of continuing
"""
if type(args.mode) is not list:
# backwards compatibility: run single mode
Expand All @@ -124,6 +125,8 @@ def modes_simulation(schedule, scenario, args):
schedule, scenario = func(schedule, scenario, args, i)
logging.debug("Finished mode " + mode)
except Exception as e:
if args.propagate_mode_errors:
raise
msg = f"{e.__class__.__name__} during {mode}: {e}"
logging.error('*'*len(msg))
logging.error(msg)
Expand Down
5 changes: 4 additions & 1 deletion simba/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ def get_args():
help='Remove rotations from schedule that violate assumptions. ')
parser.add_argument('--show-plots', action='store_true',
help='show plots for users to view in "report" mode')
parser.add_argument('--propagate-mode-errors', default=False,
help='Re-raise errors instead of continuing during simulation modes')

# #### Physical setup of environment #####
parser.add_argument('--preferred-charging-type', '-pct', default='depb',
choices=['depb', 'oppb'], help="Preferred charging type. Choose one\
Expand Down Expand Up @@ -348,7 +351,7 @@ def get_args():
nargs=2, default=[], action='append',
help='append additional argument to price signals')
parser.add_argument('--optimizer_config', default=None,
help="For station_optimization a optimizer_config is needed. \
help="For station_optimization an optimizer_config is needed. \
Input a path to an .cfg file or use the default_optimizer.cfg")

parser.add_argument('--config', help='Use config file to set arguments')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_consumption.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_calculate_consumption(self, tmp_path):
:param tmp_path: pytest fixture to create a temporary path
"""
schedule, scenario = TestSchedule().basic_run()
schedule, scenario, _ = TestSchedule().basic_run()
trip = next(iter(schedule.rotations.values())).trips.pop(0)
consumption = trip.__class__.consumption
consumption.temperatures_by_hour = {hour: hour * 2 - 15 for hour in range(0, 24)}
Expand Down
41 changes: 19 additions & 22 deletions tests/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import spice_ev.scenario as scenario
from spice_ev.util import set_options_from_config

from simba.simulate import pre_simulation
from tests.conftest import example_root, file_root
from tests.helpers import generate_basic_schedule
from simba import consumption, rotation, schedule, trip, util
Expand Down Expand Up @@ -33,33 +34,23 @@ class TestSchedule:
vehicle_types = util.uncomment_json_file(file)

def basic_run(self):
"""Returns a schedule and scenario after running SimBA.
:return: schedule, scenario
"""Returns a schedule, scenario and args after running SimBA.
:return: schedule, scenario, args
"""
path_to_trips = example_root / "trips_example.csv"
# set the system variables to imitate the console call with the config argument.
# first element has to be set to something or error is thrown
sys.argv = ["foo", "--config", str(example_root / "simba.cfg")]
args = util.get_args()
args.config = example_root / "simba.cfg"
args.days = None
args.seed = 5

trip.Trip.consumption = consumption.Consumption(
self.vehicle_types, outside_temperatures=self.temperature_path,
level_of_loading_over_day=self.lol_path)

path_to_all_station_data = example_root / "all_stations.csv"
generated_schedule = schedule.Schedule.from_csv(
path_to_trips, self.vehicle_types, self.electrified_stations, **mandatory_args,
station_data_path=path_to_all_station_data)

set_options_from_config(args, verbose=False)
args.ALLOW_NEGATIVE_SOC = True
args.attach_vehicle_soc = True

scen = generated_schedule.run(args)
return generated_schedule, scen
sched = pre_simulation(args)
scen = sched.run(args)
return sched, scen, args

def test_mandatory_options_exit(self):
"""
Expand Down Expand Up @@ -105,7 +96,7 @@ def test_station_data_reading(self):
def test_basic_run(self):
""" Check if running a basic example works and if a scenario object is returned
"""
schedule, scen = self.basic_run()
sched, scen, args = self.basic_run()
assert type(scen) is scenario.Scenario

def test_assign_vehicles(self):
Expand All @@ -131,13 +122,15 @@ def test_assign_vehicles(self):
def test_calculate_consumption(self):
""" Test if calling the consumption calculation works
"""
# Changing self.vehicle_types can propagate to other tests
vehicle_types = deepcopy(self.vehicle_types)
trip.Trip.consumption = consumption.Consumption(
self.vehicle_types, outside_temperatures=self.temperature_path,
vehicle_types, outside_temperatures=self.temperature_path,
level_of_loading_over_day=self.lol_path)

path_to_trips = file_root / "trips_assign_vehicles.csv"
generated_schedule = schedule.Schedule.from_csv(
path_to_trips, self.vehicle_types, self.electrified_stations, **mandatory_args)
path_to_trips, vehicle_types, self.electrified_stations, **mandatory_args)

# set mileage to a constant
mileage = 10
Expand Down Expand Up @@ -173,12 +166,16 @@ def test_get_common_stations(self):

def test_get_negative_rotations(self):
"""Check if the single rotation '1' with a negative soc is found """

# make use of the test_run() which has to return schedule and scenario object
sched, scen = self.basic_run()

sched, scen, args = self.basic_run()
for rot in sched.rotations.values():
for t in rot.trips:
t.distance = 0.01
sched.rotations["1"].trips[0].distance = 9999999
sched.calculate_consumption()
scen = sched.run(args)
neg_rots = sched.get_negative_rotations(scen)
assert '2' in neg_rots
assert ['1'] == neg_rots

def test_rotation_filter(self, tmp_path):
s = schedule.Schedule(self.vehicle_types, self.electrified_stations, **mandatory_args)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class TestSimulate:

# Add propagate_mode_errors as developer setting to raise Exceptions.
DEFAULT_VALUES = {
"vehicle_types": example_path / "vehicle_types.json",
"electrified_stations": example_path / "electrified_stations.json",
Expand All @@ -39,6 +39,7 @@ class TestSimulate:
"desired_soc_deps": 1,
"min_charging_time": 0,
"default_voltage_level": "MV",
"propagate_mode_errors": True,
}

def test_basic(self):
Expand All @@ -48,12 +49,16 @@ def test_basic(self):
def test_missing(self):
# every value in DEFAULT_VALUES is expected to be set, so omitting one should raise an error
values = self.DEFAULT_VALUES.copy()
# except propagate_modes_error
del self.DEFAULT_VALUES["propagate_mode_errors"]
for k, v in self.DEFAULT_VALUES.items():
del values[k]
with pytest.raises(Exception):
simulate(Namespace(**values))
# reset
values[k] = v
# restore the setting for further testing
self.DEFAULT_VALUES["propagate_mode_errors"] = values["propagate_mode_errors"]

# required file missing
for file_type in ["vehicle_types", "electrified_stations", "cost_parameters_file"]:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_get_git_revision_hash(self):
assert type(git_hash) is str

def test_get_buffer_time(self):
schedule, scenario = TestSchedule().basic_run()
schedule, scenario, _ = TestSchedule().basic_run()
trip = next(iter(schedule.rotations.values())).trips.pop(0)
util.get_buffer_time(trip)
buffer_time = {"10-22": 2,
Expand Down

0 comments on commit d7fae9c

Please sign in to comment.