2024-heraklion-scientific-p.../notebooks/walker/Step_1_classes/Step_1_classes_exercise.ipynb

209 lines
140 KiB
Plaintext
Raw Permalink Normal View History

2024-08-27 14:52:41 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"# 1. Take a look at this (working) code\n",
"\n",
"... and run it."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2023-08-15T08:17:58.966008Z",
"start_time": "2023-08-15T08:17:58.961545Z"
},
"execution": {
"iopub.execute_input": "2024-08-05T14:06:56.784Z",
"iopub.status.busy": "2024-08-05T14:06:56.776Z",
"iopub.status.idle": "2024-08-05T14:06:56.789Z",
"shell.execute_reply": "2024-08-05T14:06:56.793Z"
},
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"from walker import create_context_map, plot_trajectory, sample_next_step"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2023-08-15T08:18:00.805824Z",
"start_time": "2023-08-15T08:18:00.167515Z"
},
"execution": {
"iopub.execute_input": "2024-08-05T14:12:32.248Z",
"iopub.status.busy": "2024-08-05T14:12:32.244Z",
"iopub.status.idle": "2024-08-05T14:12:32.938Z",
"shell.execute_reply": "2024-08-05T14:12:32.947Z"
},
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAawAAAGkCAYAAABtmxHBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOx9d3jVRvb2K9173W0MxsY2NmDTe28JLQUICYT0nk2vm7bJJptsvk3bbNpuOum9k0p6Qgg19N6bwTZgwBgX3Mu90nx/SDOaGUnX12nE+el9HnvujNp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SvqTa0IlNJWJnWHapRr5Xu5rIrhKy1ANmGe0Dlje3c6lVZrZxQD4HsadUIvdTRmWqU2jevEGVlqsEqmr95vehkIVz1g5EYYxK13UzVcTUrA9te6Kq1m9FlFapWoX2OVMdKrDUlnQf2rdUOKbSrFlOBJWgxNhs6iK4Q1L7wtbXsKkEbX3toBp0V/2Jal7an/Q9gwq7epdWTZUe4nACucSomCKBU+XKdWKqQcqWaD+xtiGM3jEGJbcXU8PRPLHeF3nfEM2bLIYyLVVlDJ4+34rUp5Rx0f0cIdVJZlisPn5VUO8bqZHXZdUg18c2lZ/8XDiojp1Ugk7k0A1HTSV466234uKLL8awYcMwevRovPzyy9i7dy+uvfbao1UlDx48ePDwB8ZRG7DOPfdclJWV4YEHHsDBgwfRr18/fPvtt+jcuXPE59CiFZAoY6jWOPbEmJXMqNyM+wJDEA29RNa5Ohl
"text/plain": [
"<Figure size 480x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"i, j = 100, 50 # initial position\n",
"sigma_i, sigma_j = 3, 4 # parameters of the next step map\n",
"size = 200 # size of the image\n",
"context_map = create_context_map(size, 'hills') # fixed context map\n",
"\n",
"# Sample a next step 1000 times\n",
"trajectory = []\n",
"for _ in range(1000):\n",
" i, j = sample_next_step(i, j, sigma_i, sigma_j, context_map)\n",
" trajectory.append((i, j))\n",
"\n",
"plot_trajectory(trajectory, context_map)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"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"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Write the code that uses your new `walker` interface here!\n",
"# walker = Walker(sigma_i=3, sigma_j=4, ...)\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"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
}