EoN.subsample¶
-
EoN.
subsample
(report_times, times, status1, status2=None, status3=None)[source]¶ Given a list/array of times to report at, returns the number of nodes of each status at those times.
- returns them
- subsampled at specific report_times.
Arguments: - report_times iterable (ordered)
- times at which we want to know state of system
- times iterable (ordered)
- times at which we have the system state (assumed no change between these times)
- status1 iterable
generally S, I, or R
number of nodes in given status at corresponding time in times.
- status2 iterable (optional, default None)
generally S, I, or R
number of nodes in given status at corresponding time in times.
- status3 iterable (optional, default None)
generally S, I, or R
number of nodes in given status at corresponding time in times.
Returns: - If only status1 is defined
- report_status1 numpy array
gives
status1
subsampled just atreport_times
. - If more are defined then it returns a list, either
- [report_status1, report_status2]
- or
- [report_status1, report_status2, report_status3]
In each case, these are subsampled just at report_times.
SAMPLE USE: import networkx as nx import EoN import numpy as np import matplotlib.pyplot as plt """ in this example we will run 100 stochastic simulations. Each simulation will produce output at a different set of times. In order to calculate an average we will use subsample to find the epidemic sizes at a specific set of times given by report_times. """ G = nx.fast_gnp_random_graph(10000,0.001) tau = 1. gamma = 1. report_times = np.linspace(0,5,101) Ssum = np.zeros(len(report_times)) Isum = np.zeros(len(report_times)) Rsum = np.zeros(len(report_times)) iterations = 100 for counter in range(iterations): t, S, I, R = EoN.fast_SIR(G, tau, gamma, initial_infecteds = range(10)) #t, S, I, and R have an entry for every single event. newS, newI, newR = EoN.subsample(report_times, t, S, I, R) #could also do: newI = EoN.subsample(report_times, t, I) plt.plot(report_times, newS, linewidth=1, alpha = 0.4) plt.plot(report_times, newI, linewidth=1, alpha = 0.4) plt.plot(report_times, newR, linewidth=1, alpha = 0.4) Ssum += newS Isum += newI Rsum += newR Save = Ssum / float(iterations) Iave = Isum / float(iterations) Rave = Rsum / float(iterations) plt.plot(report_times, Save, "--", linewidth = 5, label = "average") plt.plot(report_times, Iave, "--", linewidth = 5) plt.plot(report_times, Rave, "--", linewidth = 5) plt.legend(loc = "upper right") plt.savefig("tmp.pdf")
If only one of the sample times is given then returns just that.
If report_times goes longer than times, then this simply assumes the system freezes in the final state.
This uses a recursive approach if multiple arguments are defined.