示例#1
0
def test_createInfectedPersons(helpers):
    """
    #create population with 5 people ages [9,19,29,39,49]
    #test that each day one of them is getting immuned
    """
    helpers.clean_outputs()
    #Editig confige file saving is nessery 
    config_path = os.path.join(os.path.dirname(__file__),"..","src","config.json")
    ConfigData = None
    
    #reading the confige file 
    with open(config_path) as json_data_file:
        ConfigData = json.load(json_data_file)
        paramsDataPath = ConfigData['ParamsFilePath']
    Params.load_from(os.path.join(os.path.dirname(__file__),"..","src", paramsDataPath), override=True)

    persons_arr = list(map(Person, [9,19,29,39,49,59]))
    assert len(persons_arr) == 6
    env_arr = []
    my_world = World(
        all_people = persons_arr,
        all_environments=env_arr,
        generating_city_name = "test",
        generating_scale = 1)

    my_simulation = Simulation(world = my_world, initial_date= INITIAL_DATE)
    my_simulation.run_simulation(7,"test_simulation",datas_to_plot = None,extensionsList = ["ImmuneByAgeExtension","EmptyExtension"] )
    cnt = sum([1 for p in persons_arr if p.get_disease_state() == DiseaseState.IMMUNE])
    assert cnt == 4
    
示例#2
0
    def create_and_run_simulation(self,
                                  outdir,
                                  stop_early,
                                  with_population_caching=True,
                                  verbosity=False):
        """
        The main function that handles the run of the simulation by the task.
        It updated the params changes, loads or creates the population, initializes the simulation and runs it.
        :param outdir: the output directory for the task
        :param stop_early: only relevant to R computation, see Simulation doc
        :param with_population_caching: bool, if False generates the population, else - tries to use the cache and save time.
        :param verbosity: bool, if it's True then additional output logs will be printed to the screen
        """
        seed.set_random_seed()
        config_path = os.path.join(os.path.dirname(__file__), "config.json")
        with open(config_path) as json_data_file:
            ConfigData = json.load(json_data_file)
            citiesDataPath = ConfigData['CitiesFilePath']
            paramsDataPath = ConfigData['ParamsFilePath']
            Extensionslst = ConfigData['ExtensionsNamelst']
        Params.load_from(os.path.join(os.path.dirname(__file__),
                                      paramsDataPath),
                         override=True)
        for param, val in self.params_to_change.items():
            Params.loader()[param] = val
        DiseaseState.init_infectiousness_list()

        citiesDataPath = citiesDataPath

        population_loader = PopulationLoader(
            citiesDataPath,
            added_description=Params.loader().description(),
            with_caching=with_population_caching,
            verbosity=verbosity)

        world = population_loader.get_world(city_name=self.city_name,
                                            scale=self.scale,
                                            is_smart=True)

        ExtensionType = None

        sim = Simulation(world,
                         self.initial_date,
                         self.interventions,
                         verbosity=verbosity,
                         outdir=outdir,
                         stop_early=stop_early)
        self.infection_params.infect_simulation(sim, outdir)
        if len(Extensionslst) > 0:
            sim.run_simulation(self.days,
                               self.scenario_name,
                               datas_to_plot=self.datas_to_plot,
                               extensionsList=Extensionslst)
        else:
            sim.run_simulation(self.days,
                               self.scenario_name,
                               datas_to_plot=self.datas_to_plot,
                               extensionsList=None)
示例#3
0
def test_CreateDeltaFile(helpers):
    helpers.clean_outputs()
    config_path = os.path.join(os.path.dirname(__file__), "..", "src",
                               "config.json")
    with open(config_path) as json_data_file:
        ConfigData = json.load(json_data_file)
        paramsDataPath = ConfigData['ParamsFilePath']
    Params.load_from(os.path.join(os.path.dirname(__file__), "..", "src",
                                  paramsDataPath),
                     override=True)

    ageList = [random.randint(0, 40) for i in range(10)]
    PersonList = list(map(Person, ageList))
    events_acc = []
    for person in PersonList:
        states_table = ((DiseaseState.LATENT, daysdelta(3)),
                        (DiseaseState.ASYMPTOMATICINFECTIOUS,
                         daysdelta(3)), (DiseaseState.IMMUNE, daysdelta(3)),
                        (DiseaseState.IMMUNE, None))
        events = person.gen_and_register_events_from_seir_times(
            date=INITIAL_DATE, states_and_times=states_table)
        events_acc += events
        # person.set_disease_state(DiseaseState.LATENT)
    env_arr = []
    my_world = World(all_people=PersonList,
                     all_environments=env_arr,
                     generating_city_name="test",
                     generating_scale=1)

    my_simulation = Simulation(world=my_world, initial_date=INITIAL_DATE)
    my_simulation.register_events(events_acc)
    my_simulation.run_simulation(num_days=10, name="test")
    #assert events dictionary is not empty
    txt = my_simulation.stats.get_state_stratified_summary_table(
        table_format=TableFormat.CSV)
    test_data = StringIO(txt)
    tbl = pd.read_csv(test_data)
    assert len(tbl) == 7

    print(tbl)

    assert tbl.iloc[0, DiseaseState.SUSCEPTIBLE.value] == 10
    assert tbl.iloc[3, DiseaseState.ASYMPTOMATICINFECTIOUS.value] == 10
    assert tbl.iloc[6, DiseaseState.IMMUNE.value] == 10
示例#4
0
def test_SymptomaticIsolationIntervention_Genarete_events(helpers):
    #pretesting
    helpers.clean_outputs()

    config_path = os.path.join(os.path.dirname(__file__), "..", "src",
                               "config.json")
    with open(config_path) as json_data_file:
        ConfigData = json.load(json_data_file)
        paramsDataPath = ConfigData['ParamsFilePath']
    Params.load_from(os.path.join(os.path.dirname(__file__), "..", "src",
                                  paramsDataPath),
                     override=True)

    my_intervention = SymptomaticIsolationIntervention(compliance=1,
                                                       start_date=INITIAL_DATE,
                                                       duration=daysdelta(40))
    assert my_intervention is not None

    persons_arr = list(map(Person, [10, 20, 30]))
    assert len(persons_arr) == 3
    env_arr = []
    small_world = World(all_people=persons_arr,
                        all_environments=env_arr,
                        generating_city_name="test",
                        generating_scale=1)

    my_simulation = Simulation(world=small_world,
                               initial_date=INITIAL_DATE,
                               interventions=[my_intervention])

    #test
    lst = my_intervention.generate_events(small_world)
    #Assert results
    assert lst is not None
    assert len(lst) == 3
    for i in range(1):
        assert isinstance(lst[i], DayEvent)
    for person in persons_arr:
        assert len(list(person.state_to_events.keys())) == (1 + 4)

    my_simulation.run_simulation(name="test", num_days=60)
示例#5
0
def test_CreateDeltaFileAtlit(helpers):
    helpers.clean_outputs()
    config_path = os.path.join(os.path.dirname(__file__), "..", "src",
                               "config.json")
    with open(config_path) as json_data_file:
        ConfigData = json.load(json_data_file)
        citiesDataPath = ConfigData['CitiesFilePath']
        paramsDataPath = ConfigData['ParamsFilePath']
    Params.load_from(os.path.join(os.path.dirname(__file__), "..", "src",
                                  paramsDataPath),
                     override=True)
    Params.loader()["person"]["state_macine_type"] = "SIR"

    DiseaseState.init_infectiousness_list()
    pop = population_loader.PopulationLoader(citiesDataPath)
    my_world = pop.get_world(city_name='Atlit', scale=1, is_smart=False)

    sim = Simulation(world=my_world, initial_date=INITIAL_DATE)
    sim.infect_random_set(num_infected=500, infection_doc="")
    sim.run_simulation(num_days=180, name="test")
    #assert events dictionary is not empty
    txt = sim.stats.get_state_stratified_summary_table(
        table_format=TableFormat.CSV)
    test_data = StringIO(txt)

    tbl = pd.read_csv(test_data)
    cnt_start = tbl.iloc[0, DiseaseState.SUSCEPTIBLE.value] + tbl.iloc[
        0, DiseaseState.LATENT.value]
    cnt_end = 0
    for i in range(len(tbl)):
        cnt_end = cnt_end + tbl.iloc[i, DiseaseState.IMMUNE.value] + tbl.iloc[
            i, DiseaseState.DECEASED.value]
    plug_number = len([
        p for p in sim._world.all_people()
        if p.get_disease_state() == DiseaseState.SUSCEPTIBLE
    ])
    assert cnt_start >= cnt_end + plug_number