Ejemplo n.º 1
0
def main():
    """Save figures showing the pre-existing Urban Observatory network of sensors and
    comparisons with optimised networks using our approach.
    """
    print("Saving Urban Observatory figures...")
    set_fig_style()

    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    networks_path = get_single_obj_filepath(config)
    networks = load_pickle(networks_path)
    uo_sensors = load_uo_sensors(config)

    figs_dir = get_figures_save_dir(config)

    population_groups, all_groups = get_objectives(config)
    oa_weights = get_weights(lad20cd, population_groups)
    theta, _ = get_default_optimisation_params(config)

    uo_sensor_dict = get_uo_sensor_dict(lad20cd, uo_sensors=uo_sensors)
    uo_coverage = get_uo_coverage_oa(lad20cd, uo_sensor_dict, theta,
                                     all_groups, oa_weights)

    fig_uo_sensor_locations(lad20cd, uo_sensors, figs_dir)
    fig_uo_coverage_grid(lad20cd, uo_sensors, theta, figs_dir)
    fig_uo_coverage_grid_diff(lad20cd, uo_sensors, theta, all_groups, networks,
                              figs_dir)
    fig_uo_coverage_oa(uo_coverage, theta, all_groups, figs_dir)
    fig_uo_coverage_oa_diff(lad20cd, uo_coverage, theta, all_groups, networks,
                            figs_dir)
Ejemplo n.º 2
0
def main():
    """
    Save figures showing the results of running the mulit-objective optimisation (NSGA2)
    with two objectives.
    """
    print("Saving two-objective network figures...")
    set_fig_style()
    config = get_config()
    figs_dir = get_figures_save_dir(config)

    networks_path = get_two_objs_filepath(config)
    networks = load_pickle(networks_path)
    theta, n_sensors = get_default_optimisation_params(config)
    n = networks[f"theta{theta}"][f"{n_sensors}sensors"]
    scores, solutions = extract_all(n)
    scores = -scores

    population_groups, all_groups = get_objectives(config)
    plot_objs = config["optimisation"]["two_objectives"]["objectives"]
    lad20cd = lad20nm_to_lad20cd(config["la"])
    inputs = get_multi_obj_inputs(lad20cd, population_groups)

    fig_obj1_vs_obj2(plot_objs, scores, all_groups, theta, n_sensors, figs_dir)
    fig_two_objs_spectrum(
        lad20cd,
        plot_objs,
        scores,
        solutions,
        inputs,
        all_groups,
        theta,
        n_sensors,
        figs_dir,
    )
Ejemplo n.º 3
0
def main():
    """Save figures showing the results of running the multi-objective optimisation
    (with the NSGA2 algorithm)
    """
    print("Saving multi-objective network figures...")
    set_fig_style()
    config = get_config()

    lad20cd = lad20nm_to_lad20cd(config["la"])
    networks_path = get_multi_objs_filepath(config)
    networks = load_pickle(networks_path)

    figs_dir = get_figures_save_dir(config)

    theta, n_sensors = get_default_optimisation_params(config)
    n = networks[f"theta{theta}"][f"{n_sensors}sensors"]
    scores, solutions = extract_all(n)
    scores = -scores

    population_groups, all_groups = get_objectives(config)
    objs = [g["title"] for g in all_groups.values()]

    threshold = config["figures"]["multi_objectives"]["all_coverage_threshold"]
    fig_all_above_threshold(scores, objs, threshold, theta, n_sensors,
                            figs_dir)

    threshold = config["figures"]["multi_objectives"][
        "work_coverage_threshold"]
    work_name = config["objectives"]["workplace"]["title"]
    fig_work_above_threshold(scores, objs, threshold, theta, n_sensors,
                             work_name, figs_dir)

    child_name = config["objectives"]["population_groups"]["pop_children"][
        "title"]
    inputs = get_multi_obj_inputs(lad20cd, population_groups)
    fig_max_child_work_above_threshold(
        lad20cd,
        scores,
        objs,
        threshold,
        theta,
        n_sensors,
        solutions,
        inputs,
        work_name,
        child_name,
        figs_dir,
    )

    uo_sensors = load_uo_sensors(config)
    uo_coverage = get_uo_coverage_oa(lad20cd, None, theta, all_groups,
                                     inputs["oa_weight"])
    n_uo_oa = uo_sensors["oa11cd"].nunique()
    fig_coverage_above_uo(uo_coverage, scores, objs, theta, n_uo_oa,
                          all_groups, figs_dir)

    fig_max_min_coverage(lad20cd, scores, objs, theta, n_sensors, solutions,
                         inputs, figs_dir)
Ejemplo n.º 4
0
def get_la_save_dir(config: dict) -> Path:
    """Get the path to the parent directory to use for saving local authority results.

    Parameters
    ----------
    config : dict
        Parameters as loaded by utils.get_config

    Returns
    -------
    Path
        Directory to save local authority results to
    """
    save_dir = config["save_dir"]
    lad20cd = lad20nm_to_lad20cd(config["la"])
    return Path(save_dir, lad20cd)
Ejemplo n.º 5
0
def main():
    """
    Generate single objective networks for a local authority and save them to the path
    specified in config.yml.
    """
    print("Generating single objective networks...")
    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    save_path = get_single_obj_filepath(config)
    population_groups, _ = get_objectives(config)
    thetas, n_sensors = get_all_optimisation_params(config)

    results = make_single_obj_networks(lad20cd, population_groups, thetas,
                                       n_sensors)
    with open(save_path, "wb") as f:
        pickle.dump(results, f)
Ejemplo n.º 6
0
def main():
    """
    Save Urban Observatory sensor locations to the path specified by
    "save_dir" and "filename" in the "urb_obs" section of config.yml.
    """
    print("Getting Urban Observatory data...")
    config = get_config()
    save_dir = config["save_dir"]
    lad20cd = lad20nm_to_lad20cd(config["la"])
    uo_dir = config["urb_obs"]["save_dir"]
    uo_name = config["urb_obs"]["filename"]
    save_path = Path(save_dir, lad20cd, uo_dir, uo_name)
    os.makedirs(save_path.parent, exist_ok=True)

    uo_sensors = save_uo_sensors(lad20cd, save_path)
    add_n_uo_sensors_to_config(uo_sensors, config)
Ejemplo n.º 7
0
def main():
    """Save figures showing the distribution of differrent sub-populations around the
    local authority.
    """
    print("Saving demographic figures...")
    set_fig_style()

    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    figs_dir = get_figures_save_dir(config)

    theta, _ = get_default_optimisation_params(config)
    population_groups, all_groups = get_objectives(config)

    oa_weights = get_weights(lad20cd, population_groups)
    fig_importance(lad20cd, all_groups, oa_weights, theta, figs_dir)
    oa = calc_oa_density(lad20cd, all_groups, population_groups)
    fig_density(lad20cd, oa, all_groups, figs_dir)
Ejemplo n.º 8
0
def main():
    """
    Generate two-objective networks for a local authority and save them to the path
    specified in config.yml.
    """
    print("Generating two-objective networks...")
    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    save_path = get_two_objs_filepath(config)

    population_groups, _ = get_objectives(config)
    thetas, n_sensors = get_all_optimisation_params(config)
    gen = config["optimisation"]["two_objectives"]["gen"]
    population_size = config["optimisation"]["two_objectives"]["population_size"]
    seed = config["optimisation"]["two_objectives"]["seed"]
    pg.set_global_rng_seed(seed=seed)
    objectives = config["optimisation"]["two_objectives"]["objectives"]
    inputs = get_two_obj_inputs(lad20cd, objectives, population_groups)

    results = make_two_obj_networks(inputs, thetas, n_sensors, gen, population_size)
    with open(save_path, "wb") as f:
        pickle.dump(results, f)
Ejemplo n.º 9
0
def main():
    """
    Generate multi-objective networks for a local authority for a range of theta values,
    numbers of sensors, and objectives. Save them in pkl and json format, as well as
    meta data.
    """
    print("Generating multi-objective networks...")
    la = "Gateshead"
    thetas = [500]  # [100, 250, 500]
    n_sensors = [10]  # [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    gen = 2  # 1000
    population_size = 8  # 200
    seed = 123
    population_groups = {
        "pop_total": {
            "min": 0,
            "max": 90,
            "weight": 1,
            "title": "Total Residents",
        },
        "pop_children": {
            "min": 0,
            "max": 15,
            "weight": 1,
            "title": "Residents Under 16",
        },
        "pop_elderly": {
            "min": 66,
            "max": 90,
            "weight": 1,
            "title": "Residents Over 65",
        },
    }
    workplace_name = "workplace"
    objectives = [
        ["workplace", "pop_elderly", "pop_children", "pop_total"],
    ]
    include_oa_coverage = True

    lad20cd = lad20nm_to_lad20cd(la)
    pg.set_global_rng_seed(seed=seed)
    save_path = Path(
        f"data/networks/{datetime.now().strftime('%Y-%m-%d_%H%M%S')}")
    os.makedirs(save_path)

    with open(Path(save_path, "meta.json"), "w") as f:
        json.dump(
            {
                "la": la,
                "lad20cd": lad20cd,
                "thetas": thetas,
                "n_sensors": n_sensors,
                "gen": gen,
                "population_size": population_size,
                "seed": seed,
                "population_groups": population_groups,
                "workplace_name": workplace_name,
                "objectives": objectives,
            },
            f,
            indent=4,
        )

    make_multi_obj_networks(
        lad20cd,
        population_groups,
        objectives,
        thetas,
        n_sensors,
        gen,
        population_size,
        save_path,
        workplace_name=workplace_name,
        include_oa_coverage=include_oa_coverage,
    )
Ejemplo n.º 10
0
def main():
    """
    Creates a formatted report including all the generated figures for a local auhtority
    and decriptions of what each figure shows (using the template reoprt_template.md).
    Both a Markdown (report.md) and HTML (report.html) version of the report are saved.
    """
    print("Generating formatted reports...")
    with open("report_template.md") as f:
        template = Template(f.read())

    config = get_config()
    fig_dir = get_figures_save_dir(config)
    report_dir = fig_dir.parent
    rel_fig_dir = fig_dir.stem

    la_name = config["la"]
    la_code = lad20nm_to_lad20cd(la_name)
    fig_density = Path(rel_fig_dir, "demographics_density.png")
    fig_importance = Path(rel_fig_dir, "demographics_importance.png")
    fig_coverage_vs_nsensors = Path(rel_fig_dir, "coverage_vs_nsensors.png")

    _, all_groups = get_objectives(config)
    total_pop_name = all_groups["pop_total"]["title"]
    fig_totalpop = find_fig_path("pop_total_theta*_nsensors*.png", fig_dir)
    children_name = all_groups["pop_children"]["title"]
    fig_children = find_fig_path("pop_children_theta*_nsensors*.png", fig_dir)
    older_name = all_groups["pop_elderly"]["title"]
    fig_older = find_fig_path("pop_elderly_theta*_nsensors*.png", fig_dir)
    work_name = all_groups["workplace"]["title"]
    fig_workers = find_fig_path("workplace_theta*_nsensors*.png", fig_dir)

    fig_urb_obs_sensors = find_fig_path("urb_obs_sensors_nsensors_*.png", fig_dir)
    fig_urb_obs_coverage_grid = find_fig_path(
        "urb_obs_coverage_grid_theta_*_nsensors_*.png", fig_dir
    )
    fig_urb_obs_coverage_diff_grid = find_fig_path(
        "urb_obs_coverage_difference_grid_theta_*_nsensors_*.png", fig_dir
    )
    fig_urb_obs_coverage_oa = find_fig_path(
        "urb_obs_coverage_oa_theta_*_nsensors_*.png", fig_dir
    )
    fig_urb_obs_coverage_diff_oa = find_fig_path(
        "urb_obs_coverage_difference_grid_theta_*_nsensors_*.png", fig_dir
    )
    all_threshold = config["figures"]["multi_objectives"]["all_coverage_threshold"]
    fig_all_above_threshold = find_fig_path(
        f"multiobj_theta*_*sensors_above{round(all_threshold * 100)}cov.png",
        fig_dir,
    )
    work_threshold = config["figures"]["multi_objectives"]["work_coverage_threshold"]
    fig_work_above_threshold = find_fig_path(
        f"multiobj_theta*_*sensors_workabove{round(work_threshold * 100)}cov.png",
        fig_dir,
    )
    fig_max_child_work_above_threshold = find_fig_path(
        "multiobj_wplace*_child*_theta*_*sensors.png", fig_dir
    )
    fig_coverage_above_uo = find_fig_path(
        "multiobj_theta*_*sensors_above_urbobs.png", fig_dir
    )
    fig_max_min_coverage = find_fig_path(
        "multiobj_compromise_theta*_*sensors_cov*.png", fig_dir
    )
    obj_1 = config["optimisation"]["two_objectives"]["objectives"][0]
    obj_2 = config["optimisation"]["two_objectives"]["objectives"][1]
    obj_1 = all_groups[obj_1]["title"]
    obj_2 = all_groups[obj_2]["title"]
    fig_obj1_vs_obj2 = find_fig_path("2obj_theta*_*sensors.png", fig_dir)
    fig_spectrum = find_fig_path("2obj_spectrum_theta*_*sensors.png", fig_dir)
    fig_width = config["report"]["fig_width"]

    filled_template = template.render(
        la_name=la_name,
        la_code=la_code,
        fig_density=fig_density,
        fig_importance=fig_importance,
        fig_coverage_vs_nsensors=fig_coverage_vs_nsensors,
        total_pop_name=total_pop_name,
        fig_totalpop=fig_totalpop,
        children_name=children_name,
        fig_children=fig_children,
        older_name=older_name,
        fig_older=fig_older,
        work_name=work_name,
        fig_workers=fig_workers,
        fig_urb_obs_sensors=fig_urb_obs_sensors,
        fig_urb_obs_coverage_grid=fig_urb_obs_coverage_grid,
        fig_urb_obs_coverage_diff_grid=fig_urb_obs_coverage_diff_grid,
        fig_urb_obs_coverage_oa=fig_urb_obs_coverage_oa,
        fig_urb_obs_coverage_diff_oa=fig_urb_obs_coverage_diff_oa,
        all_threshold=all_threshold,
        fig_all_above_threshold=fig_all_above_threshold,
        work_threshold=work_threshold,
        fig_work_above_threshold=fig_work_above_threshold,
        fig_max_child_work_above_threshold=fig_max_child_work_above_threshold,
        fig_coverage_above_uo=fig_coverage_above_uo,
        fig_max_min_coverage=fig_max_min_coverage,
        obj_1=obj_1,
        obj_2=obj_2,
        fig_obj1_vs_obj2=fig_obj1_vs_obj2,
        fig_spectrum=fig_spectrum,
        fig_width=fig_width,
    )

    with open(Path(report_dir, "report.md"), "w") as f:
        f.write(filled_template)

    html = markdown(filled_template)
    with open(Path(report_dir, "report.html"), "w") as f:
        f.write(html)
    print("Reports saved to", report_dir)