def __init__(self, t=TimeAlways()): super().__init__(t=t, name='flu-spike-evt') def apply(self, pop, group, iter, t): return [ GroupSplitSpec(p=0.90, attr_set={ 'flu': 'i' }), GroupSplitSpec(p=0.10) ] def is_applicable(self, group, iter, t): return super().is_applicable(group, iter, t) and iter == 30 and (group.ha({ 'flu': 's' }) or group.ha({ 'flu': 'r' })) (Simulation(). add_probe(GroupSizeProbe.by_attr('flu', 'flu', ['s', 'i', 'r'], msg_mode=ProbeMsgMode.DISP)). add_rule(SIRSModel('flu', beta=0.05, gamma=0.50, alpha=0.10)). add_rule(FluSpikeEvent()). add_group(Group(m=1000, attr={ 'flu': 's' })). run(48) ) # ---------------------------------------------------------------------------------------------------------------------- # dpath_cwd = os.path.dirname(__file__) # fpath_db = os.path.join(dpath_cwd, f'sim.sqlite3') # # if os.path.isfile(fpath_db): # os.remove(fpath_db) # # probe = GroupSizeProbe( # name='flu',
''' A test of the mass transfer graph. ''' from pram.entity import Group from pram.model.epi import SIRSModel from pram.sim import Simulation from pram.traj import Trajectory, TrajectoryEnsemble # ---------------------------------------------------------------------------------------------------------------------- def get_out_dir(filename): return os.path.join(os.path.dirname(__file__), 'out', filename) te = (TrajectoryEnsemble().add_trajectories([ Trajectory(sim=(Simulation().add( [SIRSModel('flu', beta, 0.50, 0.10), Group(m=1000, attr={'flu': 's'})])), name=f'SIR: b={round(beta,2)}') for beta in [0.05, 0.10] ]).set_group_names([(0, 'S', Group.gen_hash(attr={'flu': 's'})), (1, 'I', Group.gen_hash(attr={'flu': 'i'})), (2, 'R', Group.gen_hash(attr={'flu': 'r'}))]).run(100)) # te.plot_mass_locus_line ((1200,300), get_out_dir('_plot-line.png'), iter_range=(-1, -1)) # te.plot_mass_locus_line_aggr((1200,300), get_out_dir('_plot-aggr.png'), iter_range=(-1, -1)) # te.traj[1].plot_mass_locus_streamgraph((900,600), get_out_dir('_plot.png'), do_sort=True) # te.traj[1].plot_heatmap((800,800), get_out_dir('_plot-heatmap.png'), (-1,20))
''' from pram.data import ProbePersistenceDB, ProbeMsgMode, GroupSizeProbe from pram.entity import Group, GroupQry, GroupSplitSpec from pram.model.model import MCSolver from pram.model.epi import SIRSModel from pram.sim import Simulation # ---------------------------------------------------------------------------------------------------------------------- (Simulation().add_probe( GroupSizeProbe.by_attr('flu', 'flu', ['s', 'i', 'r'], msg_mode=ProbeMsgMode.DISP)).add_rule( SIRSModel('flu', beta=0.05, gamma=0.50, alpha=0.10, solver=MCSolver())).add_group( Group(m=1000, attr={'flu': 's'})).run(48)) # ---------------------------------------------------------------------------------------------------------------------- # dpath_cwd = os.path.dirname(__file__) # fpath_db = os.path.join(dpath_cwd, f'sim.sqlite3') # # if os.path.isfile(fpath_db): # os.remove(fpath_db) # # probe = GroupSizeProbe( # name='flu', # queries=[
fpath_db = os.path.join(os.path.dirname(__file__), f'trajectory.sqlite3') # ---------------------------------------------------------------------------------------------------------------------- # (1) Create the database and run a trajectory ensemble: if os.path.isfile(fpath_db): os.remove(fpath_db) te = (TrajectoryEnsemble(fpath_db). add_trajectories([ Trajectory( (Simulation(). add([ SIRSModel('flu', beta, 0.50, 0.00), Group(m=1000, attr={ 'flu': 's' }) ]) ), f'SIR: b={round(beta,2)}' ) for beta in [0.05] # np.arange(0.05, 0.06, 0.01) ]). set_group_names([ (0, 'S', Group.gen_hash(attr={ 'flu': 's' })), (1, 'I', Group.gen_hash(attr={ 'flu': 'i' })), (2, 'R', Group.gen_hash(attr={ 'flu': 'r' })) ]). run(100) ) # ----------------------------------------------------------------------------------------------------------------------
A simulation implementing the SIRS model of infectious disease spread in a population and in particular demonstrating probe plotting. ''' from pram.data import ProbePersistenceMem, GroupSizeProbe from pram.entity import Group from pram.model.epi import SIRSModel from pram.sim import Simulation # ---------------------------------------------------------------------------------------------------------------------- p = GroupSizeProbe.by_attr('flu', 'flu', ['s', 'i', 'r'], persistence=ProbePersistenceMem()) (Simulation().add_probe(p).add_rule( SIRSModel('flu', beta=0.05, gamma=0.50, alpha=0.10)).add_group(Group(m=1000, attr={'flu': 's'})).run(100)) series = [{ 'var': 'p0', 'lw': 0.75, 'ls': 'solid', 'marker': 'o', 'color': 'red', 'ms': 0, 'lbl': 'S' }, { 'var': 'p1', 'lw': 0.75, 'ls': 'dashed', 'marker': '+',
from pram.entity import Group, GroupQry, GroupSplitSpec from pram.model.model import MCSolver from pram.model.epi import SIRSModel from pram.sim import Simulation import matplotlib.pyplot as plt import time # ---------------------------------------------------------------------------------------------------------------------- sir_probe = GroupSizeProbe.by_attr('flu', 'flu', ['s', 'i', 'r'], persistence=ProbePersistenceDB(), msg_mode=ProbeMsgMode.DISP) s = (Simulation().add_probe(sir_probe).add_rule( SIRSModel('flu', beta=0.05, gamma=0.50, alpha=0.10, solver=MCSolver())).add_group(Group(m=1000, attr={'flu': 's'}))) runs = 48 t0 = time.time() s.run(runs) time_elapsed = time.time() - t0 print(f'Time elapsed in {runs} runs: {time_elapsed} seconds') series = [ { 'var': 'p0', 'lw': 2, 'linestyle': '-', 'marker': '', 'color': 'blue',
] def is_applicable(self, group, iter, t): return super().is_applicable( group, iter, t) and 1000 <= iter <= 3000 and group.ha({'flu': 'r'}) # ---------------------------------------------------------------------------------------------------------------------- # if os.path.isfile(fpath_db): os.remove(fpath_db) te = TrajectoryEnsemble(fpath_db) if te.is_db_empty: # generate simulation data if the trajectory ensemble database is empty te.set_pragma_memoize_group_ids(True) te.add_trajectory((Simulation().add([ SIRSModel('flu', beta=0.20, gamma=0.02, solver=ODESolver()), FluGammaProcess(), Group(m=950, attr={'flu': 's'}), Group(m=50, attr={'flu': 'i'}) ]))) te.set_group_names(group_names) te.run(3000) # te.traj[1].plot_mass_flow_time_series(filepath=get_out_dir('_plot.png'), iter_range=(-1,10), v_prop=False, e_prop=True) # te.traj[1].plot_mass_locus_streamgraph((1200,600), get_out_dir('_plot.png')) # te.traj[1].plot_heatmap((800,800), get_out_dir('_plot.png'), (-1,20)) # te.traj[1].plot_mass_locus_recurrence((16,8), get_out_dir('_plot.png'), Group.gen_hash(attr={ 'flu': 's' }), iter_range=(-1, -1)) # te.traj[1].plot_mass_locus_recurrence((16,8), get_out_dir('_plot.png'), Group.gen_hash(attr={ 'flu': 'i' }), iter_range=(-1, 4000)) # te.traj[1].plot_mass_locus_recurrence((16,8), get_out_dir('_plot.png'), Group.gen_hash(attr={ 'flu': 'r' }), iter_range=(-1, 4000))
# ---------------------------------------------------------------------------------------------------------------------- # Simulations: if os.path.isfile(fpath_db): os.remove(fpath_db) # te = TrajectoryEnsemble(fpath_db) # te = TrajectoryEnsemble(fpath_db, cluster_inf=ClusterInf(address='auto')) te = TrajectoryEnsemble(fpath_db, cluster_inf=ClusterInf(num_cpus=6, memory=500*1024*1024, object_store_memory=500*1024*1024, include_webui=False)) if te.is_db_empty: # generate simulation data if the trajectory ensemble database is empty te.set_pragma_memoize_group_ids(True) te.add_trajectories([ (Simulation(). add([ SIRSModel('flu', beta=0.10, gamma=0.05, solver=MCSolver()), # model 1 SIRSModel('flu', beta=0.50, gamma=U(0.01, 0.15), solver=MCSolver(), i=[int(5 + TN(0,50, 5,10)), 50]), # model 2 MakeSusceptibleProcess(i=[50,0], a=3.0, scale=flu_proc_scale), # model 3 Group(m=1000, attr={ 'flu': 's' }) ]) ) for flu_proc_scale in U(1,5, 20) # a 20-trajectory ensemble ]) te.set_group_names(group_names) te.run(120) te.plot_mass_locus_line ((1200,300), os.path.join(os.path.dirname(__file__), 'out', '_plot-line.png'), opacity_min=0.2) te.plot_mass_locus_line_aggr((1200,300), os.path.join(os.path.dirname(__file__), 'out', '_plot-iqr.png')) # fpath_diag = os.path.join(os.path.dirname(__file__), 'out', 'sim-03.diag') # fpath_pdf = os.path.join(os.path.dirname(__file__), 'out', 'sim-03.pdf') # te.traj[2].sim.gen_diagram(fpath_diag, fpath_pdf)
# ---------------------------------------------------------------------------------------------------------------------- fpath_db = os.path.join(os.path.dirname(__file__), 'sim-01.sqlite3') group_names = [(0, 'S', Group.gen_hash(attr={'flu': 's'})), (1, 'I', Group.gen_hash(attr={'flu': 'i'})), (2, 'R', Group.gen_hash(attr={'flu': 'r'}))] # ---------------------------------------------------------------------------------------------------------------------- # if os.path.isfile(fpath_db): os.remove(fpath_db) te = TrajectoryEnsemble(fpath_db) if te.is_db_empty: te.add_trajectories([ Trajectory((Simulation().add([ SIRSModel('flu', 0.10, 0.50, 0.00, solver=MCSolver()), Group(m=900, attr={'flu': 's'}), Group(m=100, attr={'flu': 'i'}) ]))) ]) te.set_group_names(group_names) te.run(100) # ---------------------------------------------------------------------------------------------------------------------- # te.traj[1].plot_mass_locus_line((1200,300), os.path.join(os.path.dirname(__file__), 'sim-01.png')) print(te.traj[1].gen_agent(4)) # a single agent in a four-iteration simulation print(te.traj[1].gen_agent_pop( 2, 3)) # two-agent population simulated for three iterations