General updates
[covid19.git] / international_comparison.py
1 #!/usr/bin/env python
2 # coding: utf-8
3 # %%
4 # Data from [European Centre for Disease Prevention and Control](https://www.ecdc.europa.eu/en/publications-data/download-todays-data-geographic-distribution-covid-19-cases-worldwide)
5
6 # %%
7 import itertools
8 import collections
9 import json
10 import pandas as pd
11 import numpy as np
12 from scipy.stats import gmean
13 import datetime
14
15 from sqlalchemy.types import Integer, Text, String, DateTime, Date, Float
16 from sqlalchemy import create_engine
17
18 import matplotlib as mpl
19 import matplotlib.pyplot as plt
20 plt.ioff()
21 # %matplotlib inline
22
23
24 # %%
25 connection_string = 'postgresql://covid:3NbjJTkT63@localhost/covid'
26
27
28 # %%
29 engine = create_engine(connection_string)
30
31
32 # %%
33 DEATH_COUNT_THRESHOLD = 10
34 COUNTRIES_CORE = tuple(sorted('ITA DEU GBR ESP IRL FRA BEL'.split()))
35 # COUNTRIES_NORDIC = tuple('SE NO DK FI UK'.split())
36 COUNTRIES_FRIENDS = tuple('ITA GBR ESP BEL SVN MEX'.split())
37 # COUNTRIES_FRIENDS = 'IT UK ES BE SI PT'.split()
38
39 # COUNTRIES_AMERICAS = ('AG', 'AR', 'AW', 'BS', 'BB', 'BZ', 'BM', 'BO', 'BR', 'VG', 'KY', # excluding Canada and USA
40 # 'CL', 'CO', 'CR', 'CU', 'CW', 'DM', 'DO', 'EC', 'SV', 'GL', 'GD', 'GT',
41 # 'GY', 'HT', 'HN', 'JM', 'MX', 'MS', 'NI', 'PA', 'PY', 'PE', 'PR', 'KN',
42 # 'LC', 'VC', 'SX', 'SR', 'TT', 'TC', 'VI', 'UY', 'VE')
43 # COUNTRIES_OF_INTEREST = tuple(set(COUNTRIES_CORE + COUNTRIES_FRIENDS))
44 # COUNTRIES_ALL = tuple(set(COUNTRIES_CORE + COUNTRIES_FRIENDS + COUNTRIES_NORDIC + COUNTRIES_AMERICAS))
45
46
47 # %%
48 query_string = f'''select date, country_code, deaths_weekly, culm_deaths
49 from weekly_cases
50 where country_code in {COUNTRIES_CORE}
51 order by date, country_code'''
52
53 country_data = pd.read_sql_query(query_string,
54 engine,
55 index_col = 'date',
56 parse_dates = ['date']
57 )
58
59
60 # %%
61 deaths_culm = country_data.pivot(columns='country_code', values='culm_deaths')
62
63
64 # %%
65 # country_data
66
67 # %%
68 # deaths_culm
69
70 # %%
71 ax = deaths_culm.loc['2020-03-15':].plot(figsize=(10, 6), title="Total deaths, linear")
72 ax.set_xlabel(f"Date")
73 for c in COUNTRIES_CORE:
74 lvi = deaths_culm[c].last_valid_index()
75 ax.text(x = lvi + pd.Timedelta(days=1), y = deaths_culm[c][lvi], s = f"{c}: {deaths_culm[c][lvi]:.0f}")
76 plt.savefig('covid_deaths_total_linear.png')
77
78
79 # %%
80 deaths_weekly = country_data.pivot(columns='country_code', values='deaths_weekly')
81
82
83 # %%
84 ax = deaths_weekly.loc['2020-03-01':, COUNTRIES_CORE].plot(figsize=(10, 6), title="Deaths per week")
85 ax.set_xlabel('Date')
86 for c in COUNTRIES_CORE:
87 lvi = deaths_weekly[c].last_valid_index()
88 ax.text(x = lvi + pd.Timedelta(days=1), y = deaths_weekly[c][lvi], s = c)
89 plt.savefig('covid_deaths_per_week.png')
90
91
92 # %%
93 ax = deaths_weekly.iloc[-6:].plot(figsize=(10, 6), title="Deaths per week, last 6 weeks")#, ylim=(-10, 100))
94 ax.set_xlabel("Date")
95
96 text_x_pos = deaths_weekly.last_valid_index() + pd.Timedelta(days=0.5)
97
98 for c in COUNTRIES_CORE:
99 lvi = deaths_weekly[c].last_valid_index()
100 # if c != 'ES':
101 ax.text(x = text_x_pos, y = deaths_weekly[c][lvi], s = f"{c}: {deaths_weekly[c][lvi]:.0f}")
102 plt.savefig('deaths_by_date_last_6_weeks.png')
103
104
105 # %%
106
107
108
109