summary_longformat.py 6.43 KB
Newer Older
Chris Jewell's avatar
Chris Jewell committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"""Produces a long-format summary of fitted model results"""

import pickle as pkl
from datetime import date
import numpy as np
import pandas as pd
import xarray

from gemlib.util import compute_state
from covid.model_spec import STOICHIOMETRY
from covid import model_spec
from covid.formats import make_dstl_template


def xarray2summarydf(arr):
    mean = arr.mean(dim="iteration").to_dataset(name="value")
17
18
19
    q = np.arange(start=0.05, stop=1.0, step=0.05)
    quantiles = arr.quantile(q=q, dim="iteration").to_dataset(dim="quantile")
    ds = mean.merge(quantiles).rename_vars({qi: f"{qi:.2f}" for qi in q})
Chris Jewell's avatar
Chris Jewell committed
20
21
22
    return ds.to_dataframe().reset_index()


23
24
25
26
def prevalence(prediction, popsize):
    prev = compute_state(
        prediction["initial_state"], prediction["events"], STOICHIOMETRY
    )
Chris Jewell's avatar
Chris Jewell committed
27
28
29
30
    prev = xarray.DataArray(
        prev.numpy(),
        coords=[
            np.arange(prev.shape[0]),
31
32
            prediction.coords["location"],
            prediction.coords["time"],
Chris Jewell's avatar
Chris Jewell committed
33
34
35
36
37
38
            np.arange(prev.shape[-1]),
        ],
        dims=["iteration", "location", "time", "state"],
    )
    prev_per_1e5 = (
        prev[..., 1:3].sum(dim="state").reset_coords(drop=True)
39
        / np.array(popsize)[np.newaxis, :, np.newaxis]
Chris Jewell's avatar
Chris Jewell committed
40
41
42
43
44
        * 100000
    )
    return xarray2summarydf(prev_per_1e5)


45
def weekly_pred_cases_per_100k(prediction, popsize):
Chris Jewell's avatar
Chris Jewell committed
46
    """Returns weekly number of cases per 100k of population"""
47
48

    prediction = prediction[..., 2]  # Case removals
49
50
    prediction = prediction.reset_coords(drop=True)

Chris Jewell's avatar
Chris Jewell committed
51
52
    # TODO: Find better way to sum up into weeks other than
    # a list comprehension.
53
54
55
56
57
    dates = pd.DatetimeIndex(prediction.coords["time"].data)
    first_sunday_index = np.where(dates.weekday == 6)[0][0]
    weeks = range(first_sunday_index, prediction.coords["time"].shape[0], 7)[
        :-1
    ]
58
59
60
61
62
63
64
65
66
    week_incidence = [
        prediction[..., week : (week + 7)].sum(dim="time") for week in weeks
    ]
    week_incidence = xarray.concat(
        week_incidence, dim=prediction.coords["time"][weeks]
    )
    week_incidence = week_incidence.transpose(
        *prediction.dims, transpose_coords=True
    )
Chris Jewell's avatar
Chris Jewell committed
67
    # Divide by population sizes
68
    week_incidence = (
69
        week_incidence / np.array(popsize)[np.newaxis, :, np.newaxis] * 100000
70
71
72
73
    )
    return xarray2summarydf(week_incidence)


Chris Jewell's avatar
Chris Jewell committed
74
75
76
77
78
def summary_longformat(input_files, output_file):
    """Draws together pipeline results into a long format
       csv file.

    :param input_files: a list of filenames [data_pkl,
79
80
81
82
                                             insample7_nc
                                             insample14_nc,
                                             medium_term_pred_nc,
                                             ngm_nc]
Chris Jewell's avatar
Chris Jewell committed
83
84
85
86
    :param output_file: the output CSV with columns `[date,
                        location,value_name,value,q0.025,q0.975]`
    """

87
88
89
90
    data = xarray.open_dataset(input_files[0], group="constant_data")
    cases = xarray.open_dataset(input_files[0], group="observations")["cases"]

    df = cases.to_dataframe(name="value").reset_index()
Chris Jewell's avatar
Chris Jewell committed
91
92
93
94
95
96
    df["value_name"] = "newCasesBySpecimenDate"
    df["0.05"] = np.nan
    df["0.5"] = np.nan
    df["0.95"] = np.nan

    # Insample predictive incidence
97
    insample = xarray.open_dataset(input_files[1], group="predictions")
98
99
100
    insample_df = xarray2summarydf(
        insample["events"][..., 2].reset_coords(drop=True)
    )
101
102
103
    insample_df["value_name"] = "insample7_Cases"
    df = pd.concat([df, insample_df], axis="index")

104
    insample = xarray.open_dataset(input_files[2], group="predictions")
105
106
107
    insample_df = xarray2summarydf(
        insample["events"][..., 2].reset_coords(drop=True)
    )
Chris Jewell's avatar
Chris Jewell committed
108
109
110
    insample_df["value_name"] = "insample14_Cases"
    df = pd.concat([df, insample_df], axis="index")

111
    # Medium term absolute incidence
112
    medium_term = xarray.open_dataset(input_files[3], group="predictions")
113
114
115
    medium_df = xarray2summarydf(
        medium_term["events"][..., 2].reset_coords(drop=True)
    )
116
117
118
    medium_df["value_name"] = "absolute_incidence"
    df = pd.concat([df, medium_df], axis="index")

119
120
121
122
123
124
125
    # Cumulative cases
    medium_df = xarray2summarydf(
        medium_term["events"][..., 2].cumsum(dim="time").reset_coords(drop=True)
    )
    medium_df["value_name"] = "cumulative_absolute_incidence"
    df = pd.concat([df, medium_df], axis="index")

126
127
128
129
    # Medium term incidence per 100k
    medium_df = xarray2summarydf(
        (
            medium_term["events"][..., 2].reset_coords(drop=True)
130
            / np.array(data["N"])[np.newaxis, :, np.newaxis]
131
132
133
134
        )
        * 100000
    )
    medium_df["value_name"] = "incidence_per_100k"
Chris Jewell's avatar
Chris Jewell committed
135
136
    df = pd.concat([df, medium_df], axis="index")

137
    # Weekly incidence per 100k
138
139
140
    weekly_incidence = weekly_pred_cases_per_100k(
        medium_term["events"], data["N"]
    )
141
142
143
    weekly_incidence["value_name"] = "weekly_cases_per_100k"
    df = pd.concat([df, weekly_incidence], axis="index")

Chris Jewell's avatar
Chris Jewell committed
144
145
    # Medium term prevalence
    prev_df = prevalence(medium_term, data["N"])
146
    prev_df["value_name"] = "prevalence"
Chris Jewell's avatar
Chris Jewell committed
147
148
149
    df = pd.concat([df, prev_df], axis="index")

    # Rt
Chris Jewell's avatar
Chris Jewell committed
150
151
    rt = xarray.load_dataset(input_files[4], group="posterior_predictive")[
        "R_it"
152
    ]
Chris Jewell's avatar
Chris Jewell committed
153
    rt_summary = xarray2summarydf(rt.isel(time=-1))
Chris Jewell's avatar
Chris Jewell committed
154
    rt_summary["value_name"] = "R"
Chris Jewell's avatar
Chris Jewell committed
155
    rt_summary["time"] = rt.coords["time"].data[-1] + np.timedelta64(1, "D")
Chris Jewell's avatar
Chris Jewell committed
156
157
    df = pd.concat([df, rt_summary], axis="index")

158
159
    quantiles = df.columns[df.columns.str.startswith("0.")]

Chris Jewell's avatar
Chris Jewell committed
160
161
162
    return make_dstl_template(
        group="Lancaster",
        model="SpatialStochasticSEIR",
163
        scenario="Nowcast",
Chris Jewell's avatar
Chris Jewell committed
164
165
166
167
168
169
        creation_date=date.today(),
        version=model_spec.VERSION,
        age_band="All",
        geography=df["location"],
        value_date=df["time"],
        value_type=df["value_name"],
Chris Jewell's avatar
Chris Jewell committed
170
        value=df["value"],
171
        quantiles={q: df[q] for q in quantiles},
172
    ).to_excel(output_file, index=False)
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
200
201
202


if __name__ == "__main__":

    import os
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--output", "-o", type=str, required=True, help="Output file"
    )
    parser.add_argument(
        "resultsdir",
        type=str,
        help="Results directory",
    )
    args = parser.parse_args()

    input_files = [
        os.path.join(args.resultsdir, d)
        for d in [
            "pipeline_data.pkl",
            "insample7.nc",
            "insample14.nc",
            "medium_term.nc",
            "ngm.nc",
        ]
    ]

    summary_longformat(input_files, args.output)