Skip to content

Commit

Permalink
Merge pull request #206 from rl-institut/feature/scenario_name_v2
Browse files Browse the repository at this point in the history
scenario name
  • Loading branch information
j-brendel authored Sep 11, 2024
2 parents d92aec6 + ff3e5fa commit 3963722
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
3 changes: 3 additions & 0 deletions data/examples/simba.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# general info: identifier of scenario, appended to results
scenario_name = example

##### Paths #####
### Input and output files and paths ###
# Input file containing trip information (required)
Expand Down
4 changes: 4 additions & 0 deletions docs/source/simulation_parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ The example (data/simba.cfg) contains parameter descriptions which are explained
- Default value
- Expected values
- Description
* - scenario_name
- Optional: no default given
- string
- scenario identifier, appended to output directory name and report file names
* - input_schedule
- Mandatory: no default given
- Path as string
Expand Down
7 changes: 6 additions & 1 deletion simba/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
args = util.get_args()

time_str = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")

if vars(args).get("scenario_name"):
dir_name = time_str + '_' + args.scenario_name
else:
dir_name = time_str
if args.output_directory is not None:
args.output_directory = Path(args.output_directory) / time_str
args.output_directory = Path(args.output_directory) / dir_name
# create subfolder for specific sim results with timestamp.
# if folder doesn't exist, create folder.
# needs to happen after set_options_from_config since
Expand Down
48 changes: 36 additions & 12 deletions simba/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
from spice_ev.report import aggregate_global_results, plot, generate_reports


def open_for_csv(filepath):
def open_for_csv(file_path):
""" Create a file handle to write to.
:param filepath: Path to new file, overwritten if existing
:type filepath: string or pathlib.Path
:param file_path: Path to new file, overwritten if existing
:type file_path: string or pathlib.Path
:return: Function to open file
:rtype: function
"""
return open(filepath, "w", newline='', encoding='utf-8')
return open(file_path, "w", newline='', encoding='utf-8')


def generate_gc_power_overview_timeseries(scenario, args):
Expand All @@ -34,7 +34,11 @@ def generate_gc_power_overview_timeseries(scenario, args):
if not gc_list:
return

with open_for_csv(args.results_directory / "gc_power_overview_timeseries.csv") as f:
file_path = args.results_directory / "gc_power_overview_timeseries.csv"
if vars(args).get("scenario_name"):
file_path = file_path.with_stem(file_path.stem + '_' + args.scenario_name)

with open_for_csv(file_path) as f:
csv_writer = csv.writer(f)
csv_writer.writerow(["time"] + gc_list)
stations = []
Expand Down Expand Up @@ -65,8 +69,11 @@ def generate_gc_overview(schedule, scenario, args):
all_gc_list = list(schedule.stations.keys())
used_gc_list = list(scenario.components.grid_connectors.keys())
stations = getattr(schedule, "stations")
file_path = args.results_directory / "gc_overview.csv"
if vars(args).get("scenario_name"):
file_path = file_path.with_stem(file_path.stem + '_' + args.scenario_name)

with open_for_csv(args.results_directory / "gc_overview.csv") as f:
with open_for_csv(file_path) as f:
csv_writer = csv.writer(f)
csv_writer.writerow(["station_name",
"station_type",
Expand Down Expand Up @@ -161,8 +168,14 @@ def generate_plots(scenario, args):
plt.clf()
plot(scenario)
plt.gcf().set_size_inches(10, 10)
plt.savefig(args.results_directory / "run_overview.png")
plt.savefig(args.results_directory / "run_overview.pdf")
file_path_png = args.results_directory / "run_overview.png"
if vars(args).get("scenario_name"):
file_path_png = file_path_png.with_stem(file_path_png.stem + '_' + args.scenario_name)
plt.savefig(file_path_png)
file_path_pdf = args.results_directory / "run_overview.pdf"
if vars(args).get("scenario_name"):
file_path_pdf = file_path_pdf.with_stem(file_path_pdf.stem + '_' + args.scenario_name)
plt.savefig(file_path_pdf)
if args.show_plots:
plt.show()
# revert logging override
Expand All @@ -184,7 +197,10 @@ def generate(schedule, scenario, args):

# generate simulation_timeseries.csv, simulation.json and vehicle_socs.csv in SpiceEV
# re-route output paths
args.save_soc = args.results_directory / "vehicle_socs.csv"
file_path = args.results_directory / "vehicle_socs.csv"
if vars(args).get("scenario_name"):
file_path = file_path.with_stem(file_path.stem + '_' + args.scenario_name)
args.save_soc = file_path
# bundle station-specific output files in subdirectory
gc_dir = args.results_directory / "gcs"
gc_dir.mkdir(exist_ok=True)
Expand Down Expand Up @@ -282,10 +298,16 @@ def generate(schedule, scenario, args):
t = sim_start_time + i * scenario.interval
socs = [str(rotation_socs[k][i]) for k in rotations]
data.append([str(t)] + socs)
write_csv(data, args.results_directory / "rotation_socs.csv",
propagate_errors=args.propagate_mode_errors)

with open_for_csv(args.results_directory / "rotation_summary.csv") as f:
file_path = args.results_directory / "rotation_socs.csv"
if vars(args).get("scenario_name"):
file_path = file_path.with_stem(file_path.stem + '_' + args.scenario_name)
write_csv(data, file_path, propagate_errors=args.propagate_mode_errors)

file_path = args.results_directory / "rotation_summary.csv"
if vars(args).get("scenario_name"):
file_path = file_path.with_stem(file_path.stem + '_' + args.scenario_name)
with open_for_csv(file_path) as f:
csv_writer = csv.DictWriter(f, list(rotation_infos[0].keys()))
csv_writer.writeheader()
csv_writer.writerows(rotation_infos)
Expand All @@ -297,6 +319,8 @@ def generate(schedule, scenario, args):
# summary of used vehicle types and all costs
if args.cost_calculation:
file_path = args.results_directory / "summary_vehicles_costs.csv"
if vars(args).get("scenario_name"):
file_path = file_path.with_stem(file_path.stem + '_' + args.scenario_name)
csv_report = None
try:
csv_report = scenario.costs.to_csv_lists()
Expand Down
5 changes: 4 additions & 1 deletion simba/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,8 @@ def create_results_directory(args, i):
args.results_directory.mkdir(parents=True, exist_ok=True)
# save used modes in report version
used_modes = ['sim'] + [m for m in args.mode[:i] if m not in ['sim', 'report']]
with open(args.results_directory / "used_modes.txt", "w", encoding='utf-8') as f:
file_path = args.results_directory / "used_modes.txt"
if vars(args).get("scenario_name"):
file_path = file_path.with_stem(file_path.stem + '_' + args.scenario_name)
with open(file_path, "w", encoding='utf-8') as f:
f.write(f"Used modes in this scenario: {', '.join(used_modes)}")
5 changes: 3 additions & 2 deletions simba/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,11 @@ def setup_logging(args, time_str):
def get_args():
parser = argparse.ArgumentParser(
description='SimBA - Simulation toolbox for Bus Applications.')
parser.add_argument('--scenario-name', help='Identifier of scenario, appended to results')

# #### Paths #####
parser.add_argument('--input-schedule',
help='Path to CSV file containing all trips of schedule to be analyzed.')
help='Path to CSV file containing all trips of schedule to be analyzed')
parser.add_argument('--output-directory', default="data/sim_outputs",
help='Location where all simulation outputs are stored')
parser.add_argument('--electrified-stations', help='include electrified_stations json')
Expand Down Expand Up @@ -302,7 +303,7 @@ 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,
parser.add_argument('--propagate-mode-errors', action='store_true',
help='Re-raise errors instead of continuing during simulation modes')
parser.add_argument('--create-scenario-file', help='Write scenario.json to file')
parser.add_argument('--create-trips-in-report', action='store_true',
Expand Down

0 comments on commit 3963722

Please sign in to comment.