summary_geopackage.py 1.65 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
"""Summarises posterior distribution into a geopackage"""

import pickle as pkl
import pandas as pd
import geopandas as gp


def _tier_enum(design_matrix):
    """Turns a factor variable design matrix into
    an enumerated column"""
    df = design_matrix[-1].to_dataframe()[["value"]]
    df = df[df["value"] == 1.0].reset_index()
    return df["alert_level"]


def summary_geopackage(input_files, output_file, config):
    """Creates a summary geopackage file

    :param input_files: a list of data file names [data pkl,
                                                   next_generation_matrix,
                                                   insample7,
                                                   insample14,
                                                   medium_term]
    :param output_file: the output geopackage file
    :param config: SummaryGeopackage configuration information
    """

    # Read in the first input file
    with open(input_files.pop(0), "rb") as f:
        data = pkl.load(f)

    # Load and filter geopackage
Chris Jewell's avatar
Chris Jewell committed
33
    geo = gp.read_file(config["base_geopackage"], layer=config["base_layer"])
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    geo = geo[geo["lad19cd"].isin(data["locations"]["lad19cd"])]
    geo = geo.sort_values(by="lad19cd")

    # Dump data into the geopackage
    while len(input_files) > 0:
        fn = input_files.pop()
        print(f"Collating {fn}")
        try:
            columns = pd.read_csv(fn, index_col="location")
        except ValueError as e:
            raise ValueError(f"Error reading file '{fn}': {e}")

        geo = geo.merge(
            columns, how="left", left_on="lad19cd", right_index=True
        )

    geo.to_file(output_file, driver="GPKG")