Commit e221712e authored by Plamenna Venkova-Marchevska's avatar Plamenna Venkova-Marchevska
Browse files

Auto-saving for plamennavenkova on branch master from commit 09d70f57

parent 09d70f57
Pipeline #30177 passed with stage
in 34 seconds
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
```
%% Cell type:code id: tags:
``` python
from pathlib import Path
import altair as alt
import pandas as pd
from IPython.display import display, HTML
from covid_19_utils import helper, plotting
from covid_19_utils.converters import CaseConverter
```
%% Cell type:code id: tags:parameters
``` python
save_figures = False
data_path = '../data/openzh-covid-19'
atlas_path = '../data/atlas'
```
%% Cell type:code id: tags:
``` python
html_credits=HTML('''
<p style="font-size: smaller">Data Sources:
<a href="https://github.com/openZH/covid_19">OpenData Zuerich</a>,
<a href="https://www.bfs.admin.ch">Federal Statistical Office</a>
<br>
Analysis:
<a href="https://renkulab.io/projects/covid-19/covid-19-public-data">Covid-19 Public Data Collaboration Project</a>
</p>''')
```
%%%% Output: error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-4f8381516fe8> in <module>
----> 1 html_credits=HTML('''
2 <p style="font-size: smaller">Data Sources:
3 <a href="https://github.com/openZH/covid_19">OpenData Zuerich</a>,
4 <a href="https://www.bfs.admin.ch">Federal Statistical Office</a>
5 <br>
NameError: name 'HTML' is not defined
%% Cell type:markdown id: tags:
## Summary data for Covid-19 cases in Switzerland
The data for Switzerland comes from the effort initiated by [OpenData Zürich](https://github.com/openZH/covid_19) and collected during the [Case data #covid19mon hackathon challenge](https://db.schoolofdata.ch/project/73).
Below we make plots of total cases, total cases per 10k population and total deaths. You can click on the canton abbreviations in the legend to highlight individual lines.
%% Cell type:code id: tags:
``` python
# read in cantonal data and produce one dataframe
converter = CaseConverter(atlas_path)
df = converter.read_convert(data_path)
df["canton"] = df.apply(lambda row: row["region_iso"][3:], axis=1)
```
%%%% Output: error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-88cb7f8fc4d7> in <module>
1 # read in cantonal data and produce one dataframe
----> 2 converter = CaseConverter(atlas_path)
3 df = converter.read_convert(data_path)
4 df["canton"] = df.apply(lambda row: row["region_iso"][3:], axis=1)
NameError: name 'CaseConverter' is not defined
%% Cell type:markdown id: tags:
### Total cases
%% Cell type:code id: tags:
``` python
base = alt.Chart(df)
base.configure_header(titleFontSize=25)
base.configure_axis(labelFontSize=15, titleFontSize=15)
# cumul = generate_canton_chart(base, 'positive', 'Cases', 'Cases')
cumul = plotting.generate_region_chart(
base,
column='positive',
region_column='canton',
ytitle='Cases',
legend_title='Canton',
tooltip_title='Cases')
cumul_100k = plotting.generate_region_chart(
base,
column='positive_100k',
region_column='canton',
ytitle='Cases per 100k population',
legend_title='Canton',
tooltip_title='Cases/100k')
chart = alt.hconcat(
cumul, cumul_100k, title='Covid-19 cases in Switzerland by Canton'
).configure_title(
anchor='middle'
)
display(chart)
if save_figures:
chart.save(str(Path(figures_path) / 'switzerland-cases-by-canton.html'))
display(html_credits)
```
%% Cell type:markdown id: tags:
### Deaths
%% Cell type:code id: tags:
``` python
base = alt.Chart(df)
base.configure_header(titleFontSize=25)
base.configure_axis(labelFontSize=15, titleFontSize=15)
deaths = plotting.generate_region_chart(
base,
column='deceased',
region_column='canton',
ytitle='Deaths',
tooltip_title='Deaths',
legend_title='Canton'
)
deaths_10k = plotting.generate_region_chart(
base,
column='deceased_100k',
region_column='canton',
ytitle='Deaths per 100k population',
tooltip_title='Deaths/100k',
legend_title='Canton'
)
chart = alt.hconcat(
deaths, deaths_10k, title='Covid-19 deaths in Switzerland by Canton'
).configure_title(
anchor='middle'
)
display(chart)
display(html_credits)
if save_figures:
chart.save(str(Path(figures_path) / 'switzerland-deaths-by-canton.html'))
```
%% Cell type:code id: tags:
``` python
since_100th_case = helper.make_since_df(df, 'positive', 'canton')
base = alt.Chart(since_100th_case, title="Switzerland: total cases since 100th case").encode(alt.Y(scale=alt.Scale(type='log')))
lineChart = plotting.make_region_since_chart(
base,
'positive',
'sinceDay0',
'canton',
'Days since 100th case',
'Cumulative positive cases',
'Cases',
'Canton'
).properties(
width=450,
height=450
)
rule_chart = plotting.make_rule_chart(max_days=max(since_100th_case['sinceDay0']), pos_day=(9,5000), pos_3days=(19,8000))
lineChart + rule_chart
```
%% Cell type:code id: tags:
``` python
```
......
......@@ -128,7 +128,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.3"
}
},
"nbformat": 4,
......
%% Cell type:markdown id: tags:
# Gather Population Data from Wikidata
%% Cell type:code id: tags:
``` python
import os
import pandas as pd
from covid_19_utils import helper
```
%% Cell type:code id: tags:parameters
``` python
out_folder = '../../data/atlas/wikidata'
PAPERMILL_OUTPUT_PATH = None
```
%% Cell type:code id: tags:
``` python
def write_population_data(iso_code, df):
if PAPERMILL_OUTPUT_PATH is None:
return
out_path = os.path.join(out_folder, f"{iso_code.lower()}-population.csv")
print(f"Writing {len(df)} rows to {out_path}")
df.to_csv(out_path)
```
%% Cell type:markdown id: tags:
## Italy
%% Cell type:code id: tags:
``` python
iso_code = "ITA"
pops = helper.get_region_populations(
iso_code,
additional_fields="?istatid",
additional_query="?region wdt:P635 ?istatid .",
)
df = pd.DataFrame(pops)
write_population_data(iso_code, df)
df.head(2)
```
%%%% Output: execute_result
region_iso region_label country_label istatid population
0 IT-34 Veneto Italy 05 4926818
1 IT-25 Lombardy Italy 03 10067494
region_iso region_label country_label istatid population
0 IT-34 Veneto Italy 05 4926818
1 IT-25 Lombardy Italy 03 10067494
%% Cell type:markdown id: tags:
## Switzerland
%% Cell type:code id: tags:
``` python
iso_code = "CHE"
pops = helper.get_region_populations(iso_code)
df = pd.DataFrame(pops)
write_population_data(iso_code, df)
df.head(2)
```
%%%% Output: execute_result
region_iso region_label country_label population
0 CH-GE Canton of Geneva Switzerland 499480
1 CH-JU Canton of Jura Switzerland 73419
region_iso region_label country_label population
0 CH-GE Canton of Geneva Switzerland 499480
1 CH-JU Canton of Jura Switzerland 73419
%% Cell type:markdown id: tags:
## United States
%% Cell type:code id: tags:
``` python
iso_code = "USA"
pops = helper.get_region_populations(iso_code)
df = pd.DataFrame(pops)
write_population_data(iso_code, df)
df.head(2)
```
%%%% Output: execute_result
region_iso region_label country_label population
0 US-DE Delaware United States of America 945934
1 US-OR Oregon United States of America 4028977
region_iso region_label country_label population
0 US-DE Delaware United States of America 945934
1 US-OR Oregon United States of America 4028977
%% Cell type:code id: tags:
``` python
```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment