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 generate_simulator_from_checkpoint( world: World, checkpoint_path: str, interaction: Interaction, chunk_size: Optional[int] = 50000, infection_selector: Optional[InfectionSelector] = None, policies: Optional[Policies] = None, infection_seed: Optional[InfectionSeed] = None, leisure: Optional[Leisure] = None, travel: Optional[Travel] = None, config_filename: str = default_config_filename, record: "Record" = None, # comment: Optional[str] = None, ): simulator = Simulator.from_file( world=world, interaction=interaction, infection_selector=infection_selector, policies=policies, infection_seed=infection_seed, leisure=leisure, travel=travel, config_filename=config_filename, record=record, # comment=comment, ) return restore_simulator_to_checkpoint(world=world, checkpoint_path=checkpoint_path, chunk_size=chunk_size, simulator=simulator)
def test__full_run(dummy_world, selector): world = dummy_world # restore health status of people for person in world.people: person.health_information = None person.susceptibility = 1.0 person.dead = False leisure_instance = leisure.generate_leisure_for_world( world=world, list_of_leisure_groups=[ "pubs", "cinemas", "groceries", "household_visits", "care_home_visits", ], ) leisure_instance.distribute_social_venues_to_households(world.households) interaction = Interaction.from_file() policies = Policies.from_file() sim = Simulator.from_file( world=world, interaction=interaction, infection_selector=selector, config_filename=test_config, leisure=leisure_instance, policies=policies, save_path=None, ) seed = InfectionSeed(sim.world.super_areas, selector) seed.unleash_virus(1) sim.run()
def make_policy_simulator(dummy_world, interaction, selector): config_name = paths.configs_path / "tests/test_simulator_simple.yaml" sim = Simulator.from_file( dummy_world, interaction, infection_selector=selector, config_filename=config_name, save_path=None, policies=None, leisure=None, ) return sim
def setup_sim(dummy_world, selector): world = dummy_world leisure_instance = leisure.generate_leisure_for_world( world=world, list_of_leisure_groups=["pubs", "cinemas", "groceries"]) leisure_instance.distribute_social_venues_to_households(world.households) interaction = Interaction.from_file() policies = Policies.from_file() sim = Simulator.from_file( world=world, infection_selector=selector, interaction=interaction, config_filename=test_config, leisure=leisure_instance, policies=policies, ) sim.activity_manager.leisure.generate_leisure_probabilities_for_timestep( 3, False) return sim
def generate_simulator(self): domain = self.generate_domain() health_index_generator = self.generate_health_index_generator() infection_selector = self.generate_infection_selector( health_index_generator=health_index_generator) interaction = self.generate_interaction( baseline_interaction_path=self.paths["baseline_interaction_path"], population=domain.people, ) leisure = self.generate_leisure(domain=domain) travel = self.generate_travel() policies = self.generate_policies() record = self.generate_record() record.static_data(world=domain) infection_seed = self.generate_infection_seed( world=domain, infection_selector=infection_selector, ) record.meta_information( comment=self.purpose_of_the_run, random_state=self.random_seed, number_of_cores=mpi_size, ) simulator = Simulator.from_file( world=domain, interaction=interaction, config_filename=self.paths["simulation_config_path"], leisure=leisure, travel=travel, infection_seed=infection_seed, infection_selector=infection_selector, policies=policies, record=record, ) # change number of days, this can only be done like this for now simulator.timer.total_days = self.n_days simulator.timer.final_date = simulator.timer.initial_date + datetime.timedelta( days=self.n_days) return simulator
def run_simulator(selector): world = create_world() world.to_hdf5("./checkpoint_world.hdf5") # restore health status of people for person in world.people: person.health_information = None person.susceptibility = 1.0 person.dead = False interaction = Interaction.from_file() policies = Policies([]) sim = Simulator.from_file( world=world, interaction=interaction, infection_selector=selector, config_filename=test_config, leisure=None, policies=policies, save_path="tests", ) seed = InfectionSeed(sim.world.super_areas, selector) seed.unleash_virus(20) sim.run() return sim
def create_sim(world, interaction, selector): leisure_instance = leisure.generate_leisure_for_config( world=world, config_filename=test_config) leisure_instance.distribute_social_venues_to_households(world.households) policies = Policies([ Quarantine(n_days=5), Quarantine(n_days=10), CloseLeisureVenue(start_time="2020-3-1", end_time="2020-3-30", venues_to_close=['pub', 'cinema']) ]) infection_seed = InfectionSeed(super_areas=world.super_areas, selector=selector) n_cases = 2 infection_seed.unleash_virus(n_cases) sim = Simulator.from_file(world=world, interaction=interaction, infection_selector=selector, config_filename=test_config, leisure=leisure_instance, policies=policies) return sim
def simulation(args): gf.print_flush(args) print("Physical cores:", psutil.cpu_count(logical=False)) print("Total cores:", psutil.cpu_count(logical=True)) print("=" * 20, "Memory Information", "=" * 20) # get the memory details svmem = psutil.virtual_memory() print(f"Total: {get_size(svmem.total)}") print(f"Available: {get_size(svmem.available)}") print(f"Used: {get_size(svmem.used)}") print(f"Percentage: {svmem.percent}%") pid = os.getpid() py = psutil.Process(pid) memoryUse = py.memory_info()[0] # initialise world from file gf.print_flush("Initialising world...") world_file = "{}.hdf5".format(args.world) world = generate_world_from_hdf5(world_file, chunk_size=1_000_000) gf.print_flush("World loaded successfully...") geography = load_geography_from_hdf5(world_file) # leisure gf.print_flush("Initialising leisure...") world.pubs = Pubs.for_geography(geography) world.cinemas = Cinemas.for_geography(geography) world.groceries = Groceries.for_super_areas(geography.super_areas) # cemeteries gf.print_flush("Initialising cemeteries...") world.cemeteries = Cemeteries() # commute gf.print_flush("Initialising commute...") world.initialise_commuting() # infection selector gf.print_flush("Selecting infection...") selector = InfectionSelector.from_file() interaction = ContactAveraging.from_file(selector=selector) lhs_array = np.load("lhs_array.npy") parameters = generate_parameters_from_lhs(lhs_array, args.idx) interaction = set_interaction_parameters(parameters, interaction) gf.print_flush("Betas = {}, alpha = {}".format(interaction.beta, interaction.alpha_physical)) if not os.path.exists(SAVE_PATH): os.makedirs(SAVE_PATH) # save out parameters for later with open(SAVE_PATH + '/parameters.json', 'w') as f: json.dump(parameters, f) # seed infections seed = Seed.from_file( super_areas=world.super_areas, selector=selector, ) print(f"Memory used by JUNE's world: {get_size(memoryUse)}") simulator = Simulator.from_file(world, interaction, selector, seed=seed, config_filename=CONFIG_PATH, save_path=SAVE_PATH) simulator.run() # read = ReadLogger(SAVE_PATH) # world_df = read.world_summary() # ages_df = read.age_summary([0,10,20,30,40, # 50,60,70,80,90,100]) # loc_df = read.get_locations_infections() # r_df = read.get_r() # world_df.to_csv(SAVE_PATH + '/world_df.csv') # ages_df.to_csv(SAVE_PATH + '/ages_df.csv') # loc_df.to_csv(SAVE_PATH + '/loc_df.csv') # r_df.to_csv(SAVE_PATH + '/r_df.csv') gf.print_flush("Simulation finished!!!!") return None
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
def generate_simulator(): record = Record(record_path=save_path, record_static_data=True, mpi_rank=mpi_rank) if mpi_rank == 0: with h5py.File(world_file, "r") as f: super_area_names = f["geography"]["super_area_name"][:] super_area_ids = f["geography"]["super_area_id"][:] super_area_names = [name.decode() for name in super_area_names] super_area_name_to_id = { key: value for key, value in zip(super_area_names, super_area_ids) } # make dictionary super_area_id -> domain domain_splitter = DomainSplitter(number_of_domains=mpi_size, world_path=world_file) super_areas_per_domain = domain_splitter.generate_domain_split( niter=20) super_area_names_to_domain_dict = {} super_area_ids_to_domain_dict = {} for domain, super_areas in super_areas_per_domain.items(): for super_area in super_areas: super_area_names_to_domain_dict[super_area] = domain super_area_ids_to_domain_dict[int( super_area_name_to_id[super_area])] = domain with open("super_area_ids_to_domain.json", "w") as f: json.dump(super_area_ids_to_domain_dict, f) with open("super_area_names_to_domain.json", "w") as f: json.dump(super_area_names_to_domain_dict, f) mpi_comm.Barrier() if mpi_rank > 0: with open("super_area_ids_to_domain.json", "r") as f: super_area_ids_to_domain_dict = json.load(f, object_hook=keys_to_int) domain = Domain.from_hdf5( domain_id=mpi_rank, super_areas_to_domain_dict=super_area_ids_to_domain_dict, hdf5_file_path=world_file, ) record.static_data(world=domain) # regenerate lesiure leisure = generate_leisure_for_config(domain, config_path) # # health index and infection selecctor health_index_generator = HealthIndexGenerator.from_file() infection_selector = InfectionSelector( health_index_generator=health_index_generator) oc = Observed2Cases.from_file( health_index_generator=health_index_generator, smoothing=True) daily_cases_per_region = oc.get_regional_latent_cases() daily_cases_per_super_area = oc.convert_regional_cases_to_super_area( daily_cases_per_region, dates=["2020-02-28", "2020-03-02"]) infection_seed = InfectionSeed( world=domain, infection_selector=infection_selector, daily_super_area_cases=daily_cases_per_super_area, seed_strength=100, ) # interaction interaction = Interaction.from_file( config_filename="./config_interaction.yaml", population=domain.people) # policies policies = Policies.from_file() # events events = Events.from_file() # create simulator travel = Travel() simulator = Simulator.from_file( world=domain, policies=policies, events=events, interaction=interaction, leisure=leisure, travel=travel, infection_selector=infection_selector, infection_seed=infection_seed, config_filename=config_path, record=record, ) print("simulator ready to go") return simulator
selector, ) n_cases = 50 seed.unleash_virus(n_cases) # play around with the initial number of cases # # Set policies policies = Policies.from_file() # # Run the simulation # Since the timer configuration is a bit cumbersome, it is read from the config file at ``configs/config_example.yml CONFIG_PATH = "configs/config_example.yaml" leisure = generate_leisure_for_config(world=world, config_filename=CONFIG_PATH) simulator = Simulator.from_file(world, interaction, selector, config_filename=CONFIG_PATH, leisure=leisure, policies=policies) # Run the simulator simulator.run() # While the simulation runs (and afterwards) we can launch the visualization webpage by running # ```python june/visualizer.py path/to/results``` # # Getting the results # All results are stored in a json file specified in the ``save_path`` variable in the config file. We can also access it from ``world.logger`` directly. import pandas as pd read = ReadLogger()
t2 = time.time() print(f"Creating the world took {t2 -t1} seconds to run.") # *********** INITIALIZE SEED ***************** # symptoms = SymptomsConstant(recovery_rate=0.05) transmission = TransmissionConstant(probability=0.3) infection = Infection(transmission, symptoms) interaction = DefaultInteraction.from_file() seed = Seed(world.boxes, infection, ) # Use if you want to seed the disease per region # with real data #seed.unleash_virus_per_region() seed.unleash_virus(100, box_mode=True) # *********** INITIALIZE SIMULATOR ***************** # simulator = Simulator.from_file( world, interaction, infection, config_filename=args.config ) # update health status of seeded people, depending on symptoms # class might be unnecessary #simulator.update_health_status(0,0) simulator.run() t3 = time.time() print(f"Running the simulation took {t3 -t2} seconds to run.") simulator.logger.plot_infection_curves_per_day()
# initial infection seeding infection_seed = InfectionSeed( world.super_areas, infection_selector, ) infection_seed.unleash_virus(50) # number of initial cases # policies policies = Policies.from_file() # create simulator simulator = Simulator.from_file( world=world, policies=policies, interaction=interaction, leisure=leisure, infection_selector=infection_selector, config_filename=config_path, save_path="results", ) print("simulator ready to go") t1 = time.time() simulator.run() t2 = time.time() print(f" Simulation took {t2-t1} seconds")
communals=world.communals ) leisure_instance.leisure_distributors[ "female_communals" ] = FemaleCommunalDistributor.from_config(female_communals=world.female_communals) # associate social activities to shelters leisure_instance.distribute_social_venues_to_households(world.shelters) # ==================================================================================# # =================================== simulator ===============================# Simulator.ActivityManager = CampActivityManager simulator = Simulator.from_file( world=world, interaction=interaction, leisure=leisure_instance, policies=policies, config_filename=CONFIG_PATH, infection_selector=selector, save_path="results_no_comorbidities" ) leisure_instance.leisure_distributors simulator.timer.reset() simulator.run() # ==================================================================================#
leisure.distribute_social_venues_to_areas(world.areas, world.super_areas) # ==================================================================================# # =================================== simulator ===============================# # records record = Record(record_path=args.save_path, record_static_data=True) record.static_data(world=world) Simulator.ActivityManager = CampActivityManager simulator = Simulator.from_file( world=world, interaction=interaction, leisure=leisure, policies=policies, config_filename=CONFIG_PATH, infection_selector=selector, record=record, ) leisure.leisure_distributors simulator.timer.reset() simulator.run() # ==================================================================================# # =================================== read logger ===============================#