release exercise 4

This commit is contained in:
Andrew Straw 2024-11-04 13:51:31 +01:00
parent cfa0bf64b2
commit 86e3c32230
2 changed files with 744 additions and 0 deletions

View file

@ -37,6 +37,7 @@ http://pythontutor.com/
| exercise-01 | 2024-10-16 |
| exercise-02 | 2024-10-23 |
| exercise-03 | 2024-10-30 |
| exercise-04 | 2024-11-06 |
## Submitting assignments

View file

@ -0,0 +1,743 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reminder: Do not rename this file (or allow your computer to do that)\n",
"\n",
"Please overwrite the original exercise release file(s) when uploading your assignment. I still had to fix some of your submissions last week."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "2153e7cb8aea59d434ff324e7cad8a0e",
"grade": false,
"grade_id": "cell-f2aebf59df557e79",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# You must run this cell, but you can ignore its contents.\n",
"\n",
"import hashlib\n",
"\n",
"def ads_hash(ty):\n",
" \"\"\"Return a unique string for input\"\"\"\n",
" ty_str = str(ty).encode()\n",
" m = hashlib.sha256()\n",
" m.update(ty_str)\n",
" return m.hexdigest()[:10]"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "a3a1891dc42363fbaa454d2c2a182fef",
"grade": false,
"grade_id": "cell-5b7440fcb507ff19",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# Flow control - if statements\n",
"\n",
"## Q1\n",
"\n",
"Write a function called `is_over10` which accepts a single input argument and which returns the boolean `True` if the input is greater than 10. Otherwise the function should return `False`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "677e38959b3db89f34c18224983845df",
"grade": false,
"grade_id": "cell-dd1841337da413fe",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "18cd5f7690d9425f4850b97d50476f47",
"grade": true,
"grade_id": "cell-29a08b5273e7cfd5",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# If this runs without error, it means the answer in your previous cell was correct.\n",
"assert ads_hash(is_over10(9)) == '60a33e6cf5'\n",
"assert ads_hash(is_over10(9.9999)) == '60a33e6cf5'\n",
"assert ads_hash(is_over10(11)) == '3cbc87c768'\n",
"assert ads_hash(is_over10(10.000001)) == '3cbc87c768'"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "149b843d4575239a6616814a2273206d",
"grade": false,
"grade_id": "cell-24766e51350b14a2",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Q2\n",
"\n",
"Write a function called `is_over_a` which accepts two input arguments `x` (the first argument) and `a` (the second argument). The function should return the boolean `True` if `x` is greater to or equal than `a`. Otherwise the function should return `False`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "5af2f42819fe145f9d9c23af405144d6",
"grade": false,
"grade_id": "cell-fbff7f837b8a992a",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "7a113655ca3e664bce604937bf8d980d",
"grade": true,
"grade_id": "cell-eacb9f0672c5988e",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# If this runs without error, it means the answer in your previous cell was correct.\n",
"assert ads_hash(is_over_a(9,10)) == '60a33e6cf5'\n",
"assert ads_hash(is_over_a(9.9999,10)) == '60a33e6cf5'\n",
"assert ads_hash(is_over_a(10,10)) == '3cbc87c768'\n",
"assert ads_hash(is_over_a(11,10)) == '3cbc87c768'\n",
"assert ads_hash(is_over_a(10.000001,10)) == '3cbc87c768'\n",
"assert ads_hash(is_over_a(-9,-10)) == '3cbc87c768'\n",
"assert ads_hash(is_over_a(-9,10)) == '60a33e6cf5'"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "4141c9b8856f9fbffe785ce4714ef98f",
"grade": false,
"grade_id": "cell-53136179efb5686e",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# Flow control - `while` loops using $R_t$, the *effective reproduction number*\n",
"\n",
"To make our examples more interesting, we are going to use some concepts from epidemiology. We will perform some simple epidemiological modeling of disease spread. First, let's define $R_t$, the [*effective reproduction number*](https://en.wikipedia.org/wiki/Basic_reproduction_number#Effective_reproduction_number). This is the the average number of new infections caused by a single infected individual at time $t$ in a partially susceptible population.\n",
"\n",
"Let's do some simulations to understand the effect different $R_t$ values would have.\n",
"\n",
"Consider this python code:\n",
"\n",
"```python\n",
"Rt = 1.5\n",
"\n",
"infected = 1\n",
"num_generations = 0\n",
"while infected < 1000:\n",
" infected = infected*Rt\n",
" num_generations += 1\n",
" \n",
"print(f\"It took {num_generations} generations to reach 1000 infected\")\n",
"```\n",
"\n",
"If we wanted to write this using mathematical notation, we would write this as\n",
"\n",
"$N_{t+1} = R_t N_t$\n",
"\n",
"where $N_t$ is the number of infected individuals at generation $t$ and $N_{t+1}$ is the number of infected individuals at the next generation ($t+1$).\n",
"\n",
"**What is a \"generation\"?** What we call \"generation\" here is called the serial interval (SI) in the scientific literature. According to [this paper](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6092233/), \"the serial interval (SI), defined as the time between disease symptom onset of a case and that of its infector, is a surrogate for the generation interval— an unobservable quantity defined as the time between the infection of a case and the time of infection of its infector.\" \n",
"\n",
"With regard to SARS-CoV-2 in Germany, according to [the Corona-Warn-App FAQ (Section \"App Features\" / \"Other features\" / \"In-App statistics\")](https://web.archive.org/web/20221105141411/https://www.coronawarn.app/en/faq/results/?search=&topic=application#other_features), \"The concept of the R-value and the data basis of its calculation is described in detail in the Epidemiological Bulletin of the RKI ([#17/2020 of 04/23/2020](https://www.rki.de/DE/Content/Infekt/EpidBull/Archiv/2020/17/Art_02.html)). Sample calculations and the daily updated R-values (4-day and 7-day R-value) are available as an Excel table at http://www.rki.de/covid-19-nowcasting.\" The data from 2 March 2020 to 17 June 2023 are available at [github.com/robert-koch-institut/SARS-CoV-2-Nowcasting_und_-R-Schaetzung](https://github.com/robert-koch-institut/SARS-CoV-2-Nowcasting_und_-R-Schaetzung/blob/main/Nowcast_R_aktuell.csv).\n",
"\n",
"Here is a [guide to R](https://www.nature.com/articles/d41586-020-02009-w) from July 2020.\n",
"\n",
"## Q1\n",
"\n",
"Modify the code above so that you create a variable called `generations_to_1000` which has the number of generations required for 1 person to reach 1000 people infected."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "dbb261a834f94eda89faee70003b9d7a",
"grade": false,
"grade_id": "cell-baf6db377ffe7ab0",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "d3eca878aa14a02695808799f822eb9e",
"grade": true,
"grade_id": "cell-38f63e894825dd02",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# If this runs without error, it means the answer in your previous cell was correct.\n",
"assert ads_hash(generations_to_1000)=='4ec9599fc2'"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "749bafb7624f499c8a542312ddc3f99e",
"grade": false,
"grade_id": "cell-f87155f652e119f8",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Q2\n",
"\n",
"Now create a function called `find_cycles_to_reach_1000` which takes 1 argument, `Rt`. This function should return the number of cycles required to reach 1000 infected people.\n",
"\n",
"Your function signature should look like this:\n",
"\n",
"```python\n",
" def find_generations_to_reach_1000(Rt):\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "4d26295cc81304b4a38bde6b847a1e1f",
"grade": false,
"grade_id": "cell-c3cb6fb8fbd84f76",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "46248dd59ecd41d5373809d076313624",
"grade": true,
"grade_id": "cell-abfc63314cbd6f15",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# If this runs without error, it means the answer in your previous cell was correct.\n",
"\n",
"assert ads_hash(find_generations_to_reach_1000(1.1))=='96061e92f5'\n",
"assert ads_hash(find_generations_to_reach_1000(1.5))=='4ec9599fc2'\n",
"assert ads_hash(find_generations_to_reach_1000(2.0))=='4a44dc1536'"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "fef28b9fd22ef257eeca354f3337dbcf",
"grade": false,
"grade_id": "cell-5c2ff80d01aae555",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Q3\n",
"\n",
"Now create a function called `simulate_generation` which takes two arguments, `a` and `Rt`. This function should return `None`. The first argument `a` will be a list with the number of infected individuals and `Rt` is the effective reproduction number, as before. The function should compute the number of newly infected individuals after one generation and append this to the list passed as the first argument.\n",
"\n",
"Your function signature should look like this:\n",
"\n",
"```python\n",
" def simulate_generation(a,Rt):\n",
"```\n",
"\n",
"Here is an example that works:\n",
"\n",
"```python\n",
"b = [1]\n",
"simulate_generation(b,3)\n",
"simulate_generation(b,3)\n",
"simulate_generation(b,3)\n",
"b\n",
"```\n",
"\n",
"In this above example, `b` would be equal to `[1, 3, 9, 27]` at the end.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "9ec90a30e9737fbe59a3f60a5b646c2b",
"grade": false,
"grade_id": "cell-c8679e225738c336",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "23ff5647166afa932652f7b2fd8b812f",
"grade": true,
"grade_id": "cell-a97791d9f3b1166a",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# If this runs without error, it means the answer in your previous cell was correct.\n",
"\n",
"aa = [1]\n",
"assert ads_hash(simulate_generation(aa, 22)) == 'dc937b5989'\n",
"assert ads_hash(aa)=='c7e4ccf872'"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "0d31d64a6461fe61e7c427f459886faf",
"grade": false,
"grade_id": "cell-b81fa7ce231855ae",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Q4\n",
"\n",
"Now create a function called `calculate_timeseries_to_1000` which takes one argument, `Rt`. This function should return a list containing the number of infected individuals after each cycle up to and including the first value over 1000 individuals.\n",
"\n",
"Your function signature should look like this:\n",
"\n",
"```python\n",
" def calculate_timeseries_to_1000(Rt):\n",
"```\n",
"\n",
"Your code should work so that `calculate_timeseries_to_1000(2)` would return `[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]` and that `calculate_timeseries_to_1000(3)` would return `[1, 3, 9, 27, 81, 243, 729, 2187]`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "18f7b7866b8dfbb583c33ab009dd5e9c",
"grade": false,
"grade_id": "cell-1c6492f08ae6ad56",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "2d3636fcd12b7357b9fd548cde67583a",
"grade": true,
"grade_id": "cell-2c8986f98612af75",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"assert ads_hash(calculate_timeseries_to_1000(2))=='a9ad958975'\n",
"assert ads_hash(calculate_timeseries_to_1000(3))=='7aa0d3bbca'"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "c35b5e5fd79dffbaf4d3fff3bac82908",
"grade": false,
"grade_id": "cell-f196cf19e3293382",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Bonus - plotting the results\n",
"\n",
"Let's now see the result of these simulations."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "701666e35e93cd6bf19d51e5ee549119",
"grade": false,
"grade_id": "cell-de643bc87190397a",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# Below, we will use matplotlib, so we need to import it here.\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "364c81018ad623bcbfdc989c71784e38",
"grade": false,
"grade_id": "cell-24654bdb7a3d2e91",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"plt.plot(calculate_timeseries_to_1000(1.1), label=\"$R_t$ = 1.1\")\n",
"plt.plot(calculate_timeseries_to_1000(1.5), label=\"$R_t$ = 1.5\")\n",
"plt.plot(calculate_timeseries_to_1000(2.0), label=\"$R_t$ = 2.0\")\n",
"plt.xlabel(\"cycles\")\n",
"plt.ylabel(\"number of infected individuals\")\n",
"plt.legend();"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "7477b3c44e470ce5f48bef73dd8b2a1b",
"grade": false,
"grade_id": "cell-a060dcd11e607e7a",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# String formatting\n",
"\n",
"Remember from lecture various ways of formatting strings with template in Python. We are going to use f-strings here. \n",
"\n",
"Here is an example:\n",
"\n",
"```python\n",
"city=\"Freiburg\"\n",
"continent=\"Europe\"\n",
"result = f\"The city {city} is in the continent {continent}\"\n",
"```\n",
"\n",
"This will set `result` to `'The city Freiburg is in the continent Europe'`.\n",
"\n",
"## Q1\n",
"\n",
"Create a function named `greeting` which takes two arguments, `name` and `age`. It should return a string with these values inserted in to a string so that when called like\n",
"\n",
"```python\n",
"result = greeting(\"Andrew\",7)\n",
"```\n",
"\n",
"`result` will be `'Hello. My name is Andrew. My age is 7'`.\n",
"\n",
"The function signature will be\n",
"\n",
"```python\n",
"def greeting(name, age):\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "51aa10ba1c5fc27151813272740b890d",
"grade": false,
"grade_id": "cell-3849b2b9c48cf490",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "2a1dd166313b4b62c16ee4821cee078f",
"grade": true,
"grade_id": "cell-5e904f2c9578e11c",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# If this runs without error, it means the answer in your previous cell was correct.\n",
"\n",
"assert ads_hash(greeting(\"Andrew\",7))=='1370611a0d'\n",
"assert ads_hash(greeting(\"Bob\",47))=='3b8fd03444'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Running Python from the terminal\n",
"\n",
"# Q1\n",
"\n",
"Create a Python script called `my_name.py` which does two things:\n",
"\n",
"1) prints your name\n",
"2) computes the value of 1001 * 22 and then prints this\n",
"\n",
"# Final upload of your assignment today\n",
"\n",
"When you upload your assignnment, you should upload:\n",
"\n",
"1. This `1__Flow_control_Plotting_Strings.ipynb` notebook file as you completed it. Remember not to change its name.\n",
"2. The Python script `my_name.py` which does the correct actions. Both these files should be placed in the `release/exercise-04` directory."
]
}
],
"metadata": {
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}