Skip to content

Commit

Permalink
Merge pull request #110 from NREL/update-test-output
Browse files Browse the repository at this point in the history
fixing test_output folder and related issues
  • Loading branch information
calbaker authored Mar 26, 2024
2 parents 92da6ca + 3d17c4b commit 2f00b8d
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 97 deletions.
1 change: 0 additions & 1 deletion python/fastsim/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
test_output/
3 changes: 2 additions & 1 deletion python/fastsim/demos/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
plots/
*.csv
test_output/
demo_output/
test_output/
28 changes: 24 additions & 4 deletions python/fastsim/demos/cav_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,31 @@
import matplotlib.pyplot as plt
import numpy as np

import pathlib
import tempfile
import os

import fastsim
from fastsim.tests.test_coasting import make_coasting_plot


DO_SHOW = False
FRACTION_EXTENDED_TIME = 0.25
ABSOLUTE_EXTENDED_TIME_S = 20.0 # 180.0
OUTPUT_DIR = Path(__file__).parent.absolute() / 'test_output'
THIS_DIR = pathlib.Path(__file__).parent.absolute()
# If the current directory is the fastsim installation directory, then the
# output directory should be temporary directory
if "site-packages/fastsim" in str(pathlib.Path(THIS_DIR)):
OUTPUT_DIR_FULL = tempfile.TemporaryDirectory()
OUTPUT_DIR = OUTPUT_DIR_FULL.name
is_temp_dir = True
# If the current directory is not the fastsim isntallation directory, find or
# create "demo_output" directory to save outputs
else:
OUTPUT_DIR = pathlib.Path(THIS_DIR) / "demo_output"
if not OUTPUT_DIR.exists():
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
is_temp_dir = False
MIN_ECO_CRUISE_TARGET_SPEED_m_per_s = 8.0
ECO_COAST_ALLOW_PASSING = False
RESAMPLE_TO_1HZ = True
Expand Down Expand Up @@ -77,7 +94,7 @@ def make_save_file(prefix, postfix, save_dir=None, use_rust=False):
prefix_addition = ''
if use_rust:
prefix_addition = '_rust'
return save_dir / f'{prefix}{prefix_addition}_{postfix}'
return os.path.join(save_dir, f'{prefix}{prefix_addition}_{postfix}')
return None


Expand Down Expand Up @@ -303,7 +320,7 @@ def run_for_powertrain(save_dir, outputs, cyc_name, veh, powertrain, init_soc=No
def main(cycle_name=None, powertrain=None, do_show=None, use_rust=False, verbose=True, save_dir=None, maneuver=None):
"""
"""
if save_dir is not None:
if save_dir is not None and save_dir != OUTPUT_DIR:
save_dir.mkdir(parents=True, exist_ok=True)
cyc_names = [cycle_name] if cycle_name is not None else [
"hwfet", "udds", "us06", "NREL13", "trapz", "trapz-x2", "stacked-trapz", "TSDC_tripno_42648_cycle",
Expand Down Expand Up @@ -339,7 +356,7 @@ def main(cycle_name=None, powertrain=None, do_show=None, use_rust=False, verbose

keys = CSV_KEYS
if save_dir is not None:
with open(save_dir / 'output.csv', 'w', newline='') as csvfile:
with open(os.path.join(save_dir, 'output.csv'), 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(keys)
for item in outputs:
Expand Down Expand Up @@ -373,3 +390,6 @@ def main(cycle_name=None, powertrain=None, do_show=None, use_rust=False, verbose
print("PLATFORM : PURE PYTHON")
sys.stdout.flush()
main(cycle_name, powertrain=powertrain, do_show=do_show, use_rust=use_rust, save_dir=OUTPUT_DIR)

if is_temp_dir:
OUTPUT_DIR_FULL.cleanup()
29 changes: 22 additions & 7 deletions python/fastsim/demos/vehicle_import_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@
import fastsim.fastsimrust as fsr
import fastsim.utils as utils

import tempfile

# for testing demo files, false when running automatic tests
SHOW_PLOTS = utils.show_plots()
SAVE_OUTPUT = SHOW_PLOTS

# %%
# Setup some directories
THIS_DIR = pathlib.Path(__file__).parent.absolute()
OUTPUT_DIR = pathlib.Path(THIS_DIR) / "test_output"
if not OUTPUT_DIR.exists():
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
if SHOW_PLOTS:
# Setup some directories
THIS_DIR = pathlib.Path(__file__).parent.absolute()
# If the current directory is the fastsim installation directory, then the
# output directory should be temporary directory
if "site-packages/fastsim" in str(pathlib.Path(THIS_DIR)):
OUTPUT_DIR_FULL = tempfile.TemporaryDirectory()
OUTPUT_DIR = OUTPUT_DIR_FULL.name
is_temp_dir = True
# If the current directory is not the fastsim installation directory, find or
# create "demo_output" directory to save outputs
else:
OUTPUT_DIR = pathlib.Path(THIS_DIR) / "demo_output"
if not OUTPUT_DIR.exists():
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
is_temp_dir = False

# %%
# List available options for the given year / make / model
Expand Down Expand Up @@ -75,7 +89,8 @@

rv = fsr.vehicle_import_by_id_and_year(data.id, int(year), other_inputs)

rv.to_file(OUTPUT_DIR / "demo-vehicle.yaml")
if SAVE_OUTPUT:
rv.to_file(OUTPUT_DIR / "demo-vehicle.yaml")

# %%
# Alternative API for importing all vehicles at once
Expand All @@ -92,4 +107,4 @@
vehs = fsr.import_all_vehicles(int(year), make, model, other_inputs)
if SHOW_PLOTS:
for v in vehs:
print(f"Imported {v.scenario_name}")
print(f"Imported {v.scenario_name}")
Loading

0 comments on commit 2f00b8d

Please sign in to comment.