Skip to content

Instantly share code, notes, and snippets.

View xenatisch's full-sized avatar

Pouria Hadjibagheri xenatisch

View GitHub Profile
@xenatisch
xenatisch / covid19-dash-APIv2.ipynb
Created January 7, 2021 19:31
UK COVID19 Dashboard - APIv2 sample
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@xenatisch
xenatisch / population_test.ipynb
Last active January 16, 2021 18:17
Reproduction of population from incidence rates
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@xenatisch
xenatisch / rolling_change.py
Created April 9, 2021 09:32
Rolling change calculations - UK Coronavirus Dashboard
#!/usr/bin python3
# Imports
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Python:
import logging
# 3rd party:
from pandas import DataFrame, to_datetime
from numpy import NaN
@xenatisch
xenatisch / covid19_headline_charts.py
Created May 20, 2021 10:27
Charts (waffle and line) for the UK COVID-19 Dashboard homepage
#!/usr/bin python3
# Imports
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Python:
from typing import Dict, Callable, Union
from operator import itemgetter
# 3rd party:
from plotly import graph_objects as go
@xenatisch
xenatisch / local_data.sql
Created May 26, 2021 18:53
Retrieves data at the most granular level for a postcode
WITH
location AS (
SELECT id, ref.area_type, area_code, area_name, postcode, priority
FROM covid19.postcode_lookup
JOIN covid19.area_reference AS ref ON ref.id = area_id
JOIN covid19.area_priorities AS ap ON ref.area_type = ap.area_type
WHERE UPPER(REPLACE(postcode, ' ', '')) = $2
),
metrics AS (
SELECT id, metric
@xenatisch
xenatisch / demographics.r
Created May 27, 2021 15:17
Convert APIv2 response for the age demographics data from the UK Coronavirus Dashboard to unstacked (wide) format
library(dplyr)
library(tidyr)
data <- read.csv("https://api.coronavirus.data.gov.uk/v2/data?areaType=ltla&metric=newCasesBySpecimenDateAgeDemographics&format=csv")
# Add the age bands you want to exclude
excluded_age_bands <- c("unassigned", "60+", "00_59")
# Add area codes you want to include - leave blank for everything.
included_area_codes <- c()
@xenatisch
xenatisch / cumulative2daily.ipynb
Last active July 26, 2021 10:53
Calculates daily time series using the cumulative `value_metric` columns in data downloaded from the UK Coronavirus Dashboard - APIv2.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@xenatisch
xenatisch / area_reference.csv
Created July 27, 2021 08:22
List of upper and lower tier local authorities in the United Kingdom - 2019 boundaries.
area_type area_code area_name
ltla E06000001 Hartlepool
utla E06000001 Hartlepool
ltla E06000002 Middlesbrough
utla E06000002 Middlesbrough
ltla E06000003 Redcar and Cleveland
utla E06000003 Redcar and Cleveland
ltla E06000004 Stockton-on-Tees
utla E06000004 Stockton-on-Tees
ltla E06000005 Darlington
@xenatisch
xenatisch / cache_repopulation.sql
Last active November 18, 2021 19:36
Query to repopulate cache for postcode pages of the UK Coronavirus Dashboard
SELECT
'area-' || release_date::TEXT || '-' || area_id::TEXT AS key,
JSONB_AGG(
JSONB_BUILD_OBJECT(
'area_code', area_code,
'area_type', area_type,
'area_name', area_name,
'date', to_char(date::DATE, 'YYYY-MM-DD'),
'metric', metric,
'value', value,
@xenatisch
xenatisch / admission_demographics_row.json
Last active November 18, 2021 19:52
Payload examples
[
{
"age": "0_to_5",
"rate": 175.2,
"value": 868
},
{
"age": "65_to_84",
"rate": 2802.1,
"value": 32250