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
# There are two options implemented in the seed at the moment, either you specify the number of cases and these are then homogeneously distributed by population to the different areas, or you use UK data on cases per region. For now use the first case. seed = InfectionSeed( world.super_areas, 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
# 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) world.groceries = Groceries.for_geography(geography) leisure = generate_leisure_for_config(world, config_filename=config_path) leisure.distribute_social_venues_to_households( world.households) # this assigns possible social venues to people. t2 = time.time() print(f"Took {t2 -t1} seconds to run.") # save the world to hdf5 to load it later print("Saving hdf5...") world.to_hdf5("tests.hdf5") print("Done :)")
np.random.seed(seed) set_seed_numba(seed) random.seed(seed) return set_random_seed() world_file = "./tests.hdf5" config_path = "./config_simulation.yaml" world = generate_world_from_hdf5(world_file, chunk_size=1_000_000) print("World loaded succesfully") # regenerate lesiure leisure = generate_leisure_for_config(world, config_path) # health index and infection selecctor health_index_generator = HealthIndexGenerator.from_file(asymptomatic_ratio=0.2) infection_selector = InfectionSelector.from_file( health_index_generator=health_index_generator) # interaction interaction = Interaction.from_file() # initial infection seeding infection_seed = InfectionSeed( world.super_areas, infection_selector, )
def generate_leisure(self, domain: Domain): leisure = generate_leisure_for_config( domain, self.paths["simulation_config_path"]) return leisure