case_data.py 6.95 KB
Newer Older
1
2
"""Loads COVID-19 case data"""

Chris Jewell's avatar
Chris Jewell committed
3
import time
Chris Jewell's avatar
Chris Jewell committed
4
5
6
from warnings import warn
import requests
import json
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
import pandas as pd

from covid.data.util import (
    invalidInput,
    get_date_low_high,
    check_date_bounds,
    check_date_format,
    check_lad19cd_format,
    merge_lad_codes,
)
from covid.data import AreaCodeData


class CasesData:
    def get(config):
        """
        Retrieve a pandas DataFrame containing the cases/line list data.
        """
        settings = config["CasesData"]
        if settings["input"] == "url":
            df = CasesData.getURL(settings["address"], config)
        elif settings["input"] == "csv":
            print(
                "Reading case data from local CSV file at", settings["address"]
            )
            df = CasesData.getCSV(settings["address"])
        elif settings["input"] == "processed":
            print(
                "Reading case data from preprocessed CSV at",
                settings["address"],
            )
            df = pd.read_csv(settings["address"], index_col=0)
        else:
            invalidInput(settings["input"])

        return df

    def getURL(url, config):
        """
        Placeholder, in case we wish to interface with an API.
        """
49
50
51
52
53
54
55
56
57
58
59
60
        max_tries = 5
        secs = 5
        for i in range(max_tries):
            try:
                print("Attempting to download...", end="", flush=True)
                response = requests.get(url)
                content = json.loads(response.content)
                df = pd.read_json(json.dumps(content["body"]))
                print("Success", flush=True)
                return df
            except ConnectionResetError:
                print("Failed", flush=True)
Chris Jewell's avatar
Chris Jewell committed
61
                time.sleep(secs * 2 ** i)
62
63
64
65

        raise ConnectionError(
            f"Data download timed out after {max_tries} attempts"
        )
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

    def getCSV(file):
        """
        Format as per linelisting
        """
        columns = ["pillar", "LTLA_code", "specimen_date", "lab_report_date"]
        dfs = pd.read_csv(file, chunksize=50000, iterator=True, usecols=columns)
        df = pd.concat(dfs)
        return df

    def check(df, config):
        """
        Check that data format seems correct
        """
        nareas = len(config["lad19cds"])
        date_low, date_high = get_date_low_high(config)
        dates = pd.date_range(start=date_low, end=date_high, closed="left")
        days = len(dates)
        entries = days * nareas

        if not (
            ((dims[1] >= 3) & (dims[0] == entries))
            | ((dims[1] == days) & (dims[0] == nareas))
        ):
            print(df)
            raise ValueError("Incorrect CasesData dimensions")

        if "date" in df:
            _df = df
        elif df.columns.name == "date":
            _df = pd.DataFrame({"date": df.columns})
        else:
            raise ValueError("Cannot determine date axis")

        check_date_bounds(df, date_low, date_high)
        check_date_format(df)
        check_lad19cd_format(df)
103
        df = df.rename(columns={"date": "time"})
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        return True

    def adapt(df, config):
        """
        Adapt the line listing data to the desired dataframe format.
        """
        # Extract the yaml config settings
        date_low, date_high = get_date_low_high(config)
        settings = config["CasesData"]
        pillars = settings["pillars"]
        measure = settings["measure"].casefold()

        # this key might not be stored in the config file
        # if it's not, we need to grab it using AreaCodeData
        if "lad19cds" not in config:
            _df = AreaCodeData.process(config)
        areacodes = config["lad19cds"]

        if settings["input"] == "processed":
            return df

        if settings["format"].lower() == "phe":
            df = CasesData.adapt_phe(
                df,
                date_low,
                date_high,
                pillars,
                measure,
                areacodes,
            )
Chris Jewell's avatar
Chris Jewell committed
134
135
136
137
        elif (settings["input"] == "url") and (settings["format"] == "json"):
            df = CasesData.adapt_gov_api(
                df, date_low, date_high, pillars, measure, areacodes
            )
138
139
140

        return df

Chris Jewell's avatar
Chris Jewell committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    def adapt_gov_api(df, date_low, date_high, pillars, measure, areacodes):

        warn("Using API data: 'pillar' and 'measure' will be ignored")

        df = df.rename(
            columns={"areaCode": "location", "newCasesBySpecimenDate": "cases"}
        )
        df = df[["location", "date", "cases"]]
        df["date"] = pd.to_datetime(df["date"])
        df["location"] = merge_lad_codes(df["location"])
        df = df[df["location"].isin(areacodes)]
        df.index = pd.MultiIndex.from_frame(df[["location", "date"]])
        df = df.sort_index()

        dates = pd.date_range(date_low, date_high, closed="left")
        multi_index = pd.MultiIndex.from_product([areacodes, dates])
        ser = df["cases"].reindex(multi_index, fill_value=0.0)
158
        ser.index.names = ["location", "time"]
Chris Jewell's avatar
Chris Jewell committed
159
160
161
        ser.name = "cases"
        return ser

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    def adapt_phe(df, date_low, date_high, pillars, measure, areacodes):
        """
        Adapt the line listing data to the desired dataframe format.
        """
        # Clean missing values
        df.dropna(inplace=True)
        df = df.rename(columns={"LTLA_code": "lad19cd"})

        # Clean time formats
        df["specimen_date"] = pd.to_datetime(df["specimen_date"], dayfirst=True)
        df["lab_report_date"] = pd.to_datetime(
            df["lab_report_date"], dayfirst=True
        )

        df["lad19cd"] = merge_lad_codes(df["lad19cd"])

        # filters for pillars, date ranges, and areacodes if given
        filters = df["pillar"].isin(pillars)
        filters &= df["lad19cd"].isin(areacodes)
        if measure == "specimen":
            filters &= (date_low <= df["specimen_date"]) & (
                df["specimen_date"] < date_high
            )
        else:
            filters &= (date_low <= df["lab_report_date"]) & (
                df["lab_report_date"] < date_high
            )
        df = df[filters]
        df = df.drop(columns="pillar")  # No longer need pillar column

        # Aggregate counts
        if measure == "specimen":
            df = df.groupby(["lad19cd", "specimen_date"]).count()
            df = df.rename(columns={"lab_report_date": "cases"})
        else:
            df = df.groupby(["lad19cd", "lab_report_date"]).count()
            df = df.rename(columns={"specimen_date": "cases"})

200
        df.index.names = ["lad19cd", "time"]
201
202
203
204
205
        df = df.sort_index()

        # Fill in all dates, and add 0s for empty counts
        dates = pd.date_range(date_low, date_high, closed="left")
        multi_indexes = pd.MultiIndex.from_product(
206
            [areacodes, dates], names=["location", "time"]
207
208
        )
        results = df["cases"].reindex(multi_indexes, fill_value=0.0)
Chris Jewell's avatar
Chris Jewell committed
209
        return results.sort_index()
210
211
212
213
214

    def process(config):
        df = CasesData.get(config)
        df = CasesData.adapt(df, config)
        return df