Skip to content

Instantly share code, notes, and snippets.

@fonnesbeck
Created February 5, 2014 16:02
Show Gist options
  • Save fonnesbeck/8826853 to your computer and use it in GitHub Desktop.
Save fonnesbeck/8826853 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:9d5bf26900d9e09589f5024bff53e6ea027f383f4d57bba1085222d0d40b1182"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.parallel import Client\n",
"\n",
"cli = Client()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create `DirectView` on engines"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dview = cli[:]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Activate the `DirectView` to enable parallel magic commands."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dview.activate()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Model Setup"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from pandas import Series, DataFrame\n",
"import numpy as np\n",
"from am4fmd import fmdoutbreak, distances as dist"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"max_days = 150\n",
"\n",
"# Parameter dictionary\n",
"params = {\n",
"'n_feedlots': 30,\n",
"'mean_size': 100, # mean of poisson dist\n",
"\n",
"# Geographic parameters\n",
"'x_range': [0, 48*3],\n",
"'y_range': [0, 48*3], \n",
"\n",
"# Column vectors of susceptibility and transmissibility, each column is species type\n",
"'susceptibility': 0.1, # susceptibility per animal\n",
"'transmissibility': 15.4, # transmissibility per animal (Sellers... ?)\n",
"\n",
"# Times after infection\n",
"'tau_incubate': 4, # incubation period\n",
"'tau_detect': 9, # time until detection\n",
"'tau_cull': [1, 4], # culling time\n",
"\n",
"# Kernel parameters\n",
"'kernel_type': 'power',\n",
"'kernel_scalar': -4,\n",
"'kernel_power': -10\n",
"}\n",
"\n",
"# Generate the index cases\n",
"params['index_cases'] = [0]\n",
"params['n_index_cases'] = len(params['index_cases'])\n",
"\n",
"# Geography\n",
"params['xy'] = np.random.randint(np.diff(params['x_range'])[0], size = params['n_feedlots']*2).reshape([params['n_feedlots'], 2])\n",
"\n",
"# Create the distance matrix between all farms\n",
"params['D'] = np.matrix(dist.EuclideanDistance( params['xy'] ))\n",
"\n",
"# Codes for feedlot states, as used in 'infection_status' vector\n",
"params[\"states\"] = {\n",
"'susceptible':-1,\n",
"'culled':-2\n",
"}\n",
"\n",
"# Time when culling begins for each feedlot\n",
"# this is a uniform random integer: tau_detect + U(tau_cull[0], tau_cull[1])\n",
"params['cull_time'] = params['tau_detect'] + params['tau_cull'][0] + np.random.randint(params['tau_cull'][1], size = params['n_feedlots'])\n",
"params['cull_rate'] = np.array([40]*params['n_feedlots'])\n",
"\n",
"#########################\n",
"# CONTROL #\n",
"#########################\n",
"\n",
"# Set the time horizons to allow control to change\n",
"control_horizons = [7] + 3*np.arange(max_days/3)\n",
"control_horizons = np.insert(control_horizons, 0, 3)\n",
"\n",
"control_params = {\n",
"'horizons': control_horizons,\n",
"# Control measures\n",
"'control': \n",
"{\n",
"0: 'No control',\n",
"1: 'Stamping out IP',\n",
"2: 'Protective vaccination',\n",
"3: 'Suppressive vaccination'\n",
"},\n",
"# Keys are vaccine delivery horizons, values are dose sizes being delivered. \n",
"'vaccine_horizons':\n",
"{\n",
"7: 3000,\n",
"14: 5000,\n",
"21: 5000,\n",
"28: 5000,\n",
"35: 5000,\n",
"42: 2000\n",
"}\n",
"}\n",
"\n",
"control_params['inner_radius'] = 10\n",
"control_params['outer_radius'] = 20\n",
"control_params['tau_vaccination'] = 50\n",
"\n",
"# Create a dictionary of control measures (set all to 0, no control)\n",
"# keys are time horizons, values are control measure to be taken at that time horizon. \n",
"control = dict.fromkeys(control_horizons, [1, 2]) # stamping out and protective vaccination\n",
"control[3] = [1] # Only stamping-out at t=3 (no vaccine has arrived yet)\n",
"control_params['control_regime'] = control\n",
"\n",
"# Control measures \n",
"params['vaccination_rate'] = np.array([40]*params['n_feedlots']) # daily vaccination rate\n",
"\n",
"# Generate the number of animals in each feedlot\n",
"params['starting_N'] = np.random.poisson(params['mean_size'], params['n_feedlots'])\n",
"control_params['vacc_alloc_rule'] = 'size'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run simulation on cluster"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"runs = dview.apply(fmdoutbreak.fmdoutbreak, params, control_params, max_days)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 25,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Retrieve results as dict"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"results = runs.get_dict()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 32,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Shape of a single output"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"results[0].shape"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 49,
"text": [
"(1020, 66)"
]
}
],
"prompt_number": 49,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use `concat` to turn them into a `DataFrame`. Pandas will use the dict key as a hierarchical index, so we can tell the runs apart."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"frame_simulation = pd.concat(results)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 45,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Shape of combined results"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"frame_simulation.shape"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 46,
"text": [
"(4500, 66)"
]
}
],
"prompt_number": 46,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"frame_simulation.head()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>N</th>\n",
" <th>N_I</th>\n",
" <th>N_I_pseudo</th>\n",
" <th>N_V</th>\n",
" <th>N_carcass</th>\n",
" <th>V0</th>\n",
" <th>V1</th>\n",
" <th>V10</th>\n",
" <th>V11</th>\n",
" <th>V12</th>\n",
" <th>V13</th>\n",
" <th>V14</th>\n",
" <th>V15</th>\n",
" <th>V16</th>\n",
" <th>V17</th>\n",
" <th>V18</th>\n",
" <th>V19</th>\n",
" <th>V2</th>\n",
" <th>V20</th>\n",
" <th>V21</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th>0</th>\n",
" <td> 99</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td> 96</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td> 76</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td> 86</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td> 102</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows \u00d7 66 columns</p>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 47,
"text": [
" N N_I N_I_pseudo N_V N_carcass V0 V1 V10 V11 V12 V13 V14 \\\n",
"0 0 99 0 0 0 0 0 0 0 0 0 0 0 \n",
" 1 96 0 0 0 0 0 0 0 0 0 0 0 \n",
" 2 76 0 0 0 0 0 0 0 0 0 0 0 \n",
" 3 86 0 0 0 0 0 0 0 0 0 0 0 \n",
" 4 102 0 0 0 0 0 0 0 0 0 0 0 \n",
"\n",
" V15 V16 V17 V18 V19 V2 V20 V21 \n",
"0 0 0 0 0 0 0 0 0 0 ... \n",
" 1 0 0 0 0 0 0 0 0 ... \n",
" 2 0 0 0 0 0 0 0 0 ... \n",
" 3 0 0 0 0 0 0 0 0 ... \n",
" 4 0 0 0 0 0 0 0 0 ... \n",
"\n",
"[5 rows x 66 columns]"
]
}
],
"prompt_number": 47,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"frame_simulation.tail()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>N</th>\n",
" <th>N_I</th>\n",
" <th>N_I_pseudo</th>\n",
" <th>N_V</th>\n",
" <th>N_carcass</th>\n",
" <th>V0</th>\n",
" <th>V1</th>\n",
" <th>V10</th>\n",
" <th>V11</th>\n",
" <th>V12</th>\n",
" <th>V13</th>\n",
" <th>V14</th>\n",
" <th>V15</th>\n",
" <th>V16</th>\n",
" <th>V17</th>\n",
" <th>V18</th>\n",
" <th>V19</th>\n",
" <th>V2</th>\n",
" <th>V20</th>\n",
" <th>V21</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"5\" valign=\"top\">3</th>\n",
" <th>1285</th>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 101</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1286</th>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 106</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1287</th>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 93</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1288</th>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 96</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1289</th>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 87</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows \u00d7 66 columns</p>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 48,
"text": [
" N N_I N_I_pseudo N_V N_carcass V0 V1 V10 V11 V12 V13 V14 \\\n",
"3 1285 0 0 0 0 101 0 0 0 0 0 0 0 \n",
" 1286 0 0 0 0 106 0 0 0 0 0 0 0 \n",
" 1287 0 0 0 0 93 0 0 0 0 0 0 0 \n",
" 1288 0 0 0 0 96 0 0 0 0 0 0 0 \n",
" 1289 0 0 0 0 87 0 0 0 0 0 0 0 \n",
"\n",
" V15 V16 V17 V18 V19 V2 V20 V21 \n",
"3 1285 0 0 0 0 0 0 0 0 ... \n",
" 1286 0 0 0 0 0 0 0 0 ... \n",
" 1287 0 0 0 0 0 0 0 0 ... \n",
" 1288 0 0 0 0 0 0 0 0 ... \n",
" 1289 0 0 0 0 0 0 0 0 ... \n",
"\n",
"[5 rows x 66 columns]"
]
}
],
"prompt_number": 48,
"trusted": true
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment