예제 #1
0
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import dde

def model(Y, t, d):
    x, y = Y(t)
    xd, yd = Y(t-d)
    return np.array([0.5*x*(1-yd), -0.5*y*(1-xd)])
 
g = lambda t : np.array([0, 0])

tt = np.linspace(2, 30, 20000)
(fig, ax) = plt.subplots(1)
 
for d in [0, 0.2]:
    yy = dde.ddeint(model, g, tt, fargs=(d,))
    # WE PLOT X AGAINST Y
    ax.plot(yy[:, 0], yy[:, 1], lw=2, label='delay = {0:.01f}'.format(d))
 
ax.legend()
plt.show()
예제 #2
0
def main():
    np.set_printoptions(suppress=True, precision=10)
    np.seterr(all='raise')
    args = docopt(__doc__)
    runs = int(args['--runs'])

    N0 = float(args['--initialpop'])
    outbreak_start = 365 * float(args['--outbreak'])
    control_years = float(args['--releasetime'])
    pre_control_years = float(args['--timebefore'])
    post_control_years = float(args['--timeafter'])
    release_ratio = float(args['--releaseratio'])
    control_start = 365.0 * pre_control_years
    control_end = 365.0 * control_years + control_start
    simulation_end =  365.0 * post_control_years + control_end
    dt = 1.0

    tt = np.linspace(0, simulation_end, simulation_end*dt, endpoint=True)
    yys = []
    parameters = generate_params(runs, release_ratio, N0, control_start, 
                                 control_end, outbreak_start)
    start_states = generate_start_states(parameters, N0)

    finished_runs = []
    for r in range(runs):
        #states for this run
        ss = start_states if runs == 1 else start_states[r, :]
        #history Function
        g = lambda t : ss
        #parameters for this run
        par = parameters if runs == 1 else parameters[r, :]
        print_run_info(ss, par, r)
        try:
            yys.append(dde.ddeint(RIDL_dde, g, tt, fargs=(par, )))
        except FloatingPointError as err:
            print("RUN {} FAILED: {}".format(r, err))
        else:
            finished_runs.append(r)

    out = open('run_parameters.csv', 'w')
    out.write(','.join(p_names)+'\n')
    if len(finished_runs) == 1:
        out.write(','.join(str(x) for x in parameters)+'\n')
    else:
        for r in finished_runs:
            out.write(','.join(str(x) for x in parameters[r, :])+'\n')
    out.close()

    gr = len(finished_runs)
    if gr >= 1:
        vars_to_graph = [_F, _Yi, _S, _Ii, _Ri, _Iij, _R]
        num_vars = len(vars_to_graph)
        f, axarr = plt.subplots(num_vars)
        graph_start = 0#int(control_start - 300)
        graph_end = int(simulation_end)

        for i, v in enumerate(vars_to_graph):
            yy = np.array([y[graph_start:graph_end, v] for y in yys])
            axarr[i].plot(tt[graph_start:graph_end], yy.T)
            axarr[i].set_xlim(graph_start, graph_end)
            y_lim = axarr[i].get_ybound()
            axarr[i].set_ylim(0, y_lim[1])
            axarr[i].axvline(control_start, color='black', linestyle='--')
            axarr[i].axvline(control_end, color='black', linestyle='--')
            if i+1 != num_vars:
                axarr[i].set_xticklabels([])
            else:
                axarr[i].set_xlabel('Days')

            axarr[i].set_ylabel(s_names[v], rotation='horizontal')
            axarr[i].yaxis.set_label_position("right")

        plt.figlegend(labels=('Run_{}'.format(x) for x in finished_runs),
                      handles= axarr[-1].get_lines(), loc=1)
        plt.savefig('fig.pdf', orientation='landscape', papertype='letter', 
                    transparent=True, format='pdf')
        plt.show()