{ "cells": [ { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "from fn import op, _, F\n", "from operator import add, mul\n", "from itertools import repeat, chain\n", "from functools import reduce\n", "# from objproxies import LazyProxy\n", "import json\n", "from copy import deepcopy, copy\n", "from pipetools import pipe\n", "from functools import partial\n", "import numpy as np\n", "from datetime import datetime, timedelta\n", "from decimal import Decimal, getcontext, ROUND_DOWN\n", "TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01')\n", "import pandas as pd\n", "import pprint\n", "# getcontext().prec=None\n", "# getcontext().rounding = ROUND_DOWN" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "# datetime_range('2018-10-01 01:00:00', '2018-10-01 02:00:00', timedelta(minutes=15))\n", "\n", "# # datetime(2018, 10, 1, 1, 0, 0) + timedelta(seconds=30)\n", "# # datetime.strptime('2018-10-01 15:16:24', '%Y-%m-%d %H:%M:%S')\n", "\n", "# time_step('2018-10-01 15:16:24', t_delta=timedelta(seconds=30))" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "flatten = lambda l: [item for sublist in l for item in sublist]\n", "\n", "def flatmap(f, items):\n", " return list(map(f, items))\n", " \n", "def datetime_range(start, end, delta, dt_format='%Y-%m-%d %H:%M:%S'):\n", " reverse_head = end\n", " [start, end] = [datetime.strptime(x, dt_format) for x in [start, end]]\n", " def _datetime_range(start, end, delta):\n", " current = start\n", " while current < end:\n", " yield current\n", " current += delta\n", " \n", " reverse_tail = [dt.strftime(dt_format) for dt in _datetime_range(start, end, delta)]\n", " return reverse_tail + [reverse_head]\n", " \n", "def last_index(l):\n", " return len(l)-1\n", "\n", "def round_down(x, fp=TWOPLACES):\n", " return x.quantize(TWOPLACES, rounding=ROUND_DOWN)\n", "\n", "def retrieve_state(l, offset):\n", " return l[last_index(l) + offset + 1]\n", "# Stochastic Process\n", "# Input RNG Seed\n", "def bound_norm_random(rng, low, high):\n", " # Add RNG Seed\n", " res = rng.normal((high+low)/2,(high-low)/6)\n", " if (reshigh):\n", " res = bound_norm_random(rng, low, high)\n", " return Decimal(res)\n", "\n", "def env_proc(trigger_step, update_f):\n", " def env_step_trigger(trigger_step, update_f, step):\n", " if step == trigger_step:\n", " return update_f\n", " else:\n", " return lambda x: x\n", " return partial(env_step_trigger, trigger_step, update_f)\n", "\n", "def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', t_delta=timedelta(seconds=1)):\n", " dt = datetime.strptime(dt_str, dt_format)\n", " t = dt + t_delta #(days=days, minutes=minutes, seconds=seconds)\n", " return t.strftime(dt_format)\n", "\n", "def ep_time_step(s, dt_str, dt_format='%Y-%m-%d %H:%M:%S', t_delta=timedelta(seconds=1)):\n", " if s['mech_step'] == 0:\n", " return time_step(dt_str, dt_format, t_delta)\n", " else: \n", " return dt_str\n", " \n", "def shift_time(time_seq, t_delta, dt_format='%Y-%m-%d %H:%M:%S'):\n", " time_seq = [\n", " time_step(time,dt_format,t_delta) for time in time_seq\n", " ]\n", " time_seq = time_seq[:-1]\n", "# x = (datetime.strptime(time_seq[0], dt_format) - t_delta).strftime(dt_format)\n", " return time_seq" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "\n", "\n", "\n", "# esceptions:\n", "# point to line giving problems and pass a sentinel" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "seed = {\n", " 'z': np.random.RandomState(1),\n", " 'a': np.random.RandomState(2),\n", " 'b': np.random.RandomState(3),\n", " 'c': np.random.RandomState(3)\n", "}\n", "\n", "# UI 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", "# UI Internal States per Mechanism\n", "def s1m1(step, sL, s, _input):\n", " s['s1'] = s['s1'] + _input\n", "def s2m1(step, sL, s, _input):\n", " s['s2'] = s['s2'] + _input\n", "\n", "def s1m2(step, sL, s, _input):\n", " s['s1'] = s['s1'] + _input\n", "def s2m2(step, sL, s, _input):\n", " s['s2'] = s['s2'] + _input\n", " \n", "def s1m3(step, sL, s, _input):\n", " s['s1'] = s['s1'] + _input\n", "def s2m3(step, sL, s, _input):\n", " s['s2'] = s['s2'] + _input\n", "\n", "# UI Exogenous States #per Mechanism\n", "proc_one_coef_A = 0.7\n", "proc_one_coef_B = 1.3\n", "def es3p1(step, sL, s, _input):\n", " s['s3'] = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)\n", "def es4p2(step, sL, s, _input):\n", " s['s4'] = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) \n", "def es5p2(step, sL, s, _input):\n", " s['timestamp'] = ep_time_step(s, s['timestamp'], t_delta=timedelta(seconds=1))\n", "\n", "\n", "# add env process f(s) read from time es\n", "# funcs execute in order\n", "def env_a(x): \n", " return 10\n", "def env_b(x): \n", " return 10\n", " \n", "def what_ever(x): \n", " return x + 1\n", "\n", "# Genesis States \n", "state_dict = {\n", " 's1': Decimal(0.0),\n", " 's2': Decimal(0.0),\n", " 's3': Decimal(1.0),\n", " 's4': Decimal(1.0),\n", " 'timestamp': '2018-10-01 15:16:24'\n", "}\n", "\n", "# simulation = {\n", "# \"s3\": es3p1\n", "# }\n", "\n", "exogenous_states = {\n", " \"s3\": es3p1,\n", " \"s4\": es4p2,\n", " \"timestamp\": es5p2\n", "}\n", "\n", "# Update time at the end of each pipeline (once per 'block')\n", "# Enable the acceptance of user defined time\n", "env_processes = {\n", " \"s3\": env_proc('2018-10-01 15:16:25', env_a),\n", " \"s4\": env_proc('2018-10-01 15:16:25', env_b)\n", "}\n", " \n", "mechanisms = {\n", " \"m1\": {\n", " \"behaviors\": {\n", " \"b1\": b1m1,\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", "}" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "# if beh list empty, repeat 0 x n_states in list \n", "def generate_config(mechanisms, exogenous_states):\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", "\n", "# partials = list(map(lambda f: partial(f, step, sL), funcs))\n", "def getColResults(step, sL, s, funcs):\n", " return list(map(lambda f: f(step, sL, s), funcs))\n", "\n", "def getBehaviorInput(step, sL, s, funcs): \n", " return op.foldr(_ + _)(getColResults(step, sL, s, funcs))\n", "\n", "def apply_env_proc(env_processes, state_dict, step):\n", " for state in state_dict.keys(): \n", " if state in list(env_processes.keys()):\n", " state_dict[state] = env_processes[state](step)(state_dict[state])\n", "# return state_dict\n", " \n", "def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step):\n", " # Truncate reffs\n", " in_copy, mutatable_copy, out_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL)\n", " last_in_obj, last_mut_obj = in_copy[-1], mutatable_copy[-1]\n", " \n", " # * force eval of _input withing state functions\n", " _input = getBehaviorInput(m_step, sL, last_in_obj, behavior_funcs)\n", "\n", " for f in state_funcs:\n", " f(m_step, sL, last_mut_obj, _input)\n", " \n", " apply_env_proc(env_processes, last_mut_obj, last_mut_obj['timestamp'])\n", " \n", " last_mut_obj[\"mech_step\"], last_mut_obj[\"time_step\"] = m_step, t_step\n", " out_copy.append(last_mut_obj)\n", " \n", " del last_in_obj, last_mut_obj, in_copy, mutatable_copy,\n", " return out_copy\n", "\n", "def block_gen(states_list, configs, env_processes, t_step):\n", " m_step = 0\n", " states_list_copy = deepcopy(states_list)\n", " genesis_states = states_list_copy[-1]\n", " genesis_states['mech_step'], genesis_states['time_step'] = m_step, t_step\n", " states_list = [genesis_states]\n", " \n", " m_step += 1\n", " for config in configs:\n", " s_conf, b_conf = config[0], config[1]\n", " states_list = mech_step(m_step, states_list, s_conf, b_conf, env_processes, t_step) \n", " m_step += 1 \n", " \n", " return states_list\n", "\n", "def pipeline(states_list, configs, env_processes, time_seq, shift_time):\n", " simulation_list = [states_list]\n", " for time_step in shift_time(time_seq):\n", " pipeline_run = block_gen(simulation_list[-1], configs, env_processes, time_step)\n", " head, *pipeline_run = pipeline_run\n", " simulation_list.append(pipeline_run)\n", " \n", " return simulation_list\n", "\n", "def simulation(states_list, configs, env_processes, time_seq, shift_time, runs):\n", " pipeline_run = []\n", " for run in range(runs):\n", " if run == 0:\n", " head, *tail = pipeline(states_list, configs, env_processes, time_seq, shift_time)\n", " head[-1]['mech_step'], head[-1]['time_step'] = 0, time_seq[0]\n", " simulation_list = [head] + tail\n", " pipeline_run += simulation_list\n", " else:\n", " transient_states_list = [pipeline_run[-1][-1]]\n", " head, *tail = pipeline(transient_states_list, configs, env_processes, time_seq, shift_time)\n", " pipeline_run += tail\n", " \n", " return pipeline_run" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "states_list = [state_dict]\n", "configs = generate_config(mechanisms, exogenous_states)\n", "t_delta = timedelta(seconds=1)\n", "dt_format = '%Y-%m-%d %H:%M:%S'\n", "time_seq = datetime_range('2018-10-01 15:16:25', '2018-10-01 15:16:30', t_delta)\n", "shift_time = partial(shift_time, t_delta=t_delta, dt_format=dt_format)\n", "# t_step_func = partial(time_step, dt_format='%Y-%m-%d %H:%M:%S', t_delta=timedelta(seconds=1))\n", "p = pipeline(states_list, configs, env_processes, time_seq, shift_time)\n", "s = simulation(states_list, configs, env_processes, time_seq, shift_time, 2)\n", "# x = flatten(s)\n", "# transient_state = p[-1][1]\n", "# transient_state['mech_step'], transient_state['time_step'] = 0, 0\n", "# head, *tail = pipeline([transient_state], configs, env_processes, range(1))\n", "# head\n", "# datetime_range('2018-10-01 15:16:24', '2018-10-01 15:16:34', timedelta(seconds=1))" ] }, { "cell_type": "code", "execution_count": 54, "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", " \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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mech_steps1s2s3s4time_steptimestamp
0000112018-10-01 15:16:252018-10-01 15:16:24
112210102018-10-01 15:16:262018-10-01 15:16:25
228810102018-10-01 15:16:262018-10-01 15:16:25
33262610102018-10-01 15:16:262018-10-01 15:16:25
4180809.25212905070613711444593718610.982367434258160088234035362018-10-01 15:16:272018-10-01 15:16:26
522422429.2604793293781581772893284659.7731345058729513536637179592018-10-01 15:16:272018-10-01 15:16:26
637287288.4473093299465593230703386358.6149725954443761336362023872018-10-01 15:16:272018-10-01 15:16:26
71218621868.3151645472453109788880537148.4378057706662679590462523472018-10-01 15:16:282018-10-01 15:16:27
82656065608.5285070998804997690357825709.6917888873177238586425865962018-10-01 15:16:282018-10-01 15:16:27
9319682196827.6852261861160185983612183969.9212092961334366408881060662018-10-01 15:16:282018-10-01 15:16:27
10159048590487.4248338415401289899902334348.9054906313852615134218889702018-10-01 15:16:292018-10-01 15:16:28
1121771461771467.2494711230295541049943073208.2705352050013358583851428962018-10-01 15:16:292018-10-01 15:16:28
1235314405314406.7872049630835546240430691038.7876462554412913892893969482018-10-01 15:16:292018-10-01 15:16:28
131159432215943225.9811481626462335535366479828.6465927899455055158089434402018-10-01 15:16:302018-10-01 15:16:29
142478296847829685.1310970818651083011392059447.9818113056136786776620306252018-10-01 15:16:302018-10-01 15:16:29
15314348906143489065.0523372068032774940089205187.7982051236420256745577235812018-10-01 15:16:302018-10-01 15:16:29
16143046720430467204.9164005577184022716975280268.3792152830504215750802121852018-10-01 15:16:262018-10-01 15:16:30
1721291401621291401626.01342985025404430139895525610.035041050547687614822850592018-10-01 15:16:262018-10-01 15:16:30
1833874204883874204884.5492994479458098194069849958.7865581827460001299032897272018-10-01 15:16:262018-10-01 15:16:30
191116226146611622614664.6005821105589341313986505288.2361533190647364616826988332018-10-01 15:16:272018-10-01 15:16:31
202348678440034867844004.7710081613916512229755818427.5741592403048790956696869282018-10-01 15:16:272018-10-01 15:16:31
21310460353202104603532025.4196905869231921717961983055.7419071233851519945110779322018-10-01 15:16:272018-10-01 15:16:31
22131381059608313810596085.6916816648804170770158190785.2114743243950101390479712232018-10-01 15:16:282018-10-01 15:16:32
23294143178826941431788265.2111820989474961994765808034.6778841004947243276054999432018-10-01 15:16:282018-10-01 15:16:32
2432824295364802824295364805.2111871858280999754537460345.2036679628460901368278308712018-10-01 15:16:282018-10-01 15:16:32
2518472886094428472886094425.4938172632464933153106493595.1350241761391434987878104692018-10-01 15:16:292018-10-01 15:16:33
262254186582832825418658283285.3215815887653263865182782554.3014631752252631070243835802018-10-01 15:16:292018-10-01 15:16:33
273762559748498676255974849865.7318817757685398283108686034.5796282396514924322318034882018-10-01 15:16:292018-10-01 15:16:33
28122876792454960228767924549604.6611142979307841236635259674.4164694765590526589602192842018-10-01 15:16:302018-10-01 15:16:34
29268630377364882686303773648825.4680392578267927008990906193.6466165582548077241873518992018-10-01 15:16:302018-10-01 15:16:34
3032058911320946482058911320946486.2705713557932471377732515903.4290413119705678069392881232018-10-01 15:16:302018-10-01 15:16:34
\n", "
" ], "text/plain": [ " mech_step s1 s2 \\\n", "0 0 0 0 \n", "1 1 2 2 \n", "2 2 8 8 \n", "3 3 26 26 \n", "4 1 80 80 \n", "5 2 242 242 \n", "6 3 728 728 \n", "7 1 2186 2186 \n", "8 2 6560 6560 \n", "9 3 19682 19682 \n", "10 1 59048 59048 \n", "11 2 177146 177146 \n", "12 3 531440 531440 \n", "13 1 1594322 1594322 \n", "14 2 4782968 4782968 \n", "15 3 14348906 14348906 \n", "16 1 43046720 43046720 \n", "17 2 129140162 129140162 \n", "18 3 387420488 387420488 \n", "19 1 1162261466 1162261466 \n", "20 2 3486784400 3486784400 \n", "21 3 10460353202 10460353202 \n", "22 1 31381059608 31381059608 \n", "23 2 94143178826 94143178826 \n", "24 3 282429536480 282429536480 \n", "25 1 847288609442 847288609442 \n", "26 2 2541865828328 2541865828328 \n", "27 3 7625597484986 7625597484986 \n", "28 1 22876792454960 22876792454960 \n", "29 2 68630377364882 68630377364882 \n", "30 3 205891132094648 205891132094648 \n", "\n", " s3 s4 \\\n", "0 1 1 \n", "1 10 10 \n", "2 10 10 \n", "3 10 10 \n", "4 9.252129050706137114445937186 10.98236743425816008823403536 \n", "5 9.260479329378158177289328465 9.773134505872951353663717959 \n", "6 8.447309329946559323070338635 8.614972595444376133636202387 \n", "7 8.315164547245310978888053714 8.437805770666267959046252347 \n", "8 8.528507099880499769035782570 9.691788887317723858642586596 \n", "9 7.685226186116018598361218396 9.921209296133436640888106066 \n", "10 7.424833841540128989990233434 8.905490631385261513421888970 \n", "11 7.249471123029554104994307320 8.270535205001335858385142896 \n", "12 6.787204963083554624043069103 8.787646255441291389289396948 \n", "13 5.981148162646233553536647982 8.646592789945505515808943440 \n", "14 5.131097081865108301139205944 7.981811305613678677662030625 \n", "15 5.052337206803277494008920518 7.798205123642025674557723581 \n", "16 4.916400557718402271697528026 8.379215283050421575080212185 \n", "17 6.013429850254044301398955256 10.03504105054768761482285059 \n", "18 4.549299447945809819406984995 8.786558182746000129903289727 \n", "19 4.600582110558934131398650528 8.236153319064736461682698833 \n", "20 4.771008161391651222975581842 7.574159240304879095669686928 \n", "21 5.419690586923192171796198305 5.741907123385151994511077932 \n", "22 5.691681664880417077015819078 5.211474324395010139047971223 \n", "23 5.211182098947496199476580803 4.677884100494724327605499943 \n", "24 5.211187185828099975453746034 5.203667962846090136827830871 \n", "25 5.493817263246493315310649359 5.135024176139143498787810469 \n", "26 5.321581588765326386518278255 4.301463175225263107024383580 \n", "27 5.731881775768539828310868603 4.579628239651492432231803488 \n", "28 4.661114297930784123663525967 4.416469476559052658960219284 \n", "29 5.468039257826792700899090619 3.646616558254807724187351899 \n", "30 6.270571355793247137773251590 3.429041311970567806939288123 \n", "\n", " time_step timestamp \n", "0 2018-10-01 15:16:25 2018-10-01 15:16:24 \n", "1 2018-10-01 15:16:26 2018-10-01 15:16:25 \n", "2 2018-10-01 15:16:26 2018-10-01 15:16:25 \n", "3 2018-10-01 15:16:26 2018-10-01 15:16:25 \n", "4 2018-10-01 15:16:27 2018-10-01 15:16:26 \n", "5 2018-10-01 15:16:27 2018-10-01 15:16:26 \n", "6 2018-10-01 15:16:27 2018-10-01 15:16:26 \n", "7 2018-10-01 15:16:28 2018-10-01 15:16:27 \n", "8 2018-10-01 15:16:28 2018-10-01 15:16:27 \n", "9 2018-10-01 15:16:28 2018-10-01 15:16:27 \n", "10 2018-10-01 15:16:29 2018-10-01 15:16:28 \n", "11 2018-10-01 15:16:29 2018-10-01 15:16:28 \n", "12 2018-10-01 15:16:29 2018-10-01 15:16:28 \n", "13 2018-10-01 15:16:30 2018-10-01 15:16:29 \n", "14 2018-10-01 15:16:30 2018-10-01 15:16:29 \n", "15 2018-10-01 15:16:30 2018-10-01 15:16:29 \n", "16 2018-10-01 15:16:26 2018-10-01 15:16:30 \n", "17 2018-10-01 15:16:26 2018-10-01 15:16:30 \n", "18 2018-10-01 15:16:26 2018-10-01 15:16:30 \n", "19 2018-10-01 15:16:27 2018-10-01 15:16:31 \n", "20 2018-10-01 15:16:27 2018-10-01 15:16:31 \n", "21 2018-10-01 15:16:27 2018-10-01 15:16:31 \n", "22 2018-10-01 15:16:28 2018-10-01 15:16:32 \n", "23 2018-10-01 15:16:28 2018-10-01 15:16:32 \n", "24 2018-10-01 15:16:28 2018-10-01 15:16:32 \n", "25 2018-10-01 15:16:29 2018-10-01 15:16:33 \n", "26 2018-10-01 15:16:29 2018-10-01 15:16:33 \n", "27 2018-10-01 15:16:29 2018-10-01 15:16:33 \n", "28 2018-10-01 15:16:30 2018-10-01 15:16:34 \n", "29 2018-10-01 15:16:30 2018-10-01 15:16:34 \n", "30 2018-10-01 15:16:30 2018-10-01 15:16:34 " ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(flatten(s))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# pd.DataFrame(flatten(p))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# flatten(pipeline(states_list, configs, env_processes, range(10)))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "strptime() argument 1 must be str, not int", "output_type": "error", "traceback": [ "\u001b[0;31m--------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mconfigs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgenerate_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmechanisms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexogenous_states\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# type(pipeline(states_list, configs, env_processes, range(10)))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflatten\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpipeline\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mpipeline\u001b[0;34m(states_list, configs, env_processes, time_seq)\u001b[0m\n\u001b[1;32m 63\u001b[0m \u001b[0msimulation_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 64\u001b[0m \u001b[0mshift_time_partial\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpartial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshift_time\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_delta\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mt_delta\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdt_format\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'%Y-%m-%d %H:%M:%S'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 65\u001b[0;31m \u001b[0mtime_seq\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mshift_time_partial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtime_seq\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 66\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtime_step\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 67\u001b[0m \u001b[0mpipeline_run\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mblock_gen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msimulation_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtime_step\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mshift_time\u001b[0;34m(time_seq, t_delta, dt_format)\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mshift_time\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtime_seq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_delta\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdt_format\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'%Y-%m-%d %H:%M:%S'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 55\u001b[0m time_seq = [\n\u001b[0;32m---> 56\u001b[0;31m \u001b[0mtime_step\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdt_format\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mt_delta\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtime\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 57\u001b[0m ]\n\u001b[1;32m 58\u001b[0m \u001b[0mtime_seq\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mshift_time\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtime_seq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_delta\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdt_format\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'%Y-%m-%d %H:%M:%S'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 55\u001b[0m time_seq = [\n\u001b[0;32m---> 56\u001b[0;31m \u001b[0mtime_step\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdt_format\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mt_delta\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtime\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 57\u001b[0m ]\n\u001b[1;32m 58\u001b[0m \u001b[0mtime_seq\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mtime_step\u001b[0;34m(dt_str, dt_format, t_delta)\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtime_step\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdt_str\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdt_format\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'%Y-%m-%d %H:%M:%S'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_delta\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimedelta\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseconds\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 41\u001b[0;31m \u001b[0mdt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrptime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdt_str\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdt_format\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 42\u001b[0m \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdt\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mt_delta\u001b[0m \u001b[0;31m#(days=days, minutes=minutes, seconds=seconds)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrftime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdt_format\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: strptime() argument 1 must be str, not int" ] } ], "source": [ "states_list = [state_dict]\n", "configs = generate_config(mechanisms, exogenous_states)\n", "# type(pipeline(states_list, configs, env_processes, range(10)))\n", "pd.DataFrame(flatten(pipeline(states_list, configs, env_processes, range(10))))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(range(2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Decimal('240.0').quantize(Decimal('0.01'), ROUND_DOWN)\n", "# round(Decimal('240.01'), 2)\n", "state_dict = {'s1': Decimal('3323'), 's2': Decimal('2572'), 's3': Decimal('2.657'), 's4': Decimal('7.914'), 'timestamp': '2018-10-01 15:16:26', 'mech_step': 1, 'time_step': 2}\n", "state_dict = {k: v.quantize(TWOPLACES, ROUND_DOWN) for k, v in state_dict.items() if type(v) == Decimal}\n", "state_dict.update(non_numeric)\n", "state_dict" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "states_list = [state_dict]\n", "states_list\n", "# {'s1': 8.742503764992283e+170,\n", "# 's2': 1.119720780594405e+171,\n", "# 's3': 4.251811100418906,\n", "# 's4': 6.92728042985628,\n", "# 'timestamp': '2018-10-01 15:16:31',\n", "# 'mech_step': 3,\n", "# 'time_step': 7}\n", "# {'s1': Decimal('2.221384236472411425810660220E+330'),\n", "# 's2': Decimal('2.845096899831187764579913612E+330'),\n", "# 's3': Decimal('3.622519693611211445932083740'),\n", "# 's4': Decimal('5.161487081026327858663750787'),\n", "# 'timestamp': '2018-10-01 15:16:32',\n", "# 'mech_step': 3,\n", "# 'time_step': 8}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from decimal import Decimal, MAX_EMAX\n", "# Decimal(2.5347751615574597e+167)**2\n", "MAX_EMAX**5" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "1000 * 1000" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "from datetime import datetime, timedelta\n", "# from datetime import timedelta \n", "# st = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')\n", "st = datetime.now()\n", "print(st)\n", "x = st + timedelta(seconds=60)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = datetime.now().strftime('%Y-%m-%d %H:%M:%S')\n", "datetime_object = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')\n", "datetime_object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "datetime.fromtimestamp()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime, timedelta\n", "\n", "# def time_step_func(year, month, day, hour, minute, second):\n", "a = datetime(2016, 11, 15, 9, 59, 25)#.strftime(\"%Y-%m-%d %H:%M:%S\")\n", "b = a + timedelta(seconds=60) # days, seconds, then other fields.\n", "print(a)\n", "print(b)\n", "st = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')\n", "def time_step_func(dt_str, fromat_str='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=1):\n", " dt = datetime.strptime(dt_str, fromat_str)\n", " t = dt + timedelta(days=days, minutes=minutes, seconds=seconds)\n", " return t.strftime(fromat_str)\n", "\n", "time_step_func('2018-10-01 15:16:24')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def time_step_func(x, y):\n", " return x + y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for m in M: #M is the parameter sweep array\n", " # for Pairwise, run below separately\n", " for n in N: #N is the total number of Montecarlo runs\n", " for t in T: #T[len(T)]-T[0] is the duration of each simulation\n", " # Pipeline\n", " for b in Behaviors:\n", " for M in Mechanisms:\n", " for s in States:\n", " update(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[{'s1': 2}] + [{'s1': 3}]\n", "list(zip([block_gen] * 2, range(2)))\n", "len([0,1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "states_list = [state_dict]\n", "configs = generate_config(mechanisms, exogenous_states)\n", "def step_func(x, y):\n", " return x + y\n", "pipeline(states_list, configs, env_processes, step_func)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# def s3step1(step, sL, s, _input):\n", "# s['s3'] = s['s3'] \n", "# # def s3step2(step, sL, s, _input):\n", "# s['s3'] = s['s3'] + 1\n", "# def s3step3(step, sL, s, _input):\n", "# s['s3'] = s['s3'] * bound_norm_random(proc_one_coef_A, proc_one_coef_B)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 15 + 28.933333333333334 + 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# def multiply(x,y,z):\n", "# return x * y * z\n", "\n", "# # create a new function that multiplies by 2\n", "# dbl = partial(partial(multiply,2), multiply, 2)\n", "# print(dbl(2))" ] } ], "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 }