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
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
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
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
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
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
def create_big_geography(): g = Geography.from_file( filter_key={ "super_area": [ "E02003282", "E02001720", "E00088544", "E02002560", "E02002559", "E02004314" ] }) return g
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
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'
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
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]
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)
def create_geography(): return Geography.from_file(filter_key={"super_area": ["E02004935"]})
def make_geography(): geography = Geography.from_file({"super_area": ["E02000140"]}) return geography
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)
def make_geography(): geography = Geography.from_file( {"super_area": ["E02003282", "E02002559", "E02006887", "E02003034"]} # {"super_area": ["E02003282", "E02002559"]} ) return geography
def create_geography(): geography = Geography.from_file( {"super_area": ["E02004935", "E02004935", "E02004935"]} ) return geography
def make_dummy_world(): g = Geography.from_file(filter_key={"super_area": ["E02002559"]}) super_area = g.super_areas.members[0] company = Company(super_area=super_area, n_workers_max=100, sector="Q") school = School( coordinates=super_area.coordinates, n_pupils_max=100, age_min=4, age_max=10, sector="primary", ) household = Household() household.area = super_area.areas[0] hospital = Hospital( n_beds=40, n_icu_beds=5, super_area=super_area.name, coordinates=super_area.coordinates, ) worker = Person.from_attributes(age=40) worker.area = super_area household.add(worker, subgroup_type=household.SubgroupType.adults) worker.sector = "Q" company.add(worker) pupil = Person.from_attributes(age=6) pupil.area = super_area household.add(pupil, subgroup_type=household.SubgroupType.kids) household.area = super_area school.add(pupil) student = Person.from_attributes(age=21) student.area = super_area household.add(student, subgroup_type=household.SubgroupType.adults) university = University( coordinates=super_area.coordinates, n_students_max=100, ) university.add(student) commuter = Person.from_attributes(sex="m", age=30) commuter.mode_of_transport = ModeOfTransport(description="bus", is_public=True) commuter.mode_of_transport = "public" household.add(commuter) world = World() world.schools = Schools([school]) world.hospitals = Hospitals([hospital]) world.households = Households([household]) world.universities = Universities([]) world.companies = Companies([company]) world.universities = Universities([university]) world.care_homes = CareHomes([CareHome()]) world.people = Population([worker, pupil, student, commuter]) world.areas = Areas([super_area.areas[0]]) world.areas[0].people = world.people world.super_areas = SuperAreas([super_area]) cinema = Cinema() cinema.coordinates = super_area.coordinates world.cinemas = Cinemas([cinema]) pub = Pub() pub.coordinates = super_area.coordinates world.pubs = Pubs([pub]) grocery = Grocery() grocery.coordinates = super_area.coordinates world.groceries = Groceries([grocery]) # commute city = CommuteCity() hub = CommuteHub(None, None) city.commutehubs = [hub] world.commutehubs = CommuteHubs([city]) world.commutehubs.members = [hub] world.commutecities = CommuteCities() world.commutecities.members = [city] world.commutehubs[0].add(commuter) world.commuteunits = CommuteUnits(world.commutehubs.members) world.commuteunits.init_units() world.commutecityunits = CommuteCityUnits(world.commutecities) world.cemeteries = Cemeteries() return world
def create_geography(worker_super_areas): return Geography.from_file(filter_key={"super_area": worker_super_areas})
def make_super_areas(): geo = Geography.from_file({"super_area": ["E02003353"]}) geo.care_homes = CareHomes.for_geography(geo) world = generate_world_from_geography(geo, include_households=True) return world
def create_geography(): g = Geography.from_file(filter_key={"super_area" : ["E02002559"]}) return g.super_areas.members[0]
def create_area(): g = Geography.from_file(filter_key={"area": ["E00081795"]}, ) return g.areas.members[0]
def create_area(): g = Geography.from_file(filter_key={"super_area": ["E02003353"]}, ) world = generate_world_from_geography(g) return world
def make_geography(): geography = Geography.from_file({"super_area": ["E02002512", "E02001697"]}) return geography
def create_geography(super_area_companies): return Geography.from_file(filter_key={"super_area": [super_area_commute]})
def create_geography_nc(self, super_area_commute_nc): geography = Geography.from_file({"super_area": super_area_commute_nc}) return geography
def get_geography(): geography = Geography.from_file(filter_key={"super_area": SUPER_AREA_LIST}) return geography
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
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")
def test__creating_carehomes_for_geography(self): geography = Geography.from_file( filter_key={"area": ["E00081795", "E00082111"]}) return CareHomes.for_geography(geography)