From b4cfeef00f3b57f5f046b87121615ecc1407f133 Mon Sep 17 00:00:00 2001 From: Chad Baker Date: Fri, 27 Oct 2023 15:10:38 -0600 Subject: [PATCH] improved path handling to be user/platform agnostic --- applications/demos/rollout_demo.py | 2 +- python/altrios/rollout.py | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/applications/demos/rollout_demo.py b/applications/demos/rollout_demo.py index 6b911628..ab2dd3cb 100644 --- a/applications/demos/rollout_demo.py +++ b/applications/demos/rollout_demo.py @@ -27,7 +27,7 @@ scenario_infos, metrics = rollout.simulate_prescribed_rollout( max_bel_share=target, number_of_years=1, - results_folder = 'C:/Users/mbruchon/Documents/Repos/ALTRIOS/Case Study/Rollout Results', + results_folder=Path(__file__).parent / "results/case study/", demand_file_path=File, train_planner_config=train_planner_config, count_unused_locomotives=False, diff --git a/python/altrios/rollout.py b/python/altrios/rollout.py index f48e0f6e..b6ad4355 100644 --- a/python/altrios/rollout.py +++ b/python/altrios/rollout.py @@ -7,16 +7,16 @@ import time import pandas as pd import polars as pl -from typing import List, Tuple, Optional +from typing import List, Tuple, Optional, Union from pathlib import Path import os DEBUG = True - def simulate_prescribed_rollout( max_bel_share: float, number_of_years: int, + results_folder: Path, start_year: int = defaults.BASE_ANALYSIS_YEAR, # If you do not have this path, please add it! We are trying to keep confidential documents out of the git repo network_filename_path: str = str(alt.resources_root() / "networks/Taconite.yaml"), @@ -24,7 +24,6 @@ def simulate_prescribed_rollout( freight_demand_percent_growth:float = 0.0, demand_file_path= defaults.DEMAND_FILE, train_planner_config: train_planner.TrainPlannerConfig = train_planner.TrainPlannerConfig(), - results_folder: Optional[str] = '../../Case Study/Rollout Results', count_unused_locomotives = False, write_complete_results: Optional[bool] = False, write_metrics: Optional[bool] = False, @@ -35,11 +34,18 @@ def simulate_prescribed_rollout( target_bel_shares[0] = max_bel_share else: for idx, _ in enumerate(target_bel_shares): - if idx==0: target_bel_shares[idx] = 0.0 - else: target_bel_shares[idx] = ((idx) / (len(years)-1)) * max_bel_share + if idx==0: + target_bel_shares[idx] = 0.0 + else: + target_bel_shares[idx] = ((idx) / (len(years)-1)) * max_bel_share - save_dir = Path(results_folder) + save_dir = Path(results_folder) # make sure it's a path save_dir.mkdir(exist_ok=True, parents=True) + with open(save_dir / "README.md", "w") as file: + file.writelines(["This directory contains results from demo files and can usually be safely deleted."]) + with open(save_dir / ".gitignore", "w") as file: + file.writelines(["*"]) + base_freight_demand_df = pd.read_csv(demand_file_path) demand_paths = [] @@ -131,8 +137,16 @@ def simulate_prescribed_rollout( metrics = metric_calculator.main(scenarios) if write_metrics: - print(results_folder +'/Metrics_Demand {}_DemandFile {}.xlsx'.format(scenario_year, os.path.basename(demand_file_path)).replace('.csv','')) - metrics.to_pandas().to_excel(results_folder +'/Metrics_Demand {}_DemandFile {}.xlsx'.format(scenario_year, os.path.basename(demand_file_path)).replace('.csv','')) + print + (results_folder / + 'Metrics_Demand {}_DemandFile {}.xlsx'.format( + scenario_year, os.path.basename(demand_file_path) + ).replace('.csv','') + ) + metrics.to_pandas().to_excel( + results_folder / 'Metrics_Demand {}_DemandFile {}.xlsx'.format( + scenario_year, os.path.basename(demand_file_path)).replace('.csv','') + ) t3 = time.perf_counter() if DEBUG: