Skip to content

Commit

Permalink
Added notebook for mortality rate BC
Browse files Browse the repository at this point in the history
  • Loading branch information
andreped committed Aug 7, 2023
1 parent 637addd commit 6872983
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 8 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
*.DS_Store
*.idea/
*.vs/
*__pycache__/
*__pycache__/
*venv/
*.html
*.log
*.png
*.pdf
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ A python package without useful utility tools was developed to aid extraction
and visualization. The package is compatible with `Python >= 3.8` and has the
following dependencies:

* pandas

* numpy

* matplotlib
* folium

* pycountry

* geopandas

* Pillow

Note that these dependencies will be installed directly when launching the Jypyter Notebooks.

## Installation

Expand All @@ -31,8 +37,14 @@ Example application notebooks are available in the [apps/](https://github.com/an

## Troubleshoot

1) Virtual environment activation

To activate the virtual environment on Windows, instead of `source venv/bin/activate` run `./venv/Scripts/activate`.

2) `ImportError: No module named selenium`

To export the generated map as a PNG image, the [Firefox](https://www.mozilla.org/en-US/firefox/new/) explorer is required.

## License

This repository has [MIT license](https://github.com/andreped/breast-cancer-stats/blob/main/LICENSE).
Expand Down
94 changes: 94 additions & 0 deletions apps/breast_cancer_mortality_rate.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"# Install dependencies\n",
"!pip install -q geopandas folium pycountry Pillow selenium"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import folium\n",
"import pandas as pd\n",
"import pycountry\n",
"import io\n",
"from PIL import Image\n",
"\n",
"cancer_data = pd.read_csv(\"../data/SA_0000001439.csv\")\n",
"political_countries_url = (\n",
" \"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson\"\n",
")\n",
"\n",
"# convert country code to full name\n",
"tmp = []\n",
"for x in cancer_data[\"COUNTRY\"]:\n",
" try:\n",
" tmp.append(pycountry.countries.get(alpha_3=x).name)\n",
" except AttributeError:\n",
" tmp.append(x)\n",
"cancer_data[\"COUNTRY\"] = tmp\n",
"\n",
"m = folium.Map(location=(30, 10), zoom_start=2, tiles=\"cartodb positron\")\n",
"folium.Choropleth(\n",
" geo_data=political_countries_url,\n",
" data=cancer_data,\n",
" columns=[\"COUNTRY\", \"Numeric\"],\n",
" key_on=\"feature.properties.name\",\n",
" fill_color=\"Reds\",\n",
" nan_fill_color=\"Grey\",\n",
" fill_opacity=0.7,\n",
" line_opacity=0.2,\n",
" legend_name=\"Breast cancer mortality rate.\"\n",
").add_to(m)\n",
"\n",
"# to export snapshot of map as PNG\n",
"img_data = m._to_png(5)\n",
"img = Image.open(io.BytesIO(img_data))\n",
"img.save('mortality_rate_breast_cancer.png')\n",
"\n",
"# to export as interactive viewer in HTML\n",
"m.save(\"mortality_rate_breast_cancer.html\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"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.16"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
9 changes: 5 additions & 4 deletions assets/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pandas
numpy
matplotlib
setuptools
geopandas
folium
pycountry
Pillow
selenium

0 comments on commit 6872983

Please sign in to comment.