Esempio n. 1
0
def load_companies_from_hdf5(file_path: str, chunk_size=50000):
    """
    Loads companies from an hdf5 file located at ``file_path``.
    Note that this object will not be ready to use, as the links to
    object instances of other classes need to be restored first.
    This function should be rarely be called oustide world.py
    """
    with h5py.File(file_path, "r", libver="latest", swmr=True) as f:
        print("loading companies from hdf5 ", end="")
        companies = f["companies"]
        companies_list = []
        n_companies = companies.attrs["n_companies"]
        n_chunks = int(np.ceil(n_companies / chunk_size))
        for chunk in range(n_chunks):
            print(".", end="")
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_companies)
            ids = companies["id"][idx1:idx2]
            super_areas = companies["super_area"][idx1:idx2]
            sectors = companies["sector"][idx1:idx2]
            n_workers_maxs = companies["n_workers_max"][idx1:idx2]
            for k in range(idx2 - idx1):
                super_area = super_areas[k]
                if super_area == nan_integer:
                    super_area = None
                company = Company(super_area, n_workers_maxs[k],
                                  sectors[k].decode())
                company.id = ids[k]
                companies_list.append(company)
    print("\n", end="")
    return Companies(companies_list)
def make_world(geography):
    geography.hospitals = Hospitals.for_geography(geography)
    geography.schools = Schools.for_geography(geography)
    geography.companies = Companies.for_geography(geography)
    geography.care_homes = CareHomes.for_geography(geography)
    geography.universities = Universities.for_super_areas(
        geography.super_areas)
    world = generate_world_from_geography(geography,
                                          include_households=False,
                                          include_commute=False)
    return world
Esempio n. 3
0
def load_companies_from_hdf5(file_path: str,
                             chunk_size=50000,
                             domain_super_areas=None):
    """
    Loads companies from an hdf5 file located at ``file_path``.
    Note that this object will not be ready to use, as the links to
    object instances of other classes need to be restored first.
    This function should be rarely be called oustide world.py
    """
    logger.info("loading companies...")
    with h5py.File(file_path, "r", libver="latest", swmr=True) as f:
        companies = f["companies"]
        companies_list = []
        n_companies = companies.attrs["n_companies"]
        n_chunks = int(np.ceil(n_companies / chunk_size))
        for chunk in range(n_chunks):
            logger.info(f"Companies chunk {chunk} of {n_chunks}")
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_companies)
            length = idx2 - idx1
            ids = read_dataset(companies["id"], idx1, idx2)
            sectors = read_dataset(companies["sector"], idx1, idx2)
            n_workers_maxs = read_dataset(companies["n_workers_max"], idx1,
                                          idx2)
            super_areas = read_dataset(companies["super_area"], idx1, idx2)
            for k in range(length):
                if domain_super_areas is not None:
                    super_area = super_areas[k]
                    if super_area == nan_integer:
                        raise ValueError(
                            "if ``domain_super_areas`` is True, I expect not Nones super areas."
                        )
                    if super_area not in domain_super_areas:
                        continue
                company = Company(
                    super_area=None,
                    n_workers_max=n_workers_maxs[k],
                    sector=sectors[k].decode(),
                )
                company.id = ids[k]
                companies_list.append(company)
    return Companies(companies_list)
Esempio n. 4
0
def create_world(geography_h5):
    with h5py.File("test.hdf5", "w"):
        pass  # reset file
    geography = geography_h5
    geography.hospitals = Hospitals.for_geography(geography)
    geography.schools = Schools.for_geography(geography)
    geography.companies = Companies.for_geography(geography)
    geography.care_homes = CareHomes.for_geography(geography)
    geography.universities = Universities.for_super_areas(geography.super_areas)
    world = generate_world_from_geography(
        geography=geography, include_households=True, include_commute=True
    )
    world.pubs = Pubs.for_geography(geography)
    world.cinemas = Cinemas.for_geography(geography)
    world.groceries = Groceries.for_geography(geography)
    leisure = generate_leisure_for_world(
        ["pubs", "cinemas", "groceries", "household_visits", "care_home_vists"], world
    )
    leisure.distribute_social_venues_to_households(world.households)
    return world
Esempio n. 5
0
def simulation(args):
    gf.print_flush(args)

    msoaslist = [
        "E02005702", "E02005704", "E02005736", "E02005734", "E02001697",
        "E02001701", "E02001704", "E02001702", "E02001812", "E02001803",
        "E02001806", "E02001796", "E02001801", "E02001802", "E02001795",
        "E02001818", "E02001821", "E02001814", "E02001808", "E02001817",
        "E02001816", "E02001819", "E02001813", "E02001804", "E02001811",
        "E02001805", "E02001791", "E02001794", "E02001792", "E02004320",
        "E02004321", "E02004322", "E02004325", "E02004327", "E02004329",
        "E02004330", "E02004328", "E02001798", "E02001793", "E02005706",
        "E02002496", "E02002498", "E02002500", "E02002503", "E02002504",
        "E02002515", "E02002516", "E02006910", "E02002518", "E02002519",
        "E02002513", "E02002550", "E02002555", "E02002549", "E02002542",
        "E02002547", "E02002545", "E02002543", "E02002537", "E02002544",
        "E02002541", "E02002523", "E02002540", "E02002536", "E02002538",
        "E02002535", "E02006909", "E02002489", "E02002484", "E02002487",
        "E02002485", "E02002483", "E02002493", "E02002490", "E02002492",
        "E02002494", "E02002488", "E02002491", "E02004332", "E02002505",
        "E02002497", "E02002502", "E02006812", "E02002499", "E02002506",
        "E02006811", "E02002509", "E02002501", "E02002508", "E02002507",
        "E02002529", "E02002514", "E02002512"
    ]

    gf.print_flush("Generating world from msoalist...")

    geography = Geography.from_file({"msoa": msoaslist})
    print('memory % used:', psutil.virtual_memory()[2])

    geography.hospitals = Hospitals.for_geography(geography)
    geography.schools = Schools.for_geography(geography)
    geography.companies = Companies.for_geography(geography)
    geography.care_homes = CareHomes.for_geography(geography)
    demography = Demography.for_geography(geography)
    gf.print_flush("Geography and demography generated...")
    world = World(geography,
                  demography,
                  include_households=True,
                  include_commute=False)

    gf.print_flush("World generated...")
    # leisure
    world.cinemas = Cinemas.for_geography(geography)
    world.pubs = Pubs.for_geography(geography)
    world.groceries = Groceries.for_super_areas(world.super_areas,
                                                venues_per_capita=1 / 500)

    gf.print_flush("Initialised leisure...")

    # commute
    world.initialise_commuting()
    gf.print_flush("Initialised commute...")

    # cemeteries
    world.cemeteries = Cemeteries()
    gf.print_flush("Initialised cemeteries...")

    # infection selector
    selector = InfectionSelector.from_file()
    interaction = ContactAveraging.from_file(selector=selector)
    gf.print_flush("Infection selected...")

    # define groups for betas
    groups = {
        "leisure": ['pub', 'grocery', 'cinema'],
        "commute": ['commute_unit', 'commute_city_unit', 'travel_unit'],
        "hospital": ['hospital'],
        "care_home": ['care_home'],
        "company": ['company'],
        "school": ['school'],
        "household": ['household']
    }

    # define problem for latin hypercube sampling
    problem = {
        'num_vars': len(groups),
        'names': list(groups.keys()),
        'bounds': [[1, 2] for _ in range(len(groups))]
    }

    lhs = latin.sample(problem, N=args.num_runs, seed=1)[args.idx]

    betas = {}
    for i, key in enumerate(groups):
        for sub_key in groups[key]:
            betas[sub_key] = lhs[i]
    # save out betas for later
    with open(SAVE_PATH + '/betas.json', 'w') as f:
        json.dump(betas, f)

    # set betas in simulation to sampled ones
    for key in betas:
        interaction.beta[key] = betas[key]

    gf.print_flush(interaction.beta)

    # seed infections
    seed = Seed(
        world.super_areas,
        selector,
    )
    n_cases = int(len(world.people) / 10)
    seed.unleash_virus(n_cases)

    simulator = Simulator.from_file(world,
                                    interaction,
                                    selector,
                                    config_filename=CONFIG_PATH,
                                    save_path=SAVE_PATH)

    print('memory % used:', psutil.virtual_memory()[2])

    simulator.run()

    gf.print_flush("Simulation finished!!!!")

    return None
Esempio n. 6
0
from june.infection.infection import InfectionSelector
from june.world import generate_world_from_hdf5, generate_world_from_geography

geography = Geography.from_file({
    "super_area": [
        "E02003282",
        "E02001720",
        "E00088544",
        "E02002560",
        "E02002559",
        "E02004314",
    ]
})
geography.hospitals = Hospitals.for_geography(geography)
geography.schools = Schools.for_geography(geography)
geography.companies = Companies.for_geography(geography)
geography.care_homes = CareHomes.for_geography(geography)
geography.universities = Universities.for_super_areas(geography.super_areas)
world = generate_world_from_geography(geography,
                                      include_households=True,
                                      include_commute=True)

print("World length", len(world.people))
world.to_hdf5("world.hdf5")

world = generate_world_from_hdf5("world.hdf5")

# leisure
geography = load_geography_from_hdf5("world.hdf5")
world.cinemas = Cinemas.for_geography(geography)
world.pubs = Pubs.for_geography(geography)
Esempio n. 7
0
def make_dummy_world(geog):
    super_area = geog.super_areas.members[0]
    company = Company(super_area=super_area, n_workers_max=100, sector="Q")

    household1 = Household()
    household1.area = super_area.areas[0]
    hospital = Hospital(
        n_beds=40,
        n_icu_beds=5,
        super_area=super_area.name,
        coordinates=super_area.coordinates,
    )
    uni = University(
        coordinates=super_area.coordinates,
        n_students_max=2500,
    )

    worker1 = Person.from_attributes(age=44,
                                     sex='f',
                                     ethnicity='A1',
                                     socioecon_index=5)
    worker1.area = super_area.areas[0]
    household1.add(worker1, subgroup_type=household1.SubgroupType.adults)
    worker1.sector = "Q"
    company.add(worker1)

    worker2 = Person.from_attributes(age=42,
                                     sex='m',
                                     ethnicity='B1',
                                     socioecon_index=5)
    worker2.area = super_area.areas[0]
    household1.add(worker2, subgroup_type=household1.SubgroupType.adults)
    worker2.sector = "Q"
    company.add(worker2)

    student1 = Person.from_attributes(age=20,
                                      sex='f',
                                      ethnicity='A1',
                                      socioecon_index=5)
    student1.area = super_area.areas[0]
    household1.add(student1, subgroup_type=household1.SubgroupType.adults)
    uni.add(student1)

    pupil1 = Person.from_attributes(age=8,
                                    sex='m',
                                    ethnicity='C1',
                                    socioecon_index=5)
    pupil1.area = super_area.areas[0]
    household1.add(pupil1, subgroup_type=household1.SubgroupType.kids)
    #school.add(pupil1)

    pupil2 = Person.from_attributes(age=5,
                                    sex='f',
                                    ethnicity='A1',
                                    socioecon_index=5)
    pupil2.area = super_area.areas[0]
    household1.add(pupil2, subgroup_type=household1.SubgroupType.kids)
    #school.add(pupil2)

    world = World()
    world.schools = Schools([])
    world.hospitals = Hospitals([hospital])
    world.households = Households([household1])
    world.universities = Universities([uni])
    world.companies = Companies([company])
    world.people = Population([worker1, worker2, student1, pupil1, pupil2])
    world.super_areas = geog.super_areas
    world.areas = geog.areas
    world.cemeteries = Cemeteries()
    cinema = Cinema()
    cinema.coordinates = super_area.coordinates
    world.cinemas = Cinemas([cinema])
    pub = Pub()
    pub.coordinates = super_area.coordinates
    world.pubs = Pubs([pub])

    world.areas[0].people = world.people

    return world