Skip to content

Commit

Permalink
Merge pull request #10 from IGITUGraz/ArbitraryOutputDirs
Browse files Browse the repository at this point in the history
Make it necessary to specify the output directory to SimManager.

the earlier directory name can be created using the 'make_param_string'.
Additional changes:
    imported tools within the __init__.py of the tools package
    make optional arguments compulsory keyword arguments
  • Loading branch information
maharjun authored Aug 23, 2021
2 parents cb12f94 + 3b4fd77 commit f823e66
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion simmanager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = "Arjun Rao, Anand Subramoney"
__version__ = "0.8.3"
__version__ = "0.9.0"

from .paths import Paths
from .simmanager import SimManager
Expand Down
20 changes: 0 additions & 20 deletions simmanager/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,6 @@ def _get_output(args, input_str=None, as_bytes=False):
return stdout_, stderr_


def make_param_string(delimiter='-', **kwargs):
"""
Takes a dictionary and constructs a string of the form key1-val1-key2-val2-... (denoted here as {key-val*})
The keys are alphabetically sorted
:param str delimiter: Delimiter to use (default is '-')
:param dict kwargs: A python dictionary
:return:
"""
string_list = []
for key, value in sorted(kwargs.items()):
string_list.append(key)
if isinstance(value, float):
value_string = "{:.2f}".format(value)
else:
value_string = "{}".format(value)
string_list.append(value_string)
param_string = delimiter.join(string_list)
return param_string


def order_dict_alphabetically(d):
"""
Sort a given dictionary alphabetically
Expand Down
2 changes: 1 addition & 1 deletion simmanager/paths.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from ._utils import make_param_string
from .tools import make_param_string

__author__ = 'anand'

Expand Down
23 changes: 11 additions & 12 deletions simmanager/simmanager.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os

from .tools.stdouthandling import stdout_teed
from .tools.params import make_param_string
from .simmetadatamanager import SimMetadataManager, SimMetadataManagerError
from .paths import Paths
import subprocess
import shlex
from ._utils import _rm_anything_recursive
from ._utils import _get_output

from ._utils import make_param_string
from ._utils import order_dict_alphabetically

__author__ = 'anand'
Expand Down Expand Up @@ -53,10 +53,12 @@ class SimManager:
:param sim_name: Simulation Name
:param root_dir: Root directory containing results. See point 1. above for more
details
:param output_dir_name: string containing the name of the output
directory. If specified, param_dict and suffix are ignored.
:param param_dict: Dictionary in the form of ``dict(paramname1=param1val, paramname2=param2val)``.
See point 1. above to see it's usage in creating the output directory.
Default is an empty dictionary.
:param suffix: Suffix used for various output files. This is passed on as an
:param file_suffix: Suffix used for various output files. This is passed on as an
argument in the creation of the contained Paths object
:param write_protect_dirs: Enable/Disable write protecting the directories
:param tee_stdx_to: Give file name here to tee the output to the provided file name.
Expand All @@ -68,25 +70,26 @@ class SimManager:
from the EDITOR environment variable. It defaults to vim.
"""

def __init__(self, sim_name, root_dir, param_dict={}, suffix="", write_protect_dirs=True, tee_stdx_to=None, open_desc_for_edit=False):
def __init__(self, sim_name, root_dir, output_dir_name, *, file_suffix="", write_protect_dirs=True, tee_stdx_to=None, open_desc_for_edit=False):

self._sim_name = sim_name
self._root_dir = root_dir
if not os.path.exists(root_dir):
raise SimManagerError("The root directory {} does not exist. Please create it.".format(root_dir))
self._suffix = suffix
self._param_combo = order_dict_alphabetically(param_dict)
self._output_dir_name = output_dir_name
self._file_suffix = file_suffix

self._write_protect_dirs = write_protect_dirs
self.tee_stdx_to = tee_stdx_to
self.open_desc_for_edit = open_desc_for_edit
self.stdout_redirected_obj = None

def __enter__(self):
output_dir_path = self._aquire_output_dir()
self._paths = Paths(output_dir_path, suffix=self._suffix)
self._paths = Paths(output_dir_path, suffix=self._file_suffix)
try:
self._store_sim_reproduction_data()
except SimMetadataManagerError as E:
except SimMetadataManagerError:
_rm_anything_recursive(output_dir_path)
raise
try:
Expand Down Expand Up @@ -118,11 +121,7 @@ def _aquire_output_dir(self):
in it. If directory already exists, raise a :class:`PathsError`
complaining about this.
"""
param_string = make_param_string(**self._param_combo)
if param_string == "":
output_dir_path = os.path.join(self._root_dir, self._sim_name)
else:
output_dir_path = os.path.join(self._root_dir, self._sim_name, param_string)
output_dir_path = os.path.join(self._root_dir, self._sim_name, self._output_dir_name)
try:
os.makedirs(output_dir_path)
except OSError:
Expand Down
5 changes: 5 additions & 0 deletions simmanager/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .timer import Timer
from .stdouthandling import stdout_teed
from .stdouthandling import stdout_redirected
from .stdouthandling import stdout_discarded
from .params import make_param_string
18 changes: 18 additions & 0 deletions simmanager/tools/params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def make_param_string(delimiter='-', **kwargs):
"""
Takes a dictionary and constructs a string of the form key1-val1-key2-val2-... (denoted here as {key-val*})
The keys are alphabetically sorted
:param str delimiter: Delimiter to use (default is '-')
:param dict kwargs: A python dictionary
:return:
"""
string_list = []
for key, value in sorted(kwargs.items()):
string_list.append(key)
if isinstance(value, float):
value_string = "{:.2f}".format(value)
else:
value_string = "{}".format(value)
string_list.append(value_string)
param_string = delimiter.join(string_list)
return param_string

0 comments on commit f823e66

Please sign in to comment.