# ) # ) for _ in range(n_traj) # ]). # 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(n_iter) # ) # ---------------------------------------------------------------------------------------------------------------------- # Load: te = TrajectoryEnsemble(fpath_db).stats() # ---------------------------------------------------------------------------------------------------------------------- # Plot: def get_out_dir(filename): return os.path.join(os.path.dirname(__file__), 'out', filename) # te.traj[1].plot_mass_locus_streamgraph((1200,600), get_out_dir('_plot.png'), iter_range=(-1, 2000), do_sort=True) # te.traj[1].plot_mass_locus_fft((1200,200), get_out_dir('_plot.png'), sampling_rate=100, do_sort=True) # te.traj[1].plot_mass_locus_spectrogram((16,8), get_out_dir('_plot.png'), sampling_rate=None, win_len=100, do_sort=True) # te.traj[1].plot_mass_locus_scaleogram((16,8), get_out_dir('_plot.png'), sampling_rate=100, do_sort=True) # te.plot_mass_locus_line((2400,600), get_out_dir('_plot.png'), iter_range=(-1, -1), nsamples=10, do_sort=True) # te.plot_mass_locus_line_aggr((2400,600), get_out_dir('_plot.png'), iter_range=(-1, -1), do_sort=True)
class FluProcess(GammaDistributionProcess): def apply(self, pop, group, iter, t): p = self.get_p(iter) return [ GroupSplitSpec(p=p, attr_set={ 'flu': 's' }), GroupSplitSpec(p=1-p) ] def is_applicable(self, group, iter, t): return super().is_applicable(group, iter, t) and group.has_attr({ 'flu': 'r' }) # ---------------------------------------------------------------------------------------------------------------------- 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=True)) if te.is_db_empty: te.set_pragma_memoize_group_ids(True) te.add_trajectories([ Trajectory( (Simulation(). add([ SIRModel('flu', beta=0.10, gamma=0.05, solver=MCSolver()), SIRModel('flu', beta=0.50, gamma=U(0.01, 0.15), i=[int(5 + TN(0,50, 5,10)), 50], solver=MCSolver()), FluProcess(i=[50,0], p_max=None, a=3.0, scale=flu_proc_scale), Group(m=950, attr={ 'flu': 's' }), Group(m= 50, attr={ 'flu': 'i' }) ])
prop_pop_inf = 0.0001 # fractional mass: 20 students and 59 workers; integer mass: _ students and 7 workers prop_pop_inf = 0.0005 # fractional mass: _ students and _ workers; integer mass: 34 students and 61 workers def grp_setup(pop, group): return [ GroupSplitSpec(p=1 - prop_pop_inf, attr_set={disease_name: 'S'}), GroupSplitSpec(p=prop_pop_inf, attr_set={disease_name: 'IA'}) ] if do_rm_traj_ens_db and os.path.isfile(fpath_traj_ens_db): os.remove(fpath_traj_ens_db) te = TrajectoryEnsemble(fpath_traj_ens_db) # te = TrajectoryEnsemble(fpath_traj_ens_db, cluster_inf=ClusterInf(num_cpus=12, memory=2000*1024*1024, object_store_memory=2000*1024*1024)) # te = TrajectoryEnsemble(fpath_traj_ens_db, cluster_inf=ClusterInf(num_cpus=6, memory=16000*1024*1024, object_store_memory=16000*1024*1024)) if te.is_db_empty: te.set_pragma_memoize_group_ids(True) te.add_trajectories([ Trajectory(( Simulation(pop_hist_len=0).set().pragma_analyze( False).pragma_autocompact(True).pragma_comp_summary( False).pragma_fractional_mass(False). pragma_live_info(False).fn_group_setup(grp_setup).done().add([ DailyBehaviorRule('school', p_home_IS=0.90), DailyBehaviorRule('work', p_home_IS=0.90), # DiseaseRule('school', r0=1.8, p_E_IA=0.40, p_IA_IS=0.40, p_IS_R=0.40, p_home_E=0.025, p_social_E=0.05, soc_dist_comp_young=sdc, soc_dist_comp_old=sdc, p_fat_by_age_group=p_covid19_fat_by_age_group_comb), # DiseaseRule('work', r0=1.8, p_E_IA=0.40, p_IA_IS=0.40, p_IS_R=0.40, p_home_E=0.025, p_social_E=0.05, soc_dist_comp_young=sdc, soc_dist_comp_old=sdc, p_fat_by_age_group=p_covid19_fat_by_age_group_comb),
s = (Simulation().set().pragma_autocompact(True).pragma_live_info( False).done().add([ SegregationModel('team', len(loc)), Group(m=200, attr={'team': 'blue'}, rel={Site.AT: loc[0]}), Group(m=300, attr={'team': 'blue'}, rel={Site.AT: loc[1]}), Group(m=100, attr={'team': 'red'}, rel={Site.AT: loc[0]}), Group(m=400, attr={'team': 'red'}, rel={Site.AT: loc[1]}) ])) # ---------------------------------------------------------------------------------------------------------------------- te = (TrajectoryEnsemble().add_trajectory(Trajectory( 'segregation', None, s)).set_group_names([ (0, 'Blue A', Group.gen_hash(attr={'team': 'blue'}, rel={Site.AT: loc[0]})), (1, 'Blue B', Group.gen_hash(attr={'team': 'blue'}, rel={Site.AT: loc[1]})), (2, 'Red A', Group.gen_hash(attr={'team': 'red'}, rel={Site.AT: loc[0]})), (3, 'Red B', Group.gen_hash(attr={'team': 'red'}, rel={Site.AT: loc[1]})) ]).run(100)) def get_out_dir(filename): return os.path.join(os.path.dirname(__file__), 'out', filename) # te.traj[1].plot_mass_flow_time_series(filepath=get_out_dir('_plot.png'), iter_range=(-1,7), v_prop=False, e_prop=True) # te.traj[1].plot_mass_locus_streamgraph((900,400), get_out_dir('_plot.png')) # te.traj[1].plot_heatmap((800,800), get_out_dir('_plot.png'), (-1,20))
''' 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))
# ---------------------------------------------------------------------------------------------------------------------- # (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) ) # ---------------------------------------------------------------------------------------------------------------------- # (2) Restore a trajectory ensemble from the database for additional runs: # TrajectoryEnsemble(fpath_db).run(20)
super().__init__(i=i) self.seqihr_model = seqihr_model self.chi = chi self.rules.append(self.seqihr_model) def apply(self, pop, group, iter, t): self.seqihr_model.set_params(chi=self.chi) # ---------------------------------------------------------------------------------------------------------------------- # Simulations: if os.path.isfile(fpath_db): os.remove(fpath_db) te = TrajectoryEnsemble(fpath_db) if te.is_db_empty: te.set_pragma_memoize_group_ids(True) te.add_trajectories([(Simulation().add([ SARSQuarantineIntervention(SEQIHRModel('sars', beta=0.80, alpha_n=0.75, alpha_q=0.40, delta_n=0.01, delta_h=0.03, mu=0.01, chi=0.01, phi=0.20, rho=0.75, solver=ODESolver()),
if os.path.isfile(fpath_db): os.remove(fpath_db) te = ( TrajectoryEnsemble(fpath_db).set_pragma_memoize_group_ids(True). add_trajectories([ Trajectory(sim=( Simulation().add([ sir_a1, # sir_b1, # make_sir_b1_fixed_delay(iter0), # make_sir_b1_random_delay(iter0), # make_sir_b1_random_delay(), # make_sir_b1_random_delay(iter0_dist=gamma(a=5.0, loc=50.0, scale=25.0)), make_sir_b1_random_delay_gamma(), RecurrentFluProcess(i=IterInt(2000, 0), p_max=gamma_proc_p_max, a=5.0, scale=50.0), Group(m=950, attr={'flu': 's'}), Group(m=50, attr={'flu': 'i'}) ])) # ) for _ in range(5) # ) for iter0 in [900, 950, 1000, 1050, 1100] # ) for gamma_proc_p_max in uniform(loc=0.75, scale=0.20).rvs(1) ) for gamma_proc_p_max in [1.00] ]).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(4000)) # ---------------------------------------------------------------------------------------------------------------------- # Load data:
# ---------------------------------------------------------------------------------------------------------------------- conflict_dur = 1*12 # [months] env = Environment( MonthlyTemperature([(24,5), (26,5), (29,5), (33,5), (35,5), (35,5), (33,5), (32,5), (33,5), (33,5), (29,5), (25,5)]) # Sudan ) # ---------------------------------------------------------------------------------------------------------------------- # Simulations: fpath_traj_db = os.path.join(os.path.dirname(__file__), f'sim-05-traj.sqlite3') if os.path.isfile(fpath_traj_db): os.remove(fpath_traj_db) te = TrajectoryEnsemble(fpath_traj_db) if te.is_db_empty: te.set_pragma_memoize_group_ids(True) te.add_trajectories([ Trajectory( (Simulation(). add([ ConflictRule(severity=0.05, scale=scale, i=[0, conflict_dur]), MigrationRule(env, env_harshness_death_mult=0.1, migration_death_mult=0.0001), # most deaths due to environment (to show the seasonal effect) PopProbe(), Group(m=1*1000*1000, attr={ 'is-migrating': False }, rel={ Site.AT: site_conflict }) ]) ) ) for scale in np.arange(0.10, 0.30, 0.10) ])
def apply(self, pop, group, iter, t): p = self.p(iter) return [ GroupSplitSpec(p=p, attr_set={'flu': 's'}), GroupSplitSpec(p=1 - p) ] 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))
if os.path.isfile(fpath_db): os.remove(fpath_db) te = ( TrajectoryEnsemble(fpath_db).set_pragma_memoize_group_ids(True). add_trajectories([ Trajectory(sim=( Simulation().add([ # make time interaction more clear # make making/running distinction more clear make_sir(0.10, 0.05, i=IterAlways(), dt=0.1), make_sir(0.50, uniform(loc=0.01, scale=0.14).rvs(), i=IterInt( 900 + gamma(a=5.0, loc=5.0, scale=25.0).rvs(), 0), dt=0.1), RecurrentFluProcess(i=IterInt(2000, 0), p_max=gamma_proc_p_max, a=5.0, scale=50.0), Group(m=950, attr={'flu': 's'}), Group(m=50, attr={'flu': 'i'}) ]))) for gamma_proc_p_max in [None] # uniform(loc=0.75, scale=0.20).rvs(5) ]).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(4000)) # ---------------------------------------------------------------------------------------------------------------------- # Load data:
'school': school_l, 'home': home }), Group(m=50, attr={ 'flu': 'i', 'sex': 'm', 'income': 'l', 'pregnant': 'no', 'mood': 'annoyed' }, rel={ Site.AT: school_l, 'school': school_l, 'home': home }) ])) # ---------------------------------------------------------------------------------------------------------------------- def get_out_dir(filename): return os.path.join(os.path.dirname(__file__), 'out', filename) te = TrajectoryEnsemble().add_trajectory(Trajectory(s)).run(100) # te.traj[1].plot_mass_flow_time_series(filepath=get_out_dir('_plot-ts.png'), iter_range=(-1,10), v_prop=False, e_prop=True) te.traj[1].plot_mass_locus_streamgraph((1200, 300), get_out_dir('_plot-steam.png')) # te.traj[1].plot_heatmap((800,800), get_out_dir('_plot-heatmap.png'), (-1,20))
def graph(self): if os.path.isfile(fpath_db): os.remove(fpath_db) te = TrajectoryEnsemble(fpath_db) if te.is_db_empty: te.set_pragma_memoize_group_ids(True) te.add_trajectories([ (Simulation(). add([ SARSQuarantineIntervention( SEQIHRModel('sars', beta=0.80, alpha_n=0.75, alpha_q=0.40, delta_n=0.01, delta_h=0.03, mu=0.01, chi=0.01, phi=0.20, rho=0.75, solver=ODESolver()), chi=0.99, i=int(intervention_onset) ), Group(m=95000, attr={ 'sars': 's' }), Group(m= 5000, attr={ 'sars': 'e' }) ]) ) for intervention_onset in TN(30,120, 75,100, 5) ]) te.set_group_names(group_names) sys.stdout = open('out.dat', 'w') s.run(400) sys.stdout.close() with open('out.dat') as file: self.data = file.readlines() self.data = str(self.data) apple = self.data.replace('\\n', '<br>') # print(apple) te.plot_mass_locus_line ((1200,300), os.path.join(os.path.dirname(__file__), 'out', 'plot-line.png'), col_scheme='tableau10', opacity_min=0.35) te.plot_mass_locus_line_aggr((1200,300), os.path.join(os.path.dirname(__file__), 'out', 'plot-ci.png'), col_scheme='tableau10') fpath_diag = os.path.join(os.path.dirname(__file__), 'out', 'sim-04.diag') fpath_pdf = os.path.join(os.path.dirname(__file__), 'out', 'sim-04.pdf') te.traj[2].sim.gen_diagram(fpath_diag, fpath_pdf)
GroupSplitSpec(p=p, attr_set={ 'flu': 's' }), GroupSplitSpec(p=1-p) ] def is_applicable(self, group, iter, t): return super().is_applicable(group, iter, t) and group.has_attr({ 'flu': 'r' }) # ---------------------------------------------------------------------------------------------------------------------- # 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)
return [ GroupSplitSpec(p=0.8, rel_set={ Site.AT: group.get_rel('school') }), GroupSplitSpec(p=0.2) ] return None # ---------------------------------------------------------------------------------------------------------------------- home = Site('home') school_l = Site('school-l') school_m = Site('school-m') # if os.path.isfile(fpath_db): os.remove(fpath_db) te = TrajectoryEnsemble(fpath_db) if te.is_db_empty: te.add_trajectories([ Trajectory( (Simulation(). # set_pragma_live_info(True). add([ FluProgressRule(), FluLocationRule(), Group('g1', 450, attr={ 'flu': 's', 'sex': 'f', 'income': 'm', 'pregnant': 'no', 'mood': 'happy' }, rel={ Site.AT: school_m, 'school': school_m, 'home': home }), Group('g2', 50, attr={ 'flu': 'i', 'sex': 'f', 'income': 'm', 'pregnant': 'no', 'mood': 'annoyed' }, rel={ Site.AT: school_m, 'school': school_m, 'home': home }), Group('g3', 450, attr={ 'flu': 's', 'sex': 'm', 'income': 'm', 'pregnant': 'no', 'mood': 'happy' }, rel={ Site.AT: school_m, 'school': school_m, 'home': home }), Group('g4', 50, attr={ 'flu': 'i', 'sex': 'm', 'income': 'm', 'pregnant': 'no', 'mood': 'annoyed' }, rel={ Site.AT: school_m, 'school': school_m, 'home': home }), Group('g5', 450, attr={ 'flu': 's', 'sex': 'f', 'income': 'l', 'pregnant': 'no', 'mood': 'happy' }, rel={ Site.AT: school_l, 'school': school_l, 'home': home }), Group('g6', 50, attr={ 'flu': 'i', 'sex': 'f', 'income': 'l', 'pregnant': 'no', 'mood': 'annoyed' }, rel={ Site.AT: school_l, 'school': school_l, 'home': home }),
from pram.model.model import MCSolver from pram.model.epi import SIRSModel from pram.sim import Simulation from pram.traj import Trajectory, TrajectoryEnsemble # ---------------------------------------------------------------------------------------------------------------------- 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'))