201 lines
118 KiB
Plaintext
201 lines
118 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"pycharm": {
|
||
|
"name": "#%% md\n"
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"# 2. Rewrite the code to look like it would in your wildest dreams!\n",
|
||
|
"\n",
|
||
|
"How could you restructure the interface of the `walker` library so that the functionality stays the same, but the code is less smelly?\n",
|
||
|
"\n",
|
||
|
"1. could this code work better/more efficiently as a class?\n",
|
||
|
" - e.g. `walker = Walker(...)`\n",
|
||
|
"2. how should the setup/initialization work?\n",
|
||
|
" - e.g. which parameters would you pass to instantiate the Walker class? How do you pass them to the class?\n",
|
||
|
"3. which functions should be part of the class? \n",
|
||
|
" - e.g. is `sample_next_step` a method of `Walker`? Is `create_context_map` a method of Walker? How about `plot_trajectory`? If so, call them with `walker.sample_next_step()`\n",
|
||
|
"4. what would the import statement look like?\n",
|
||
|
"\n",
|
||
|
"**Important:** We are *not* asking you to implement the changes in `walker.py`! Imagine the new interface, and write the code that would use it in the next cell. Of course it won't run (yet)\n",
|
||
|
"\n",
|
||
|
"Here are some things to keep in mind:\n",
|
||
|
"- Which things will you do a lot? They should be easy to do. The interface for doing those things should be simple.\n",
|
||
|
"- Which things should be modifiable from external code?\n",
|
||
|
"- As a rule of thumb, think about how it sounds when you read it aloud\n",
|
||
|
" - “Giving orders”-style\n",
|
||
|
" - “Telling a story“-style\n",
|
||
|
"\n",
|
||
|
" \n",
|
||
|
"For inspiration think about how you call code from other libraries. Here is an example of instantiating an object and calling one of its methods:\n",
|
||
|
"````python\n",
|
||
|
"import scipy as sp\n",
|
||
|
"\n",
|
||
|
"# Sampling from a gaussian with specific mean and sd:\n",
|
||
|
"# Create a Gaussian (normal) distribution object\n",
|
||
|
"gaussian_dist = sp.stats.norm(loc=mean, scale=sd)\n",
|
||
|
"# Sample 10 values from the Gaussian distribution\n",
|
||
|
"samples = gaussian_dist.rvs(size=10)\n",
|
||
|
"````\n",
|
||
|
"\n",
|
||
|
"\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2022-08-18T09:45:09.308173Z",
|
||
|
"start_time": "2022-08-18T11:45:08.852431+02:00"
|
||
|
},
|
||
|
"execution": {
|
||
|
"iopub.execute_input": "2022-08-20T06:21:31.332Z",
|
||
|
"iopub.status.busy": "2022-08-20T06:21:31.327Z",
|
||
|
"iopub.status.idle": "2022-08-20T06:21:31.339Z",
|
||
|
"shell.execute_reply": "2022-08-20T06:21:31.343Z"
|
||
|
},
|
||
|
"pycharm": {
|
||
|
"name": "#%%\n"
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%matplotlib inline\n",
|
||
|
"\n",
|
||
|
"from walker import Walker, plot_trajectory"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {
|
||
|
"execution": {
|
||
|
"iopub.execute_input": "2022-08-20T06:21:31.938Z",
|
||
|
"iopub.status.busy": "2022-08-20T06:21:31.932Z",
|
||
|
"iopub.status.idle": "2022-08-20T06:21:32.867Z",
|
||
|
"shell.execute_reply": "2022-08-20T06:21:32.871Z"
|
||
|
},
|
||
|
"pycharm": {
|
||
|
"name": "#%%\n"
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Create a Walker instance\n",
|
||
|
"walker = Walker(sigma_i=3, sigma_j=4, size=200, map_type='hills')\n",
|
||
|
"\n",
|
||
|
"# Sample a next step 1000 times\n",
|
||
|
"i, j = 100, 50\n",
|
||
|
"trajectory = []\n",
|
||
|
"for _ in range(1000):\n",
|
||
|
" i, j = walker.sample_next_step(i, j)\n",
|
||
|
" trajectory.append((i, j))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"execution": {
|
||
|
"iopub.execute_input": "2022-08-20T06:21:32.883Z",
|
||
|
"iopub.status.busy": "2022-08-20T06:21:32.879Z",
|
||
|
"iopub.status.idle": "2022-08-20T06:21:33.026Z",
|
||
|
"shell.execute_reply": "2022-08-20T06:21:33.033Z"
|
||
|
},
|
||
|
"pycharm": {
|
||
|
"name": "#%%\n"
|
||
|
}
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAawAAAGkCAYAAABtmxHBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOxdd3wVxdp+ZvecFEJCDSn03ntHBWwIir2Aer1Yrt2riL1de7+Wa+8dK/aCigoIIkjvndBrgBAg7Zzd+f7YndkpuycniGL89vn9kj0z22d2d97nbUMopRQhQoQIESLEXxzGob6AECFChAgRIhmEA1aIECFChKgWCAesECFChAhRLRAOWCFChAgRologHLBChAgRIkS1QDhghQgRIkSIaoFwwAoRIkSIENUC4YAVIkSIECGqBcIBK0SIECFCVAuEA1aIECFChKgWqLYD1nPPPYfmzZsjLS0NPXv2xJQpUw71Jf0tcNddd4EQIv3l5uby9ZRS3HXXXcjPz0d6ejoGDx6MxYsXH8Irrp74+eefceKJJyI/Px+EEHz22WfS+mTauby8HP/+979Rv359ZGRk4KSTTsLGjRv/xLuoXqiszc8//3zt2e/Xr5+0TdjmhxbVcsD64IMPMHr0aNx2222YO3cujjjiCAwbNgzr168/1Jf2t0DHjh2xZcsW/rdw4UK+7pFHHsHjjz+OZ555BjNnzkRubi6OPfZY7N279xBecfXD/v370bVrVzzzzDO+65Np59GjR+PTTz/F+++/j6lTp2Lfvn0YPnw4LMv6s26jWqGyN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dU
|
||
|
"text/plain": [
|
||
|
"<Figure size 480x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plot_trajectory(trajectory, walker.context_map)\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {
|
||
|
"pycharm": {
|
||
|
"name": "#%%\n"
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {
|
||
|
"collapsed": true,
|
||
|
"jupyter": {
|
||
|
"outputs_hidden": false,
|
||
|
"source_hidden": false
|
||
|
},
|
||
|
"nteract": {
|
||
|
"transient": {
|
||
|
"deleting": false
|
||
|
}
|
||
|
},
|
||
|
"pycharm": {
|
||
|
"name": "#%%\n"
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"hide_input": false,
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"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.11.3"
|
||
|
},
|
||
|
"nteract": {
|
||
|
"version": "0.28.0"
|
||
|
},
|
||
|
"toc": {
|
||
|
"nav_menu": {
|
||
|
"height": "12px",
|
||
|
"width": "252px"
|
||
|
},
|
||
|
"navigate_menu": true,
|
||
|
"number_sections": true,
|
||
|
"sideBar": true,
|
||
|
"threshold": 4,
|
||
|
"toc_cell": false,
|
||
|
"toc_section_display": "block",
|
||
|
"toc_window_display": false
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 1
|
||
|
}
|