コード例 #1
0
def test_set_outbreak():
    state = State(regions, routes)

    assert_equal(state.region_sir[1382].susceptible, 39132)
    assert_equal(state.region_sir[1382].infected, 0)
    assert_equal(state.region_sir[1382].removed, 0)

    state.set_outbreak('Paris', 1000)

    assert_equal(state.region_sir[1382].susceptible, 38132)
    assert_equal(state.region_sir[1382].infected, 1000)
    assert_equal(state.region_sir[1382].removed, 0)
コード例 #2
0
def test_simulate():
    state = State(regions, routes)
    state.set_outbreak('Paris', 1000)

    sim = Simulator(state)

    total_population_pre = sim.state.total_sir().total_pop
    sim.step()
    total_population_post = sim.state.total_sir().total_pop

    # check that transfers and disease spread didn't change the world
    # population'
    assert_equal(total_population_pre, total_population_post)
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt

from simulator import State, Simulator
from world import regions, routes

if __name__ == "__main__":
    DO_PLOT = True
    if DO_PLOT:
        plt.figure()
    for i in range(0, 1):
        print('run {0:d}'.format(i))
        state = State(regions, routes, verbose=True)
        state.set_outbreak('Sidney', 1000)
        sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5, verbose=True)

        sol = []
        for state in sim.run(iterations=120):
            sol.append(state.total_sir().as_tuple(total=True))
            #sol.append(state.region_sir[4029].as_tuple(total=True))

        sol = np.asarray(sol)


        if DO_PLOT:
            p1, = plt.plot(sol[:, 0], color='SteelBlue', alpha=0.5, label='Susceptible')
            p2, = plt.plot(sol[:, 1], color='IndianRed', alpha=0.5, label='Infected')
            p3, = plt.plot(sol[:, 2], color='Olive', alpha=0.5, label='Removed')
            p4, = plt.plot(sol[:, 3], color='Gray', alpha=0.5, label='Total')
def execute_simulation(add_rio=False,
                       ol_start=0,
                       rio_length=18,
                       rio_visitors=380e3,
                       n_simulations=5):

    sol_global = []
    sol_rio = []
    sol_moscow = []
    sol_berlin = []
    sol_beijing = []
    sol_sydney = []
    sol_new_york = []
    params = {}
    params['global'] = []
    params['rio'] = []
    params['moscow'] = []
    params['berlin'] = []
    params['beijing'] = []
    params['sydney'] = []
    params['new'] = []
    for j in range(n_simulations):
        print("running simulation {0} / {1}".format(j + 1, n_simulations))
        state = State(regions, routes, verbose=True)
        state.set_outbreak('Rio De Janeiro', 1e3)  #'Rio De Janeiro', 1000)
        sim = Simulator(state,
                        transfer_prob=0.005,
                        beta=2,
                        gamma=0.5,
                        verbose=True)

        sol_global.append([])
        sol_rio.append([])
        sol_moscow.append([])
        sol_berlin.append([])
        sol_beijing.append([])
        sol_sydney.append([])
        sol_new_york.append([])
        state_list = []
        for i, state in enumerate(sim.run(iterations=120)):
            state_list.append(state)
            if i == ol_start and add_rio:  # start outbreak x days before olympics
                sim.add_event(2560,
                              days=rio_length,
                              total_transfer=rio_visitors)

            sol_global[j].append(state.total_sir().as_tuple(total=True))
            sol_rio[j].append(state.region_sir[2560].as_tuple(total=True))
            sol_moscow[j].append(state.region_sir[4029].as_tuple(total=True))
            sol_berlin[j].append(state.region_sir[351].as_tuple(total=True))
            sol_beijing[j].append(state.region_sir[3364].as_tuple(total=True))
            sol_sydney[j].append(state.region_sir[3361].as_tuple(total=True))
            sol_new_york[j].append(state.region_sir[3797].as_tuple(total=True))

        params['global'].append(
            sir.ParameterEstimator(iter([x.total_sir() for x in state_list]),
                                   method='max').beta)
        params['rio'].append(
            sir.ParameterEstimator(iter(
                [x.region_sir[2560] for x in state_list]),
                                   method='max').beta)
        params['moscow'].append(
            sir.ParameterEstimator(iter(
                [x.region_sir[4029] for x in state_list]),
                                   method='max').beta)
        params['berlin'].append(
            sir.ParameterEstimator(iter(
                [x.region_sir[351] for x in state_list]),
                                   method='max').beta)
        params['beijing'].append(
            sir.ParameterEstimator(iter(
                [x.region_sir[3364] for x in state_list]),
                                   method='max').beta)
        params['sydney'].append(
            sir.ParameterEstimator(iter(
                [x.region_sir[3361] for x in state_list]),
                                   method='max').beta)
        params['new'].append(
            sir.ParameterEstimator(iter(
                [x.region_sir[2560] for x in state_list]),
                                   method='max').beta)

    if add_rio:
        fig_name = "rio-{0}-{1}-{2:d}.pdf".format(ol_start, rio_length,
                                                  int(rio_visitors))
    else:
        fig_name = "no_rio.pdf"

    plot_sir([
        sol_global, sol_rio, sol_new_york, sol_berlin, sol_moscow, sol_beijing,
        sol_sydney
    ], [
        'Global', 'Rio De Janeiro', 'New York', 'Berlin', 'Moscow', 'Beijing',
        'Sydney'
    ], fig_name)

    # estimate means and variance
    global_values = sol_global
    peak_times_global = [np.argmax([x[1] for x in y]) for y in global_values]
    peak_amount_global = [
        y[peak][1] for peak, y in zip(peak_times_global, global_values)
    ]

    peak_times_rio = [np.argmax([x[1] for x in y]) for y in sol_rio]
    peak_times_new_york = [np.argmax([x[1] for x in y]) for y in sol_new_york]
    peak_times_berlin = [np.argmax([x[1] for x in y]) for y in sol_berlin]
    peak_times_moscow = [np.argmax([x[1] for x in y]) for y in sol_moscow]
    peak_times_beijing = [np.argmax([x[1] for x in y]) for y in sol_beijing]
    peak_times_sydney = [np.argmax([x[1] for x in y]) for y in sol_sydney]

    t_deviations = scipy.stats.t.ppf(0.975, len(peak_times_rio) - 1)

    # estimate variance with control variates
    with open('control-{0}.csv'.format(add_rio), 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        writer.writerow([
            'global_amount', 'global_amount_control', 'global_peak_time',
            'global_peak_time_control', 'rio_time', 'rio_time_control',
            'new_york_time', 'new_york_time_control', 'berlin_time',
            'berlin_time_control', 'moscow_time', 'moscow_time_control',
            'beijing_time', 'beijing_time_control', 'sydney_time',
            'sydney_time_control'
        ])
        for i in range(n_simulations):
            writer.writerow([
                peak_amount_global[i], params['global'][i],
                peak_times_global[i], params['global'][i], peak_times_rio[i],
                params['rio'][i], peak_times_rio[i], params['new'][i],
                peak_times_rio[i], params['berlin'][i], peak_times_rio[i],
                params['moscow'][i], peak_times_rio[i], params['beijing'][i],
                peak_times_rio[i], params['sydney'][i]
            ])
    amount_global_control_conf = control_variate_conf(peak_amount_global,
                                                      params['global'])
    time_global_control_conf = control_variate_conf(peak_times_global,
                                                    params['global'])
    time_rio_control_conf = control_variate_conf(peak_times_rio, params['rio'])
    time_new_york_control_conf = control_variate_conf(peak_times_new_york,
                                                      params['new'])
    time_berlin_control_conf = control_variate_conf(peak_times_berlin,
                                                    params['berlin'])
    time_moscow_control_conf = control_variate_conf(peak_times_moscow,
                                                    params['moscow'])
    time_beijing_control_conf = control_variate_conf(peak_times_beijing,
                                                     params['beijing'])
    time_sydney_control_conf = control_variate_conf(peak_times_sydney,
                                                    params['sydney'])

    return [(np.mean(peak_amount_global), t_deviations *
             np.std(peak_amount_global, ddof=1) / math.sqrt(n_simulations),
             amount_global_control_conf),
            (np.mean(peak_times_global), t_deviations *
             np.std(peak_times_global, ddof=1) / math.sqrt(n_simulations),
             time_global_control_conf),
            (np.mean(peak_times_rio), t_deviations *
             np.std(peak_times_rio, ddof=1) / math.sqrt(n_simulations),
             time_rio_control_conf),
            (np.mean(peak_times_new_york), t_deviations *
             np.std(peak_times_new_york, ddof=1) / math.sqrt(n_simulations),
             time_new_york_control_conf),
            (np.mean(peak_times_berlin), t_deviations *
             np.std(peak_times_berlin, ddof=1) / math.sqrt(n_simulations),
             time_berlin_control_conf),
            (np.mean(peak_times_moscow), t_deviations *
             np.std(peak_times_moscow, ddof=1) / math.sqrt(n_simulations),
             time_moscow_control_conf),
            (np.mean(peak_times_beijing), t_deviations *
             np.std(peak_times_beijing, ddof=1) / math.sqrt(n_simulations),
             time_beijing_control_conf),
            (np.mean(peak_times_sydney), t_deviations *
             np.std(peak_times_sydney, ddof=1) / math.sqrt(n_simulations),
             time_sydney_control_conf)]
コード例 #5
0
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt

from simulator import State, Simulator
from world import regions, routes

if __name__ == "__main__":
    DO_PLOT = True
    if DO_PLOT:
        plt.figure()
    for i in range(0, 1):
        print('run {0:d}'.format(i))
        state = State(regions, routes, verbose=True)
        state.set_outbreak('Sidney', 1000)
        sim = Simulator(state,
                        transfer_prob=0.005,
                        beta=2,
                        gamma=0.5,
                        verbose=True)

        sol = []
        for state in sim.run(iterations=120):
            sol.append(state.total_sir().as_tuple(total=True))
            #sol.append(state.region_sir[4029].as_tuple(total=True))

        sol = np.asarray(sol)

        if DO_PLOT:
            p1, = plt.plot(sol[:, 0],
def execute_simulation(add_rio=False, ol_start=0, rio_length=18,
                       rio_visitors=380e3, n_simulations=5):

    sol_global = []
    sol_rio = []
    sol_moscow = []
    sol_berlin = []
    sol_beijing = []
    sol_sydney = []
    sol_new_york = []
    params = {}
    params['global'] = []
    params['rio'] = []
    params['moscow'] = []
    params['berlin'] = []
    params['beijing'] = []
    params['sydney'] = []
    params['new'] = []
    for j in range(n_simulations):
        print("running simulation {0} / {1}".format(j + 1, n_simulations))
        state = State(regions, routes, verbose=True)
        state.set_outbreak('Rio De Janeiro', 1e3)#'Rio De Janeiro', 1000)
        sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5,
                        verbose=True)

        sol_global.append([])
        sol_rio.append([])
        sol_moscow.append([])
        sol_berlin.append([])
        sol_beijing.append([])
        sol_sydney.append([])
        sol_new_york.append([])
        state_list = []
        for i, state in enumerate(sim.run(iterations=120)):
            state_list.append(state)
            if i == ol_start and add_rio: # start outbreak x days before olympics
                sim.add_event(2560, days=rio_length, total_transfer=rio_visitors)

            sol_global[j].append(state.total_sir().as_tuple(total=True))
            sol_rio[j].append(state.region_sir[2560].as_tuple(total=True))
            sol_moscow[j].append(state.region_sir[4029].as_tuple(total=True))
            sol_berlin[j].append(state.region_sir[351].as_tuple(total=True))
            sol_beijing[j].append(state.region_sir[3364].as_tuple(total=True))
            sol_sydney[j].append(state.region_sir[3361].as_tuple(total=True))
            sol_new_york[j].append(state.region_sir[3797].as_tuple(total=True))

        params['global'].append(sir.ParameterEstimator(
            iter([x.total_sir() for x in state_list]), method='max').beta)
        params['rio'].append(sir.ParameterEstimator(
            iter([x.region_sir[2560] for x in state_list]), method='max').beta)
        params['moscow'].append(sir.ParameterEstimator(
            iter([x.region_sir[4029] for x in state_list]), method='max').beta)
        params['berlin'].append(sir.ParameterEstimator(
            iter([x.region_sir[351] for x in state_list]), method='max').beta)
        params['beijing'].append(sir.ParameterEstimator(
            iter([x.region_sir[3364] for x in state_list]), method='max').beta)
        params['sydney'].append(sir.ParameterEstimator(
            iter([x.region_sir[3361] for x in state_list]), method='max').beta)
        params['new'].append(sir.ParameterEstimator(
            iter([x.region_sir[2560] for x in state_list]), method='max').beta)

    if add_rio:
        fig_name = "rio-{0}-{1}-{2:d}.pdf".format(ol_start, rio_length,
                                                  int(rio_visitors))
    else:
        fig_name = "no_rio.pdf"

    plot_sir([sol_global, sol_rio, sol_new_york, sol_berlin,
              sol_moscow, sol_beijing, sol_sydney],
             ['Global', 'Rio De Janeiro', 'New York', 'Berlin',
              'Moscow', 'Beijing', 'Sydney'], fig_name)

    # estimate means and variance
    global_values = sol_global
    peak_times_global = [np.argmax([x[1] for x in y])
                         for y in global_values]
    peak_amount_global = [y[peak][1]
                          for peak, y in zip(peak_times_global, global_values)]


    peak_times_rio = [np.argmax([x[1] for x in y])
                      for y in  sol_rio]
    peak_times_new_york = [np.argmax([x[1] for x in y])
                           for y in  sol_new_york]
    peak_times_berlin = [np.argmax([x[1] for x in y])
                         for y in  sol_berlin]
    peak_times_moscow = [np.argmax([x[1] for x in y])
                         for y in  sol_moscow]
    peak_times_beijing = [np.argmax([x[1] for x in y])
                          for y in  sol_beijing]
    peak_times_sydney = [np.argmax([x[1] for x in y])
                         for y in  sol_sydney]

    t_deviations = scipy.stats.t.ppf(0.975, len(peak_times_rio)-1)

    # estimate variance with control variates
    with open('control-{0}.csv'.format(add_rio), 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        writer.writerow(['global_amount', 'global_amount_control',
                         'global_peak_time', 'global_peak_time_control',
                         'rio_time', 'rio_time_control',
                         'new_york_time', 'new_york_time_control',
                         'berlin_time', 'berlin_time_control',
                         'moscow_time', 'moscow_time_control',
                         'beijing_time', 'beijing_time_control',
                         'sydney_time', 'sydney_time_control'])
        for i in range(n_simulations):
            writer.writerow([peak_amount_global[i], params['global'][i],
                             peak_times_global[i], params['global'][i],
                             peak_times_rio[i], params['rio'][i],
                             peak_times_rio[i], params['new'][i],
                             peak_times_rio[i], params['berlin'][i],
                             peak_times_rio[i], params['moscow'][i],
                             peak_times_rio[i], params['beijing'][i],
                             peak_times_rio[i], params['sydney'][i]
                             ])
    amount_global_control_conf = control_variate_conf(peak_amount_global, params['global'])
    time_global_control_conf = control_variate_conf(peak_times_global, params['global'])
    time_rio_control_conf = control_variate_conf(peak_times_rio, params['rio'])
    time_new_york_control_conf = control_variate_conf(peak_times_new_york, params['new'])
    time_berlin_control_conf = control_variate_conf(peak_times_berlin, params['berlin'])
    time_moscow_control_conf = control_variate_conf(peak_times_moscow, params['moscow'])
    time_beijing_control_conf = control_variate_conf(peak_times_beijing, params['beijing'])
    time_sydney_control_conf = control_variate_conf(peak_times_sydney, params['sydney'])

    return [(np.mean(peak_amount_global),
             t_deviations * np.std(peak_amount_global, ddof=1) / math.sqrt(n_simulations),
             amount_global_control_conf),
            (np.mean(peak_times_global),
             t_deviations * np.std(peak_times_global, ddof=1) / math.sqrt(n_simulations),
             time_global_control_conf),
            (np.mean(peak_times_rio),
             t_deviations * np.std(peak_times_rio, ddof=1) / math.sqrt(n_simulations),
             time_rio_control_conf),
            (np.mean(peak_times_new_york),
             t_deviations * np.std(peak_times_new_york, ddof=1) / math.sqrt(n_simulations),
             time_new_york_control_conf),
            (np.mean(peak_times_berlin),
             t_deviations * np.std(peak_times_berlin, ddof=1) / math.sqrt(n_simulations),
             time_berlin_control_conf),
            (np.mean(peak_times_moscow),
             t_deviations * np.std(peak_times_moscow, ddof=1) / math.sqrt(n_simulations),
             time_moscow_control_conf),
            (np.mean(peak_times_beijing),
             t_deviations * np.std(peak_times_beijing, ddof=1) / math.sqrt(n_simulations),
             time_beijing_control_conf),
            (np.mean(peak_times_sydney),
             t_deviations * np.std(peak_times_sydney, ddof=1) / math.sqrt(n_simulations),
             time_sydney_control_conf)
           ]
import _setup

import os.path as path
import time

from display import WorldMap
from simulator import State, Simulator
from world import regions, routes

this_dir = path.dirname(path.realpath(__file__))

len_simulation = 90

state = State(regions, routes, verbose=True)
state.set_outbreak('Rio De Janeiro', 1000)

sim = Simulator(
    state,
    transfer_prob=0.005,
    beta=2,
    gamma=0.5,
    verbose=True,
)
sim.add_event(2560, days=18, total_transfer=380e3)
base_map = WorldMap(resolution="c")
base_map.animate(sim, frames=len_simulation, max_infected=0.1)
base_map.ani.save(path.join(this_dir, '../../report/plots/gifs/rio.mp4'),
                  writer="mencoder_file",
                  fps=3,
                  savefig_kwargs={'bbox_inches': 'tight'})
import _setup

from display import WorldMap
from simulator import State, Simulator
from world import regions, routes

state = State(regions, routes, verbose=True)
state.set_outbreak('Rio De Janeiro', 1000)

# sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5, verbose=True)
# sim.add_event(2560, days=18, total_transfer=380e3)
# # plot regions on a world map
# base_map = WorldMap(resolution="c")
# base_map.animate(sim, frames=120, max_infected=0.1)
# base_map.show()

sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5, verbose=True)
# plot regions on a world map
base_map = WorldMap(resolution="c")
base_map.animate(sim, frames=120, max_infected=0.1)
base_map.show()
            beta / sum(Y[6:]) * Y[7] * Y[6] - gamma * Y[7] + tau * (Y[4]-Y[7]),
            gamma * Y[7] + tau * (Y[5] - Y[8])]

start_infection_n1 = 2 / 50
t = np.arange(0, 365, 1)
asol = 100 * integrate.odeint(solver, [1 - R0, R0, 0,
                                       1, 0, 0,
                                       1, 0, 0], t)

plt.plot(t, asol[:, 1], ls='-', color='black', alpha=0.8, lw=2)
plt.plot(t, asol[:, 4], ls='-', color='black', alpha=0.8, lw=2)
ptrue, = plt.plot(t, asol[:, 7], ls='-', color='black', alpha=0.8, lw=2)

for i in range(0, 10):
    state = State(regions, routes, verbose=True)
    state.set_outbreak('Region 0', R0 * N)
    sim = Simulator(state, transfer_prob=tau, beta=beta, gamma=gamma, verbose=True)

    solution = np.zeros((365+1, 9))
    for i, state in enumerate(sim.run(iterations=365)):
        print(i)
        for region_id, sir in state.region_sir.items():
            print(str(sir))
            solution[i, region_id * 3 + 0] = state.region_sir[region_id].susceptible / N
            solution[i, region_id * 3 + 1] = state.region_sir[region_id].infected / N
            solution[i, region_id * 3 + 2] = state.region_sir[region_id].removed / N

    asol = np.asarray(solution) * 100
    t = np.arange(0, asol.shape[0])

    p1, = plt.plot(t, asol[:, 0], ls='-', color='SteelBlue')
import _setup

import scipy.stats
import math
import numpy as np

import sir
import world
from simulator import Simulator, State
import matplotlib.pyplot as plt

state = State(world.regions, world.routes)
state.set_outbreak('Paris', 1000)

# param_list = []
# for i in range(0, 25):
#     print("run {0}".format(i))
#     sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5, verbose=True)
#     param_est = sir.ParameterEstimator(sim.run(iterations=120), method='max')
#     param_list.append(str(param_est))

# for x in param_list:
#     print(x)




sample_a = np.asarray([
     # gamma,       beta,          N,    I, max_infected, max_infected_i
    [16.921686, 35.575031, 7399998550, 1000, 359733948, 53],
コード例 #11
0
import _setup

import scipy.stats
import math
import numpy as np

import sir
import world
from simulator import Simulator, State
import matplotlib.pyplot as plt

state = State(world.regions, world.routes)
state.set_outbreak('Paris', 1000)

# param_list = []
# for i in range(0, 25):
#     print("run {0}".format(i))
#     sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5, verbose=True)
#     param_est = sir.ParameterEstimator(sim.run(iterations=120), method='max')
#     param_list.append(str(param_est))

# for x in param_list:
#     print(x)

sample_a = np.asarray([
    # gamma,       beta,          N,    I, max_infected, max_infected_i
    [16.921686, 35.575031, 7399998550, 1000, 359733948, 53],
    [16.725976, 35.164427, 7399998550, 1000, 363948376, 53],
    [16.994056, 35.727980, 7399998550, 1000, 358206776, 53],
    [16.858108, 35.434701, 7399998550, 1000, 361050212, 53],
    [16.680545, 35.065132, 7399998550, 1000, 364916209, 53],