diff --git a/class_materials/exercises/particle_update_position.ipynb b/class_materials/exercises/particle_update_position.ipynb index b3d829a..6baba13 100644 --- a/class_materials/exercises/particle_update_position.ipynb +++ b/class_materials/exercises/particle_update_position.ipynb @@ -35,51 +35,65 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": { "pycharm": { "name": "#%%\n" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.6800000000000002\n" - ] - } - ], + "outputs": [], "source": [ "class Particle:\n", - " def __init__(self, mass, velocity):\n", + " def __init__(self, mass=1, velocity=0., position=0.):\n", " self.mass = mass\n", " self.velocity = velocity\n", + " self.position = position\n", "\n", " def momentum(self):\n", " return self.mass * self.velocity\n", "\n", - "particle = Particle(mass=2.1, velocity=0.8)\n", - "print(particle.momentum())" + " def update_position(self, dt):\n", + " new_position = self.position + dt * self.velocity\n", + " self.position = new_position\n", + " return" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "8.28\n" + "1.6800000000000002\n", + "0.0\n" ] } ], "source": [ - "position = 8.2\n", - "new_position = update_position(particle.velocity, position, dt=0.1)\n", - "print(new_position)" + "particle = Particle(mass=2.1, velocity=0.8, position=0.)\n", + "print(particle.momentum())\n", + "print(particle.position)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.08000000000000002\n" + ] + } + ], + "source": [ + "particle.update_position(dt=0.1)\n", + "print(particle.position)" ] }, { @@ -118,7 +132,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.13.6" }, "toc": { "nav_menu": { @@ -142,5 +156,5 @@ } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/class_materials/walker/Step_3_break_out_the_context_map_initialization/.ipynb_checkpoints/Step_3_break_out_the_context_map_initialization-checkpoint.ipynb b/class_materials/walker/Step_3_break_out_the_context_map_initialization/.ipynb_checkpoints/Step_3_break_out_the_context_map_initialization-checkpoint.ipynb new file mode 100644 index 0000000..f994388 --- /dev/null +++ b/class_materials/walker/Step_3_break_out_the_context_map_initialization/.ipynb_checkpoints/Step_3_break_out_the_context_map_initialization-checkpoint.ipynb @@ -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 +} diff --git a/class_materials/walker/Step_3_break_out_the_context_map_initialization/.ipynb_checkpoints/plotting-checkpoint.py b/class_materials/walker/Step_3_break_out_the_context_map_initialization/.ipynb_checkpoints/plotting-checkpoint.py new file mode 100644 index 0000000..b07ea67 --- /dev/null +++ b/class_materials/walker/Step_3_break_out_the_context_map_initialization/.ipynb_checkpoints/plotting-checkpoint.py @@ -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') diff --git a/class_materials/walker/Step_3_break_out_the_context_map_initialization/Step_3_break_out_the_context_map_initialization.ipynb b/class_materials/walker/Step_3_break_out_the_context_map_initialization/Step_3_break_out_the_context_map_initialization.ipynb index 04a4339..f994388 100644 --- a/class_materials/walker/Step_3_break_out_the_context_map_initialization/Step_3_break_out_the_context_map_initialization.ipynb +++ b/class_materials/walker/Step_3_break_out_the_context_map_initialization/Step_3_break_out_the_context_map_initialization.ipynb @@ -47,7 +47,7 @@ "metadata": { "collapsed": true, "jupyter": { - "outputs_hidden": false, + "outputs_hidden": true, "source_hidden": false }, "nteract": { @@ -95,14 +95,43 @@ "execution_count": null, "metadata": {}, "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", "execution_count": null, "metadata": {}, "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", @@ -151,7 +180,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.13.6" }, "nteract": { "version": "0.28.0" @@ -171,5 +200,5 @@ } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 }