Commit e805fca1 authored by David Trudgian's avatar David Trudgian
Browse files

Compute parameter combinations and command lines

parent d9f18928
from subprocess import check_output
import os
import sys
import datetime
def LocalExecutor(object):
commands = []
logdir =""
def __init__(self, commands, cwd):
self.commands = commands
logdir = os.path.join(cwd, "param_runner_%s" % datetime.datetime.strftime('%Y%m%d-%H%M') )
try:
os.mkdir(logdir)
except IOError:
raise IOError("Could not create log directory: %s" % logdir)
self.logdir = logdir
import anyconfig
import os
import logging
from copy import deepcopy
from collections import OrderedDict
from yamllint.config import YamlLintConfig, YamlLintConfigError
from yamllint import linter
from yamllint.cli import Format
......@@ -31,14 +33,17 @@ class ParamFile(object):
Attributes:
param_file (str): path to the input parameter yaml file
vals (dict): Dictionary of configuration data extracted
vals (dict): Dictionary of configuration data extracted reflecting yaml
combination_count (int): Number of parameter combinations
commands: Array of commands to be run, computed from param ranges
"""
param_file = ''
vals = {}
params = OrderedDict()
combination_count = None
commands = []
mypath = os.path.dirname(os.path.abspath(__file__))
......@@ -115,6 +120,8 @@ class ParamFile(object):
metadata = anyconfig.load(self.param_file)
self.vals = metadata
self.__compute_param_ranges()
self.commands = self.__compute_commands()
def __compute_param_ranges(self):
"""Compute the parameter ranges as lists, from their definitions"""
......@@ -144,6 +151,102 @@ class ParamFile(object):
logger.info(" - Total combinations to explore: %d" % self.combination_count)
def __compute_commands(self):
"""Compute the commands to be run across parameter ranges
Returns:
cmd_arr(list): List of string commands to be executed over param space
"""
logger.info("Computing Commands...")
arg_combns = self.__recurse_arg_combns()
logger.info(" - Computed %d argument combinations" % len(arg_combns))
assert len(arg_combns) == self.combination_count
cmd_arr = []
for arg_combn in arg_combns:
arg_combn['__command'] = { 'value': self.__cmd_from_args(arg_combn) }
logger.info(" - Generated %d commands" % len(arg_combns))
return arg_combns
def __recurse_arg_combns(self, param_idx=0, current_combination=OrderedDict(), all_combinations=[]):
"""Recursively find a combinations of arguments for a execution
Args:
param_idx(int): Index of the current parameter explored
at this recursion level.
current_comination(dict): Current combination of args being built
down through the recursive levels.
all_combinations(list): List of argument combinations dicts computed so
far.
Returns:
all_combinations(list): List of argument combinations dicts computed so
far.
"""
params = self.vals['parameters']
# Tail of recursion - we have a combination so add it to our list
if param_idx == len(params):
all_combinations.append(current_combination)
return all_combinations
current_param = params[param_idx]
current_id = current_param['id']
current_flag = current_param.get('flag', None)
num_values = len(current_param['range'])
for val_idx in range(num_values):
current_value = current_param['range'][val_idx]
current_combination[current_id] = {'flag': current_flag, 'value': current_value }
all_combinations = self.__recurse_arg_combns(param_idx+1, deepcopy(current_combination), all_combinations)
return all_combinations
def __cmd_from_args(self, arg_combn):
"""Convert a dict of arguments into a full command string
Args:
arg_combn(dict): Dictionary of arguments with flag and value
Returns:
cmd_str(str): Executable command string
"""
cmd_str = self.vals['command'] + ' '
for arg in arg_combn.values():
if arg['flag'] and arg['value']:
cmd_str += arg['flag'] + ' '
if arg['value']:
cmd_str += str(arg['value']) + ' '
return cmd_str.rstrip(' ')
class IntRange(object):
"""Compute integer parameters from an int_range definition
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment