X-Git-Url: https://git.njae.me.uk/?p=covid19.git;a=blobdiff_plain;f=test_and_case_data.py;h=a5e8db2cc2c86af7a470c9f802733e9daf912a82;hp=7de6bf0cca54cf3131c4020f0518933f90475d1b;hb=HEAD;hpb=5afedd66506be7575034ae6deebcfaa7c2ced978 diff --git a/test_and_case_data.py b/test_and_case_data.py index 7de6bf0..a5e8db2 100644 --- a/test_and_case_data.py +++ b/test_and_case_data.py @@ -1,6 +1,19 @@ -#!/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 @@ -8,28 +21,30 @@ import pandas as pd 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 +# # %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, @@ -38,11 +53,42 @@ query_string = '''select uk_data.date, 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-'], @@ -58,16 +104,18 @@ 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: , '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() @@ -77,8 +125,7 @@ ax2.legend(['Cases (7 day moving average)'], loc='center left') 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) @@ -91,8 +138,7 @@ for d in tests_data.dropna().loc[chart_start_date::15].index: 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') @@ -134,13 +180,41 @@ animator.save('tests_vs_fraction_positive.mp4') # 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"