From 098fc04f3d33014fb3d9a8dc55e21a0e2a577eb8 Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Tue, 6 Nov 2018 15:04:23 -0500 Subject: [PATCH] bug fix: not displaying all mech steps --- engine/configProcessor.py | 35 +++++++++++++++---- engine/mechanismExecutor.py | 10 ++---- engine/run.py | 9 +++-- engine/utils.py | 31 ++++++++++++++--- ui/config.py | 68 +++++++++++++++++++++---------------- 5 files changed, 101 insertions(+), 52 deletions(-) diff --git a/engine/configProcessor.py b/engine/configProcessor.py index bf87437..6ab874b 100644 --- a/engine/configProcessor.py +++ b/engine/configProcessor.py @@ -31,18 +31,39 @@ def create_matrix_field(mechanisms, key): return pd.DataFrame({'empty' : []}) -def generate_config(mechanisms, env_poc): +def generate_config(state_dict, mechanisms, exo_proc): def no_behavior_handler(bdf, sdf): if bdf.empty == False: sdf_values, bdf_values = sdf.values.tolist(), bdf.values.tolist() else: sdf_values = sdf.values.tolist() - bdf_values = [[b_identity] * len(sdf_values)] + # print(sdf_values) + bdf_values = [ [b_identity] * len(sdf_values) for n in range(mechanisms) ] + # print(bdf_values) return sdf_values, bdf_values - bdf = create_matrix_field(mechanisms, 'behaviors') - sdf = create_matrix_field(mechanisms, 'states') - sdf_values, bdf_values = no_behavior_handler(bdf, sdf) - zipped_list = list(zip(sdf_values, bdf_values)) + def only_ep_handler(state_dict): + sdf_functions = [ lambda step, sL, s, _input: (k, v) for k, v in zip(state_dict.keys(), state_dict.values()) ] + sdf_values = [ sdf_functions ] + bdf_values = [ [b_identity] * len(sdf_values) ] + return sdf_values, bdf_values - return list(map(lambda x: (x[0] + env_poc, x[1]), zipped_list)) \ No newline at end of file + zipped_list = [] + if len(mechanisms) != 0: + bdf = create_matrix_field(mechanisms, 'behaviors') + sdf = create_matrix_field(mechanisms, 'states') + sdf_values, bdf_values = no_behavior_handler(bdf, sdf) + zipped_list = list(zip(sdf_values, bdf_values)) + else: + sdf_values, bdf_values = only_ep_handler(state_dict) + zipped_list = list(zip(sdf_values, bdf_values)) + + return list(map(lambda x: (x[0] + exo_proc, x[1]), zipped_list)) + +def create_tensor_field(mechanisms, exo_proc, keys=['behaviors', 'states']): + dfs = [ create_matrix_field(mechanisms, k) for k in keys ] + df = pd.concat(dfs, axis=1) + for es, i in zip(exo_proc, range(len(exo_proc))): + df['es'+str(i)] = es + df['m'] = df.index + 1 + return df \ No newline at end of file diff --git a/engine/mechanismExecutor.py b/engine/mechanismExecutor.py index 7231b57..84ebfc5 100644 --- a/engine/mechanismExecutor.py +++ b/engine/mechanismExecutor.py @@ -4,11 +4,9 @@ from fn import op, _ def getColResults(step, sL, s, funcs): return list(map(lambda f: f(step, sL, s), funcs)) - def getBehaviorInput(step, sL, s, funcs): return op.foldr(_ + _)(getColResults(step, sL, s, funcs)) - def apply_env_proc(env_processes, state_dict, step): for state in state_dict.keys(): if state in list(env_processes.keys()): @@ -28,10 +26,12 @@ def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step): _input = exception_handler(getBehaviorInput, m_step, sL, last_in_obj, behavior_funcs) + # print(len(state_funcs)) + last_mut_obj = dict([ exception_handler(f, m_step, sL, last_mut_obj, _input) for f in state_funcs ]) - print(str(m_step) + ': ' + str(last_mut_obj)) + # print(str(m_step) + ': ' + str(last_mut_obj)) apply_env_proc(env_processes, last_mut_obj, last_mut_obj['timestamp']) @@ -45,11 +45,7 @@ def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step): def block_gen(states_list, configs, env_processes, t_step): m_step = 0 - print("states_list") - print(states_list) states_list_copy = deepcopy(states_list) - print("states_list_copy") - print(states_list_copy) genesis_states = states_list_copy[-1] genesis_states['mech_step'], genesis_states['time_step'] = m_step, t_step states_list = [genesis_states] diff --git a/engine/run.py b/engine/run.py index 5a7a940..71a5537 100644 --- a/engine/run.py +++ b/engine/run.py @@ -1,15 +1,18 @@ from ui.config import state_dict, mechanisms, exogenous_states, env_processes, sim_config -from engine.configProcessor import generate_config +from engine.configProcessor import generate_config, create_tensor_field from engine.mechanismExecutor import simulation from engine.utils import flatten - +from tabulate import tabulate #from tabulate import tabulate import pandas as pd def main(): states_list = [state_dict] ep = list(exogenous_states.values()) - configs = generate_config(mechanisms, ep) + configs = generate_config(state_dict, mechanisms, ep) + print(len(configs)) + print(tabulate(create_tensor_field(mechanisms, ep), headers='keys', tablefmt='psql')) + print # print(configs) # print(states_list) # print(configs) diff --git a/engine/utils.py b/engine/utils.py index 9adbd00..34d7a68 100644 --- a/engine/utils.py +++ b/engine/utils.py @@ -35,14 +35,13 @@ def bound_norm_random(rng, low, high): res = bound_norm_random(rng, low, high) return Decimal(res) -def env_proc(trigger_step, update_f): - def env_step_trigger(trigger_step, update_f, step): +def proc_trigger(trigger_step, update_f): + def step_trigger(trigger_step, update_f, step): if step == trigger_step: return update_f else: return lambda x: x - return partial(env_step_trigger, trigger_step, update_f) - + return partial(step_trigger, trigger_step, update_f) # accept timedelta instead of timedelta params def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=30): @@ -63,4 +62,26 @@ def ep_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', days=0, minutes=0, s # for es, i in zip(env_poc, range(len(env_poc))): # df['es'+str(i)] = es # df['m'] = df.index + 1 -# return df \ No newline at end of file +# return df +################# + +# def exo_proc_trigger(mech_step, update_f, y): +# if mech_step == 1: +# return update_f +# else: +# return lambda step, sL, s, _input: (y, s[y]) + + + +# def apply_exo_proc(s, x, y): +# if s['mech_step'] == 1: +# return x +# else: +# return s[y] + +# def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params +# y = 'timestamp' +# x = ep_time_step(s, s['timestamp'], seconds=1) +# return (y, x) + + diff --git a/ui/config.py b/ui/config.py index 19690ea..e5d7df2 100644 --- a/ui/config.py +++ b/ui/config.py @@ -1,4 +1,4 @@ -from engine.utils import bound_norm_random, ep_time_step, env_proc +from engine.utils import bound_norm_random, ep_time_step, proc_trigger import numpy as np from decimal import Decimal @@ -60,12 +60,20 @@ proc_one_coef_A = 0.7 proc_one_coef_B = 1.3 def es3p1(step, sL, s, _input): y = 's3' - x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B) - return (y, x) + if s['mech_step']+1 == 1: # inside f body to reduce performance costs + x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B) + return (y, x) + else: + return (y, s[y]) + def es4p2(step, sL, s, _input): y = 's4' - x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) - return (y, x) + if s['mech_step']+1 == 1: # inside f body to reduce performance costs + x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) + return (y, x) + else: + return (y, s[y]) + def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params y = 'timestamp' x = ep_time_step(s, s['timestamp'], seconds=1) @@ -95,8 +103,8 @@ exogenous_states = { } env_processes = { - "s3": env_proc('2018-10-01 15:16:25', env_a), - "s4": env_proc('2018-10-01 15:16:25', env_b) + "s3": proc_trigger('2018-10-01 15:16:25', env_a), + "s4": proc_trigger('2018-10-01 15:16:25', env_b) } # test return vs. non-return functions as lambdas @@ -105,34 +113,34 @@ env_processes = { mechanisms = { "m1": { "behaviors": { - # "b1": b1m1, # lambda step, sL, s: s['s1'] + 1, + "b1": b1m1, # lambda step, sL, s: s['s1'] + 1, "b2": b2m1 }, "states": { # exclude only. TypeError: reduce() of empty sequence with no initial value - # "s1": s1m1, - # "s2": s2m1 + "s1": s1m1, + "s2": s2m1 } }, - # "m2": { - # "behaviors": { - # "b1": b1m2, - # "b2": b2m2 - # }, - # "states": { - # "s1": s1m2, - # "s2": s2m2 - # } - # }, - # "m3": { - # "behaviors": { - # "b1": b1m3, - # "b2": b2m3 #toggle for error - # }, - # "states": { - # "s1": s1m3, - # "s2": s2m3 - # } - # } + "m2": { + "behaviors": { + "b1": b1m2, + "b2": b2m2 + }, + "states": { + "s1": s1m2, + "s2": s2m2 + } + }, + "m3": { + "behaviors": { + "b1": b1m3, + "b2": b2m3 #toggle for error + }, + "states": { + "s1": s1m3, + "s2": s2m3 + } + } } sim_config = {