def animate(size, sweeps, pS, pI, pR): ''' black= infected white = rec Animation for the sirs model for the given input. ''' model = sirs(size, pS, pI, pR) cMap = 'magma' im = plt.imshow(model.lattice, cmap=cMap, animated=True, vmin=-1, vmax=1) plt.draw() plt.pause(0.0001) for i in range(sweeps): model.update() plt.cla() im = plt.imshow(model.lattice, cmap=cMap, animated=True, vmin=-1, vmax=1) # plt.xlabel("X axis") # plt.ylabel("Y axis") # plt.title("Animation for SIRS model") plt.pause(0.0001) # if(model.infected==0): # print(f"Infected = 0 at {i}") plt.show()
turns = 100 crit = 1 initInfect = 3 size = 50 model = "SIRS" series = [] grave = [] rule = np.arange(0, turns + 1, step=crit) fig, ax = plt.subplots(2, 1) for ind in range(size): covid = sirs(p, i, r, graph) print("Epoch: {}/{}".format(ind + 1, size)) infect, removed = covid.start(turns, initInfect, crit) series.append(infect) grave.append(removed) ax[0].plot(rule, infect, linestyle='--', linewidth=0.5) ax[1].plot(rule, removed, linestyle='--', linewidth=0.5) series = np.array(series) grave = np.array(grave) ax[0].plot(rule, np.average(series, axis=0), linewidth=1.5, label='Ave') ax[1].plot(rule, np.average(grave, axis=0), linewidth=1.5, label='Ave') ax[0].legend() ax[1].legend()
def task3(size, sweeps): ''' Runs the simulation for p2=0.5 over p1-p3 plane and saves the average infected and the scaled variance in a dat file. Parameters: ----------- size: Type Integer Size of the system sweeps: Type Integer Number of sweeps for the simulation. Returns: -------- Saves a dat file of the average infected and scaled variance over p1-p3 plane. ''' print("Starting Task3") p2 = 0.5 p1s = np.linspace(0, 1, 21) p3s = np.linspace(0, 1, 21) #hard setting sweep to 1,000 for this task sweeps = 1000 #Saving the data at each point in allArray allArray = np.zeros((21 * 21, 4)) N = size * size t1 = time.time() #keeping track of the index counter = 0 for i in range(21): print(i) start = time.time() for j in range(21): #create a sirs object which initialises a lattice with the given arguments model = sirs(size, p1s[i], p2, p3s[j]) #empty array that will contain the number of infected over sweeps infectedTotal = [] #iterate for sweeps for n in range(sweeps): #update the lattice for each sweep model.update() #wait 100 sweeps, equilibirum wait if (n >= 100): #then add the number of infected in a system to the infectedTotal array infected = model.infected infectedTotal.append(infected) #stop sweeps if system reaches absorbing state if (infected == 0): #has reached absorbing state so break out of sweeps loop break if (infected == 0): #is absorbing state, so set <I> and variance to 0 averageInfected = 0 variance = 0 else: #not absorbing state so we calculate the average infected and scaled variance infectedTotal = np.asarray(infectedTotal) variance = np.var((infectedTotal)) / N averageInfected = np.mean(infectedTotal) / N #storing the data into a huge array, which we will save as a dat file allArray[counter] = [p1s[i], p3s[j], averageInfected, variance] #incrementing counter for the array index counter += 1 print(f"Finished {i} in time {time.time()-start}s") print(f"time taken = {time.time()-t1}") np.savetxt("data/Task3ProcessedData.dat", allArray, fmt='%.7f')
def task5_Part2(size, sweeps): ''' Runs the simulation for p1=0.8, p2=0.1,p3=0.02 for varying fraction of immunity for a 100x100 system. ----------- size: Type Integer Size of the system sweeps: Type Integer Number of sweeps for the simulation. Returns: -------- Saves a dat file of the average infected at a coressponding fraction of immunity. ''' print("Starting Task5 part 2") #initialising variables to fit the task p1 = 0.8 p2 = 0.1 p3 = .02 immuneProbabilities = np.linspace(0, 1, 101) precision = len(immuneProbabilities) N = size * size simulations = 5 #creating a 101x5 array to store data infectionArray = np.zeros((precision, simulations)) #run the program for multiple simulations for s in range(simulations): print(s) t1 = time.time() #looping for immune probability 0->1 at an interval of 0.01 for i in range(len(immuneProbabilities)): print(f"Starting immune fraction {immuneProbabilities[i]}") #initialising a sirs object with new immune fraction. model = sirs(size, p1, p2, p3, isImmune=True, immuneProbability=immuneProbabilities[i]) infectedTotal = [] for n in range(sweeps): #update system for each sweep model.update() #take measurements after the equilibirum wait if (n >= 100): infected = model.infected infectedTotal.append(infected) if (infected == 0): #is absorbing state so break from sweeps loop break #sets average infection to 0 if system in absoribing state if (infected == 0): infectionArray[i, s] = 0 #else take the averag scaled infection else: infectionArray[i, s] = np.mean() / N print(f"Time taken {s} == {time.time()-t1}s") #calculating the standard error and mean scaled variance over 5 simulations finalArray = [] errors = [] #using scipy.stats.sem to calculate standard error for i in range(precision): finalArray.append(np.mean(infectionArray[i])) errors.append(sem(infectionArray[i])) #saving arrays as a dat file for plotting combined = np.array((immuneProbabilities, finalArray, errors)) np.savetxt('data/Task7_RawDataCombined.dat', (infectionArray), fmt='%.6ft') np.savetxt('data/Task7_ProcessedData.dat', np.transpose(combined), fmt='%.6f')
def task4(size, sweeps): ''' Runs the simulation for p2=p3=0.5 over varying p1 and saves the scaled variance in a dat file. Parameters: ----------- size: Type Integer Size of the system sweeps: Type Integer Number of sweeps for the simulation. Returns: -------- Saves a dat file of the scaled variance over a cut of p2,p3. ''' print("Starting Task4") t1 = time.time() p3 = 0.5 p2 = 0.5 p1s = np.linspace(0.2, 0.5, 31) N = size * size times = 5 allArray = np.zeros((len(p1s), times)) for s in range(times): print(f"Starting {s}") for i in range(len(p1s)): start = time.time() #create a sirs object which initialises a lattice with the given arguments model = sirs(size, p1s[i], p2, p3) #empty array that will contain the number of infected over sweeps infectedTotal = [] for n in range(sweeps): #update system for each sweep model.update() #wait 100 sweeps, equilibirum wait if (n >= 100): #then add the number of infected in a system to the infectedTotal array infected = model.infected infectedTotal.append(infected) #stop sweeps if system reaches absorbing state if (infected == 0): #has reached absorbing state so break out of sweeps loop break if (infected == 0): #is absorbing state so set variance to 0 variance = 0 else: #is not absorbing state so caluclate the scaled variance variance = np.var(infectedTotal) / N #storing the variance in the array for index i,s allArray[i, s] = variance print(f"Time for {s} {i} at time = {time.time()-start}") #saving the array as a dat file np.savetxt(f"data/Task4_RawDataCombined.dat", np.transpose(allArray), fmt='%.6f') #calculating the standard error and the average scaled varaince over 5 simulations averageInfected = [] standardErrors = [] for i in range(len(p1s)): averageInfected.append(np.mean(allArray[i])) standardErrors.append(sem(allArray[i])) combined = np.array((p1s, averageInfected, standardErrors)) np.savetxt("data/Task4_ProcessedData.dat", np.transpose(combined), fmt='%.6f') print("DONEEE")