Beispiel #1
0
 def test3(self):
     """Testing that interventions decrease the number of deaths."""
     t_interventions_1 = 255
     t_interventions_2 = 150
     k_1 = StochLib.StochCalc(StochParams, ModelParams, Interventions,
                              t_interventions_1, OutputFileName,
                              num_threads)
     k_2 = StochLib.StochCalc(StochParams, ModelParams, Interventions,
                              t_interventions_2, OutputFileName,
                              num_threads)
     self.assertLess(k_2, k_1)
     print "Test 3: Applying interventions decreases total deaths."
Beispiel #2
0
 def test4(self):
     """Testing StochCalc solver in parallel with 4 threads."""
     t_interventions = 255
     num_threads = 4
     k = StochLib.StochCalc(StochParams, ModelParams, Interventions,
                            t_interventions, OutputFileName, num_threads)
     print "Test 4: StochCalc runs successfully in parallel with 4 threads."
def run_with_interventions(alloc, OrigParams, StochParams, MyConstraints, \
                           out_file, n_threads=1):
    """Run the simulations once with the specified interventions."""
    ModifiedParams = calc_interventions(alloc, OrigParams, MyConstraints)
    cost = StochLib.StochCalc(StochParams, OrigParams, ModifiedParams,
                              MyConstraints.t_interventions, out_file,
                              n_threads)
    print_heading(MyConstraints)
    print_output(alloc, cost)
    return cost
Beispiel #4
0
 def test5(self):
     """Testing that deaths increase as time of  interventions increases."""
     t_interventions = [10, 30, 50, 70, 90, 150, 200]
     k_list = []
     for t_int in t_interventions:
         k = StochLib.StochCalc(StochParams, ModelParams, Interventions,
                                t_int, OutputFileName, num_threads)
         k_list.append(k)
     for i in range(0, len(k_list) - 1):
         self.assertLess(k_list[i], k_list[i + 1])
     print "Test 5: Deaths increase as time of first interventions increases."
    def __call__(self, alloc, out_file="NONE"):
        ModifiedParams = calc_interventions(alloc, self.OrigParams,
                                            self.MyConstraints)

        cost = StochLib.StochCalc(self.StochParams, self.OrigParams, ModifiedParams, \
                                  self.MyConstraints.t_interventions, out_file, \
                                  self.n_threads)

        if self.disp:
            print_output(alloc, cost, str(self.n))

        self.n += 1
        return cost
def setup_stoch_params(N_samples, trajectories, t_final, N, I_init, \
                       H_init, F_init, R_init, E_init):
    """Generate a StochParams object instance."""
    StochParams = StochLib.pyStochParams()
    StochParams.set("N_samples", N_samples)
    StochParams.set("Trajectories", trajectories)
    StochParams.set("I_init", I_init)
    StochParams.set("S_init", N - I_init)
    StochParams.set("H_init", H_init)
    StochParams.set("F_init", F_init)
    StochParams.set("R_init", R_init)
    StochParams.set("E_init", E_init)
    StochParams.set("t_final", t_final)
    return StochParams
Beispiel #7
0
 def test6(self):
     """Testing for speedup in parallel."""
     import time
     StochParams.set("Trajectories", 100)
     t_interventions = 255
     num_threads = [1, 2]
     t_list = []
     for n in num_threads:
         t0 = time.time()
         k = StochLib.StochCalc(StochParams, ModelParams, Interventions,
                                t_interventions, OutputFileName, n)
         t1 = time.time()
         t_list.append(t1 - t0)
     for i in range(0, len(t_list) - 1):
         self.assertGreater(t_list[i], t_list[i + 1])
     print "Test 6: Code experiences speedup in parallel."
Beispiel #8
0
 def test1(self):
     """Testing StochCalc solver without interventions."""
     t_interventions = 255
     k = StochLib.StochCalc(StochParams, ModelParams, Interventions,
                            t_interventions, OutputFileName, num_threads)
     print "Test 1: StochCalc runs successfully without interventions."
Beispiel #9
0
        t_interventions = 255
        num_threads = [1, 2]
        t_list = []
        for n in num_threads:
            t0 = time.time()
            k = StochLib.StochCalc(StochParams, ModelParams, Interventions,
                                   t_interventions, OutputFileName, n)
            t1 = time.time()
            t_list.append(t1 - t0)
        for i in range(0, len(t_list) - 1):
            self.assertGreater(t_list[i], t_list[i + 1])
        print "Test 6: Code experiences speedup in parallel."


if __name__ == '__main__':
    StochParams = StochLib.pyStochParams()
    ModelParams = StochLib.pyModelParams()
    Interventions = StochLib.pyModelParams()
    OutputFileName = "NONE"
    num_threads = 1

    StochParams.set("N_samples", 50)
    StochParams.set("Trajectories", 30)
    StochParams.set("I_init", 3)
    StochParams.set("S_init", 199997)
    StochParams.set("H_init", 0)
    StochParams.set("F_init", 0)
    StochParams.set("R_init", 0)
    StochParams.set("E_init", 0)
    StochParams.set("t_final", 250.0)
def run_no_interventions(OrigParams, StochParams, out_file, n_threads=1):
    """Run the simulations once with no interventions."""
    t_interventions = StochParams.get('t_final') + 1
    cost = StochLib.StochCalc(StochParams, OrigParams, OrigParams,
                              t_interventions, out_file, n_threads)
    return cost
Beispiel #11
0
def fit_params(data_file, country, N, plot_fit=False):
    """Perform fitting of the deterministic model to the given cases data,
    generate a ModelParams object instance holding the parameters, and return it."""
    # Make sure N is a float.
    N=float(N)    

    # Read in the case data for the chosen country.
    days, cases = parse_data(data_file, country)

    # Make the initial guess for the parameter values.
    betaI = 0.128
    betaH = 0.08
    betaF = 0.111
    alpha = 1./10
    gammah = 1./4.12
    gammadh = 1./6.26
    gammaf = 1./4.50
    gammai = 1./20.00
    gammad = 1./10.38
    gammaih =  1./15.88
    theta1 = 0.197
    delta1 = 0.75
    delta2 = 0.75
    
    # Generate the Model.
    # P[0] = S, # P[1] = E,
    # P[2] = I, # P[3] = H,
    # P[4] = F, # P[5] = R.

    def SIRode(P, t, N, betaI, betaH, betaF, alpha, gammah, gammadh, 
               gammaf, gammai, gammad, gammaih, theta1, delta1, delta2):
        return(-1 / N * (betaI * P[0] * P[2] + betaH * P[0] * P[3] + betaF * \
                             P[0] * P[4]),    
                1 / N * (betaI * P[0] * P[2] + betaH * P[0] * P[3] + betaF * \
                             P[0] * P[4]) - alpha * P[1],
                alpha * P[1] - (gammah * theta1 + gammai * (1 - theta1) * (1 \
                             - delta1) + gammad * (1 - theta1) * delta1) * P[2],
                gammah * theta1 * P[2] - (gammadh * delta2 + gammaih * (1 - \
                             delta2)) * P[3],
                gammad * (1 - theta1) * delta1 * P[2] + gammadh * delta2 * \
                             P[3] - gammaf * P[4],
                gammai * (1 - theta1) * (1 - delta1) * P[2] + gammaih * (1 \
                             - delta2) * P[3] + gammaf * P[4]
                )
    
    # Integrate the time series data using the initial parameter guesses.
    P = [N, 10, 0, 0, 0, 0]
    Nt = integrate.odeint(SIRode, P, days, args=(N, betaI, betaH, betaF, 
                          alpha, gammah, gammadh, gammaf, gammai, gammad, 
                          gammaih, theta1, delta1, delta2))
    
    NI = [row[2] for row in Nt]
    NH = [row[3] for row in Nt]
    NF = [row[4] for row in Nt]
    NR = [row[5] for row in Nt]
    NI = np.array(NI)
    NH = np.array(NH)
    NF = np.array(NF)
    NR = np.array(NR)
    
    # Plot the initial fit with the data before optimization.
    if plot_fit:
        plt.clf()
        plt.plot(days, cases, 'o',label = 'Data')
        plt.plot(days, NI+NH+NF+NR,'r--',label = 'Before Optimization')
        
    # Take the parameter guesses and return the fit with the data.
    def LLode(x):
        betaI = x[0]
        betaH = x[1]
        betaF = x[2]
        alpha = x[3]
        gammah = x[4]
        gammadh = x[5]
        gammaf = x[6]
        gammai = x[7]
        gammad = x[8]
        gammaih = x[9]
        theta1 = x[10]
        delta1 = x[11]
        delta2 = x[12]
    
        # Integrate the time series data using the parameters.
        Nt = integrate.odeint(SIRode, P, days, args=(N, betaI, betaH, betaF, 
                              alpha, gammah, gammadh, gammaf, gammai, gammad, 
                              gammaih, theta1, delta1, delta2))
    
        NI = [row[2] for row in Nt]
        NH = [row[3] for row in Nt]
        NF = [row[4] for row in Nt]
        NR = [row[5] for row in Nt]
        NI = np.array(NI)
        NH = np.array(NH)
        NF = np.array(NF)
        NR = np.array(NR)

        difference = cases - (NI + NH + NF + NR)    
        LL = np.dot(difference, difference)

        # Return the error of the fit.
        return LL
    
    x0 = [betaI, betaH, betaF, alpha, gammah, gammadh, gammaf, 
          gammai, gammad, gammaih, theta1, delta1, delta2]
    
    # Initialize the parameter constraints.
    cons = ( {'type': 'ineq', 'fun': lambda x: x[0]},
             {'type': 'ineq', 'fun': lambda x: x[1]},
             {'type': 'ineq', 'fun': lambda x: x[2]},
             {'type': 'ineq', 'fun': lambda x: x[3]},
             {'type': 'ineq', 'fun': lambda x: x[4]}, 
             {'type': 'ineq', 'fun': lambda x: x[5]}, 
             {'type': 'ineq', 'fun': lambda x: x[6]}, 
             {'type': 'ineq', 'fun': lambda x: x[7]}, 
             {'type': 'ineq', 'fun': lambda x: x[8]},   
             {'type': 'ineq', 'fun': lambda x: x[9]}, 
             {'type': 'ineq', 'fun': lambda x: x[10]}, 
             {'type': 'ineq', 'fun': lambda x: -x[10]+1}, 
             {'type': 'ineq', 'fun': lambda x: x[11]}, 
             {'type': 'ineq', 'fun': lambda x: -x[11]+1}, 
             {'type': 'ineq', 'fun': lambda x: x[12]},  
             {'type': 'ineq', 'fun': lambda x: -x[12]+1} )
    
    # Perform the minimization and return the parameter guesses.
    results = minimize(LLode, x0,constraints=cons, method='COBYLA')
    
    estParams = results.x
    betaI = estParams[0]
    betaH = estParams[1]
    betaF = estParams[2]
    alpha = estParams[3]
    gammah = estParams[4]
    gammadh = estParams[5]
    gammaf = estParams[6]
    gammai = estParams[7]
    gammad = estParams[8]
    gammaih = estParams[9]
    theta1 = estParams[10]
    delta1 = estParams[11]
    delta2 = estParams[12]
    
    # Integrate the time series forward using the optimized parameters.
    Nt = integrate.odeint(SIRode, P, days, args=(N, betaI, betaH, betaF, 
                          alpha, gammah, gammadh, gammaf, gammai, gammad, 
                          gammaih, theta1, delta1, delta2))
    
    NI = [row[2] for row in Nt]
    NH = [row[3] for row in Nt]
    NF = [row[4] for row in Nt]
    NR = [row[5] for row in Nt]
    NI = np.array(NI)
    NH = np.array(NH)
    NF = np.array(NF)
    NR = np.array(NR)
    
    # Plot the final time series using the optimized parameters.
    if plot_fit:
        plt.plot(days, NI+NH+NF+NR,'k',label = 'After Optimization')
        plt.legend(fontsize=23)
        plt.title('Model Parameter Fitting',fontsize=23)
        plt.xlabel('Time (Days)',fontsize=23)
        plt.ylabel('Cumulative Infections ',fontsize=23)
        plt.tick_params(axis='both', which='major', labelsize=20)
        plt.tick_params(axis='both', which='minor', labelsize=18)        
        plt.show()
    
    # Initialize a pyModelParams object with the optimized parameter
    # guesses which will be used to call the StochCalc solver.
    OrigParams = StochLib.pyModelParams()
    OrigParams.set("beta_I", estParams[0])
    OrigParams.set("beta_H", estParams[1])
    OrigParams.set("beta_F", estParams[2])
    OrigParams.set("alpha", estParams[3])
    OrigParams.set("gamma_h", estParams[4])
    OrigParams.set("gamma_f", estParams[6])
    OrigParams.set("gamma_i", estParams[7])
    OrigParams.set("gamma_d", estParams[8])
    OrigParams.set("theta_1", estParams[10])
    OrigParams.set("delta_1", estParams[11])
    OrigParams.set("delta_2", estParams[12])
    return OrigParams