コード例 #1
0
def simulate(config_file, scenarios, nsim, jobs, interactive, write_csv,
             write_parquet, index):
    config.set_file(config_file)

    spatial_config = config["spatial_setup"]
    spatial_base_path = pathlib.Path(spatial_config["base_path"].get())

    if not scenarios:
        scenarios = config["interventions"]["scenarios"].as_str_seq()
    print(f"Scenarios to be run: {', '.join(scenarios)}")

    if not nsim:
        nsim = config["nsimulations"].as_number()

    spatial_setup = setup.SpatialSetup(
        setup_name=spatial_config["setup_name"].get(),
        geodata_file=spatial_base_path / spatial_config["geodata"].get(),
        mobility_file=spatial_base_path / spatial_config["mobility"].get(),
        popnodes_key=spatial_config["popnodes"].get(),
        nodenames_key=spatial_config["nodenames"].get())

    start = time.monotonic()
    for scenario in scenarios:
        s = setup.Setup(
            setup_name=config["name"].get() + "_" + str(scenario),
            spatial_setup=spatial_setup,
            nsim=nsim,
            npi_scenario=scenario,
            npi_config=config["interventions"]["settings"][scenario],
            seeding_config=config["seeding"],
            ti=config["start_date"].as_date(),
            tf=config["end_date"].as_date(),
            interactive=interactive,
            write_csv=write_csv,
            write_parquet=write_parquet,
            dt=config["dt"].as_number(),
            first_sim_index=index)
        try:
            s.load_filter(config["dynfilter_path"].get())
            print(' We are using a filter')
        except:
            print('No filter used')

        print(f"""
>> Scenario: {scenario}
>> Starting {s.nsim} model runs beginning from {s.first_sim_index} on {jobs} processes
>> Setup *** {s.setup_name} *** from {s.ti} to {s.tf}
>> writing to folder : {s.datadir}{s.setup_name}
    """)

        seir.run_parallel(s, n_jobs=jobs)
    print(f">> All runs completed in {time.monotonic() - start:.1f} seconds")
コード例 #2
0
def test_constant_population():
    config.set_file(f"{DATA_DIR}/config.yml")

    ss = setup.SpatialSetup(setup_name="test_seir",
                            geodata_file=f"{DATA_DIR}/geodata.csv",
                            mobility_file=f"{DATA_DIR}/mobility.txt",
                            popnodes_key="population",
                            nodenames_key="geoid")

    s = setup.Setup(setup_name="test_seir",
                    spatial_setup=ss,
                    nsim=1,
                    npi_scenario="None",
                    npi_config=config["interventions"]["settings"]["None"],
                    ti=config["start_date"].as_date(),
                    tf=config["end_date"].as_date(),
                    interactive=True,
                    write_csv=False,
                    dt=0.25)

    seeding = np.zeros((len(s.t_inter), s.nnodes))

    mobility_geoid_indices = s.mobility.indices
    mobility_data_indices = s.mobility.indptr
    mobility_data = s.mobility.data

    npi = NPI.NPIBase.execute(npi_config=s.npi_config,
                              global_config=config,
                              geoids=s.spatset.nodenames)

    parameters = setup.parameters_quick_draw(config["seir"]["parameters"],
                                             len(s.t_inter), s.nnodes)
    parameters = setup.parameters_reduce(parameters, npi, s.dt)

    states = seir.steps_SEIR_nb(*parameters, seeding, s.dt, s.t_inter,
                                s.nnodes, s.popnodes, mobility_geoid_indices,
                                mobility_data_indices, mobility_data,
                                s.dynfilter)

    completepop = s.popnodes.sum()
    origpop = s.popnodes
    for it in range(len(s.t_inter)):
        totalpop = 0
        for i in range(s.nnodes):
            totalpop += states[:5, i, it].sum()
            #Sum of S, E, I#, R for the geoid that is 'i'
            assert (origpop[i] == states[:5, i, it].sum())
        assert (completepop == totalpop)
コード例 #3
0
def test_check_values():
    config.set_file(f"{DATA_DIR}/config.yml")

    ss = setup.SpatialSetup(setup_name="test_values",
                            geodata_file=f"{DATA_DIR}/geodata.csv",
                            mobility_file=f"{DATA_DIR}/mobility.txt",
                            popnodes_key="population",
                            nodenames_key="geoid")

    s = setup.Setup(setup_name="test_values",
                        spatial_setup=ss,
                        nsim=1,
                        npi_scenario="None",
                        npi_config=config["interventions"]["settings"]["None"],
                        ti=config["start_date"].as_date(),
                        tf=config["end_date"].as_date(),
                        interactive=True,
                        write_csv=False,
                        dt=0.25)

    with warnings.catch_warnings(record=True) as w:

        seeding = np.zeros((len(s.t_inter), s.nnodes))

        if np.all(seeding == 0):
            warnings.warn("provided seeding has only value 0", UserWarning)

        seeding[0,0] = 1

        if np.all(seeding == 0):
            warnings.warn("provided seeding has only value 0", UserWarning)

        if(np.all(s.mobility.data < 1)):
            warnings.warn("highest mobility value is less than 1", UserWarning)

        s.mobility.data[0] = 0.8
        s.mobility.data[1] = 0.5

        if(np.all(s.mobility.data < 1)):
            warnings.warn("highest mobility value is less than 1", UserWarning)

        assert(len(w) == 2)
        assert(issubclass(w[0].category, UserWarning))
        assert(issubclass(w[1].category, UserWarning))
        assert("seeding" in str(w[0].message))
        assert("mobility" in str(w[1].message))
コード例 #4
0
def test_steps_SEIR_no_spread():
    config.set_file(f"{DATA_DIR}/config.yml")

    ss = setup.SpatialSetup(setup_name="test_seir",
                            geodata_file=f"{DATA_DIR}/geodata.csv",
                            mobility_file=f"{DATA_DIR}/mobility.txt",
                            popnodes_key="population",
                            nodenames_key="geoid")

    s = setup.Setup(setup_name="test_seir",
                    spatial_setup=ss,
                    nsim=1,
                    npi_scenario="None",
                    npi_config=config["interventions"]["settings"]["None"],
                    ti=config["start_date"].as_date(),
                    tf=config["end_date"].as_date(),
                    interactive=True,
                    write_csv=False,
                    dt=0.25)

    seeding = np.zeros((len(s.t_inter), s.nnodes))
    seeding[:, 0] = 100

    mobility_geoid_indices = s.mobility.indices
    mobility_data_indices = s.mobility.indptr
    mobility_data = s.mobility.data * 0

    npi = NPI.NPIBase.execute(npi_config=s.npi_config,
                              global_config=config,
                              geoids=s.spatset.nodenames)

    parameters = setup.parameters_quick_draw(config["seir"]["parameters"],
                                             len(s.t_inter), s.nnodes)
    parameters = setup.parameters_reduce(parameters, npi, s.dt)

    for i in range(100):
        states = seir.steps_SEIR_nb(*parameters, seeding, s.dt, s.t_inter,
                                    s.nnodes, s.popnodes,
                                    mobility_geoid_indices,
                                    mobility_data_indices, mobility_data,
                                    s.dynfilter)

        assert states[seir.cumI][1].max() == 0
コード例 #5
0
def test_Setup_set_filter():
    ss = setup.SpatialSetup(setup_name=TEST_SETUP_NAME,
                            geodata_file=f"{DATA_DIR}/geodata.csv",
                            mobility_file=f"{DATA_DIR}/mobility.txt",
                            popnodes_key="population",
                            nodenames_key="geoid")

    s = setup.Setup(setup_name="test_set_filter_name",
                    spatial_setup=ss,
                    nsim=1,
                    npi_scenario="test_set_filter_scenario",
                    npi_config=confuse.Configuration("test"),
                    ti=datetime.date(2020, 1, 31),
                    tf=datetime.date(2020, 12, 31),
                    interactive=True,
                    write_csv=False,
                    dt=0.25)

    # filter has bad dimensions
    with pytest.raises(ValueError):
        s.set_filter(np.zeros((1, 1)))