Last active
October 30, 2020 00:38
-
-
Save J08nY/330fa47891979a21caea818ec88f465e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-10-29T22:13:02.339222Z", | |
"start_time": "2020-10-29T22:13:02.331179Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"from ipywidgets import interact, interactive, fixed, interact_manual\n", | |
"import ipywidgets as widgets\n", | |
"from ipywidgets import Layout\n", | |
"from IPython.display import display, HTML" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-10-29T23:39:23.130961Z", | |
"start_time": "2020-10-29T23:39:23.075581Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "7e53c308dc44493391ed66da4483d560", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"interactive(children=(IntText(value=4400000, description='Populácia', layout=Layout(width='50%')), FloatSlider…" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"def method_1():\n", | |
" pop = widgets.IntText(value=4_400_000, description=\"Populácia\", layout=Layout(width=\"50%\"))\n", | |
" attendance = widgets.FloatSlider(value=0.7, min=0, step=0.02, max=1, description=\"Účasť\", layout=Layout(width=\"50%\"))\n", | |
" true_infected = widgets.IntText(value=50_000, description=\"Nakazení\", layout=Layout(width=\"50%\"))\n", | |
" sensitivity = widgets.FloatSlider(value=0.67, min=0.3, step=0.02, max=1, description=\"Senzitivita testu\", readout_format=\".3f\", layout=Layout(width=\"50%\"))\n", | |
" specificity = widgets.FloatSlider(value=0.995, min=0.9, step=0.002, max=1, description=\"Špecificita testu\", readout_format=\".3f\", layout=Layout(width=\"50%\"))\n", | |
"\n", | |
" def compute(population, attendance, infected, sensitivity, specificity):\n", | |
" tested_population = population * attendance\n", | |
" # Assumption that attendance is uniform among infected and non-infected\n", | |
" tested_infected = infected * attendance\n", | |
" tested_clean = tested_population - tested_infected\n", | |
"\n", | |
" # Calculate the true/false and negative/positive from the tested sample, with given sensitivity and specificity\n", | |
" true_clean = tested_clean * specificity\n", | |
" false_infected = tested_clean * (1 - specificity)\n", | |
" true_infected = tested_infected * sensitivity\n", | |
" false_clean = tested_infected * (1 - sensitivity)\n", | |
"\n", | |
" # Calculate the missed infected\n", | |
" missed_infected = infected * (1 - attendance)\n", | |
" display(HTML(f\"<strong>Testovaní:</strong>{round(tested_population)}\" + \n", | |
" f\"\"\"<table>\n", | |
" <thead>\n", | |
" <tr><th>True positive</th><th>False positive</th><th>True negative</th><th>False negative</th><th>Missed positive</th></tr>\n", | |
" </thead>\n", | |
" <tr>\n", | |
" <td>{round(true_infected)}</td><td>{round(false_infected)}</td><td>{round(true_clean)}</td><td>{round(false_clean)}</td><td>{round(missed_infected)}</td>\n", | |
" </tr></table>\"\"\"))\n", | |
" return interactive(compute, population=pop, attendance=attendance, infected=true_infected, sensitivity=sensitivity, specificity=specificity)\n", | |
"method_1()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-10-29T23:37:08.533683Z", | |
"start_time": "2020-10-29T23:37:08.475317Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "40891b8f27bd4f0f972b9bd6b7e2050c", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"interactive(children=(IntText(value=4400000, description='Populácia', layout=Layout(width='50%')), IntText(val…" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"def method_2():\n", | |
" pop = widgets.IntText(value=4_400_000, description=\"Populácia\", layout=Layout(width=\"50%\"))\n", | |
" tested = widgets.IntText(value=3_080_000, description=\"Testovaní\", layout=Layout(width=\"50%\"))\n", | |
" sensitivity = widgets.FloatSlider(value=0.67, min=0.3, step=0.02, max=1, description=\"Senzitivita testu\", readout_format=\".3f\", layout=Layout(width=\"50%\"))\n", | |
" specificity = widgets.FloatSlider(value=0.995, min=0.9, step=0.002, max=1, description=\"Špecificita testu\", readout_format=\".3f\", layout=Layout(width=\"50%\"))\n", | |
" tested_positive = widgets.IntText(value=38_675, description=\"Pozitívne otestovaní\", layout=Layout(width=\"50%\"))\n", | |
"\n", | |
" def compute(population, tested, sensitivity, specificity, tested_positive):\n", | |
" attendance = tested / population\n", | |
" tested_negative = tested - tested_positive\n", | |
" \n", | |
" # Calculate the \n", | |
" tested_infected = (specificity * tested_positive - (1 - specificity) * tested_negative) / (specificity + sensitivity - 1)\n", | |
" tested_clean = tested - tested_infected\n", | |
" \n", | |
" # Assumption that attendance is uniform among infected and non-infected\n", | |
" total_infected = (tested_infected / tested) * population\n", | |
" total_clean = (tested_clean / tested) * population\n", | |
" \n", | |
" missed_infected = total_infected - tested_infected\n", | |
" \n", | |
" # Calculate the true/false and negative/positive from the tested sample, with given sensitivity and specificity\n", | |
" true_clean = tested_clean * specificity\n", | |
" false_infected = tested_clean * (1 - specificity)\n", | |
" true_infected = tested_infected * sensitivity\n", | |
" false_clean = tested_infected * (1 - sensitivity)\n", | |
" \n", | |
" display(HTML(f\"<strong>Účasť:</strong>{attendance:.2}\" +\n", | |
" f\"\"\"<table>\n", | |
" <thead>\n", | |
" <tr><th>Tested infected</th><th>Tested clean</th><th>Total infected</th><th>Total clean</th></tr>\n", | |
" </thead>\n", | |
" <tr>\n", | |
" <td>{round(tested_infected)}</td><td>{round(tested_clean)}</td><td>{round(total_infected)}</td><td>{round(total_clean)}</td>\n", | |
" </tr></table>\"\"\" +\n", | |
" f\"\"\"<table>\n", | |
" <thead>\n", | |
" <tr><th>True positive</th><th>False positive</th><th>True negative</th><th>False negative</th><th>Missed positive</th></tr>\n", | |
" </thead>\n", | |
" <tr>\n", | |
" <td>{round(true_infected)}</td><td>{round(false_infected)}</td><td>{round(true_clean)}</td><td>{round(false_clean)}</td><td>{round(missed_infected)}</td>\n", | |
" </tr></table>\"\"\"))\n", | |
" return interactive(compute, population=pop, tested=tested, sensitivity=sensitivity, specificity=specificity, tested_positive=tested_positive)\n", | |
"method_2()" | |
] | |
} | |
], | |
"metadata": { | |
"@webio": { | |
"lastCommId": "f6738dbf931441268296d3c158779ede", | |
"lastKernelId": "741c40b2-5c71-4d40-ae83-5a50d5e531ca" | |
}, | |
"hide_input": false, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.8.6" | |
}, | |
"latex_envs": { | |
"LaTeX_envs_menu_present": true, | |
"autoclose": false, | |
"autocomplete": true, | |
"bibliofile": "biblio.bib", | |
"cite_by": "apalike", | |
"current_citInitial": 1, | |
"eqLabelWithNumbers": true, | |
"eqNumInitial": 1, | |
"hotkeys": { | |
"equation": "Ctrl-E", | |
"itemize": "Ctrl-I" | |
}, | |
"labels_anchors": false, | |
"latex_user_defs": false, | |
"report_style_numbering": false, | |
"user_envs_cfg": false | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"varInspector": { | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"delete_cmd_postfix": "", | |
"delete_cmd_prefix": "del ", | |
"library": "var_list.py", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"delete_cmd_postfix": ") ", | |
"delete_cmd_prefix": "rm(", | |
"library": "var_list.r", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
], | |
"window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment