Merge pull request #7 from BlockScience/mk-missing-keys
Allow for missing keys in dictionaries of combined behaviors
This commit is contained in:
commit
0e276006a1
49
ui/config.py
49
ui/config.py
|
|
@ -27,47 +27,47 @@ seed = {
|
||||||
# Behaviors per Mechanism
|
# Behaviors per Mechanism
|
||||||
# Different return types per mechanism ??
|
# Different return types per mechanism ??
|
||||||
def b1m1(step, sL, s):
|
def b1m1(step, sL, s):
|
||||||
return {'param1': 1, 'param2': 2}
|
return {'param1': 1}
|
||||||
def b2m1(step, sL, s):
|
def b2m1(step, sL, s):
|
||||||
return {'param1': 3, 'param2': 4}
|
return {'param2': 4}
|
||||||
|
|
||||||
def b1m2(step, sL, s):
|
def b1m2(step, sL, s):
|
||||||
return {'param1': 1, 'param2': 2}
|
return {'param1': 'a', 'param2': 2}
|
||||||
def b2m2(step, sL, s):
|
def b2m2(step, sL, s):
|
||||||
return {'param1': 3, 'param2': 4}
|
return {'param1': 'b', 'param2': 4}
|
||||||
|
|
||||||
def b1m3(step, sL, s):
|
def b1m3(step, sL, s):
|
||||||
return {'param1': 1, 'param2': 2}
|
return {'param1': ['c'], 'param2': np.array([10, 100])}
|
||||||
def b2m3(step, sL, s):
|
def b2m3(step, sL, s):
|
||||||
return {'param1': 3, 'param2': 4}
|
return {'param1': ['d'], 'param2': np.array([20, 200])}
|
||||||
|
|
||||||
|
|
||||||
# Internal States per Mechanism
|
# Internal States per Mechanism
|
||||||
def s1m1(step, sL, s, _input):
|
def s1m1(step, sL, s, _input):
|
||||||
y = 's1'
|
y = 's1'
|
||||||
x = s['s1'] + _input['param1']
|
x = _input['param1']
|
||||||
return (y, x)
|
return (y, x)
|
||||||
def s2m1(step, sL, s, _input):
|
def s2m1(step, sL, s, _input):
|
||||||
y = 's2'
|
y = 's2'
|
||||||
x = s['s2'] + _input['param2']
|
x = _input['param2']
|
||||||
return (y, x)
|
return (y, x)
|
||||||
|
|
||||||
def s1m2(step, sL, s, _input):
|
def s1m2(step, sL, s, _input):
|
||||||
y = 's1'
|
y = 's1'
|
||||||
x = s['s1'] + _input['param1']
|
x = _input['param1']
|
||||||
return (y, x)
|
return (y, x)
|
||||||
def s2m2(step, sL, s, _input):
|
def s2m2(step, sL, s, _input):
|
||||||
y = 's2'
|
y = 's2'
|
||||||
x = s['s2'] + _input['param2']
|
x = _input['param2']
|
||||||
return (y, x)
|
return (y, x)
|
||||||
|
|
||||||
def s1m3(step, sL, s, _input):
|
def s1m3(step, sL, s, _input):
|
||||||
y = 's1'
|
y = 's1'
|
||||||
x = s['s1'] + _input['param1']
|
x = _input['param1']
|
||||||
return (y, x)
|
return (y, x)
|
||||||
def s2m3(step, sL, s, _input):
|
def s2m3(step, sL, s, _input):
|
||||||
y = 's2'
|
y = 's2'
|
||||||
x = s['s2'] + s['s3'] + _input['param2']
|
x = _input['param2']
|
||||||
return (y, x)
|
return (y, x)
|
||||||
|
|
||||||
# Exogenous States
|
# Exogenous States
|
||||||
|
|
@ -140,9 +140,30 @@ def foldr_dict_vals(f, d):
|
||||||
def sum_dict_values(f = _ + _):
|
def sum_dict_values(f = _ + _):
|
||||||
return foldr_dict_vals(f)
|
return foldr_dict_vals(f)
|
||||||
|
|
||||||
|
def get_neutral_element(datatype):
|
||||||
|
if datatype is str:
|
||||||
|
return ''
|
||||||
|
elif datatype is int:
|
||||||
|
return 0
|
||||||
|
elif datatype is list:
|
||||||
|
return []
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@curried
|
@curried
|
||||||
def dict_op(f, d1, d2):
|
def dict_op(f, d1, d2):
|
||||||
return {k: f(d1[k], d2[k]) for k in d2}
|
res = {}
|
||||||
|
for k in set(list(d1.keys())+list(d2.keys())):
|
||||||
|
try:
|
||||||
|
a = d1[k]
|
||||||
|
except KeyError:
|
||||||
|
a = get_neutral_element(type(d2[k]))
|
||||||
|
try:
|
||||||
|
b = d2[k]
|
||||||
|
except KeyError:
|
||||||
|
b = get_neutral_element(type(d1[k]))
|
||||||
|
res.update({k: f(a,b)})
|
||||||
|
return res
|
||||||
|
|
||||||
def dict_elemwise_sum(f = _ + _):
|
def dict_elemwise_sum(f = _ + _):
|
||||||
return dict_op(f)
|
return dict_op(f)
|
||||||
|
|
@ -190,4 +211,4 @@ mechanisms = {
|
||||||
sim_config = {
|
sim_config = {
|
||||||
"N": 2,
|
"N": 2,
|
||||||
"T": range(5)
|
"T": range(5)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue