From 16fe59c106bfc8b51025b4256eb305bd238b3a02 Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Tue, 30 Oct 2018 08:51:26 +0100 Subject: [PATCH] identity fix pt.1 --- engine/.ipynb_checkpoints/run-checkpoint.py | 21 - engine/configProcessor.py | 49 +- engine/mechanismExecutor.py | 16 +- engine/run.py | 6 +- engine/utils.py | 10 +- notebooks/config_padding.ipynb | 644 ++++++++++++++++++++ ui/config.py | 12 +- 7 files changed, 714 insertions(+), 44 deletions(-) delete mode 100644 engine/.ipynb_checkpoints/run-checkpoint.py create mode 100644 notebooks/config_padding.ipynb diff --git a/engine/.ipynb_checkpoints/run-checkpoint.py b/engine/.ipynb_checkpoints/run-checkpoint.py deleted file mode 100644 index 1adcca7..0000000 --- a/engine/.ipynb_checkpoints/run-checkpoint.py +++ /dev/null @@ -1,21 +0,0 @@ -from ui.config import state_dict, mechanisms, exogenous_states, env_processes, sim_config -from engine.configProcessor import generate_config -from engine.mechanismExecutor import simulation -from engine.utils import flatten - -#from tabulate import tabulate -import pandas as pd - -def main(): - states_list = [state_dict] - configs = generate_config(mechanisms, exogenous_states) - # p = pipeline(states_list, configs, env_processes, range(10)) - N = sim_config['N'] - r = range(5) - # Dimensions: N x r x mechs - s = simulation(states_list, configs, env_processes, r, N) - result = pd.DataFrame(flatten(s)) - print('Test') -# print(tabulate(result, headers='keys', tablefmt='psql')) -# remove print and tabulate functions, so it returns a dataframe - return result \ No newline at end of file diff --git a/engine/configProcessor.py b/engine/configProcessor.py index 5d4c0be..4123d46 100644 --- a/engine/configProcessor.py +++ b/engine/configProcessor.py @@ -1,13 +1,36 @@ -# if beh list empty, repeat 0 x n_states in list -def generate_config(mechanisms, exogenous_states): - es_funcs = [exogenous_states[state] for state in list(exogenous_states.keys())] - config = list( - map( - lambda m: ( - list(mechanisms[m]["states"].values()) + es_funcs, - list(mechanisms[m]["behaviors"].values()) - ), - list(mechanisms.keys()) - ) - ) - return config \ No newline at end of file +import pandas as pd +from functools import partial, reduce + +def state_identity(k): + def identity(step, sL, s, _input): + return (k, s[k]) + return identity + +def behavior_identity(k): + def identity(step, sL, s): + return s[k] + return identity + +def key_filter(mechanisms, keyname): + return [ v[keyname] for k, v in mechanisms.items() ] + +def fillna_with_id_func(identity, df, col): + return df[[col]].fillna(value=identity(col)) + +def apply_identity_funcs(identity, df, cols): + return list(map(lambda col: fillna_with_id_func(identity, df, col), cols)) + +def create_matrix_field(mechanisms, key): + if key == 'states': + identity = state_identity + else: + identity = behavior_identity + df = pd.DataFrame(key_filter(mechanisms, key)) + col_list = apply_identity_funcs(identity, df, list(df.columns)) + return reduce((lambda x, y: pd.concat([x, y], axis=1)), col_list) + +def generate_config(mechanisms, env_poc): + bdf = create_matrix_field(mechanisms,'behaviors') + sdf = create_matrix_field(mechanisms,'states') + zipped_list = list(zip(sdf.values.tolist(), bdf.values.tolist())) + return list(map(lambda x: (x[0] + env_poc, x[1]), zipped_list)) \ No newline at end of file diff --git a/engine/mechanismExecutor.py b/engine/mechanismExecutor.py index 9c7ce8e..e5b2d8f 100644 --- a/engine/mechanismExecutor.py +++ b/engine/mechanismExecutor.py @@ -14,14 +14,24 @@ def apply_env_proc(env_processes, state_dict, step): if state in list(env_processes.keys()): state_dict[state] = env_processes[state](step)(state_dict[state]) +def exception_handler(f, m_step, sL, last_mut_obj, _input): + try: + return f(m_step, sL, last_mut_obj, _input) + except KeyError: + print("Exception") + return f(m_step, sL, sL[-2], _input) + def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step): in_copy, mutatable_copy, out_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL) last_in_obj, last_mut_obj = in_copy[-1], mutatable_copy[-1] - _input = getBehaviorInput(m_step, sL, last_in_obj, behavior_funcs) + _input = exception_handler(getBehaviorInput, m_step, sL, last_in_obj, behavior_funcs) - last_mut_obj = dict([ f(m_step, sL, last_mut_obj, _input) for f in 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)) apply_env_proc(env_processes, last_mut_obj, last_mut_obj['timestamp']) @@ -44,6 +54,7 @@ def block_gen(states_list, configs, env_processes, t_step): for config in configs: s_conf, b_conf = config[0], config[1] states_list = mech_step(m_step, states_list, s_conf, b_conf, env_processes, t_step) + print(b_conf) m_step += 1 t_step += 1 @@ -51,6 +62,7 @@ def block_gen(states_list, configs, env_processes, t_step): return states_list +# rename pipe def pipeline(states_list, configs, env_processes, time_seq): time_seq = [x + 1 for x in time_seq] simulation_list = [states_list] diff --git a/engine/run.py b/engine/run.py index 5ef2a65..5a7a940 100644 --- a/engine/run.py +++ b/engine/run.py @@ -8,7 +8,11 @@ import pandas as pd def main(): states_list = [state_dict] - configs = generate_config(mechanisms, exogenous_states) + ep = list(exogenous_states.values()) + configs = generate_config(mechanisms, ep) + # print(configs) + # print(states_list) + # print(configs) # p = pipeline(states_list, configs, env_processes, range(10)) T = range(5) N = sim_config['N'] diff --git a/engine/utils.py b/engine/utils.py index 2e63fc8..9adbd00 100644 --- a/engine/utils.py +++ b/engine/utils.py @@ -55,4 +55,12 @@ def ep_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', days=0, minutes=0, s if s['mech_step'] == 0: return time_step(dt_str, fromat_str, days, minutes, seconds) else: - return dt_str \ No newline at end of file + return dt_str + +# def create_tensor_field(mechanisms, env_poc, keys=['behaviors', 'states']): +# dfs = [ create_matrix_field(mechanisms, k) for k in keys ] +# df = pd.concat(dfs, axis=1) +# 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 diff --git a/notebooks/config_padding.ipynb b/notebooks/config_padding.ipynb new file mode 100644 index 0000000..e322af5 --- /dev/null +++ b/notebooks/config_padding.ipynb @@ -0,0 +1,644 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "from functools import partial, reduce" + ] + }, + { + "cell_type": "code", + "execution_count": 177, + "metadata": {}, + "outputs": [], + "source": [ + "# Behaviors per Mechanism\n", + "def b1m1(step, sL, s):\n", + " return s['s1'] + 1\n", + "def b2m1(step, sL, s):\n", + " return s['s1'] + 1\n", + "\n", + "def b1m2(step, sL, s):\n", + " return s['s1'] + 1\n", + "def b2m2(step, sL, s):\n", + " return s['s1'] + 1\n", + "\n", + "def b1m3(step, sL, s):\n", + " return s['s1'] + 1\n", + "def b2m3(step, sL, s):\n", + " return s['s2'] + 1\n", + "\n", + "\n", + "# Internal States per Mechanism\n", + "def s1m1(step, sL, s, _input):\n", + " y = 's1'\n", + " x = s['s1'] + _input\n", + " return (y, x)\n", + "def s2m1(step, sL, s, _input):\n", + " y = 's2'\n", + " x = s['s2'] + _input\n", + " return (y, x)\n", + "\n", + "def s1m2(step, sL, s, _input):\n", + " y = 's1'\n", + " x = s['s1'] + _input\n", + " return (y, x)\n", + "def s2m2(step, sL, s, _input):\n", + " y = 's2'\n", + " x = s['s2'] + _input\n", + " return (y, x)\n", + "\n", + "def s1m3(step, sL, s, _input):\n", + " y = 's1'\n", + " x = s['s1'] + _input\n", + " return (y, x)\n", + "def s2m3(step, sL, s, _input):\n", + " y = 's2'\n", + " x = s['s2'] + _input\n", + " return (y, x)\n", + "\n", + "# Exogenous States\n", + "proc_one_coef_A = 0.7\n", + "proc_one_coef_B = 1.3\n", + "def es3p1(step, sL, s, _input):\n", + " y = 's3'\n", + " x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)\n", + " return (y, x)\n", + "def es4p2(step, sL, s, _input):\n", + " y = 's4'\n", + " x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B)\n", + " return (y, x)\n", + "def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params\n", + " y = 'timestamp'\n", + " x = ep_time_step(s, s['timestamp'], seconds=1)\n", + " return (y, x)\n", + "\n", + "\n", + "\n", + "# Environment States\n", + "def env_a(x):\n", + " return 10\n", + "def env_b(x):\n", + " return 10\n", + "\n", + "\n", + "exogenous_states = {\n", + " \"s3\": es3p1,\n", + " \"s4\": es4p2,\n", + " \"timestamp\": es5p2\n", + "}\n", + "\n", + "ep = list(exogenous_states.values())\n", + "\n", + "mechanisms = {\n", + " \"m1\": {\n", + " \"behaviors\": {\n", + " \"b1\": b1m1, # lambda step, sL, s: s['s1'] + 1,\n", + " \"b2\": b2m1\n", + " },\n", + " \"states\": {\n", + " \"s1\": s1m1,\n", + " \"s2\": s2m1,\n", + " }\n", + " },\n", + " \"m2\": {\n", + " \"behaviors\": {\n", + " \"b1\": b1m2,\n", + " \"b2\": b2m2\n", + " },\n", + " \"states\": {\n", + " \"s1\": s1m2,\n", + " # \"s2\": s2m2,\n", + " }\n", + " },\n", + " \"m3\": {\n", + " \"behaviors\": {\n", + " # \"b1\": b1m3,\n", + " # \"b2\": b2m3\n", + " },\n", + " \"states\": {\n", + " \"s1\": s1m3,\n", + " # \"s2\": s2m3,\n", + " }\n", + " }\n", + "}\n", + "\n", + "\n", + "def state_identity(k):\n", + " def identity(step, sL, s, _input):\n", + " return (k, s[k])\n", + " return identity\n", + "\n", + "def behavior_identity(k):\n", + " def identity(step, sL, s):\n", + " return s[k]\n", + " return identity\n", + "\n", + "def key_filter(mechanisms, keyname): \n", + " return [ v[keyname] for k, v in mechanisms.items() ]\n", + "\n", + "def fillna_with_id(identity, df, col):\n", + " return df[[col]].fillna(value=identity(col))\n", + "\n", + "def apply_identity_funcs(identity, df, cols):\n", + " return list(map(lambda col: fillna_with_id(identity, df, col), cols))\n", + "\n", + "def create_matrix_field(mechanisms, key):\n", + " if key == 'states':\n", + " identity = state_identity\n", + " else:\n", + " identity = behavior_identity\n", + " df = pd.DataFrame(key_filter(mechanisms, key))\n", + " col_list = apply_identity_funcs(identity, df, list(df.columns))\n", + " return reduce((lambda x, y: pd.concat([x, y], axis=1)), col_list)\n", + "\n", + "def create_tensor_field(mechanisms, env_poc=ep, keys=['behaviors', 'states']):\n", + " dfs = [ create_matrix_field(mechanisms, k) for k in keys ]\n", + " df = pd.concat(dfs, axis=1)\n", + " for es, i in zip(env_poc, range(len(env_poc))):\n", + " df['es'+str(i)] = es\n", + " df['m'] = df.index + 1\n", + " return df\n", + "\n", + "def generate_config(mechanisms, env_poc=ep):\n", + " bdf = create_matrix_field(mechanisms,'behaviors')\n", + " sdf = create_matrix_field(mechanisms,'states')\n", + " zipped_list = list(zip(sdf.values.tolist(), bdf.values.tolist()))\n", + " return list(map(lambda x: (x[0] + env_poc, x[1]), zipped_list))" + ] + }, + { + "cell_type": "code", + "execution_count": 183, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[,\n", + " ],\n", + " [,\n", + " ],\n", + " [.identity(step, sL, s)>,\n", + " .identity(step, sL, s)>]]" + ] + }, + "execution_count": 183, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "create_matrix_field(mechanisms,'behaviors').values.tolist()\n", + " \n", + "# exdf[exdf.index == 0].values.tolist() #['b1','b2']" + ] + }, + { + "cell_type": "code", + "execution_count": 181, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[([,\n", + " ,\n", + " ,\n", + " ,\n", + " ],\n", + " [,\n", + " ]),\n", + " ([,\n", + " .identity(step, sL, s, _input)>,\n", + " ,\n", + " ,\n", + " ],\n", + " [,\n", + " ]),\n", + " ([,\n", + " .identity(step, sL, s, _input)>,\n", + " ,\n", + " ,\n", + " ],\n", + " [.identity(step, sL, s)>,\n", + " .identity(step, sL, s)>])]" + ] + }, + "execution_count": 181, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "generate_config(mechanisms, ep)" + ] + }, + { + "cell_type": "code", + "execution_count": 180, + "metadata": {}, + "outputs": [], + "source": [ + "# def generate_config(mechanisms, exogenous_states):\n", + "# es_funcs = list(exogenous_states.values())\n", + "# # es_funcs = [ exogenous_states[state] for state in list(exogenous_states.keys()) ]\n", + "# config = list(\n", + "# map(\n", + "# lambda m: (\n", + "# list(mechanisms[m][\"states\"].values()) + es_funcs,\n", + "# list(mechanisms[m][\"behaviors\"].values())\n", + "# ),\n", + "# list(mechanisms.keys())\n", + "# )\n", + "# )\n", + "# return config\n", + "# generate_config(mechanisms, exogenous_states)" + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
b1b2s1s2es0es1es2m
0<function b1m1 at 0x118771e18><function b2m1 at 0x1188d7d90><function s1m1 at 0x1188d7730><function s2m1 at 0x1188ed730><function es3p1 at 0x1188ed400><function es4p2 at 0x1188ed378><function es5p2 at 0x1188ed158>1
1<function b1m2 at 0x1188d70d0><function b2m2 at 0x1188d7840><function s1m2 at 0x1188ed620><function state_identity.<locals>.identity at ...<function es3p1 at 0x1188ed400><function es4p2 at 0x1188ed378><function es5p2 at 0x1188ed158>2
2<function b1m3 at 0x1188d7ea0><function behavior_identity.<locals>.identity ...<function s1m3 at 0x1188d7a60><function s2m3 at 0x1188ed268><function es3p1 at 0x1188ed400><function es4p2 at 0x1188ed378><function es5p2 at 0x1188ed158>3
\n", + "
" + ], + "text/plain": [ + " b1 \\\n", + "0 \n", + "1 \n", + "2 \n", + "\n", + " b2 \\\n", + "0 \n", + "1 \n", + "2 .identity ... \n", + "\n", + " s1 \\\n", + "0 \n", + "1 \n", + "2 \n", + "\n", + " s2 \\\n", + "0 \n", + "1 .identity at ... \n", + "2 \n", + "\n", + " es0 es1 \\\n", + "0 \n", + "1 \n", + "2 \n", + "\n", + " es2 m \n", + "0 1 \n", + "1 2 \n", + "2 3 " + ] + }, + "execution_count": 170, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "create_tensor_field(mechanisms)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
b1b2
0<function b1m1 at 0x1108c88c8><function b2m1 at 0x1108c89d8>
1<function b1m2 at 0x1108c8d90><function b2m2 at 0x1108c8a60>
2<function b1m3 at 0x1108c8c80><function behavior_identity.<locals>.identity ...
\n", + "
" + ], + "text/plain": [ + " b1 \\\n", + "0 \n", + "1 \n", + "2 \n", + "\n", + " b2 \n", + "0 \n", + "1 \n", + "2 .identity ... " + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "create_matrix_field(mechanisms,'behaviors')\n", + "create_matrix_field(mechanisms,'states')" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
b1b2s1s2es0es1es2m
0<function b1m1 at 0x1105ed9d8><function b2m1 at 0x1105edbf8><function s1m1 at 0x1105c69d8><function s2m1 at 0x1105ede18><function es3p1 at 0x1187710d0><function es4p2 at 0x118771158><function es5p2 at 0x1187711e0>1
1<function b1m2 at 0x1105edc80><function b2m2 at 0x1105edd08><function s1m2 at 0x1105edea0><function state_identity.<locals>.identity at ...<function es3p1 at 0x1187710d0><function es4p2 at 0x118771158><function es5p2 at 0x1187711e0>2
2<function b1m3 at 0x1105edb70><function behavior_identity.<locals>.identity ...<function s1m3 at 0x1105ed0d0><function s2m3 at 0x118771048><function es3p1 at 0x1187710d0><function es4p2 at 0x118771158><function es5p2 at 0x1187711e0>3
\n", + "
" + ], + "text/plain": [ + " b1 \\\n", + "0 \n", + "1 \n", + "2 \n", + "\n", + " b2 \\\n", + "0 \n", + "1 \n", + "2 .identity ... \n", + "\n", + " s1 \\\n", + "0 \n", + "1 \n", + "2 \n", + "\n", + " s2 \\\n", + "0 \n", + "1 .identity at ... \n", + "2 \n", + "\n", + " es0 es1 \\\n", + "0 \n", + "1 \n", + "2 \n", + "\n", + " es2 m \n", + "0 1 \n", + "1 2 \n", + "2 3 " + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tensor_field_report(mechanisms)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('a', 0)\n", + "('b', 1)\n", + "('c', 2)\n" + ] + } + ], + "source": [ + "\n", + "for x, i in zip(['a', 'b', 'c'], range(3)):\n", + " print((x, i))" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(env_poc)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def create_tensor_field2(mechanisms, env_poc=ep):\n", + " beh_df = create_matrix_field(mechanisms, 'behaviors')\n", + " state_df = create_matrix_field(mechanisms, 'states')\n", + " ep_df = pd.DataFrame({'m' : range(len(env_poc))})\n", + " for es, i in zip(env_poc, range(len(env_poc))):\n", + " ep_df['es'+str(i)] = es\n", + " \n", + " return beh_df" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/ui/config.py b/ui/config.py index 956bfcd..0406033 100644 --- a/ui/config.py +++ b/ui/config.py @@ -109,7 +109,7 @@ mechanisms = { }, "states": { "s1": s1m1, - "s2": s2m1, + # "s2": s2m1, } }, "m2": { @@ -118,18 +118,18 @@ mechanisms = { "b2": b2m2 }, "states": { - "s1": s1m2, - "s2": s2m2, + # "s1": s1m2, + # "s2": s2m2, } }, "m3": { "behaviors": { "b1": b1m3, - "b2": b2m3 + "b2": b2m3 #toggle for error }, "states": { - "s1": s1m3, - "s2": s2m3, + # "s1": s1m3, + # "s2": s2m3, } } }