Ejemplo n.º 1
0
from sampy.agent.builtin_agent import BasicMammal
from ORM_related_addons.graph_from_ORM_xml import GraphFromORMxml

import numpy as np

from constant_examples import (ARR_PROB_DEATH_FEMALE, ARR_PROB_DEATH_MALE)

# use_debug_mode(DataFrameXS)
# use_debug_mode(SquareGridWithDiag)
# use_debug_mode(BasicMammal)

# graph
my_graph = GraphFromORMxml(
    path_to_xml=
    'C:/post_doc/data/orm_related_data/xml_landscapes/emily/ORMlandscape.xml')
# my_graph.create_vertex_attribute('K', 10.)

# agent
agents = BasicMammal(graph=my_graph)
# print(agents.df_population.list_col_name)

# # disease
# disease = ContactCustomProbTransitionPermanentImmunity(disease_name='rabies', host=agents)
# print(agents.df_population.list_col_name)

# create some agents
dict_new_agents = dict()
dict_new_agents['age'] = [52, 52, 52, 52, 52, 52, 52, 52, 52, 52]
dict_new_agents['gender'] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
dict_new_agents['territory'] = [
    0, 0, 125, 125, 1021, 1021, 2034, 2034, 6321, 6321
Ejemplo n.º 2
0
path_to_pop_csv = ''
cells_to_infect = ['', '', '']
level_initial_infection = 0.5
contact_propagation_proba = 0.035
prob_death_rabies = 0.8

path_to_output_csv_new_infection = ''
path_to_output_csv_death_from_rabies = ''

# ------------------------

starting_time = time.time()

# create the landscape
# This is the larger Ontario landscape
my_graph = GraphFromORMxml(path_to_xml='C:/post_doc/data/orm_related_data/xml_landscapes/emily/ORMlandscape.xml')
my_graph.save_table_id_of_vertices_to_indices('C:/post_doc/data/orm_related_data/xml_landscapes/emily/table_graph.csv',
                                              ';')

# create the agents
racoons = ORMLikeAgent(graph=my_graph)
racoons.load_population_from_csv(path_to_pop_csv, sep=';')

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

# infect the initial cell
for i, cell in enumerate(cells_to_infect):
    if i == 0:
        infected_agent = racoons.df_population['territory'] == my_graph.dict_cell_id_to_ind[cell]
    else:
Ejemplo n.º 3
0
from ORM_related_addons.graph_from_ORM_xml import GraphFromORMxml
from ORM_related_addons.ORM_like_agents import ORMMongooses

import numpy as np

male_proba_death = np.array([0.36])
female_proba_death = np.array([])
age_start_to_count_in_mortality = 8
independence_age = 22
max_age_mongooses = 52 * 8 - 1

my_graph = GraphFromORMxml(
    path_to_xml=
    'C:/post_doc/data/orm_related_data/xml_landscapes/caro/PR_baseline_190318.xml'
)
my_graph.save_table_id_of_vertices_to_indices(
    'C:/post_doc/data/orm_related_data/xml_landscapes/caro/table_graph.csv',
    ';')
my_graph.df_attributes.to_csv(
    'C:/post_doc/data/orm_related_data/xml_landscapes/caro/table_attributes.csv',
    ';')

mongooses = ORMMongooses(graph=my_graph, pregnancy_duration=8)

id_cell_first_couples = 'c068_091'
# inserer code pour introduire premiers couples
dict_new_agents = dict()
dict_new_agents['age'] = [52, 52, 52, 52, 52, 52, 52, 52]
dict_new_agents['gender'] = [0, 1, 0, 1, 0, 1, 0, 1]
dict_new_agents['territory'] = [
    my_graph.dict_cell_id_to_ind[id_cell_first_couples] for _ in range(8)
Ejemplo n.º 4
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
    'transmission_probability': float,
    'infectious_period': int,
    'incubation_week': float,
    'mortality_week_female': float,
    'mortality_week_male': float,
    'litter': int,
    'litter_prob': float,
    'prob_outside_cell': float,
    'moving_cell': int,
    'moving_prob': float
}

param_manager = CsvManager(path_to_param_csv, ';', dict_types=dico_type)

# 2) load the graph used for the build-up
graph = GraphFromORMxml(path_to_xml=path_to_orm_xml)

# 3) create the population
arctic_foxes = ORMLikeAgent(graph=graph)

# 4) create the initial population
dict_new_agents = dict()
dict_new_agents['age'] = [52 for _ in range(nb_initial_couples * 2)]
dict_new_agents['gender'] = [i % 2 for i in range(nb_initial_couples * 2)]
dict_new_agents['territory'] = [
    graph.dict_cell_id_to_ind[cell_initial_pop]
    for _ in range(nb_initial_couples * 2)
]
dict_new_agents['position'] = [
    graph.dict_cell_id_to_ind[cell_initial_pop]
    for _ in range(nb_initial_couples * 2)