def __init__(self, topology, tags): super().__init__() # construct the underlying network generator if topology == 'ER': self._gen = ERNetwork() elif topology == 'BA': self._gen = BANetwork() elif topology == 'PLC': self._gen = PLCNetwork() else: raise Exception(f'Unrecognised topology {topology}') self._generator = None self._tags = tags
def testTopologyMarker(self): '''Test we capture the topology of the network.''' param = dict() param[SIR.P_INFECT] = 0.1 param[SIR.P_REMOVE] = 0.01 param[SIR.P_INFECTED] = 0 # so no running time gen = FixedNetwork(networkx.gnp_random_graph(1000, 0.001)) param_g = param.copy() e = StochasticDynamics(SIR(), gen) rc = e.set(param_g).run() ps = rc[Experiment.PARAMETERS] self.assertIn(NetworkGenerator.TOPOLOGY, ps) self.assertEqual(ps[NetworkGenerator.TOPOLOGY], gen.topology()) gen = ERNetwork() param_er = param.copy() param_er[ERNetwork.N] = 1000 param_er[ERNetwork.KMEAN] = 20 e = StochasticDynamics(SIR(), gen) rc = e.set(param_er).run() ps = rc[Experiment.PARAMETERS] self.assertIn(NetworkGenerator.TOPOLOGY, ps) self.assertEqual(ps[NetworkGenerator.TOPOLOGY], gen.topology()) gen = BANetwork() param_ba = param.copy() param_ba[BANetwork.N] = 1000 param_ba[BANetwork.M] = 20 e = StochasticDynamics(SIR(), gen) rc = e.set(param_ba).run() ps = rc[Experiment.PARAMETERS] self.assertIn(NetworkGenerator.TOPOLOGY, ps) self.assertEqual(ps[NetworkGenerator.TOPOLOGY], gen.topology()) gen = PLCNetwork() param_plc = param.copy() param_plc[PLCNetwork.N] = 1000 param_plc[PLCNetwork.EXPONENT] = 2 param_plc[PLCNetwork.CUTOFF] = 5 e = StochasticDynamics(SIR(), gen) rc = e.set(param_plc).run() ps = rc[Experiment.PARAMETERS] self.assertIn(NetworkGenerator.TOPOLOGY, ps) self.assertEqual(ps[NetworkGenerator.TOPOLOGY], gen.topology())
# network topological parameters (for an ER network) N = int(1e5) kmean = 50 if not nb.already('er'): # run the simulations lab[ERNetwork.N] = N lab[ERNetwork.KMEAN] = kmean lab[SIR.P_INFECTED] = 0.001 lab[SIR.P_INFECT] = 0.0001 lab[SIR.P_REMOVE] = 0.001 lab[Monitor.DELTA] = 1 lab['repetitions'] = range(10) e = StochasticDynamics(ProcessSequence([SIR(), Monitor()]), ERNetwork()) lab.runExperiment(e) # figure 1: mean I size with error bars fig = plt.figure(figsize=(7, 5)) # sub-sampling in time and space maxt = 5000 step = 200 # wrangle the time series fore observations and infected component size df = lab.dataframe() ts = df.loc[(df[Monitor.OBSERVATIONS].apply(len) == df[ Monitor.OBSERVATIONS].apply(len).max())].iloc[0][Monitor.OBSERVATIONS] infecteds = DataFrame(df[Monitor.timeSeriesForLocus( SIR.INFECTED)].values.tolist()).rename(columns=lambda i: ts[i])
def testER(self): '''Test we can generate ER networks with all parameter combinations.''' param = dict() param[ERNetwork.N] = 1000 # test using <k> param[ERNetwork.KMEAN] = 20 gen = ERNetwork(param) _ = gen.generate() # test using phi del param[ERNetwork.KMEAN] param[ERNetwork.PHI] = 0.02 gen = ERNetwork(param) __ = gen.generate() # test working with both param[ERNetwork.KMEAN] = 20 gen = ERNetwork(param) _ = gen.generate() # test failing with neither del param[ERNetwork.KMEAN] del param[ERNetwork.PHI] gen = ERNetwork(param) with self.assertRaises(AttributeError): _ = gen.generate()
# # epydemic is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with epydemic. If not, see <http://www.gnu.org/licenses/gpl.html>. import cProfile from pstats import Stats, SortKey from epydemic import SIR, ERNetwork, StochasticDynamics profile = "profile.pstats" N = int(1e5) kmean = 10 params = dict() params[SIR.P_INFECT] = 0.05 params[SIR.P_REMOVE] = 0.01 params[SIR.P_INFECTED] = 0.001 params[ERNetwork.N] = N params[ERNetwork.KMEAN] = kmean e = StochasticDynamics(SIR(), ERNetwork()) cProfile.run('e.set(params).run()', profile) p = Stats(profile) p.sort_stats(SortKey.TIME).print_stats(10)
# network topological parameters N = 10000 kmean = 20 alpha = 2.0 kappa = 20 params = dict() params[ERNetwork.N] = N params[ERNetwork.KMEAN] = kmean params[PLCNetwork.N] = N params[PLCNetwork.EXPONENT] = alpha params[PLCNetwork.CUTOFF] = kappa # construct networks g_er = ERNetwork().set(params).generate() g_plc = PLCNetwork().set(params).generate() # bond-percolate the networks e_er = BondPercolation(g_er) rc_er = e_er.set(params).run() e_plc = BondPercolation(g_plc) rc_plc = e_plc.set(params).run() # figure 1a: the bond percolation behaviour of an ER network fig = plt.figure(figsize=(5, 5)) xs = rc_er[Experiment.RESULTS][BondPercolation.P] ys = [gcc / N for gcc in rc_er[Experiment.RESULTS][BondPercolation.GCC]] plt.plot(xs, ys, 'g-')
lab = Lab(nb) # network topological parameters (for an ER network) N = int(1e5) kmean = 50 # set up the simulations lab[ERNetwork.N] = N lab[ERNetwork.KMEAN] = kmean lab[SIR.P_INFECTED] = 0.001 lab[SIR.P_INFECT] = 0.0001 lab[SIR.P_REMOVE] = 0.001 lab[Monitor.DELTA] = 50 # run the simulations p = ProcessSequence([MonitoredSIR(), Monitor()]) if not nb.already('sto'): e = StochasticDynamics(p, ERNetwork()) lab.runExperiment(e) if not nb.already('syn'): f = SynchronousDynamics(p, ERNetwork()) lab.runExperiment(f) # figure 1: stochastic dynamics doFigure('sto', 'stochastic dynamics', 'doc/implementation/sir-stochastic.png') # figure 2: synchronous dynamics doFigure('syn', 'synchronous dynamics', 'doc/implementation/sir-synchronous.png')
# network topological parameters (ER network) N = 10000 kmean = 3 params = dict() params[ERNetwork.N] = N params[ERNetwork.KMEAN] = kmean params[SIR.P_INFECT] = pInfect params[SIR.P_REMOVE] = pRecover params[SIR.P_INFECTED] = pInfected params[Monitor.DELTA] = 10 e = StochasticDynamics(ProcessSequence([MonitoredSIR(), Monitor()]), ERNetwork()) e.process().setMaximumTime(T) rc = e.set(params).run() res = rc[Experiment.RESULTS] ts = res[Monitor.OBSERVATIONS] er_sss = list( map(lambda v: v / N, res[Monitor.timeSeriesForLocus(SIR.SUSCEPTIBLE)])) er_iis = list( map(lambda v: v / N, res[Monitor.timeSeriesForLocus(SIR.INFECTED)])) er_rrs = list( map(lambda v: v / N, res[Monitor.timeSeriesForLocus(SIR.REMOVED)])) # figure 2: same epidemic on ER network fig = plt.figure(figsize=(5, 5)) ax = fig.gca()