def test__infectivity_for_mild_carriers(self): avg_gamma = transmission.TransmissionGamma.from_file( config_path=paths.configs_path / "tests/transmission/test_transmission_constant.yaml") avg_gamma.update_probability_from_delta_time( avg_gamma.time_at_maximum_infectivity()) true_avg_peak_infectivity = avg_gamma.probability selector = InfectionSelector.from_file( transmission_config_path=paths.configs_path / "tests/transmission/test_transmission_symptoms.yaml") dummy = person.Person(sex="f", age=26) infection = infect_person(person=dummy, selector=selector, max_symptom_tag="mild") ratio = 1.0 / infection.transmission.max_infectiousness max_t = ( infection.transmission.shape - 1) * infection.transmission.scale + infection.transmission.shift infection.update_at_time(max_t) max_prob = ratio * infection.transmission.probability np.testing.assert_allclose( max_prob / true_avg_peak_infectivity, 0.48, rtol=0.05, )
def test__position_max_infectivity(self): selector = InfectionSelector.from_file() dummy = person.Person.from_attributes(sex="f", age=26) infection = infect_person(person=dummy, selector=selector, max_symptom_tag="severe") true_max_t = infection.transmission.time_at_maximum_infectivity() infectivity = [] time_steps = np.linspace(0.0, 30.0, 60) for time in time_steps: infection.transmission.update_probability_from_delta_time( time_from_infection=time) infectivity.append(infection.transmission.probability) max_t = time_steps[np.argmax(np.array(infectivity))] assert max_t == pytest.approx(true_max_t, rel=0.01)
def test__infect_person__gives_them_symptoms_and_transmission(self): selector = InfectionSelector.from_file() victim = person.Person(sex="f", age=26) victim.health_information = HealthInformation() selector.infect_person_at_time(person=victim, time=0.2) assert victim.health_information.infection.start_time == 0.2 assert isinstance( victim.health_information.infection.symptoms, june.infection.symptoms.Symptoms, ) assert isinstance( victim.health_information.infection.transmission, transmission.TransmissionGamma, )
def make_infection_selector(self, health_index_generator: HealthIndexGenerator): if self.infectivity_profile == "xnexp": transmission_path = transmission_configs / "XNExp.yaml" elif self.infectivity_profile == "nature": transmission_path = transmission_configs / "nature.yaml" elif self.infectivity_profile == "correction_nature": transmission_path = transmission_configs / "correction_nature.yaml" elif self.infectivity_profile == "constant": transmission_path = transmission_configs / "TransmissionConstant.yaml" else: raise NotImplementedError() infection_selector = InfectionSelector.from_file( transmission_config_path=transmission_path, health_index_generator=health_index_generator, ) return infection_selector
def test__lognormal_in_maxprob(self): selector = InfectionSelector.from_file( transmission_config_path=paths.configs_path / "tests/transmission/test_transmission_lognormal.yaml") avg_gamma = transmission.TransmissionGamma.from_file( config_path=paths.configs_path / "tests/transmission/test_transmission_constant.yaml") avg_gamma.update_probability_from_delta_time( avg_gamma.time_at_maximum_infectivity()) true_avg_peak_infectivity = avg_gamma.probability dummy = person.Person.from_attributes(sex="f", age=26) norms, maxprobs = [], [] for i in range(1_000): infection = selector.make_infection(time=0.1, person=dummy) norms.append(infection.transmission.max_infectiousness) max_t = infection.transmission.time_at_maximum_infectivity() infection.transmission.update_probability_from_delta_time( time_from_infection=max_t) maxprobs.append(infection.transmission.probability)
def test__substract_information_from_group(): hospital = Hospital(n_beds=None, n_icu_beds=None) person1 = Person.from_attributes() person2 = Person.from_attributes() person3 = Person.from_attributes() person4 = Person.from_attributes() infection_selector = InfectionSelector.from_file() hospital.add(person1, subgroup_type=0) hospital.add(person2, subgroup_type=0) hospital.add(person3, subgroup_type=1) hospital.add(person4, subgroup_type=2) infection_selector.infect_person_at_time(person1, 1) person1.health_information.update_health_status(5, 5) person3.susceptibility = 0.0 interactive_group = InteractiveGroup(hospital) assert len(interactive_group.infector_ids) == 1 assert interactive_group.infector_ids[0][0] == person1.id assert (interactive_group.transmission_probabilities[0] == person1.health_information.infection.transmission.probability) assert len(interactive_group.susceptible_ids) == 2 assert interactive_group.susceptible_ids[0][0] == person2.id assert interactive_group.susceptible_ids[1][0] == person4.id
def test__avg_peak_value(self): selector = InfectionSelector.from_file( transmission_config_path=paths.configs_path / "tests/transmission/test_transmission_constant.yaml") dummy = person.Person.from_attributes(sex="f", age=26) infection = infect_person(person=dummy, selector=selector, max_symptom_tag="severe") avg_gamma = transmission.TransmissionGamma.from_file( config_path=paths.configs_path / "tests/transmission/test_transmission_constant.yaml") avg_gamma.update_probability_from_delta_time( avg_gamma.time_at_maximum_infectivity()) true_avg_peak_infectivity = avg_gamma.probability peak_infectivity = [] for i in range(100): infection = selector.make_infection(time=0.1, person=dummy) max_t = infection.transmission.time_at_maximum_infectivity() infection.transmission.update_probability_from_delta_time( time_from_infection=max_t) peak_infectivity.append(infection.transmission.probability) assert np.mean(peak_infectivity) == pytest.approx( true_avg_peak_infectivity, rel=0.05)
def create_selector(): selector = InfectionSelector.from_file() selector.recovery_rate = 0.05 selector.transmission_probability = 0.7 return selector
def test__constant_filename(self): selector = InfectionSelector.from_file( transmission_config_path=paths.configs_path / "defaults/transmission/TransmissionConstant.yaml", ) assert selector.transmission_type == "constant"
def test__defaults_when_no_filename_is_given(self): selector = InfectionSelector.from_file() assert selector.transmission_type == "gamma"
def create_selector(): selector = InfectionSelector.from_file( transmission_config_path=constant_config) selector.recovery_rate = 0.05 selector.transmission_probability = 0.7 return selector
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
def make_selector(): selector_file = ( paths.configs_path / "defaults/transmission/TransmissionConstant.yaml" ) return InfectionSelector.from_file(transmission_config_path=selector_file)
def make_selector(): return InfectionSelector.from_file( transmission_config_path=constant_config)
policies.policies[7].compliance = float(args.mask_compliance) policies.policies[7].beta_factor = float(args.mask_beta_factor) else: policies = Policies.from_file( camp_configs_path / "defaults/policy/home_care_policy.yaml", base_policy_modules=("june.policy", "camps.policy"), ) # ============================================================================# # =================================== infection ===============================# selector = InfectionSelector( health_index_generator=health_index_generator, transmission_config_path=transmission_config_path, ) interaction = Interaction.from_file( config_filename=camp_configs_path / "defaults/interaction/" / args.parameters, population=world.people, ) if args.child_susceptibility: interaction = Interaction.from_file( config_filename=camp_configs_path / "defaults/interaction/ContactInteraction_med_low_low_low_child.yaml", population=world.people, ) else: