Skip to content

Commit

Permalink
make create_run_directory(), which creates a run directory for a simu…
Browse files Browse the repository at this point in the history
…lation in a date/time directory
  • Loading branch information
artgoldberg committed Dec 15, 2020
1 parent ba555cd commit fa39c31
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tests/testing/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from wc_sim.sim_config import WCSimulationConfig
from wc_sim.simulation import Simulation
from wc_sim.testing.make_models import MakeModel
from wc_sim.testing.utils import check_simul_results, plot_expected_vs_simulated, get_expected_dependencies
from wc_sim.testing.utils import (check_simul_results, plot_expected_vs_simulated, get_expected_dependencies,
create_run_directory)


class TestTestingUtils(unittest.TestCase):
Expand Down Expand Up @@ -141,3 +142,12 @@ def test_expected_dependencies(self):
eds = get_expected_dependencies()
self.assertEqual(eds['DynamicStopCondition']['reaction_9'], {'stop_condition_7'})
self.assertEqual(eds['DynamicStopCondition']['reaction_10'], set())

def test_create_run_directory(self):
def run_dir_test(self, run_dir):
self.assertTrue(run_dir)
date_dir = os.path.abspath(os.path.join(run_dir, ".."))
shutil.rmtree(date_dir)

run_dir_test(self, create_run_directory())
run_dir_test(self, create_run_directory(base_dir='/tmp/runs', in_repo=False))
23 changes: 23 additions & 0 deletions wc_sim/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from matplotlib.backends.backend_pdf import PdfPages
import ast
import copy
import datetime
import math
import numpy as np
import os
Expand Down Expand Up @@ -518,3 +519,25 @@ def get_expected_dependencies():
if dependent_ids == '':
expected_dependencies[expr_type][rxn_id] = set()
return expected_dependencies


def create_run_directory(base_dir='runs', in_repo=True):
""" Create a run directory in a date/time directory for a simulation
Args:
base_dir (:obj:`str`, optional): base directory of the run directory
in_repo (:obj:`boolean`, optional): whether the base directory is relative to the `wc_sim` repo
Returns:
:obj:`str`: a directory for a simulation run
"""
root = '/'
if in_repo:
root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
base = os.path.join(root, base_dir)
now = datetime.datetime.now()
date_dir = now.date().isoformat()
time_dir = now.time().isoformat()
run_dir = os.path.join(base, date_dir, time_dir)
os.makedirs(run_dir)
return run_dir

0 comments on commit fa39c31

Please sign in to comment.