Created
September 3, 2020 20:38
-
-
Save BenSchZA/72b1b0a529703e97cd4e53f200e1516b to your computer and use it in GitHub Desktop.
monte-carlo-run-debug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from cadCAD.configuration.utils import config_sim\n", | |
| "from cadCAD.configuration import Experiment\n", | |
| "from cadCAD.engine import ExecutionMode, ExecutionContext\n", | |
| "from cadCAD.engine import Executor\n", | |
| "\n", | |
| "import pandas as pd" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "system_params = {\n", | |
| " 'add': [10, 10, 10]\n", | |
| "}\n", | |
| "\n", | |
| "initial_state = {\n", | |
| " 'a': 0\n", | |
| "}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from numpy import random\n", | |
| "\n", | |
| "def update_a(params, substep, state_history, previous_state, policy_input, **kwargs):\n", | |
| " return 'a', previous_state['a'] + params['add'] * random.rand()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "psubs = [\n", | |
| " {\n", | |
| " 'policies': {},\n", | |
| " 'variables': {\n", | |
| " 'a': update_a\n", | |
| " }\n", | |
| " }\n", | |
| "]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from cadCAD import configs\n", | |
| "del configs[:] # Clear any prior configs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[{'N': 3, 'T': range(0, 100), 'M': {'add': 10}}, {'N': 3, 'T': range(0, 100), 'M': {'add': 10}}, {'N': 3, 'T': range(0, 100), 'M': {'add': 10}}]\n", | |
| "[{'N': 1, 'T': range(0, 100), 'M': {'add': 10}, 'subset_id': 0, 'subset_window': deque([0, None], maxlen=2), 'simulation_id': 0, 'run_id': 2}, {'N': 1, 'T': range(0, 100), 'M': {'add': 10}, 'subset_id': 1, 'subset_window': deque([0, None], maxlen=2), 'simulation_id': 1, 'run_id': 2}, {'N': 1, 'T': range(0, 100), 'M': {'add': 10}, 'subset_id': 2, 'subset_window': deque([0, None], maxlen=2), 'simulation_id': 2, 'run_id': 2}]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "exp = Experiment()\n", | |
| "\n", | |
| "sim_config = config_sim({\n", | |
| " \"N\": 3,\n", | |
| " \"T\": range(100),\n", | |
| " \"M\": system_params\n", | |
| "})\n", | |
| "print(sim_config)\n", | |
| "\n", | |
| "exp.append_configs(\n", | |
| " initial_state = initial_state,\n", | |
| " partial_state_update_blocks = psubs,\n", | |
| " sim_configs = sim_config\n", | |
| ")\n", | |
| "print(sim_config)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "\n", | |
| " ___________ ____\n", | |
| " ________ __ ___/ / ____/ | / __ \\\n", | |
| " / ___/ __` / __ / / / /| | / / / /\n", | |
| "/ /__/ /_/ / /_/ / /___/ ___ |/ /_/ /\n", | |
| "\\___/\\__,_/\\__,_/\\____/_/ |_/_____/\n", | |
| "by cadCAD\n", | |
| "\n", | |
| "Execution Mode: local_proc\n", | |
| "Configuration Count: 3\n", | |
| "Dimensions of the first simulation: (Timesteps, Params, Runs, Vars) = (100, 1, 3, 1)\n", | |
| "Execution Method: local_simulations\n", | |
| "SimIDs : [0, 0, 0, 1, 1, 1, 2, 2, 2]\n", | |
| "SubsetIDs: [0, 0, 0, 1, 1, 1, 2, 2, 2]\n", | |
| "Ns : [0, 1, 2, 0, 1, 2, 0, 1, 2]\n", | |
| "ExpIDs : [0, 0, 0, 0, 0, 0, 0, 0, 0]\n", | |
| "Execution Mode: parallelized\n", | |
| "Total execution time: 0.13s\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "exec_mode = ExecutionMode()\n", | |
| "local_mode_ctx = ExecutionContext(context=exec_mode.local_mode)\n", | |
| "\n", | |
| "simulation = Executor(exec_context=local_mode_ctx, configs=configs)\n", | |
| "\n", | |
| "raw_system_events, tensor_field, sessions = simulation.execute()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[{'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=0_0',\n", | |
| " 'simulation_id': 0,\n", | |
| " 'run_id': 0,\n", | |
| " 'subset_id': 0,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=0_1',\n", | |
| " 'simulation_id': 0,\n", | |
| " 'run_id': 1,\n", | |
| " 'subset_id': 0,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=0_2',\n", | |
| " 'simulation_id': 0,\n", | |
| " 'run_id': 2,\n", | |
| " 'subset_id': 0,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=1_0',\n", | |
| " 'simulation_id': 1,\n", | |
| " 'run_id': 0,\n", | |
| " 'subset_id': 1,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=1_1',\n", | |
| " 'simulation_id': 1,\n", | |
| " 'run_id': 1,\n", | |
| " 'subset_id': 1,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=1_2',\n", | |
| " 'simulation_id': 1,\n", | |
| " 'run_id': 2,\n", | |
| " 'subset_id': 1,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=2_0',\n", | |
| " 'simulation_id': 2,\n", | |
| " 'run_id': 0,\n", | |
| " 'subset_id': 2,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=2_1',\n", | |
| " 'simulation_id': 2,\n", | |
| " 'run_id': 1,\n", | |
| " 'subset_id': 2,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=2_2',\n", | |
| " 'simulation_id': 2,\n", | |
| " 'run_id': 2,\n", | |
| " 'subset_id': 2,\n", | |
| " 'subset_window': deque([0, None])}]" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sessions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>a</th>\n", | |
| " <th>simulation</th>\n", | |
| " <th>subset</th>\n", | |
| " <th>run</th>\n", | |
| " <th>substep</th>\n", | |
| " <th>timestep</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>0.000000</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>4.965721</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>7.113217</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>2</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>14.872412</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>17.154629</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>4</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>...</th>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>904</th>\n", | |
| " <td>475.555275</td>\n", | |
| " <td>2</td>\n", | |
| " <td>2</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>96</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>905</th>\n", | |
| " <td>480.405765</td>\n", | |
| " <td>2</td>\n", | |
| " <td>2</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>97</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>906</th>\n", | |
| " <td>484.397681</td>\n", | |
| " <td>2</td>\n", | |
| " <td>2</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>98</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>907</th>\n", | |
| " <td>486.187546</td>\n", | |
| " <td>2</td>\n", | |
| " <td>2</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>99</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>908</th>\n", | |
| " <td>495.193686</td>\n", | |
| " <td>2</td>\n", | |
| " <td>2</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>100</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "<p>909 rows × 6 columns</p>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " a simulation subset run substep timestep\n", | |
| "0 0.000000 0 0 1 0 0\n", | |
| "1 4.965721 0 0 1 1 1\n", | |
| "2 7.113217 0 0 1 1 2\n", | |
| "3 14.872412 0 0 1 1 3\n", | |
| "4 17.154629 0 0 1 1 4\n", | |
| ".. ... ... ... ... ... ...\n", | |
| "904 475.555275 2 2 3 1 96\n", | |
| "905 480.405765 2 2 3 1 97\n", | |
| "906 484.397681 2 2 3 1 98\n", | |
| "907 486.187546 2 2 3 1 99\n", | |
| "908 495.193686 2 2 3 1 100\n", | |
| "\n", | |
| "[909 rows x 6 columns]" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "simulation_result = pd.DataFrame(raw_system_events)\n", | |
| "simulation_result" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "df = simulation_result.copy()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>a</th>\n", | |
| " <th>simulation</th>\n", | |
| " <th>subset</th>\n", | |
| " <th>run</th>\n", | |
| " <th>substep</th>\n", | |
| " <th>timestep</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>0.000000</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>4.965721</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>7.113217</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>2</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>14.872412</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>17.154629</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>4</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " a simulation subset run substep timestep\n", | |
| "0 0.000000 0 0 1 0 0\n", | |
| "1 4.965721 0 0 1 1 1\n", | |
| "2 7.113217 0 0 1 1 2\n", | |
| "3 14.872412 0 0 1 1 3\n", | |
| "4 17.154629 0 0 1 1 4" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "df[df['run'] == 1].head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>a</th>\n", | |
| " <th>simulation</th>\n", | |
| " <th>subset</th>\n", | |
| " <th>run</th>\n", | |
| " <th>substep</th>\n", | |
| " <th>timestep</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>101</th>\n", | |
| " <td>0.000000</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>102</th>\n", | |
| " <td>7.806520</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>103</th>\n", | |
| " <td>13.305535</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>1</td>\n", | |
| " <td>2</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>104</th>\n", | |
| " <td>15.636235</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>1</td>\n", | |
| " <td>3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>105</th>\n", | |
| " <td>24.942497</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>1</td>\n", | |
| " <td>4</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " a simulation subset run substep timestep\n", | |
| "101 0.000000 0 0 2 0 0\n", | |
| "102 7.806520 0 0 2 1 1\n", | |
| "103 13.305535 0 0 2 1 2\n", | |
| "104 15.636235 0 0 2 1 3\n", | |
| "105 24.942497 0 0 2 1 4" | |
| ] | |
| }, | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "df[df['run'] == 2].head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>a</th>\n", | |
| " <th>simulation</th>\n", | |
| " <th>subset</th>\n", | |
| " <th>run</th>\n", | |
| " <th>substep</th>\n", | |
| " <th>timestep</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>202</th>\n", | |
| " <td>0.000000</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>203</th>\n", | |
| " <td>9.989103</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>204</th>\n", | |
| " <td>10.727481</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>2</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>205</th>\n", | |
| " <td>11.936117</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>206</th>\n", | |
| " <td>13.531981</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>4</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " a simulation subset run substep timestep\n", | |
| "202 0.000000 0 0 3 0 0\n", | |
| "203 9.989103 0 0 3 1 1\n", | |
| "204 10.727481 0 0 3 1 2\n", | |
| "205 11.936117 0 0 3 1 3\n", | |
| "206 13.531981 0 0 3 1 4" | |
| ] | |
| }, | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "df[df['run'] == 3].head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "---" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "system_params = {\n", | |
| " 'add': [10]\n", | |
| "}\n", | |
| "\n", | |
| "initial_state = {\n", | |
| " 'a': 0\n", | |
| "}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from numpy import random\n", | |
| "\n", | |
| "def update_a(params, substep, state_history, previous_state, policy_input, **kwargs):\n", | |
| " return 'a', previous_state['a'] + params['add'] * random.rand()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "psubs = [\n", | |
| " {\n", | |
| " 'policies': {},\n", | |
| " 'variables': {\n", | |
| " 'a': update_a\n", | |
| " }\n", | |
| " }\n", | |
| "]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from cadCAD import configs\n", | |
| "del configs[:] # Clear any prior configs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[{'N': 3, 'T': range(0, 100), 'M': {'add': 10}}]\n", | |
| "[{'N': 1, 'T': range(0, 100), 'M': {'add': 10}, 'subset_id': 0, 'subset_window': deque([0, None], maxlen=2), 'simulation_id': 0, 'run_id': 2}]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "exp = Experiment()\n", | |
| "\n", | |
| "sim_config = config_sim({\n", | |
| " \"N\": 3,\n", | |
| " \"T\": range(100),\n", | |
| " \"M\": system_params\n", | |
| "})\n", | |
| "print(sim_config)\n", | |
| "\n", | |
| "exp.append_configs(\n", | |
| " initial_state = initial_state,\n", | |
| " partial_state_update_blocks = psubs,\n", | |
| " sim_configs = sim_config\n", | |
| ")\n", | |
| "print(sim_config)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "\n", | |
| " ___________ ____\n", | |
| " ________ __ ___/ / ____/ | / __ \\\n", | |
| " / ___/ __` / __ / / / /| | / / / /\n", | |
| "/ /__/ /_/ / /_/ / /___/ ___ |/ /_/ /\n", | |
| "\\___/\\__,_/\\__,_/\\____/_/ |_/_____/\n", | |
| "by cadCAD\n", | |
| "\n", | |
| "Execution Mode: local_proc\n", | |
| "Configuration Count: 1\n", | |
| "Dimensions of the first simulation: (Timesteps, Params, Runs, Vars) = (100, 1, 3, 1)\n", | |
| "Execution Method: local_simulations\n", | |
| "SimIDs : [0, 0, 0]\n", | |
| "SubsetIDs: [0, 0, 0]\n", | |
| "Ns : [0, 1, 2]\n", | |
| "ExpIDs : [0, 0, 0]\n", | |
| "Execution Mode: parallelized\n", | |
| "Total execution time: 0.14s\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "exec_mode = ExecutionMode()\n", | |
| "local_mode_ctx = ExecutionContext(context=exec_mode.local_mode)\n", | |
| "\n", | |
| "simulation = Executor(exec_context=local_mode_ctx, configs=configs)\n", | |
| "\n", | |
| "raw_system_events, tensor_field, sessions = simulation.execute()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[{'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=0_0',\n", | |
| " 'simulation_id': 0,\n", | |
| " 'run_id': 0,\n", | |
| " 'subset_id': 0,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=0_1',\n", | |
| " 'simulation_id': 0,\n", | |
| " 'run_id': 1,\n", | |
| " 'subset_id': 0,\n", | |
| " 'subset_window': deque([0, None])},\n", | |
| " {'user_id': 'cadCAD_user',\n", | |
| " 'experiment_id': 0,\n", | |
| " 'session_id': 'cadCAD_user=0_2',\n", | |
| " 'simulation_id': 0,\n", | |
| " 'run_id': 2,\n", | |
| " 'subset_id': 0,\n", | |
| " 'subset_window': deque([0, None])}]" | |
| ] | |
| }, | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sessions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>a</th>\n", | |
| " <th>simulation</th>\n", | |
| " <th>subset</th>\n", | |
| " <th>run</th>\n", | |
| " <th>substep</th>\n", | |
| " <th>timestep</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>0.000000</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>4.965721</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>7.113217</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>2</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>14.872412</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>17.154629</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>4</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>...</th>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>298</th>\n", | |
| " <td>473.155075</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>96</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>299</th>\n", | |
| " <td>478.445477</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>97</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>300</th>\n", | |
| " <td>484.264235</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>98</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>301</th>\n", | |
| " <td>487.096241</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>99</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>302</th>\n", | |
| " <td>497.067108</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>100</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "<p>303 rows × 6 columns</p>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " a simulation subset run substep timestep\n", | |
| "0 0.000000 0 0 1 0 0\n", | |
| "1 4.965721 0 0 1 1 1\n", | |
| "2 7.113217 0 0 1 1 2\n", | |
| "3 14.872412 0 0 1 1 3\n", | |
| "4 17.154629 0 0 1 1 4\n", | |
| ".. ... ... ... ... ... ...\n", | |
| "298 473.155075 0 0 3 1 96\n", | |
| "299 478.445477 0 0 3 1 97\n", | |
| "300 484.264235 0 0 3 1 98\n", | |
| "301 487.096241 0 0 3 1 99\n", | |
| "302 497.067108 0 0 3 1 100\n", | |
| "\n", | |
| "[303 rows x 6 columns]" | |
| ] | |
| }, | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "simulation_result = pd.DataFrame(raw_system_events)\n", | |
| "simulation_result" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 22, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "df = simulation_result.copy()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>a</th>\n", | |
| " <th>simulation</th>\n", | |
| " <th>subset</th>\n", | |
| " <th>run</th>\n", | |
| " <th>substep</th>\n", | |
| " <th>timestep</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>0.000000</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>4.965721</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>7.113217</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>2</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>14.872412</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>17.154629</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " <td>4</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " a simulation subset run substep timestep\n", | |
| "0 0.000000 0 0 1 0 0\n", | |
| "1 4.965721 0 0 1 1 1\n", | |
| "2 7.113217 0 0 1 1 2\n", | |
| "3 14.872412 0 0 1 1 3\n", | |
| "4 17.154629 0 0 1 1 4" | |
| ] | |
| }, | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "df[df['run'] == 1].head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>a</th>\n", | |
| " <th>simulation</th>\n", | |
| " <th>subset</th>\n", | |
| " <th>run</th>\n", | |
| " <th>substep</th>\n", | |
| " <th>timestep</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>101</th>\n", | |
| " <td>0.000000</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>102</th>\n", | |
| " <td>4.965721</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>103</th>\n", | |
| " <td>7.113217</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>1</td>\n", | |
| " <td>2</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>104</th>\n", | |
| " <td>14.872412</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>1</td>\n", | |
| " <td>3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>105</th>\n", | |
| " <td>17.154629</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>2</td>\n", | |
| " <td>1</td>\n", | |
| " <td>4</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " a simulation subset run substep timestep\n", | |
| "101 0.000000 0 0 2 0 0\n", | |
| "102 4.965721 0 0 2 1 1\n", | |
| "103 7.113217 0 0 2 1 2\n", | |
| "104 14.872412 0 0 2 1 3\n", | |
| "105 17.154629 0 0 2 1 4" | |
| ] | |
| }, | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "df[df['run'] == 2].head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 25, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>a</th>\n", | |
| " <th>simulation</th>\n", | |
| " <th>subset</th>\n", | |
| " <th>run</th>\n", | |
| " <th>substep</th>\n", | |
| " <th>timestep</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>202</th>\n", | |
| " <td>0.000000</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>203</th>\n", | |
| " <td>4.965721</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>204</th>\n", | |
| " <td>7.113217</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>2</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>205</th>\n", | |
| " <td>14.872412</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>206</th>\n", | |
| " <td>17.154629</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0</td>\n", | |
| " <td>3</td>\n", | |
| " <td>1</td>\n", | |
| " <td>4</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " a simulation subset run substep timestep\n", | |
| "202 0.000000 0 0 3 0 0\n", | |
| "203 4.965721 0 0 3 1 1\n", | |
| "204 7.113217 0 0 3 1 2\n", | |
| "205 14.872412 0 0 3 1 3\n", | |
| "206 17.154629 0 0 3 1 4" | |
| ] | |
| }, | |
| "execution_count": 25, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "df[df['run'] == 3].head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python (cadCAD)", | |
| "language": "python", | |
| "name": "python-cadcad" | |
| }, | |
| "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.8.5" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment