Exercise 3
This commit is contained in:
parent
50ae6c84c5
commit
f9cff3f339
3 changed files with 260 additions and 5 deletions
|
@ -0,0 +1,204 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"nteract": {
|
||||||
|
"transient": {
|
||||||
|
"deleting": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# 1. Take a look at this (working) code\n",
|
||||||
|
"\n",
|
||||||
|
"... and run it. We discussed that the `context_map` varies independently of the walker. Identify the part of the code that will be affected by this change."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2022-08-18T09:50:40.616906Z",
|
||||||
|
"start_time": "2022-08-18T11:50:40.181358+02:00"
|
||||||
|
},
|
||||||
|
"execution": {
|
||||||
|
"iopub.execute_input": "2022-08-20T06:27:54.689Z",
|
||||||
|
"iopub.status.busy": "2022-08-20T06:27:54.685Z",
|
||||||
|
"iopub.status.idle": "2022-08-20T06:27:55.297Z",
|
||||||
|
"shell.execute_reply": "2022-08-20T06:27:55.319Z"
|
||||||
|
},
|
||||||
|
"pycharm": {
|
||||||
|
"name": "#%%\n"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"from plotting import plot_trajectory, plot_trajectory_hexbin\n",
|
||||||
|
"from walker import Walker\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true,
|
||||||
|
"jupyter": {
|
||||||
|
"outputs_hidden": true,
|
||||||
|
"source_hidden": false
|
||||||
|
},
|
||||||
|
"nteract": {
|
||||||
|
"transient": {
|
||||||
|
"deleting": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"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))\n",
|
||||||
|
"\n",
|
||||||
|
"plot_trajectory(trajectory, walker.context_map)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"nteract": {
|
||||||
|
"transient": {
|
||||||
|
"deleting": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# 2. Propose modifications to the code that calls the walker (above) to reflect the idea of a separate context_map module\n",
|
||||||
|
"\n",
|
||||||
|
"1. how would the import statement change as a result of needing a separate context_map module?\n",
|
||||||
|
"2. what input arguments do the context_map functions need to take?\n",
|
||||||
|
"3. how does the initialization of the walker change?\n",
|
||||||
|
" - i.e. instead of \"map_type\"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"from plotting import plot_trajectory, plot_trajectory_hexbin\n",
|
||||||
|
"from walker import Walker\n",
|
||||||
|
"from context_map import create_context_map"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"context_map = create_context_map(size=200, map_type='hills')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Create a Walker instance\n",
|
||||||
|
"walker = Walker(sigma_i=3, sigma_j=4, context_map)\n",
|
||||||
|
"\n",
|
||||||
|
"# Sample a next step 1000 times\n",
|
||||||
|
"initial_position = (100, 50)\n",
|
||||||
|
"N_steps = 1000\n",
|
||||||
|
"trajectory = np.array((N_steps, 2), dtype=np.int8)\n",
|
||||||
|
"\n",
|
||||||
|
"for step in range(N_steps):\n",
|
||||||
|
" i,j = walker.sample_next_step(i, j)\n",
|
||||||
|
" trajectory[step] = np.array([i,j], dtype = np.int8)\n",
|
||||||
|
"\n",
|
||||||
|
"plot_trajectory(trajectory, context_map)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"nteract": {
|
||||||
|
"transient": {
|
||||||
|
"deleting": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# 3. (optional) Actually break out the context map initialization\n",
|
||||||
|
"1. Move context map initialization to three functions in a separate `context_map.py` module which all return a `context_map` array\n",
|
||||||
|
"2. Modify the constructor of Walker to take a `context_map` array instead of a `map_type`\n",
|
||||||
|
"3. Modify this notebook to use the new code and see if the code you wrote works!\n",
|
||||||
|
"4. Try to run all the types:\n",
|
||||||
|
" - Run one simulation with a flat context map\n",
|
||||||
|
" - Run one simulation with a hill context map\n",
|
||||||
|
" - Run one simulation with a labyrinth context map\n",
|
||||||
|
"\n",
|
||||||
|
"When you have a working implementation: PR! You can link the issue in the commit message (by writing \"issue #3\")."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"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.13.6"
|
||||||
|
},
|
||||||
|
"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": 4
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def plot_trajectory(trajectory, context_map):
|
||||||
|
""" Plot a trajectory over a context map. """
|
||||||
|
trajectory = np.asarray(trajectory)
|
||||||
|
plt.matshow(context_map)
|
||||||
|
plt.plot(trajectory[:, 1], trajectory[:, 0], color='r')
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
def plot_trajectory_hexbin(trajectory):
|
||||||
|
""" Plot an hexagonal density map of a trajectory. """
|
||||||
|
trajectory = np.asarray(trajectory)
|
||||||
|
with plt.rc_context({'figure.figsize': (4, 4), 'axes.labelsize': 16,
|
||||||
|
'xtick.labelsize': 14, 'ytick.labelsize': 14}):
|
||||||
|
plt.hexbin(trajectory[:, 1], trajectory[:, 0], gridsize=30,
|
||||||
|
extent=(0, 200, 0, 200), edgecolors='none', cmap='Reds')
|
||||||
|
plt.gca().invert_yaxis()
|
||||||
|
plt.xlabel('X')
|
||||||
|
plt.ylabel('Y')
|
|
@ -47,7 +47,7 @@
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true,
|
"collapsed": true,
|
||||||
"jupyter": {
|
"jupyter": {
|
||||||
"outputs_hidden": false,
|
"outputs_hidden": true,
|
||||||
"source_hidden": false
|
"source_hidden": false
|
||||||
},
|
},
|
||||||
"nteract": {
|
"nteract": {
|
||||||
|
@ -95,14 +95,43 @@
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": []
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"from plotting import plot_trajectory, plot_trajectory_hexbin\n",
|
||||||
|
"from walker import Walker\n",
|
||||||
|
"from context_map import create_context_map"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": []
|
"source": [
|
||||||
|
"context_map = create_context_map(size=200, map_type='hills')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Create a Walker instance\n",
|
||||||
|
"walker = Walker(sigma_i=3, sigma_j=4, context_map)\n",
|
||||||
|
"\n",
|
||||||
|
"# Sample a next step 1000 times\n",
|
||||||
|
"initial_position = (100, 50)\n",
|
||||||
|
"N_steps = 1000\n",
|
||||||
|
"trajectory = np.array((N_steps, 2), dtype=np.int8)\n",
|
||||||
|
"\n",
|
||||||
|
"for step in range(N_steps):\n",
|
||||||
|
" i,j = walker.sample_next_step(i, j)\n",
|
||||||
|
" trajectory[step] = np.array([i,j], dtype = np.int8)\n",
|
||||||
|
"\n",
|
||||||
|
"plot_trajectory(trajectory, context_map)"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
@ -151,7 +180,7 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.11.3"
|
"version": "3.13.6"
|
||||||
},
|
},
|
||||||
"nteract": {
|
"nteract": {
|
||||||
"version": "0.28.0"
|
"version": "0.28.0"
|
||||||
|
@ -171,5 +200,5 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 1
|
"nbformat_minor": 4
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue