case_data.py 5.26 KB
Newer Older
1
2
3
4
5
6
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
103
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""Loads COVID-19 case data"""

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.
        """
        pass

    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)
        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,
            )

        return df

    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"})

        df.index.names = ["lad19cd", "date"]
        df = df.sort_index()

        # Fill in all dates, and add 0s for empty counts
        dates = pd.date_range(date_low, date_high, closed="left")
        indexes = [(lad19, date) for date in dates for lad19 in areacodes]
        multi_indexes = pd.MultiIndex.from_product(
            [areacodes, dates], names=["location", "date"]
        )
        results = df["cases"].reindex(multi_indexes, fill_value=0.0)
        return results

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