def test_createInfectedPersonsOredredASCENDING():
    config_path = os.path.join(os.path.dirname(__file__),"..","src","config.json")
    Expected  = -1
    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)

    kids = [0,4,8,12,16]    
    adults  = [25,29,33]    
    ageList = kids + adults 
    youngest = Person(21)
    Oldest = Person(37)
    PersonList = list(map(Person, ageList))
    PersonList = PersonList + [youngest , Oldest] 

    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.infect_random_set(num_infected = 0, infection_doc = "", per_to_immune = 0.5,order= ORDER.ASCENDING,city_name = None,min_age=18,people_per_day =1)
    my_simulation.simulate_day()
    assert youngest.get_disease_state() == DiseaseState.IMMUNE
    #Can't check day by day lots of noise with seir times
    for _ in range(4):
        my_simulation.simulate_day()
    cnt_immune =0 
    for person in my_world.all_people():
        if person.get_disease_state() == DiseaseState.IMMUNE:
            cnt_immune = cnt_immune + 1
    assert cnt_immune <= 5
def test_immune_and_get_events2():
    config_path = os.path.join(os.path.dirname(__file__), "..", "src",
                               "config.json")
    Expected = -1
    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)

    p = Person(30)
    ok,events =  p.immune_and_get_events(INITIAL_DATE , timedelta(days =  20), \
        ((DiseaseState.SUSCEPTIBLE,timedelta(10)),(DiseaseState.LATENT,timedelta(5)), \
        (DiseaseState.SUSCEPTIBLE,None)))
    assert len(events) == 3
    assert ok
    persons_arr = [p]
    env_arr = []
    small_world = 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=[])
    for i in range(10):
        my_simulation.simulate_day()
    events[0].apply(simulation=my_simulation)
    assert p.get_disease_state() == DiseaseState.LATENT
    for i in range(5):
        my_simulation.simulate_day()
    events[1].apply(simulation=my_simulation)
    assert p.get_disease_state() == DiseaseState.IMMUNE
Beispiel #3
0
def city_curfew_routine(person: Person, city_name):
    """
    Create a routine change that represents a city curfew effect on a person routine.
    Here we try to represent the changing (decreasing/ increasing of weights) of the weight in all the environment,
    due to the closure of the city.
    :param person: Person
    :return: routine change dict, keys are environment names, values are weight multipliers.
    """
    params = Params.loader()["interventions_routines"]["city_curfew"]
    routine = {}
    has_free_time = False
    for env_name, env in person._environments.items():
        if env._city is None:
            continue
        if not ((env._city.get_name() == city_name) ^ (person.get_city_name() == city_name)):
            continue
        else:
            routine[env_name] = params["out_of_city"]
            has_free_time = True
    if has_free_time:
        routine['household'] = params["in_city"]["household"]
        routine['city_community'] = params["in_city"]["city_community"]
        routine['neighborhood_community'] = params["in_city"]["neighborhood_community"]

    return routine
Beispiel #4
0
def test_sample_SIRS_seirs_times():
    #Pretest
    Params.clean()
    SIRS.clean()

    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)

    curr_machine_type = machine_type["SIRS"]
    p = Person(94)
    final_states = []
    for i in range(1000):
        table = sample_seir_times(curr_machine_type, p)
        assert table[0][0] == DiseaseState.LATENT
        final_states.append(table[-1][0])
    cnt_table = Counter(final_states)
    assert DiseaseState.DECEASED in cnt_table.keys()
    assert DiseaseState.SUSCEPTIBLE in cnt_table.keys()
    assert len(cnt_table.keys()) == 2
Beispiel #5
0
def test_latent_incubating_critical_deceased():
    #Pretest
    Params.clean()
    SIRS.clean()

    config_path = os.path.join(
        os.path.dirname(__file__), "tests_config_files",
        "test_latent_incubating_critical_deceased_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__),
                                  "tests_params_files", paramsDataPath),
                     override=True)

    p = Person(30)
    for _ in range(100):
        for cur_type in machine_type:
            tbl = sample_seir_times(cur_type, p)
            assert tbl[0][0] == DiseaseState.LATENT, "{}".format(cur_type.name)
            assert tbl[1][0] == DiseaseState.INCUBATINGPOSTLATENT, "{}".format(
                cur_type.name)
            assert tbl[2][
                0] == DiseaseState.SYMPTOMATICINFECTIOUS, "{}".format(
                    cur_type.name)
            assert tbl[3][0] == DiseaseState.CRITICAL, "{}".format(
                cur_type.name)
            assert tbl[4][0] == DiseaseState.DECEASED, "{}".format(
                cur_type.name)
Beispiel #6
0
def quarantine_routine(person: Person):
    """
    Create a routine change that represents a person being in quarantine.
    Here we try to represent the changing (decreasing of weights) of the weight in all the environment, due to the quarantine.
    :param person: Person
    :return: routine change dict, keys are environment names, values are weight multipliers.
    """
    params = Params.loader()["interventions_routines"]["quarantine"]
    return {env_name: params["all"] for env_name in person.get_routine()}
def generate_household_in_city(city):
    """
    Generates a househeold from a given city (which holds relevant distributions)
    :param city: A City object corresponding to where we want to generate a household.
    :return: A list of Person objects corresponding to a random household in that city.
    """
    assert isinstance(city, City)
    household_size = city.household_size_distribution.sample()
    return [
        Person(city.age_distribution.sample()) for i in range(household_size)
    ]
Beispiel #8
0
def test_SIRS_second_infection():
    """
    Test that in SIRS model in case that a person get sick twice,
     (and get recovered between them). 
     He will experience two different time schedule of the illness
    """
    #Pretest
    Params.clean()
    SIRS.clean()

    config_path = os.path.join(
        os.path.dirname(__file__), "tests_config_files",
        "test_latent_incubating_critical_immune_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__),
                                  "tests_params_files", paramsDataPath),
                     override=True)
    Params.loader()["person"]["state_macine_type"] = "SIRS"
    p = Person(30)

    event_lst = p.infect_and_get_events(INITIAL_DATE,
                                        InitialGroup.initial_group())
    p.set_disease_state(DiseaseState.SUSCEPTIBLE)
    event_lst2 = p.infect_and_get_events(INITIAL_DATE,
                                         InitialGroup.initial_group())
    assert not (event_lst == event_lst2)
def test_sortPersonsDescending():
    config_path = os.path.join(os.path.dirname(__file__), "..", "src",
                               "config.json")
    Expected = -1
    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 = [Person(random.randint(0, 99)) for _ in range(20)]
    persons_arr = sorted(persons_arr,
                         key=cmp_to_key(Person.person_comperator_DESCENDING))
    for i in range(1, 20):
        assert persons_arr[i - 1].get_age() >= persons_arr[i].get_age()
Beispiel #10
0
def household_isolation_routine(person: Person):
    """
    Create a routine change that represents a person being in isolation at home.
    Here we try to represent the changing (decreasing/ increasing of weights) of the weight in all the environment,
    due to the person staying at home.
    :param person: Person
    :return: routine change dict, keys are environment names, values are weight multipliers.
    """
    params = Params.loader()["interventions_routines"]["household_isolation"]
    routine = {}
    for env_name in person.get_routine():
        if env_name == 'household':
            routine['household'] = params["household"]
        else:
            routine[env_name] = params["other"]
    return routine
Beispiel #11
0
def workplace_closure_routine(person: Person):
    """
    Create a routine change that represents a closure of the person's workplace.
    Here we try to represent the changing of the weight in the workplace environment as well as other environments,
    due to the closure.
    :param person: Person
    :return: routine change dict, keys are environment names, values are weight multipliers.
    """
    params = Params.loader()["interventions_routines"]["workplace_closure"]
    routine = {}
    for env_name in person.get_routine():
        if env_name == 'workplace':
            routine['workplace'] = params["workplace"]
        else:
            routine[env_name] = params["other"]
    return routine
Beispiel #12
0
def social_distancing_routine(person: Person):
    """
    Create a routine change that represents a social distancing effect on a person routine.
    Here we try to represent the changing (decreasing/ increasing of weights) of the weight in all the environment,
    due to the closure.
    :param person: Person
    :return: routine change dict, keys are environment names, values are weight multipliers.
    """
    params = Params.loader()["interventions_routines"]["social_distancing"]
    routine = {}
    for env_name in person.get_routine():
        if env_name == 'household':
            routine['household'] = params["household"]
        elif env_name == 'workplace':
            routine['workplace'] = params["workplace"]
        elif env_name == 'school':
            routine[env_name] = params["school"]
        else:
            routine[env_name] = params["other"]
    return routine
def test_immune_and_get_events5():
    """
    Test that a person that is not Susptible nor latent when he should get immuned continues his 
    usual path to his death
    """
    config_path = os.path.join(os.path.dirname(__file__), "..", "src",
                               "config.json")
    Expected = -1
    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)

    p = Person(30)
    ok, events =  p.immune_and_get_events(INITIAL_DATE , timedelta(days =  20), \
        ((DiseaseState.SUSCEPTIBLE,timedelta(10)),(DiseaseState.LATENT,timedelta(5)),\
        (DiseaseState.ASYMPTOMATICINFECTIOUS,timedelta(5)),(DiseaseState.DECEASED,timedelta(5)),(DiseaseState.DECEASED,None)))
    assert len(events) == 4
    assert ok == False
    persons_arr = [p]
    env_arr = []
    small_world = 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=[])
    for i in range(10):
        my_simulation.simulate_day()
    events[0].apply(simulation=my_simulation)
    assert p.get_disease_state() == DiseaseState.LATENT
    for i in range(5):
        my_simulation.simulate_day()
    events[1].apply(simulation=my_simulation)
    assert p.get_disease_state() == DiseaseState.ASYMPTOMATICINFECTIOUS
    #Because this person was not susptible nor latent he cannot be immuned
    for i in range(5):
        my_simulation.simulate_day()
    events[2].apply(simulation=my_simulation)
    assert p.get_disease_state() == DiseaseState.DECEASED