-#!/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"