% Covid death data summary
% Neil Smith
-% Created on 2021-03-11
+% Created on 2021-04-10
-> Last UK data from 11 Mar 2021. Last international data from 07 Mar 2021.
+> Last UK data from 10 Apr 2021. Last international data from 04 Apr 2021.
## Headlines (UK data)
| []() | |
|:---|---:|
-| Deaths reported so far | 125168 |
-| Deaths in last 30 days | 11147 |
-| Cases in last 30 days | 237470 |
+| Deaths reported so far | 127080 |
+| Deaths in last 30 days | 1914 |
+| Cases in last 30 days | 120981 |
## International comparison
-Based on weekly data. Last data from 07 Mar 2021
+Based on weekly data. Last data from 04 Apr 2021
### Total deaths
| Country ID | Country name | Total deaths |
|:-----------|:-------------|-------------:|
-| BEL | Belgium | 22287 |
-| DEU | Germany | 71934 |
-| ESP | Spain | 71436 |
-| FRA | France | 88600 |
-| GBR | United Kingdom | 124501 |
-| IRL | Ireland | 4422 |
-| ITA | Italy | 99785 |
+| BEL | Belgium | 23198 |
+| DEU | Germany | 77013 |
+| ESP | Spain | 75783 |
+| FRA | France | 96678 |
+| GBR | United Kingdom | 126836 |
+| IRL | Ireland | 4718 |
+| ITA | Italy | 111030 |
### Deaths per week
### Total deaths
-Deaths reported up to 11 Mar 2021: 125168
+Deaths reported up to 10 Apr 2021: 127080
![Total deaths](cases_and_deaths.png)
text_representation:
extension: .md
format_name: markdown
- format_version: '1.2'
- jupytext_version: 1.9.1
+ format_version: '1.3'
+ jupytext_version: 1.11.1
kernelspec:
display_name: Python 3
language: python
```python Collapsed="false"
conn = create_engine(connection_string)
+engine = create_engine(connection_string)
```
```python Collapsed="false"
england_wales_filename = 'uk-deaths-data/publishedweek532020.xlsx'
```
-```sql Collapsed="false"
+```python
+query_string = '''
drop table if exists all_causes_deaths;
create table all_causes_deaths (
week integer,
deaths integer,
CONSTRAINT week_nation PRIMARY KEY(year, week, nation)
);
+'''
+with engine.connect() as connection:
+ connection.execute(query_string)
```
```python Collapsed="false"
%sql select * from all_causes_deaths limit 5
```
+```python
+query_string = '''
+delete from all_causes_deaths where nation = 'Scotland';
+'''
+with engine.connect() as connection:
+ connection.execute(query_string)
+```
+
```python Collapsed="false"
for year, ser in deaths_headlines_s.items():
year_i = int(year[-4:])
for week, deaths in ser.dropna().iteritems():
# print(datetime.date.fromisocalendar(year_i, week, 7), deaths)
dut = datetime.date.fromisocalendar(year_i, week, 7)
- %sql insert into all_causes_deaths(week, year, date_up_to, nation, deaths) values ({week}, {year_i}, :dut, 'Scotland', {deaths})
+ query_string = f'''insert into
+ all_causes_deaths(week, year, date_up_to, nation, deaths)
+ values ({week}, {year_i}, '{dut}', 'Scotland', {deaths});'''
+ with engine.connect() as connection:
+ connection.execute(query_string)
```
```python
```
```python
-%sql delete from all_causes_deaths where nation = 'England'
+query_string = '''
+delete from all_causes_deaths where nation = 'England';
+'''
+with engine.connect() as connection:
+ connection.execute(query_string)
```
```python
-#!/usr/bin/env python
-# coding: utf-8
-# %%
+# ---
+# jupyter:
+# jupytext:
+# formats: ipynb,py:percent
+# text_representation:
+# extension: .py
+# format_name: percent
+# format_version: '1.3'
+# jupytext_version: 1.10.2
+# kernelspec:
+# display_name: Python 3
+# language: python
+# name: python3
+# ---
+
+# %% Collapsed="false"
import itertools
import collections
import json
import numpy as np
from scipy.stats import gmean
import datetime
-import os
+
import sqlalchemy
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as ani
-plt.ioff()
# # %matplotlib inline
+# # %load_ext sql
+plt.ioff()
-
-# %%
+# %% Collapsed="false"
chart_start_date = '2020-09-15'
-
-# %%
+# %% Collapsed="false"
connection_string = 'postgresql://covid:3NbjJTkT63@localhost/covid'
+# %% Collapsed="false"
+# %sql $connection_string
# %%
engine = sqlalchemy.create_engine(connection_string)
# %%
-query_string = '''select uk_data.date,
+qstr = '''select uk_data.date,
uk_data.new_cases, uk_data_7.new_cases as new_cases_7,
uk_data.new_tests, uk_data_7.new_tests as new_tests_7,
uk_data.new_pcr_tests, uk_data_7.new_pcr_tests as new_pcr_7,
uk_data_7.new_cases / uk_data_7.new_tests as fraction_positive_7
from uk_data left outer join uk_data_7 using (date)
order by uk_data.date'''
-tests_data = pd.read_sql_query(query_string, engine,
+tests_data = pd.read_sql_query(qstr, engine,
index_col='date',
parse_dates = ['date'])
# %%
+tests_data.tail(10)
+
+# %% Collapsed="false"
+# tests_data[['new_tests', 'new_cases']].plot()
+
+# %% Collapsed="false"
+# tests_data[['fraction_positive', 'fraction_positive_7']].dropna().plot()
+
+# %% Collapsed="false"
+# ax = data_by_day.dropna().loc['2020-06-15': , ['fraction_positive', 'fraction_positive_m7']].plot(figsize=(10, 8), title='Fraction of tests with positive results')
+# ax.legend(['Fraction positive per day', 'Fraction positive, 7 day moving average'])
+# ax.set_ylabel('Fraction positive')
+# plt.savefig('fraction_positive_tests.png')
+
+# %% Collapsed="false"
+# pri_y_max = int((tests_data.dropna().loc['2020-06-15': , 'new_tests_7'].max() * 1.1) / 100 ) * 100
+# ax = tests_data.dropna().loc['2020-06-15': , 'new_tests_7'].plot(figsize=(10, 8),
+# style=['k-'],
+# legend=False,
+# ylim=(0, pri_y_max))
+# ax.set_title('Tests done and new cases (7 day moving average)')
+# ax.legend(['Tests, 7 day moving average'], loc='lower left')
+# ax.set_ylabel('Tests')
+# sec_y_max = int((tests_data.dropna().loc['2020-06-15':, 'new_cases_7'].max() * 1.1) / 100) * 100
+# ax = tests_data.dropna().loc['2020-06-15':, 'new_cases_7'].plot(ax=ax, secondary_y=True, style='r--')
+# ax.set_ylim((0, sec_y_max))
+# ax.legend(['Cases (7 day moving average)'], loc='lower right')
+# ax.set_ylabel('New cases')
+# # plt.savefig('tests_and_cases.png')
+
+# %% Collapsed="false"
pri_y_max = int((tests_data.dropna().loc[chart_start_date: , 'new_tests_7'].max() * 1.1) / 100 ) * 100
ax = tests_data.dropna().loc[chart_start_date: , 'new_tests_7'].plot(figsize=(10, 8),
style=['k-'],
ax.set_ylabel('New cases')
plt.savefig('tests_and_cases.png')
+# %% Collapsed="false"
+pri_y_max = int((tests_data.dropna().loc[chart_start_date: , 'fraction_positive_7'].max() * 1.1) * 100)
-# %%
-ax = (tests_data.loc[chart_start_date: , ['fraction_positive', 'fraction_positive_7']] * 100).plot(figsize=(10, 8),
- style=['b:', 'k-'], legend=False)
+ax = (tests_data.loc[chart_start_date: ,
+ ['fraction_positive', 'fraction_positive_7']] * 100).plot(
+ figsize=(10, 8), style=['b:', 'k-'], legend=False, ylim=(0, pri_y_max))
ax.set_title('Fraction of tests with positive results')
ax.legend(['Fraction positive (%)', 'Fraction positive (%), 7 day moving average'], loc='upper left')
ax.set_ylabel('Fraction positive')
-cases_axis_max = (1.0 * tests_data.loc[chart_start_date:].new_cases_7.max()
- * tests_data.loc[chart_start_date:].fraction_positive.max()
- / tests_data.loc[chart_start_date:].fraction_positive_7.max()
+cases_axis_max = (1.1 * tests_data.loc[chart_start_date:].new_cases_7.max()
+# * tests_data.loc[chart_start_date:].fraction_positive.max()
+# / tests_data.loc[chart_start_date:].fraction_positive_7.max()
)
ax2 = ax.twinx()
ax2.set_ylabel('New cases')
plt.savefig('fraction_positive_tests.png')
-
-# %%
+# %% Collapsed="false"
ax = tests_data.dropna().loc[chart_start_date:].plot(x='fraction_positive_7', y='new_tests_7',
figsize=(8, 8),
legend=None)
s = d.strftime("%d %B %Y"))
plt.savefig('fraction_positive_tests_vs_tests.png')
-
-# %%
+# %% Collapsed="false"
fig = plt.figure(figsize=(8, 8))
plt.ylabel('Number of tests')
plt.xlabel('Fraction of tests that are positive')
# plt.show()
-# %%
-os.system('rm tests_vs_fraction_positive_animation.png')
-os.system('ffmpeg -i tests_vs_fraction_positive.mp4 -plays 0 -final_delay 1 -f apng tests_vs_fraction_positive_animation.png')
-
-
-# %%
-
-
-
+# %% Collapsed="false"
+# !rm tests_vs_fraction_positive_animation.png
+# !ffmpeg -i tests_vs_fraction_positive.mp4 -plays 0 -final_delay 1 -f apng tests_vs_fraction_positive_animation.png
+
+# %% Collapsed="false"
+# fig = plt.figure(figsize=(8, 8))
+# plt.ylabel('Number of tests')
+# plt.xlabel('Fraction of tests that are positive')
+
+# all_data = data_by_day.dropna().loc[chart_start_date:]
+
+# minx = all_data.fraction_positive_m7.min() * 0.9
+# maxx = all_data.fraction_positive_m7.max() * 1.1
+# miny = all_data.tests_m7.min() * 0.9
+# maxy = all_data.tests_m7.max() * 1.1
+
+# plt.xlim(minx, maxx)
+# plt.ylim(miny, maxy)
+# # plt.legend(None)
+
+# def build_state_frame(i):
+# this_data = all_data[:i]
+# p = plt.plot(this_data.fraction_positive_m7, this_data.tests_m7)
+# p[0].set_color('r')
+# for d in this_data[::15].index:
+# plt.plot(this_data.loc[d, 'fraction_positive_m7'],
+# this_data.loc[d, 'tests_m7'], 'o',
+# markersize=8, markerfacecolor='r', markeredgecolor='r')
+# plt.text(this_data.loc[d, 'fraction_positive_m7'] + 0.0002,
+# this_data.loc[d, 'tests_m7'],
+# s = d.strftime("%d %B %Y"))
+
+# # animator = ani.FuncAnimation(fig, build_state_frame, interval=200)
+# # animator.save('myfirstAnimation.mp4')
+# build_state_frame(103)
+# plt.show()
+# %% Collapsed="false"