コード例 #1
0
    def test__save_geography(self, world_h5):
        areas = world_h5.areas
        super_areas = world_h5.super_areas
        geography = Geography(areas, super_areas)
        save_geography_to_hdf5(geography, "test.hdf5")
        geography_recovered = load_geography_from_hdf5("test.hdf5")
        for area, area2 in zip(areas, geography_recovered.areas):
            for attribute_name in ["id", "name"]:
                attribute = getattr(area, attribute_name)
                attribute2 = getattr(area2, attribute_name)
                if attribute is None:
                    assert attribute2 == None
                else:
                    assert attribute == attribute2
            if area.super_area is not None:
                assert area.super_area.id == area2.super_area
            else:
                assert area2.super_area is None
            assert area.coordinates[0] == area2.coordinates[0]
            assert area.coordinates[1] == area2.coordinates[1]

        for super_area, super_area2 in zip(
            super_areas, geography_recovered.super_areas
        ):
            for attribute_name in ["id", "name"]:
                attribute = getattr(super_area, attribute_name)
                attribute2 = getattr(super_area2, attribute_name)
                if attribute is None:
                    assert attribute2 == None
                else:
                    assert attribute == attribute2
            assert super_area.coordinates[0] == super_area2.coordinates[0]
            assert super_area.coordinates[1] == super_area2.coordinates[1]
コード例 #2
0
ファイル: test_box.py プロジェクト: sadielbartholomew/JUNE
def make_population():
    geography = Geography.from_file({"area": ["E00062194"]})
    demography = Demography.for_geography(geography)
    population = Population()
    for area in geography.areas:
        population.people += demography.populate(area.name)
    return population
コード例 #3
0
def test__generate_leisure_from_world():
    geography = Geography.from_file({"super_area": ["E02002135"]})
    world = generate_world_from_geography(geography,
                                          include_households=True,
                                          include_commute=False)
    world.pubs = Pubs.for_geography(geography)
    world.cinemas = Cinemas.for_geography(geography)
    world.groceries = Groceries.for_geography(geography)
    person = Person.from_attributes(sex="m", age=27)
    household = Household()
    household.area = world.areas[0]
    household.add(person)
    person.area = geography.areas[0]
    leisure = generate_leisure_for_world(
        list_of_leisure_groups=["pubs", "cinemas", "groceries"], world=world)
    leisure.distribute_social_venues_to_households([household])
    leisure.generate_leisure_probabilities_for_timestep(0.1, False)
    n_pubs = 0
    n_cinemas = 0
    n_groceries = 0
    for _ in range(0, 1000):
        subgroup = leisure.get_subgroup_for_person_and_housemates(person)
        if subgroup is not None:
            if subgroup.group.spec == "pub":
                n_pubs += 1
            elif subgroup.group.spec == "cinema":
                n_cinemas += 1
            elif subgroup.group.spec == "grocery":
                n_groceries += 1
    assert 0 < n_pubs < 100
    assert 0 < n_cinemas < 100
    assert 0 < n_groceries < 107
コード例 #4
0
def test__infection_is_isolated(selector):
    geography = Geography.from_file({"area": ["E00002559"]})
    world = generate_world_from_geography(geography, include_households=True)
    interaction = Interaction.from_file()
    infection_seed = InfectionSeed(world.super_areas, selector)
    n_cases = 5
    infection_seed.unleash_virus(
        n_cases)  # play around with the initial number of cases
    policies = Policies([])
    simulator = Simulator.from_file(
        world=world,
        interaction=interaction,
        infection_selector=selector,
        config_filename=pathlib.Path(__file__).parent.absolute() /
        "interaction_test_config.yaml",
        leisure=None,
        policies=policies,
        save_path=None,
    )
    infected_people = [person for person in world.people if person.infected]
    assert len(infected_people) == 5
    infected_households = []
    for household in world.households:
        infected = False
        for person in household.people:
            if person.infected:
                infected = True
                break
        if infected:
            infected_households.append(household)
    assert len(infected_households) <= 5
    simulator.run()
    for person in world.people:
        if not (person.residence.group in infected_households):
            assert not person.infected and person.susceptible
コード例 #5
0
 def test__demography_for_areas(self):
     geography = Geography.from_file({"area": ["E00088544"]})
     area = list(geography.areas)[0]
     demography = d.Demography.for_areas(area_names=[area.name])
     area.populate(demography)
     population = area.people
     assert len(population) == 362
     people_ages_dict = {}
     people_sex_dict = {}
     for person in population:
         if person.age == 0:
             assert person.sex == "f"
         if person.age > 90:
             assert person.sex == "f"
         if person.age == 21:
             assert person.sex == "m"
         if person.age in range(55, 69):
             assert person.ethnicity.startswith("A")
         assert person.ethnicity.startswith("D") is False
         assert person.socioecon_index == 6
         if person.age not in people_ages_dict:
             people_ages_dict[person.age] = 1
         else:
             people_ages_dict[person.age] += 1
         if person.sex not in people_sex_dict:
             people_sex_dict[person.sex] = 1
         else:
             people_sex_dict[person.sex] += 1
     assert people_ages_dict[0] == 6
     assert people_ages_dict[71] == 3
     assert max(people_ages_dict.keys()) == 90
コード例 #6
0
def test__seed_with_age_profile(selector, ):
    geography = Geography.from_file(filter_key={"super_area": ["E02004940"]})
    demography = Demography.for_geography(geography)
    for area in geography.areas:
        area.populate(demography)

    seed = InfectionSeed(super_areas=geography.super_areas,
                         selector=selector,
                         age_profile={
                             '0-9': 0.,
                             '10-39': 1.,
                             '40-100': 0.
                         })

    seed.unleash_virus(100)

    should_not_infected = [
        person for super_area in geography.super_areas
        for person in super_area.people
        if person.infected and (person.age < 10 or person.age >= 40)
    ]

    assert len(should_not_infected) == 0

    should_infected = [
        person for super_area in geography.super_areas
        for person in super_area.people
        if person.infected and (person.age >= 10 and person.age < 40)
    ]

    assert len(should_infected) == 100
コード例 #7
0
def make_geography(young_medic, old_medic):
    geography = Geography.from_file({"super_area": ["E02003999", "E02006764"]})
    for _ in range(200):
        geography.super_areas.members[0].add_worker(young_medic)
        geography.super_areas.members[0].areas[0].add(young_medic)
    for _ in range(200):
        geography.super_areas.members[0].add_worker(old_medic)
        geography.super_areas.members[0].areas[0].add(old_medic)
    return geography
コード例 #8
0
def create_big_geography():
    g = Geography.from_file(
        filter_key={
            "super_area": [
                "E02003282", "E02001720", "E00088544", "E02002560",
                "E02002559", "E02004314"
            ]
        })
    return g
コード例 #9
0
def test__social_venue_from_coordinates():
    super_areas = ["E02004935", "E02004940"]
    geo = Geography.from_file({"super_area": super_areas})
    coordinate_list = np.array([[51.752179, -0.334667], [51.741485,
                                                         -0.336645]])
    social_venues = SocialVenues.from_coordinates(coordinate_list, geo.areas)
    social_venues.add_to_super_areas(geo.super_areas)
    assert len(social_venues) == 2
    assert social_venues[0].super_area == geo.super_areas[0]
    assert social_venues[1].super_area == geo.super_areas[1]
コード例 #10
0
ファイル: test_world.py プロジェクト: sadielbartholomew/JUNE
def test__onearea_world(geography):
    geography = Geography.from_file(filter_key={"area": ["E00088544"]})
    world = generate_world_from_geography(geography)
    assert hasattr(world, "households")
    assert isinstance(world.households, Households)
    assert len(world.areas) == 1
    assert len(world.super_areas) == 1
    assert world.super_areas.members[0].name == "E02003616"
    assert len(world.areas.members[0].people) == 362
    assert len(world.households) <= 148
コード例 #11
0
def test__initialize_hospitals_from_geography():
    geography = Geography.from_file({"super_area": ["E02003282", "E02005560"]})
    hospitals = Hospitals.for_geography(geography)
    assert len(hospitals.members) == 2
    assert hospitals.members[0].n_beds + hospitals.members[
        0].n_icu_beds == 468 + 41
    assert hospitals.members[0].super_area == 'E02005560'
    assert hospitals.members[1].n_beds + hospitals.members[
        1].n_icu_beds == 2115 + 296
    assert hospitals.members[1].trust_code == 'RAJ'
コード例 #12
0
 def test__comorbidities_for_areas(self):
     geography = Geography.from_file({"area": ["E00088544"]})
     area = list(geography.areas)[0]
     demography = d.Demography.for_areas(area_names=[area.name])
     area.populate(demography)
     comorbidities = []
     for person in area.people:
         if person.comorbidity is not None:
             comorbidities.append(person.comorbidity)
     assert len(np.unique(comorbidities)) > 0
コード例 #13
0
ファイル: world.py プロジェクト: sadielbartholomew/JUNE
    def to_hdf5(self, file_path: str, chunk_size=100000):
        """
        Saves the world to an hdf5 file. All supergroups and geography
        are stored as groups. Class instances are substituted by ids of the 
        instances. To load the world back, one needs to call the
        generate_world_from_hdf5 function.

        Parameters
        ----------
        file_path
            path of the hdf5 file
        chunk_size
            how many units of supergroups to process at a time.
            It is advise to keep it around 1e5
        """
        # empty file
        with h5py.File(file_path, "w"):
            pass
        geo = Geography(self.areas, self.super_areas)
        save_geography_to_hdf5(geo, file_path)
        save_population_to_hdf5(self.people, file_path, chunk_size)
        if self.hospitals is not None:
            save_hospitals_to_hdf5(self.hospitals, file_path, chunk_size)
        if self.schools is not None:
            save_schools_to_hdf5(self.schools, file_path, chunk_size)
        if self.companies is not None:
            save_companies_to_hdf5(self.companies, file_path, chunk_size)
        if self.households is not None:
            save_households_to_hdf5(self.households, file_path, chunk_size)
        if self.care_homes is not None:
            save_care_homes_to_hdf5(self.care_homes, file_path, chunk_size)
        if self.commutecities is not None:
            save_commute_cities_to_hdf5(self.commutecities, file_path)
        if self.commutehubs is not None:
            save_commute_hubs_to_hdf5(self.commutehubs, file_path)
        if self.universities is not None:
            save_universities_to_hdf5(self.universities, file_path)
        if self.pubs is not None:
            save_pubs_to_hdf5(self.pubs, file_path)
        if self.cinemas is not None:
            save_cinemas_to_hdf5(self.cinemas, file_path)
        if self.groceries is not None:
            save_groceries_to_hdf5(self.groceries, file_path)
コード例 #14
0
def load_geography_from_hdf5(file_path: str, chunk_size=50000):
    """
    Loads geography 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:
        geography = f["geography"]
        n_areas = geography.attrs["n_areas"]
        area_list = []
        n_super_areas = geography.attrs["n_super_areas"]
        # areas
        n_chunks = int(np.ceil(n_areas / chunk_size))
        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_areas)
            ids = geography["area_id"][idx1:idx2]
            names = geography["area_name"][idx1:idx2]
            super_areas = geography["area_super_area"][idx1:idx2]
            area_coordinates = geography["area_coordinates"][idx1:idx2]
            for k in range(idx2 - idx1):
                area = Area(names[k].decode(), super_areas[k], area_coordinates[k])
                area.id = ids[k]
                area_list.append(area)
        # super areas
        super_area_list = []
        n_chunks = int(np.ceil(n_super_areas / chunk_size))
        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_super_areas)
            ids = geography["super_area_id"][idx1:idx2]
            names = geography["super_area_name"][idx1:idx2]
            super_area_coordinates = geography["super_area_coordinates"][idx1:idx2]
            for k in range(idx2 - idx1):
                super_area = SuperArea(
                    names[k].decode(), None, super_area_coordinates[k]
                )
                super_area.id = ids[k]
                super_area_list.append(super_area)
    areas = Areas(area_list)
    super_areas = SuperAreas(super_area_list)
    return Geography(areas, super_areas)
コード例 #15
0
def test__seed_strength(selector, ):
    geography = Geography.from_file(
        filter_key={"super_area": [
            "E02004940",
            "E02004935",
            "E02004936",
        ]})
    demography = Demography.for_geography(geography)
    for area in geography.areas:
        area.populate(demography)
    n_cases_region = pd.DataFrame({
        'East of England':
        np.array([100, 200, 300, 400]).astype(np.int),
        'date': [
            '2020-03-01',
            '2020-03-02',
            '2020-03-03',
            '2020-03-04',
        ]
    })
    n_cases_region.set_index('date', inplace=True)
    n_cases_region.index = pd.to_datetime(n_cases_region.index)
    seed = InfectionSeed.from_file(super_areas=geography.super_areas,
                                   selector=selector,
                                   n_cases_region=n_cases_region,
                                   seed_strength=0.2)
    timer = Timer(
        initial_day='2020-02-29',
        total_days=7,
    )
    while timer.date <= timer.final_date:
        time = timer.date
        if (time >= seed.min_date) and (time <= seed.max_date):
            seed.unleash_virus_per_region(time)
        next(timer)
    n_infected = 0
    for super_area in geography.super_areas:
        for person in super_area.people:
            if person.infected:
                n_infected += 1
    n_cases = (seed.n_cases_region["East of England"].sum())
    np.testing.assert_allclose(0.2 * n_cases, n_infected, rtol=0.05)
コード例 #16
0
def create_geography():
    g = Geography.from_file(filter_key={"super_area" : ["E02002559"]})
    return g.super_areas.members[0]
コード例 #17
0
def create_geography():
    return Geography.from_file(filter_key={"super_area": ["E02004935"]})
コード例 #18
0
def create_area():
    g = Geography.from_file(filter_key={"super_area": ["E02003353"]}, )
    world = generate_world_from_geography(g)
    return world
コード例 #19
0
def create_geography():
    geography = Geography.from_file(
        {"super_area": ["E02004935", "E02004935", "E02004935"]}
    )
    return geography
コード例 #20
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
コード例 #21
0
 def create_geography_nc(self, super_area_commute_nc):
     geography = Geography.from_file({"super_area": super_area_commute_nc})
     return geography
コード例 #22
0
def create_geography(super_area_companies):
    return Geography.from_file(filter_key={"super_area": [super_area_commute]})
コード例 #23
0
 def test__creating_carehomes_for_geography(self):
     geography = Geography.from_file(
         filter_key={"area": ["E00081795", "E00082111"]})
     return CareHomes.for_geography(geography)
コード例 #24
0
ファイル: quickstart.py プロジェクト: valeriupredoi/june
from june.groups import Hospitals, Schools, Companies, Households, CareHomes, Cemeteries, Universities
from june.groups.leisure import generate_leisure_for_config, Cinemas, Pubs, Groceries
from june.simulator import Simulator
from june.infection_seed import InfectionSeed
from june.policy import Policy, Policies
from june import paths
from june.hdf5_savers import load_geography_from_hdf5
from june.logger.read_logger import ReadLogger
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")
コード例 #25
0
def make_geography():
    geography = Geography.from_file(
        {"super_area": ["E02003282", "E02002559", "E02006887", "E02003034"]}
        # {"super_area": ["E02003282", "E02002559"]}
    )
    return geography
コード例 #26
0
from june.world import generate_world_from_hdf5, generate_world_from_geography
import pickle
import sys
import time
import numpy as np

# load london super areas
london_areas = np.loadtxt("./london_areas.txt", dtype=np.str_)

t1 = time.time()

# default config path
config_path = "../configs/config_example.yaml"

# define geography, let's run the first 20 super areas of london
geography = Geography.from_file({"super_area": london_areas[40:60]})

# add buildings
geography.hospitals = Hospitals.for_geography(geography)
geography.companies = Companies.for_geography(geography)
geography.schools = Schools.for_geography(geography)
geography.universities = Universities.for_super_areas(geography.super_areas)
geography.care_homes = CareHomes.for_geography(geography)
# generate world
world = generate_world_from_geography(geography,
                                      include_households=True,
                                      include_commute=True)

# some leisure activities
world.pubs = Pubs.for_geography(geography)
world.cinemas = Cinemas.for_geography(geography)
コード例 #27
0
def get_geography():

    geography = Geography.from_file(filter_key={"super_area": SUPER_AREA_LIST})
    return geography
コード例 #28
0
def create_area():
    g = Geography.from_file(filter_key={"area": ["E00081795"]}, )
    return g.areas.members[0]
コード例 #29
0
def create_geography(worker_super_areas):
    return Geography.from_file(filter_key={"super_area": worker_super_areas})
コード例 #30
0
def make_geography():
    geography = Geography.from_file({"super_area": ["E02000140"]})
    return geography