yy = (racoons.df_population['age'] >= 20) & (racoons.df_population['age'] < 75)
        adult = racoons.df_population['age'] >= 75
        male = racoons.df_population['gender'] == 0

        male_yy = male & yy
        female_yy = ~male & yy
        male_adult = male & adult
        female_adult = ~male & adult

        racoons.orm_dispersion(i, [37, 38, 39, 40, 41, 42], male_yy,
                               np.array([0, 1, 2, 3, 4, 5, 6, 8]),
                               np.array([75.1, 12.9, 6.5, 1.8, 0.9, 0.9, 0.92, 1.])/100.)
        racoons.orm_dispersion(i, [37, 38, 39, 40, 41, 42], female_yy,
                               np.array([0, 1, 2, 3, 4, 5, 6, 8]),
                               np.array([90.8, 4.07, 1.4, 0.3, 0.7, 0.3, 1.02, 1.]) / 100.)

        racoons.orm_dispersion(i, list(range(7, 43)), male_adult,
                               np.array([0, 1, 2, 3, 4, 5, 7, 8]),
                               np.array([88.9, 4.25, 2.6, 0.9, 0.7, 0.9, 1., 1.]) / 100.)
        racoons.orm_dispersion(i, [7, 8, 9, 10, 11, 12, 13, 14, 37, 38, 39, 40, 41, 42], female_adult,
                               np.array([0, 1, 2, 3, 4, 5, 6]),
                               np.array([92.3, 3.06, 1., 0.9, 0.4, 0.7, 0.73]) / 100.)

counts_to_csv(list_new_infection, my_graph, path_to_output_csv_new_infection, sep=';')
counts_to_csv(list_dead_from_infection, my_graph, path_to_output_csv_death_from_rabies, sep=';')

print(' --------- ')
total_time = time.time() - starting_time
minutes = int(total_time) // 60
seconds = int(total_time) % 60
print('Total time to complete the simulation:', str(minutes), 'min', str(seconds), 'seconds.')
Beispiel #2
0
def worker(total_nb_process, id_process, path_to_population_csv,
           path_to_parameters_csv, path_to_landscape, seed):

    # set the random seed
    np.random.seed(seed + id_process)

    # rabies parameters
    cells_to_infect = ['c126_136', 'c123_137', 'c127_141', 'c118_138']
    level_initial_infection = 0.5
    contact_propagation_proba = 0.035
    prob_death_rabies = 0.95

    # create the parameter manager:
    dict_types = {'start_year': int, 'start_week': int}
    param_manager = CsvManager(path_to_parameters_csv,
                               ',',
                               dict_types=dict_types,
                               nb_cores=total_nb_process,
                               id_process=id_process)

    # creates what's needed
    my_graph = GraphFromORMxml(path_to_xml=path_to_landscape)

    # start the main loop
    count_sim = 0
    param = param_manager.get_parameters()
    while param is not None:
        print('Process', id_process, 'starts simulation number',
              count_sim * nb_process + id_process + 1)

        # vaccination parameters
        path_to_vaccination_csv = param.path_to_vaccination_file
        name_col_hex_id = 'HexCellID'
        name_col_year = 'Year'
        name_col_week = 'Week'
        name_col_level = 'Level'

        # ------------------------
        # we begin with extracting the information from the vaccination campaign
        dict_timestep_to_vac = {}
        initial_timestep = 52 * param.start_year + (param.start_week - 1)
        with open(path_to_vaccination_csv, 'r') as f_vac:
            for i, line in enumerate(f_vac):
                line = line.replace('\"', '').replace('\n', '')
                data = line.split(',')
                if i == 0:
                    index_hex_id = data.index(name_col_hex_id)
                    index_year = data.index(name_col_year)
                    index_week = data.index(name_col_week)
                    index_level = data.index(name_col_level)
                    continue
                timestep = 52 * int(data[index_year]) + (
                    int(data[index_week]) - 1) - initial_timestep
                if timestep in dict_timestep_to_vac:
                    try:
                        current_level = dict_timestep_to_vac[timestep][
                            data[index_hex_id]]
                        if float(data[index_level]) > current_level:
                            dict_timestep_to_vac[timestep][
                                data[index_hex_id]] = float(data[index_level])
                    except KeyError:
                        dict_timestep_to_vac[timestep][
                            data[index_hex_id]] = float(data[index_level])
                else:
                    dict_timestep_to_vac[timestep] = {
                        data[index_hex_id]: float(data[index_level])
                    }
        # ------------------------

        # create the population
        raccoons = ORMLikeAgent(graph=my_graph)
        raccoons.load_population_from_csv(path_to_population_csv, sep=';')

        # create the disease
        rabies = ContactCustomProbTransitionPermanentImmunity(
            host=raccoons, disease_name='rabies')

        # create the intervention (vaccination here)
        intervention = BasicVaccination(disease=rabies, duration_vaccine=156)

        # start the simulation
        list_new_infection = []
        list_dead_from_infection = []
        nb_year_simu = 6

        # infect the initial cell
        for k, cell in enumerate(cells_to_infect):
            if k == 0:
                infected_agent = raccoons.df_population[
                    'territory'] == my_graph.dict_cell_id_to_ind[cell]
            else:
                infected_agent = infected_agent | (
                    raccoons.df_population['territory']
                    == my_graph.dict_cell_id_to_ind[cell])
        infected_agent = infected_agent & (np.random.uniform(
            0, 1, raccoons.df_population.nb_rows) < level_initial_infection)
        raccoons.df_population['inf_rabies'] = infected_agent
        rabies.initialize_counters_of_newly_infected(
            infected_agent,
            np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]),
            np.array([
                0.01, 0.05, 0.05, 0.1, 0.15, 0.2, 0.15, 0.1, 0.05, 0.05, 0.05,
                0.02, 0.01, 0.01
            ]))

        for i in range(nb_year_simu * 52 + 1):

            intervention.update_vaccine_status()
            if i in dict_timestep_to_vac:
                condition = ~raccoons.df_population['imm_rabies'] & \
                            ~raccoons.df_population['inf_rabies'] & \
                            ~raccoons.df_population['con_rabies']
                intervention.apply_vaccine_from_dict(my_graph,
                                                     dict_timestep_to_vac[i],
                                                     condition=condition)

            raccoons.tick()
            my_graph.tick()
            rabies.decrement_counter()

            # natural death
            raccoons.kill_too_old(52 * 8 - 1)
            my_condition = raccoons.df_population['age'] >= 20
            raccoons.mortality_from_v08(np.array(
                [0.6, .4, .3, .3, .3, .6, .6, .6]),
                                        my_condition,
                                        position_attribute='territory')
            raccoons.kill_children_whose_mother_is_dead(20)

            # rabies part
            arr_new_infected = rabies.contact_contagion(
                contact_propagation_proba,
                return_arr_new_infected=True,
                condition=(raccoons.df_population['age'] >= 20))
            rabies.initialize_counters_of_newly_infected(
                arr_new_infected,
                np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]),
                np.array([
                    0.01, 0.05, 0.05, 0.1, 0.15, 0.2, 0.15, 0.1, 0.05, 0.05,
                    0.05, 0.02, 0.01, 0.01
                ]))
            list_new_infection.append(
                raccoons.count_pop_per_vertex(condition=arr_new_infected))

            list_dead_from_infection.append(
                rabies.transition_between_states('con',
                                                 'death',
                                                 proba_death=prob_death_rabies,
                                                 return_transition_count=True))
            rabies.transition_between_states('con', 'imm')
            rabies.transition_between_states('inf',
                                             'con',
                                             arr_nb_timestep=np.array([1]),
                                             arr_prob_nb_timestep=np.array(
                                                 [1.]))

            raccoons.mov_around_with_resistance(0.2)

            if (i + param.start_week - 1) % 52 == 9:
                raccoons.find_random_mate_on_position(
                    1., position_attribute='territory')
            if (i + param.start_week - 1) % 52 == 18:
                # Since these were unknown (at least to me), I calculated them using a probability density function
                # calculator (solvemymath.com)
                yy = (raccoons.df_population['age'] >=
                      20) & (raccoons.df_population['age'] < 75)
                raccoons.create_offsprings_custom_prob(
                    np.array([0, 1, 2, 3, 4, 5, 6, 7, 8]),
                    np.array([
                        0.0001, 0.0044, 0.0539, 0.2416, 0.4, 0.2416, 0.0539,
                        0.0044, 0.0001
                    ]),
                    condition=yy,
                    prob_failure=.35)
                adult = raccoons.df_population['age'] >= 75
                raccoons.create_offsprings_custom_prob(
                    np.array([0, 1, 2, 3, 4, 5, 6, 7, 8]),
                    np.array([
                        0.0001, 0.0044, 0.0539, 0.2416, 0.4, 0.2416, 0.0539,
                        0.0044, 0.0001
                    ]),
                    condition=adult,
                    prob_failure=.05)
                raccoons.df_population['is_pregnant'] = False

            if i > 200:
                yy = (raccoons.df_population['age'] >=
                      20) & (raccoons.df_population['age'] < 75)
                adult = raccoons.df_population['age'] >= 75
                male = raccoons.df_population['gender'] == 0

                male_yy = male & yy
                female_yy = ~male & yy
                male_adult = male & adult
                female_adult = ~male & adult

                raccoons.orm_dispersion_with_resistance(
                    (i + param.start_week - 1), [37, 38, 39, 40, 41, 42],
                    male_yy, np.array([0, 1, 2, 3, 4, 5, 6, 8]),
                    np.array([75.1, 12.9, 6.5, 1.8, 0.9, 0.9, 0.92, 1.]) /
                    100.)
                raccoons.orm_dispersion_with_resistance(
                    (i + param.start_week - 1), [37, 38, 39, 40, 41, 42],
                    female_yy, np.array([0, 1, 2, 3, 4, 5, 6, 8]),
                    np.array([90.8, 4.07, 1.4, 0.3, 0.7, 0.3, 1.02, 1.]) /
                    100.)

                raccoons.orm_dispersion_with_resistance(
                    (i + param.start_week - 1), list(range(7, 43)), male_adult,
                    np.array([0, 1, 2, 3, 4, 5, 7, 8]),
                    np.array([88.9, 4.25, 2.6, 0.9, 0.7, 0.9, 1., 1.]) / 100.)
                raccoons.orm_dispersion_with_resistance(
                    (i + param.start_week - 1),
                    [7, 8, 9, 10, 11, 12, 13, 14, 37, 38, 39, 40, 41, 42],
                    female_adult, np.array([0, 1, 2, 3, 4, 5, 6]),
                    np.array([92.3, 3.06, 1., 0.9, 0.4, 0.7, 0.73]) / 100.)

        # save the output
        counts_to_csv(list_new_infection,
                      my_graph,
                      param.output_folder + '/' + param.output_filename_inf,
                      sep=';')
        counts_to_csv(list_dead_from_infection,
                      my_graph,
                      param.output_folder + '/' + param.output_filename_death,
                      sep=';')

        # ------------------------
        # final line of the loop
        param = param_manager.get_parameters()
    return
Beispiel #3
0
                                          condition=None,
                                          condition_count=my_condition)
    racoons.kill_children_whose_mother_is_dead(20)

    racoons.mov_around_territory(0.2)

    if i % 52 == 15:
        racoons.find_random_mate_on_position(1.,
                                             position_attribute='territory')
    if i % 52 == 22:
        racoons.create_offsprings_custom_prob(
            np.array([0, 5, 6, 7, 8, 9]),
            np.array([0.1, 0.2, 0.2, 0.2, 0.2, 0.1]))
    if i % 52 == 40:
        can_move = racoons.df_population['age'] > 11
        racoons.dispersion_with_varying_nb_of_steps(np.array([1, 2, 3, 4]),
                                                    np.array(
                                                        [.25, .25, .25, .25]),
                                                    condition=can_move)

racoons.save_population_to_csv('C:/post_doc/data/output_sim/emily/pop_3.csv',
                               sep=';')
counts_to_csv(list_count_male,
              my_graph,
              'C:/post_doc/data/output_sim/emily/male_per_cell.csv',
              sep=';')
counts_to_csv(list_count_female,
              my_graph,
              'C:/post_doc/data/output_sim/emily/female_per_cell.csv',
              sep=';')
            arctic_foxes.create_offsprings_custom_prob(
                param.litter,
                param.litter_prob,
                condition=yy,
                prob_failure=(1 - param.prob_juv_birth))

            adult = arctic_foxes.df_population['age'] >= param.adulthood
            arctic_foxes.create_offsprings_custom_prob(
                param.litter,
                param.litter_prob,
                condition=adult,
                prob_failure=(1 - param.prob_adulte_birth))
            arctic_foxes.df_population['is_pregnant'] = False

        # deal with dispersion
        if week > 200:
            old_enough = arctic_foxes.df_population['age'] >= param.indep_age
            arctic_foxes.orm_dispersion(week,
                                        list(param.dispersion_week),
                                        old_enough,
                                        param.moving_cell,
                                        param.moving_prob,
                                        reflexion=True)

    counts_to_csv(yearly_count,
                  graph,
                  path_to_output_folder + '/' + 'yearly_count_sim_' +
                  str(id_sim) + '.csv',
                  sep=';')
    arctic_foxes.save_population_to_csv(path_to_output_folder + '/' +
                                        'pop_sim_' + str(id_sim) + '.csv')