middleware pt 2

This commit is contained in:
Joshua E. Jodesty 2019-02-04 20:13:28 -05:00
parent 20a8bd3026
commit 17362884dc
4 changed files with 57 additions and 66 deletions

View File

@ -37,6 +37,9 @@ def dict_op(f, d1, d2):
else: else:
return target_dict[key] return target_dict[key]
# print(d1)
# print(d2)
# print()
key_set = set(list(d1.keys()) + list(d2.keys())) key_set = set(list(d1.keys()) + list(d2.keys()))
return {k: f(set_base_value(d1, d2, k), set_base_value(d2, d1, k)) for k in key_set} return {k: f(set_base_value(d1, d2, k), set_base_value(d2, d1, k)) for k in key_set}

View File

@ -18,7 +18,7 @@ class Executor:
def get_col_results(step, sL, s, funcs): def get_col_results(step, sL, s, funcs):
return list(map(lambda f: curry_pot(f, step, sL, s), funcs)) return list(map(lambda f: curry_pot(f, step, sL, s), funcs))
print(get_col_results(step, sL, s, funcs)) # print(get_col_results(step, sL, s, funcs))
return foldr(call, get_col_results(step, sL, s, funcs))(ops) return foldr(call, get_col_results(step, sL, s, funcs))(ops)
def apply_env_proc(self, env_processes, state_dict, step): def apply_env_proc(self, env_processes, state_dict, step):

View File

@ -65,6 +65,7 @@ def rename(new_name, f):
f.__name__ = new_name f.__name__ = new_name
return f return f
def curry_pot(f, *argv): def curry_pot(f, *argv):
sweep_ind = f.__name__[0:5] == 'sweep' sweep_ind = f.__name__[0:5] == 'sweep'
arg_len = len(argv) arg_len = len(argv)

View File

@ -7,6 +7,9 @@ from SimCAD import configs
from SimCAD.configuration import Configuration from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \ from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step, parameterize_mechanism, parameterize_states, sweep ep_time_step, parameterize_mechanism, parameterize_states, sweep
from SimCAD.utils import rename
from fn.func import curried
pp = pprint.PrettyPrinter(indent=4) pp = pprint.PrettyPrinter(indent=4)
@ -22,28 +25,30 @@ seed = {
# Behaviors per Mechanism # Behaviors per Mechanism
def b1m1(step, sL, s): # @curried
def b1m1(param, step, sL, s):
return {'param1': 1} return {'param1': 1}
# @curried
def b2m1(step, sL, s): def b2m1(param, step, sL, s):
return {'param2': 4} return {'param2': 4}
# @curried # @curried
def b1m2(param, step, sL, s): def b1m2(param, step, sL, s):
return {'param1': 'a', 'param2': param} return {'param1': 'a', 'param2': param}
# @curried
def b2m2(step, sL, s): def b2m2(param, step, sL, s):
return {'param1': 'b', 'param2': 0} return {'param1': 'b', 'param2': 0}
# @curried
def b1m3(step, sL, s): def b1m3(param, step, sL, s):
return {'param1': np.array([10, 100])} return {'param1': np.array([10, 100])}
# @curried
def b2m3(step, sL, s): def b2m3(param, step, sL, s):
return {'param1': np.array([20, 200])} return {'param1': np.array([20, 200])}
# Internal States per Mechanism # Internal States per Mechanism
def s1m1(step, sL, s, _input): # @curried
def s1m1(param, step, sL, s, _input):
y = 's1' y = 's1'
x = 0 x = 0
return (y, x) return (y, x)
@ -53,22 +58,23 @@ def s2m1(param, step, sL, s, _input):
y = 's2' y = 's2'
x = param x = param
return (y, x) return (y, x)
# @curried
def s1m2(step, sL, s, _input): def s1m2(param, step, sL, s, _input):
y = 's1' y = 's1'
x = _input['param2'] x = _input['param2']
return (y, x) return (y, x)
def s2m2(step, sL, s, _input): # @curried
def s2m2(param, step, sL, s, _input):
y = 's2' y = 's2'
x = _input['param2'] x = _input['param2']
return (y, x) return (y, x)
# @curried
def s1m3(step, sL, s, _input): def s1m3(param, step, sL, s, _input):
y = 's1' y = 's1'
x = 0 x = 0
return (y, x) return (y, x)
# @curried
def s2m3(step, sL, s, _input): def s2m3(param, step, sL, s, _input):
y = 's2' y = 's2'
x = 0 x = 0
return (y, x) return (y, x)
@ -83,7 +89,7 @@ def es3p1(param, step, sL, s, _input):
y = 's3' y = 's3'
x = s['s3'] + param x = s['s3'] + param
return (y, x) return (y, x)
# @curried
def es4p2(param, step, sL, s, _input): def es4p2(param, step, sL, s, _input):
y = 's4' y = 's4'
x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) + param x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) + param
@ -91,7 +97,8 @@ def es4p2(param, step, sL, s, _input):
ts_format = '%Y-%m-%d %H:%M:%S' ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1) t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input): # @curried
def es5p2(param, step, sL, s, _input):
y = 'timestamp' y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta) x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x) return (y, x)
@ -153,44 +160,44 @@ env_processes = {
mechanisms_test = { mechanisms_test = {
"m1": { "m1": {
"behaviors": { "behaviors": {
"b1": b1m1, "b1": b1m1,#(0),
"b2": b2m1 "b2": b2m1#(0)
}, },
"states": { "states": {
"s1": s1m1, "s1": s1m1,#(0),
"s2": "sweep" "s2": "sweep"
} }
}, },
"m2": { "m2": {
"behaviors": { "behaviors": {
"b1": "sweep", "b1": "sweep",
"b2": b2m2 "b2": b2m2,#(0)
}, },
"states": { "states": {
"s1": s1m2, "s1": s1m2,#(0),
"s2": s2m2 "s2": s2m2#(0)
} }
}, },
"m3": { "m3": {
"behaviors": { "behaviors": {
"b1": b1m3, "b1": b1m3,#(0),
"b2": b2m3 "b2": b2m3,#(0)
}, },
"states": { "states": {
"s1": s1m3, "s1": s1m3,#(0),
"s2": s2m3 "s2": s2m3#(0)
} }
} }
} }
from copy import deepcopy from copy import deepcopy
from funcy import curry from funcy import curry
from inspect import getfullargspec
def sweep_identifier(sweep_list, sweep_id_list, mechanisms): def sweep_identifier(sweep_list, sweep_id_list, mechanisms):
new_mechanisms = deepcopy(mechanisms) new_mechanisms = deepcopy(mechanisms)
for x in sweep_id_list: for x in sweep_id_list:
mech, update_type, update, f = x[0], x[1], x[2], x[3]
current_f = new_mechanisms[x[0]][x[1]][x[2]] current_f = new_mechanisms[x[0]][x[1]][x[2]]
if current_f is 'sweep': if current_f is 'sweep':
new_mechanisms[x[0]][x[1]][x[2]] = sweep(sweep_list, x[3]) new_mechanisms[x[0]][x[1]][x[2]] = sweep(sweep_list, x[3])
@ -198,8 +205,20 @@ def sweep_identifier(sweep_list, sweep_id_list, mechanisms):
# for mech, update_types in new_mechanisms.items(): # for mech, update_types in new_mechanisms.items():
# for update_type, fkv in update_types.items(): # for update_type, fkv in update_types.items():
# for sk, current_f in fkv.items(): # for sk, current_f in fkv.items():
# if current_f != 'sweep' and isinstance(current_f, list) == False: # if current_f != 'sweep' and isinstance(current_f, list) is False:
# new_mechanisms[mech][update_type][sk] = curry(f)(0) # # new_mechanisms[mech][update_type][sk] = rename("unsweeped", current_f(0))
# curried_f = curry(current_f)
#
# def uncurried_beh_func(a, b, c):
# return curried_f(0)(a)(b)(c)
#
# def uncurried_state_func(a, b, c, d):
# return curried_f(0)(a)(b)(c)(d)
#
# if update_type == 'behaviors':
# new_mechanisms[mech][update_type][sk] = uncurried_beh_func
# elif update_type == 'states':
# new_mechanisms[mech][update_type][sk] = uncurried_state_func
del mechanisms del mechanisms
return new_mechanisms return new_mechanisms
@ -207,42 +226,10 @@ def sweep_identifier(sweep_list, sweep_id_list, mechanisms):
sweep_id_list = [('m1', 'states', 's2', s2m1), ('m2', 'behaviors', 'b1', b1m2)] sweep_id_list = [('m1', 'states', 's2', s2m1), ('m2', 'behaviors', 'b1', b1m2)]
# pp.pprint(sweep_identifier(beta, sweep_id_list, mechanisms_test)) # pp.pprint(sweep_identifier(beta, sweep_id_list, mechanisms_test))
# exit()
mechanisms = sweep_identifier(beta, sweep_id_list, mechanisms_test) mechanisms = sweep_identifier(beta, sweep_id_list, mechanisms_test)
# mechanisms = {
# "m1": {
# "behaviors": {
# "b1": b1m1,
# "b2": b2m1
# },
# "states": {
# "s1": s1m1,
# "s2": sweep(beta, s2m1) #s2m1(1) #sweep(beta, s2m1)
# }
# },
# "m2": {
# "behaviors": {
# "b1": sweep(beta, b1m2), #b1m2(1) #sweep(beta, b1m2),
# "b2": b2m2
# },
# "states": {
# "s1": s1m2,
# "s2": s2m2
# }
# },
# "m3": {
# "behaviors": {
# "b1": b1m3,
# "b2": b2m3
# },
# "states": {
# "s1": s1m3,
# "s2": s2m3
# }
# }
# }
parameterized_mechanism = parameterize_mechanism(mechanisms) parameterized_mechanism = parameterize_mechanism(mechanisms)
pp.pprint(parameterized_mechanism) pp.pprint(parameterized_mechanism)
# exit() # exit()