|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Generate expected results for back tests\n", |
| 8 | + "\n", |
| 9 | + "They save results to `.csv` and use these to verify that the model produces consistent results." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "# pylint: disable=missing-module-docstring\n", |
| 19 | + "%load_ext autoreload\n", |
| 20 | + "%autoreload 1\n", |
| 21 | + "%aimport simulation\n", |
| 22 | + "\n", |
| 23 | + "# pylint: disable=wrong-import-position\n", |
| 24 | + "import os\n", |
| 25 | + "import pandas as pd\n", |
| 26 | + "\n", |
| 27 | + "from simulation.parameters import Param\n", |
| 28 | + "from simulation.model import Model\n", |
| 29 | + "from simulation.runner import Runner" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": 2, |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "# Define path to folder for expected results for tests\n", |
| 39 | + "TESTS = \"../tests/exp_results/\"" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "markdown", |
| 44 | + "metadata": {}, |
| 45 | + "source": [ |
| 46 | + "The raw `audit_list` from a single run of the model (converted to a dataframe)." |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 3, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "data": { |
| 56 | + "text/html": [ |
| 57 | + "<div>\n", |
| 58 | + "<style scoped>\n", |
| 59 | + " .dataframe tbody tr th:only-of-type {\n", |
| 60 | + " vertical-align: middle;\n", |
| 61 | + " }\n", |
| 62 | + "\n", |
| 63 | + " .dataframe tbody tr th {\n", |
| 64 | + " vertical-align: top;\n", |
| 65 | + " }\n", |
| 66 | + "\n", |
| 67 | + " .dataframe thead th {\n", |
| 68 | + " text-align: right;\n", |
| 69 | + " }\n", |
| 70 | + "</style>\n", |
| 71 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 72 | + " <thead>\n", |
| 73 | + " <tr style=\"text-align: right;\">\n", |
| 74 | + " <th></th>\n", |
| 75 | + " <th>time</th>\n", |
| 76 | + " <th>asu_occupancy</th>\n", |
| 77 | + " <th>rehab_occupancy</th>\n", |
| 78 | + " </tr>\n", |
| 79 | + " </thead>\n", |
| 80 | + " <tbody>\n", |
| 81 | + " <tr>\n", |
| 82 | + " <th>0</th>\n", |
| 83 | + " <td>1095</td>\n", |
| 84 | + " <td>7</td>\n", |
| 85 | + " <td>12</td>\n", |
| 86 | + " </tr>\n", |
| 87 | + " <tr>\n", |
| 88 | + " <th>1</th>\n", |
| 89 | + " <td>1096</td>\n", |
| 90 | + " <td>5</td>\n", |
| 91 | + " <td>11</td>\n", |
| 92 | + " </tr>\n", |
| 93 | + " <tr>\n", |
| 94 | + " <th>2</th>\n", |
| 95 | + " <td>1097</td>\n", |
| 96 | + " <td>5</td>\n", |
| 97 | + " <td>10</td>\n", |
| 98 | + " </tr>\n", |
| 99 | + " <tr>\n", |
| 100 | + " <th>3</th>\n", |
| 101 | + " <td>1098</td>\n", |
| 102 | + " <td>4</td>\n", |
| 103 | + " <td>10</td>\n", |
| 104 | + " </tr>\n", |
| 105 | + " <tr>\n", |
| 106 | + " <th>4</th>\n", |
| 107 | + " <td>1099</td>\n", |
| 108 | + " <td>5</td>\n", |
| 109 | + " <td>10</td>\n", |
| 110 | + " </tr>\n", |
| 111 | + " </tbody>\n", |
| 112 | + "</table>\n", |
| 113 | + "</div>" |
| 114 | + ], |
| 115 | + "text/plain": [ |
| 116 | + " time asu_occupancy rehab_occupancy\n", |
| 117 | + "0 1095 7 12\n", |
| 118 | + "1 1096 5 11\n", |
| 119 | + "2 1097 5 10\n", |
| 120 | + "3 1098 4 10\n", |
| 121 | + "4 1099 5 10" |
| 122 | + ] |
| 123 | + }, |
| 124 | + "metadata": {}, |
| 125 | + "output_type": "display_data" |
| 126 | + } |
| 127 | + ], |
| 128 | + "source": [ |
| 129 | + "# Run model\n", |
| 130 | + "model = Model(param=Param(), run_number=0)\n", |
| 131 | + "model.run()\n", |
| 132 | + "\n", |
| 133 | + "# Convert audit list to dataframe and preview\n", |
| 134 | + "audit_list = pd.DataFrame(model.audit_list)\n", |
| 135 | + "display(audit_list.head())\n", |
| 136 | + "\n", |
| 137 | + "# Save to csv\n", |
| 138 | + "audit_list.to_csv(os.path.join(TESTS, \"audit_list.csv\"), index=False)" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "metadata": {}, |
| 144 | + "source": [ |
| 145 | + "The dataframes returned by `run_single()` from `Runner`, from a single replication." |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": 4, |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [ |
| 153 | + { |
| 154 | + "data": { |
| 155 | + "text/html": [ |
| 156 | + "<div>\n", |
| 157 | + "<style scoped>\n", |
| 158 | + " .dataframe tbody tr th:only-of-type {\n", |
| 159 | + " vertical-align: middle;\n", |
| 160 | + " }\n", |
| 161 | + "\n", |
| 162 | + " .dataframe tbody tr th {\n", |
| 163 | + " vertical-align: top;\n", |
| 164 | + " }\n", |
| 165 | + "\n", |
| 166 | + " .dataframe thead th {\n", |
| 167 | + " text-align: right;\n", |
| 168 | + " }\n", |
| 169 | + "</style>\n", |
| 170 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 171 | + " <thead>\n", |
| 172 | + " <tr style=\"text-align: right;\">\n", |
| 173 | + " <th></th>\n", |
| 174 | + " <th>beds</th>\n", |
| 175 | + " <th>freq</th>\n", |
| 176 | + " <th>pct</th>\n", |
| 177 | + " <th>c_pct</th>\n", |
| 178 | + " <th>prob_delay</th>\n", |
| 179 | + " </tr>\n", |
| 180 | + " </thead>\n", |
| 181 | + " <tbody>\n", |
| 182 | + " <tr>\n", |
| 183 | + " <th>0</th>\n", |
| 184 | + " <td>1</td>\n", |
| 185 | + " <td>1</td>\n", |
| 186 | + " <td>0.000548</td>\n", |
| 187 | + " <td>0.000548</td>\n", |
| 188 | + " <td>1.000000</td>\n", |
| 189 | + " </tr>\n", |
| 190 | + " <tr>\n", |
| 191 | + " <th>1</th>\n", |
| 192 | + " <td>2</td>\n", |
| 193 | + " <td>12</td>\n", |
| 194 | + " <td>0.006575</td>\n", |
| 195 | + " <td>0.007123</td>\n", |
| 196 | + " <td>0.923077</td>\n", |
| 197 | + " </tr>\n", |
| 198 | + " <tr>\n", |
| 199 | + " <th>2</th>\n", |
| 200 | + " <td>3</td>\n", |
| 201 | + " <td>39</td>\n", |
| 202 | + " <td>0.021370</td>\n", |
| 203 | + " <td>0.028493</td>\n", |
| 204 | + " <td>0.750000</td>\n", |
| 205 | + " </tr>\n", |
| 206 | + " <tr>\n", |
| 207 | + " <th>3</th>\n", |
| 208 | + " <td>4</td>\n", |
| 209 | + " <td>76</td>\n", |
| 210 | + " <td>0.041644</td>\n", |
| 211 | + " <td>0.070137</td>\n", |
| 212 | + " <td>0.593750</td>\n", |
| 213 | + " </tr>\n", |
| 214 | + " <tr>\n", |
| 215 | + " <th>4</th>\n", |
| 216 | + " <td>5</td>\n", |
| 217 | + " <td>134</td>\n", |
| 218 | + " <td>0.073425</td>\n", |
| 219 | + " <td>0.143562</td>\n", |
| 220 | + " <td>0.511450</td>\n", |
| 221 | + " </tr>\n", |
| 222 | + " </tbody>\n", |
| 223 | + "</table>\n", |
| 224 | + "</div>" |
| 225 | + ], |
| 226 | + "text/plain": [ |
| 227 | + " beds freq pct c_pct prob_delay\n", |
| 228 | + "0 1 1 0.000548 0.000548 1.000000\n", |
| 229 | + "1 2 12 0.006575 0.007123 0.923077\n", |
| 230 | + "2 3 39 0.021370 0.028493 0.750000\n", |
| 231 | + "3 4 76 0.041644 0.070137 0.593750\n", |
| 232 | + "4 5 134 0.073425 0.143562 0.511450" |
| 233 | + ] |
| 234 | + }, |
| 235 | + "metadata": {}, |
| 236 | + "output_type": "display_data" |
| 237 | + }, |
| 238 | + { |
| 239 | + "data": { |
| 240 | + "text/html": [ |
| 241 | + "<div>\n", |
| 242 | + "<style scoped>\n", |
| 243 | + " .dataframe tbody tr th:only-of-type {\n", |
| 244 | + " vertical-align: middle;\n", |
| 245 | + " }\n", |
| 246 | + "\n", |
| 247 | + " .dataframe tbody tr th {\n", |
| 248 | + " vertical-align: top;\n", |
| 249 | + " }\n", |
| 250 | + "\n", |
| 251 | + " .dataframe thead th {\n", |
| 252 | + " text-align: right;\n", |
| 253 | + " }\n", |
| 254 | + "</style>\n", |
| 255 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 256 | + " <thead>\n", |
| 257 | + " <tr style=\"text-align: right;\">\n", |
| 258 | + " <th></th>\n", |
| 259 | + " <th>beds</th>\n", |
| 260 | + " <th>freq</th>\n", |
| 261 | + " <th>pct</th>\n", |
| 262 | + " <th>c_pct</th>\n", |
| 263 | + " <th>prob_delay</th>\n", |
| 264 | + " </tr>\n", |
| 265 | + " </thead>\n", |
| 266 | + " <tbody>\n", |
| 267 | + " <tr>\n", |
| 268 | + " <th>0</th>\n", |
| 269 | + " <td>3</td>\n", |
| 270 | + " <td>9</td>\n", |
| 271 | + " <td>0.004932</td>\n", |
| 272 | + " <td>0.004932</td>\n", |
| 273 | + " <td>1.000000</td>\n", |
| 274 | + " </tr>\n", |
| 275 | + " <tr>\n", |
| 276 | + " <th>1</th>\n", |
| 277 | + " <td>4</td>\n", |
| 278 | + " <td>25</td>\n", |
| 279 | + " <td>0.013699</td>\n", |
| 280 | + " <td>0.018630</td>\n", |
| 281 | + " <td>0.735294</td>\n", |
| 282 | + " </tr>\n", |
| 283 | + " <tr>\n", |
| 284 | + " <th>2</th>\n", |
| 285 | + " <td>5</td>\n", |
| 286 | + " <td>74</td>\n", |
| 287 | + " <td>0.040548</td>\n", |
| 288 | + " <td>0.059178</td>\n", |
| 289 | + " <td>0.685185</td>\n", |
| 290 | + " </tr>\n", |
| 291 | + " <tr>\n", |
| 292 | + " <th>3</th>\n", |
| 293 | + " <td>6</td>\n", |
| 294 | + " <td>147</td>\n", |
| 295 | + " <td>0.080548</td>\n", |
| 296 | + " <td>0.139726</td>\n", |
| 297 | + " <td>0.576471</td>\n", |
| 298 | + " </tr>\n", |
| 299 | + " <tr>\n", |
| 300 | + " <th>4</th>\n", |
| 301 | + " <td>7</td>\n", |
| 302 | + " <td>176</td>\n", |
| 303 | + " <td>0.096438</td>\n", |
| 304 | + " <td>0.236164</td>\n", |
| 305 | + " <td>0.408353</td>\n", |
| 306 | + " </tr>\n", |
| 307 | + " </tbody>\n", |
| 308 | + "</table>\n", |
| 309 | + "</div>" |
| 310 | + ], |
| 311 | + "text/plain": [ |
| 312 | + " beds freq pct c_pct prob_delay\n", |
| 313 | + "0 3 9 0.004932 0.004932 1.000000\n", |
| 314 | + "1 4 25 0.013699 0.018630 0.735294\n", |
| 315 | + "2 5 74 0.040548 0.059178 0.685185\n", |
| 316 | + "3 6 147 0.080548 0.139726 0.576471\n", |
| 317 | + "4 7 176 0.096438 0.236164 0.408353" |
| 318 | + ] |
| 319 | + }, |
| 320 | + "metadata": {}, |
| 321 | + "output_type": "display_data" |
| 322 | + } |
| 323 | + ], |
| 324 | + "source": [ |
| 325 | + "# Run model\n", |
| 326 | + "runner = Runner(param=Param())\n", |
| 327 | + "runner_df = runner.run_single(run=0)\n", |
| 328 | + "\n", |
| 329 | + "# Preview the dataframes\n", |
| 330 | + "display(runner_df[\"asu\"].head())\n", |
| 331 | + "display(runner_df[\"rehab\"].head())\n", |
| 332 | + "\n", |
| 333 | + "# Save to csv\n", |
| 334 | + "runner_df[\"asu\"].to_csv(os.path.join(TESTS, \"asu_occupancy.csv\"), index=False)\n", |
| 335 | + "runner_df[\"rehab\"].to_csv(os.path.join(TESTS, \"rehab_occupancy.csv\"),\n", |
| 336 | + " index=False)" |
| 337 | + ] |
| 338 | + } |
| 339 | + ], |
| 340 | + "metadata": { |
| 341 | + "kernelspec": { |
| 342 | + "display_name": "stroke-rap-python", |
| 343 | + "language": "python", |
| 344 | + "name": "python3" |
| 345 | + }, |
| 346 | + "language_info": { |
| 347 | + "codemirror_mode": { |
| 348 | + "name": "ipython", |
| 349 | + "version": 3 |
| 350 | + }, |
| 351 | + "file_extension": ".py", |
| 352 | + "mimetype": "text/x-python", |
| 353 | + "name": "python", |
| 354 | + "nbconvert_exporter": "python", |
| 355 | + "pygments_lexer": "ipython3", |
| 356 | + "version": "3.13.1" |
| 357 | + } |
| 358 | + }, |
| 359 | + "nbformat": 4, |
| 360 | + "nbformat_minor": 2 |
| 361 | +} |
0 commit comments