def apply(self, pop, group, iter, t): mouse_group = pop.get_group(GroupQry({'form': "m"})) mouse_population = mouse_group.m hawk_group = pop.get_group(GroupQry({'form': 'h'})) hawk_population = hawk_group.m / 5 appetite = mouse_population * hawk_population * PREDATION_COEFFICIENT if group.attr['form'] == 'h': p = PREDATOR_MORTALITY p = p * TIME_COEFFICIENT return [ GroupSplitSpec(p=p, attr_set={'form': "c"}), GroupSplitSpec(p=1 - p) ] elif group.attr['form'] == 'm': if appetite > mouse_population: appetite = mouse_population p = appetite / mouse_population p = p * TIME_COEFFICIENT return [ GroupSplitSpec(p=p, attr_set={'form': "h"}), GroupSplitSpec(p=1 - p) ] else: return [GroupSplitSpec(p=1)]
def apply_keep_migrating(self, pop, group, iter, t): migrating_groups = pop.get_groups( GroupQry(cond=[lambda g: g.has_attr({'is-migrating': True})])) if migrating_groups and len(migrating_groups) > 0: migrating_m = sum([g.m for g in migrating_groups]) migrating_p = migrating_m / pop.m * 100 else: migrating_p = 0 env_harshness = self.env.get_harshness(iter % 11, True) p_death = min( env_harshness * self.env_harshness_death_mult + migrating_p * self.migration_death_mult, 1.00) time_traveled = 1 if env_harshness <= 0.90 else 0 # no travel in very harsh climate return [ GroupSplitSpec(p=p_death, attr_set=Group.VOID), GroupSplitSpec(p=1 - p_death, attr_set={ 'migration-time': group.get_attr('migration-time') + 1, 'travel-time-left': group.get_attr('travel-time-left') - time_traveled }) ]
def apply_keep_migrating(self, pop, group, iter, t): migrating_groups = pop.get_groups( GroupQry(cond=[lambda g: g.has_attr({'is-migrating': True})])) if migrating_groups and len(migrating_groups) > 0: # most attribute access does not work; relevant methods should be called instead # migrating_m = sum([g.m for g in migrating_groups]) # migrating_p = migrating_m / pop.m * 100 # Note however an inconsistency - getting the mass of each agent will be the square of what we want # migrating_m = sum([g.get_mass() for g in migrating_groups]) migrating_m = len(migrating_groups) migrating_p = migrating_m / pop.get_mass() * 100 else: migrating_p = 0 p_death = min( self.env_harshness * self.env_harshness_death_mult + migrating_p * self.migration_death_mult, 1.00) return [ GroupSplitSpec(p=p_death, attr_set=Group.VOID), GroupSplitSpec(p=1 - p_death, attr_set={ 'migration-time': group.get_attr('migration-time') + 1, 'travel-time-left': group.get_attr('travel-time-left') - 1 }) ]
def get_split_specs(self, group, age_inc=1): ''' age_inc - how much to increment the 'age' attribute ''' age = group.ga('age') if age < self.AGE_0: return [ GroupSplitSpec(p=1.00, attr_set={ 'age': age + age_inc, 'ad': False }) ] l = self.get_lambda(age) p0 = poisson(l).pmf(0) print( f'n: {round(group.m,2):>12} age: {age:>3} l: {round(l,2):>3} p0: {round(p0,2):<4} p1: {round(1-p0,2):<4}' ) return [ GroupSplitSpec(p=1.00, attr_set={ 'age': age + age_inc, 'ad': False }) ] # testing so don't move mass
def apply(self, pop, group, iter, t): c = self.__class__ # shorthand age = group.get_attr('age') if age >= c.AGE: return [GroupSplitSpec(p=1.00, attr_set={ 'age': age + 1, 'ad': True })] else: return [GroupSplitSpec(p=1.00, attr_set={ 'age': age + 1, 'ad': False })]
def apply_morning(self, pop, group, iter, t): if pop.sim.get_var('closedown'): return None if group.has_attr({ disease_name: 'IS' }): return [ GroupSplitSpec(p=1.0 - self.p_home_IS, rel_set={ Site.AT: group.get_rel(self.default_dst_site) }), GroupSplitSpec(p= self.p_home_IS) ] return [GroupSplitSpec(p=1.0, rel_set={ Site.AT: group.get_rel(self.default_dst_site) })]
def apply(self, pop, group, iter, t): p = 0 if group.m >= pop.get_group(GroupQry({'form': "m"})).m / 4: p = 0.1 return [ GroupSplitSpec(p=p, attr_set={"form": "c"}), GroupSplitSpec(p=1 - p) ]
def apply(self, pop, group, iter, t): p_infection = 0.05 if group.get_attr('flu-stage') == AttrFluStage.NO: # if group.has_attr({ 'flu-stage': AttrFluStage.NO }): return [ GroupSplitSpec(p=1 - p_infection, attr_set={'flu-stage': AttrFluStage.NO}), GroupSplitSpec(p=p_infection, attr_set={'flu-stage': AttrFluStage.ASYMPT}), GroupSplitSpec(p=0.00, attr_set={'flu-stage': AttrFluStage.SYMPT}) ] elif group.get_attr('flu-stage') == AttrFluStage.ASYMPT: # elif group.has_attr({ 'flu-stage': AttrFluStage.ASYMPT }): return [ GroupSplitSpec(p=0.20, attr_set={'flu-stage': AttrFluStage.NO}), GroupSplitSpec(p=0.00, attr_set={'flu-stage': AttrFluStage.ASYMPT}), GroupSplitSpec(p=0.80, attr_set={'flu-stage': AttrFluStage.SYMPT}) ] elif group.get_attr('flu-stage') == AttrFluStage.SYMPT: # elif group.has_attr({ 'flu-stage': AttrFluStage.SYMPT }): return [ GroupSplitSpec(p=0.05, attr_set={'flu-stage': AttrFluStage.NO}), GroupSplitSpec(p=0.20, attr_set={'flu-stage': AttrFluStage.ASYMPT}), GroupSplitSpec(p=0.75, attr_set={'flu-stage': AttrFluStage.SYMPT}) ] else: raise ValueError("Invalid value for attribute 'flu-stage'.")
def apply(self, pop, group, iter, t): if group.has_attr({ 'flu': 'S', 'flu': 'S' }) and group.has_attr({'flu': 'S'}): return [ GroupSplitSpec(p=0.7, attr_set={'flu': 'S'}), GroupSplitSpec(p=0.3, attr_set={'flu': 'I'}), ] if group.has_attr({'flu': 'R'}): return [ GroupSplitSpec(p=0.7, attr_set={'flu': 'R'}), GroupSplitSpec(p=0.3, attr_set={'flu': 'S'}), ] if group.has_attr({'flu': 'I'}): return [ GroupSplitSpec(p=0.5, attr_set={'flu': 'I'}), GroupSplitSpec(p=0.5, attr_set={'flu': 'R'}), ] if group.has_attr({'flu': 'S'}): return [ GroupSplitSpec(p=0.7, attr_set={'flu': 'S'}), GroupSplitSpec(p=0.3, attr_set={'flu': 'I'}), GroupSplitSpec(p=0, attr_set={'flu': 'R'}), ]
def apply(self, pop, group, iter, t): # A Dynamic Bayesian net with the initial state distribution: # # flu: (1 0 0) # # and the transition model: # # s e r # s 0.95 0.00 0.10 # e 0.05 0.50 0 # r 0 0.50 0.90 p = 0.05 # prob of infection if group.has_attr({'flu': 's'}): return [ GroupSplitSpec(p=1 - p, attr_set={'flu': 's'}), GroupSplitSpec(p=p, attr_set={'flu': 'e'}), GroupSplitSpec(p=0.00, attr_set={'flu': 'r'}) ] if group.has_attr({'flu': 'e'}): return [ GroupSplitSpec(p=0.00, attr_set={'flu': 's'}), GroupSplitSpec(p=0.50, attr_set={'flu': 'e'}), GroupSplitSpec(p=0.50, attr_set={'flu': 'r'}) ] if group.has_attr({'flu': 'r'}): return [ GroupSplitSpec(p=0.10, attr_set={'flu': 's'}), GroupSplitSpec(p=0.00, attr_set={'flu': 'e'}), GroupSplitSpec(p=0.90, attr_set={'flu': 'r'}) ]
def apply(self, pop, group, iter, t): p_death = self.scale * self.severity * self.severity_death_mult p_migration = self.scale * self.scale_migration_mult site_dst = random.choice(sites_dst) return [ GroupSplitSpec(p=p_death, attr_set=Group.VOID), GroupSplitSpec(p=p_migration, attr_set={ 'is-migrating': True, 'migration-time': 0, 'travel-time-left': site_dst.get_attr('travel-time') }, rel_set={ 'site-dst': site_dst }), GroupSplitSpec(p=1 - p_death - p_migration) ]
def get_split_specs(self, group, age_inc=1): ''' age_inc - how much to increment the 'age' group attribute ''' age = group.get_attr('age') p = max(min(self.__class__.P * (age - self.__class__.AGE), 1.00), 0.00) return [ GroupSplitSpec(p= p, attr_set={ 'age': age + age_inc, 'ad': True }), GroupSplitSpec(p=1 - p, attr_set={ 'age': age + age_inc, 'ad': False }) ]
def apply(self, pop, group, iter, t): if group.has_attr({'stage': 'c'}): c_value = group.get_attr("value") or 0 # print("c_value is ",c_value) return [ GroupSplitSpec(p=0.3, attr_set={ 'stage': 'failure', 'value': 0 * c_value }), GroupSplitSpec(p=0.4, attr_set={ 'stage': 'c', 'value': 2 * c_value }), GroupSplitSpec(p=0.3, attr_set={ 'stage': 'success', 'value': 1.5 * c_value }) ] if group.has_attr({'stage': 'success'}): success_value = group.get_attr("value") or 0 return [ GroupSplitSpec(p=0.1, attr_set={ 'stage': 'failure', 'value': 0 * success_value }), GroupSplitSpec(p=0.2, attr_set={ 'stage': 'c', 'value': 2 * success_value }), GroupSplitSpec(p=0.7, attr_set={ 'stage': 'success', 'value': 1.5 * success_value }), ] if group.has_attr({'stage': 'failure'}): failure_value = group.get_attr("value") or 0 return [ GroupSplitSpec(p=0.75, attr_set={ 'stage': 'failure', 'value': 0 * failure_value }), GroupSplitSpec(p=0.2, attr_set={ 'stage': 'c', 'value': 2 * failure_value }), GroupSplitSpec(p=0.05, attr_set={ 'stage': 'success', 'value': 1.5 * failure_value }) ]
def apply(self, pop, group, iter, t): if group.has_attr({ disease_name: 'S' }): p_E = 0.0 if group.is_at_site_name(self.primary_E_site): prop_IA = group.get_site_at().get_mass_prop(gq_IA) prop_IS = group.get_site_at().get_mass_prop(gq_IS) prop_I = prop_IA + prop_IS p_E = min(1.0, self.sei2r_params.r0 * (prop_I)) else: if isinstance(self.pop_mobility, PopulationLocation): day = iter // 2 + 1 # ordinal number of day from the start of simulation (two iterations per day) p_social_E = min(self.pop_mobility.get_contacts_by_day_of_year(group.get_rel('home').name, 2020, day, True) or 0 / 100, self.p_social_E_max) elif isinstance(self.pop_mobility, float): p_social_E = min(self.pop_mobility, self.p_social_E_max) prop_IA = group.get_site_at().get_mass_prop(gq_IA) prop_IS = group.get_site_at().get_mass_prop(gq_IS) prop_I = prop_IA + prop_IS if group.get_attr('age_group') == '0-50': soc_dist_comp = self.soc_dist_comp_young else: soc_dist_comp = self.soc_dist_comp_old p_E = min(1.0, self.sei2r_params.r0 * (prop_I)) p_E = p_E * self.p_home_E + p_E * p_social_E * (1.0 - soc_dist_comp) if p_E == 0: # nothing to split (TODO: Can this be worked into PyPRAM?) return None else: return [ GroupSplitSpec(p=1 - p_E, attr_set={ disease_name: 'S' }), GroupSplitSpec(p= p_E, attr_set={ disease_name: 'E' }) ] if group.has_attr({ disease_name: 'E' }): return [ GroupSplitSpec(p=1 - self.sei2r_params.kappa_1, attr_set={ disease_name: 'E' }), GroupSplitSpec(p= self.sei2r_params.kappa_1, attr_set={ disease_name: 'IA' }), ] if group.has_attr({ disease_name: 'IA' }): return [ GroupSplitSpec(p=1 - self.sei2r_params.kappa_2, attr_set={ disease_name: 'IA' }), GroupSplitSpec(p= self.sei2r_params.kappa_2, attr_set={ disease_name: 'IS' }) ] if group.has_attr({ disease_name: 'IS' }): p_fat = self.p_fat_by_age_group.get(group.get_attr('age_group'), 0.0) if p_fat is None: raise ValueError(f'Unexpected age group: {age_group}') return [ GroupSplitSpec(p= p_fat, attr_set=Group.VOID), # TODO: Make this a special class GroupSplitSpec(p=1 - self.sei2r_params.gamma, attr_set={ disease_name: 'IS' }), GroupSplitSpec(p= self.sei2r_params.gamma - p_fat, attr_set={ disease_name: 'R' }) ]
def apply(self, pop, group, iter, t): p_death = self.scale * self.severity * self.severity_death_mult p_migration = self.scale * self.scale_migration_mult return [ GroupSplitSpec(p=p_death, attr_set=Group.VOID), GroupSplitSpec(p=p_migration, attr_set={ 'is-migrating': True, 'migration-time': 0 }), GroupSplitSpec(p=1 - p_death - p_migration) ]
def apply(self, pop, group, iter, t): if self.wave[iter] * self.wave[iter + 1] < 0: # the wave crosses the x axis (i.e., the function changes sign) self.p_iter_0 = iter self.last_applied_iter = self.last_applied_iter or iter # prevent None print(f'{iter:6} {iter - self.last_applied_iter:6}: gamma') self.last_applied_iter = iter if self.p_iter_0 is not None: p = self.p(iter) return [GroupSplitSpec(p=p, attr_set={ 'flu': 's' }), GroupSplitSpec(p=1-p)] else: return None
def apply(self, pop, group, iter, t): if group.has_attr({'attribute_0': 'S', 'attribute_1': 'M'}): return [ GroupSplitSpec(p=0.7272727272727273, attr_set={ 'attribute_0': 'S', 'attribute_1': 'M' }), ] if group.has_attr({'attribute_0': 'S'}): return [ GroupSplitSpec(p=0.7272727272727273, attr_set={'attribute_0': 'S'}), ] if group.has_attr({'attribute_0': 'R', 'attribute_1': 'M'}): return [ GroupSplitSpec(p=0.6903137789904502, attr_set={ 'attribute_0': 'R', 'attribute_1': 'M' }), GroupSplitSpec(p=0.3096862210095498, attr_set={ 'attribute_0': 'S', 'attribute_1': 'M' }), ] if group.has_attr({'attribute_0': 'R'}): return [ GroupSplitSpec(p=0.6903137789904502, attr_set={'attribute_0': 'R'}), ] if group.has_attr({'attribute_0': 'I'}): return [ GroupSplitSpec(p=0.4697674418604651, attr_set={'attribute_0': 'I'}), GroupSplitSpec(p=0.5302325581395348, attr_set={'attribute_0': 'R'}), ] if group.has_attr({'attribute_0': 'I', 'attribute_1': 'M'}): return [ GroupSplitSpec(p=0.4697674418604651, attr_set={ 'attribute_0': 'I', 'attribute_1': 'M' }), GroupSplitSpec(p=0.5302325581395348, attr_set={ 'attribute_0': 'R', 'attribute_1': 'M' }), ]
def apply(self, pop, group, iter, t): if group.has_attr({ disease_name: 'S' }): p_E = 0.0 if group.is_at_site_name(self.primary_E_site): prop_IA = group.get_site_at().get_mass_prop(gq_IA) prop_IS = group.get_site_at().get_mass_prop(gq_IS) prop_I = prop_IA + prop_IS p_E = min(1.0, self.r0 * (prop_I)) elif self.p_home_E > 0 or self.p_social_E > 0: prop_IA = group.get_site_at().get_mass_prop(gq_IA) prop_IS = group.get_site_at().get_mass_prop(gq_IS) prop_I = prop_IA + prop_IS if group.get_attr('age_group') == '0-50': soc_dist_comp = self.soc_dist_comp_young else: soc_dist_comp = self.soc_dist_comp_old p_E = min(1.0, self.r0 * (prop_I)) p_E = p_E * self.p_home_E + p_E * self.p_social_E * (1.0 - soc_dist_comp) if p_E == 0: # nothing to split (TODO: Can this be worked into PyPRAM?) return None else: return [ GroupSplitSpec(p=1 - p_E, attr_set={ disease_name: 'S' }), GroupSplitSpec(p= p_E, attr_set={ disease_name: 'E' }) ] if group.has_attr({ disease_name: 'E' }): return [ GroupSplitSpec(p=1 - self.p_E_IA, attr_set={ disease_name: 'E' }), GroupSplitSpec(p= self.p_E_IA, attr_set={ disease_name: 'IA' }), ] if group.has_attr({ disease_name: 'IA' }): return [ GroupSplitSpec(p=1 - self.p_IA_IS, attr_set={ disease_name: 'IA' }), GroupSplitSpec(p= self.p_IA_IS, attr_set={ disease_name: 'IS' }) ] if group.has_attr({ disease_name: 'IS' }): p_fat = self.p_fat_by_age_group.get(group.get_attr('age_group'), 0.0) if p_fat is None: raise ValueError(f'Unexpected age group: {age_group}') return [ GroupSplitSpec(p= p_fat, attr_set=Group.VOID), # TODO: Make this a special class GroupSplitSpec(p=1 - self.p_IS_R, attr_set={ disease_name: 'IS' }), GroupSplitSpec(p= self.p_IS_R - p_fat, attr_set={ disease_name: 'R' }) ]
def apply(self, pop, group, iter, t): move_chunk = round(self.p * group.m) if move_chunk == 0: return [GroupSplitSpec(p=1)] move_p = move_chunk / group.m return [ GroupSplitSpec(p=move_p, rel_set={ Site.AT: self.sites[random.randint( 0, len(self.sites) - 1)] }), GroupSplitSpec(p=1 - move_p) ]
def apply(self, pop, group, iter, t): if group.is_at_site_name('school'): p = group.get_rel('school').get_mass_prop( GroupQry(attr={'flu': 'i'})) else: p = self.p_infection_min if group.get_attr('flu') == 's': return [ GroupSplitSpec(p=1 - p, attr_set={'flu': 's'}), GroupSplitSpec(p=p, attr_set={'flu': 'i'}) ] elif group.get_attr('flu') == 'i': return [ GroupSplitSpec(p=0.95, attr_set={'flu': 'i'}), GroupSplitSpec(p=0.05, attr_set={'flu': 'r'}) ]
def apply(self, pop, group, iter, t): # Susceptible: if group.has_attr({ 'flu': 's' }): at = group.get_rel(Site.AT) n = at.get_pop_size() # total population at current location n_e = at.get_pop_size(GroupQry(attr={ 'flu': 'e' })) # exposed population at current location p_infection = float(n_e) / float(n) # changes every iteration (i.e., the source of the simulation dynamics) return [ GroupSplitSpec(p= p_infection, attr_set={ 'flu': 'e' }), GroupSplitSpec(p=1 - p_infection, attr_set={ 'flu': 's' }) ] # Exposed: if group.has_attr({ 'flu': 'e' }): return [ GroupSplitSpec(p=0.2, attr_set={ 'flu': 'r' }), GroupSplitSpec(p=0.5, attr_set={ 'flu': 'e' }), GroupSplitSpec(p=0.3, attr_set={ 'flu': 'e' }) ] # Recovered: if group.has_attr({ 'flu': 'r' }): return [ GroupSplitSpec(p=0.9, attr_set={ 'flu': 'r' }), GroupSplitSpec(p=0.1, attr_set={ 'flu': 's' }) ] raise ValueError('Unknown flu state')
def apply(self, pop, group, iter, t): if group.has_attr({'attribute_0': 'S', 'attribute_1': 'M'}): return [ GroupSplitSpec(p=0.7272727272727273, attr_set={ 'attribute_0': 'S', 'attribute_1': 'M' }), ] if group.has_attr({'attribute_0': 'S'}): return [ GroupSplitSpec(p=0.7272727272727273, attr_set={'attribute_0': 'S'}), ] if group.has_attr({'attribute_0': 'R'}): return [ GroupSplitSpec(p=0.6898907103825137, attr_set={'attribute_0': 'R'}), GroupSplitSpec(p=0.31010928961748635, attr_set={'attribute_0': 'S'}), ] if group.has_attr({'attribute_0': 'R', 'attribute_1': 'M'}): return [ GroupSplitSpec(p=0.6898907103825137, attr_set={ 'attribute_0': 'R', 'attribute_1': 'M' }), ] if group.has_attr({'attribute_0': 'I', 'attribute_1': 'M'}): return [ GroupSplitSpec(p=0.47086247086247085, attr_set={ 'attribute_0': 'I', 'attribute_1': 'M' }), GroupSplitSpec(p=0.5291375291375291, attr_set={ 'attribute_0': 'R', 'attribute_1': 'M' }), ] if group.has_attr({'attribute_0': 'I'}): return [ GroupSplitSpec(p=0.47086247086247085, attr_set={'attribute_0': 'I'}), GroupSplitSpec(p=0.5291375291375291, attr_set={'attribute_0': 'R'}), ]
def apply(self, pop, group, iter, t): # Susceptible: if group.has_attr({ 'flu': 's' }): at = group.get_rel(Site.AT) n = at.get_pop_size() # total population at the group's current location n_i = at.get_pop_size(GroupQry(attr={ 'flu': 'i' })) # infected population at the group's current location p_infection = float(n_i) / float(n) # changes every iteration (i.e., the source of the simulation dynamics) return [ GroupSplitSpec(p= p_infection, attr_set={ 'flu': 'i', 'mood': 'annoyed' }), GroupSplitSpec(p=1 - p_infection, attr_set={ 'flu': 's' }) ] # Infected: if group.has_attr({ 'flu': 'i' }): return [ GroupSplitSpec(p=0.2, attr_set={ 'flu': 'r', 'mood': 'happy' }), GroupSplitSpec(p=0.5, attr_set={ 'flu': 'i', 'mood': 'bored' }), GroupSplitSpec(p=0.3, attr_set={ 'flu': 'i', 'mood': 'annoyed' }) ] # Recovered: if group.has_attr({ 'flu': 'r' }): return [ GroupSplitSpec(p=0.9, attr_set={ 'flu': 'r' }), GroupSplitSpec(p=0.1, attr_set={ 'flu': 's' }) ]
def apply(self, pop, group, iter, t): if group.attr['form'] == 'c': mouse_group = pop.get_group(GroupQry({'form': "m"})) p = (mouse_group.m * MICE_REPRODUCTION_RATE) / group.m #mice_group_size/pop_size p = p * TIME_COEFFICIENT return [ GroupSplitSpec(p=p, attr_set={'form': "m"}), GroupSplitSpec(p=1 - p) ] else: return [GroupSplitSpec(p=1)]
def apply_eating(self, pop, group, iter, t): if np.random.random_sample() <= self.p_think: return if self.is_verbose: print('{:5} is done eating'.format(group.name)) group.get_rel(self.FORK_L).release(1) group.get_rel(self.FORK_R).release(1) return [GroupSplitSpec(p=1.0, attr_set={ self.ATTR: self.State.THINKING})]
def apply_at_home(self, group, t): p = { 8: 0.50, 9: 0.50, 10: 0.50, 11: 0.50, 12: 1.00 }.get(t, 0.00) # TODO: Provide these as a CDF # prob of going to school = f(time of day) return [ GroupSplitSpec(p=p, attr_set={ 'did-attend-school-today': True, 't-at-school': 0 }, rel_set={Site.AT: group.get_rel('school')}), GroupSplitSpec(p=1 - p) ]
def apply(self, pop, group, iter, t): migrating_groups = pop.get_groups( GroupQry(cond=[lambda g: g.has_attr({'is-migrating': True})])) if migrating_groups and len(migrating_groups) > 0: migrating_m = sum([g.m for g in migrating_groups]) migrating_p = migrating_m / pop.m * 100 else: migrating_p = 0 p_death = min( self.env_harshness * self.env_harshness_death_mult + migrating_p * self.migration_death_mult, 1.00) return [ GroupSplitSpec(p=p_death, attr_set=Group.VOID), GroupSplitSpec(p=1 - p_death, attr_set={ 'migration-time': group.get_attr('migration-time') + 1 }) ]
def apply(self, pop, group, iter, t): # Infected and poor: if group.has_attr({'flu': 'i', 'income': 'l'}): return [ GroupSplitSpec(p=0.1, rel_set={Site.AT: group.get_rel('home')}), GroupSplitSpec(p=0.9) ] # Infected and rich: if group.has_attr({'flu': 'i', 'income': 'm'}): return [ GroupSplitSpec(p=0.6, rel_set={Site.AT: group.get_rel('home')}), GroupSplitSpec(p=0.4) ] # Recovered: if group.has_attr({'flu': 'r'}): return [ GroupSplitSpec(p=0.8, rel_set={Site.AT: group.get_rel('school')}), GroupSplitSpec(p=0.2) ] return None
def apply(self, pop, group, iter, t): # Susceptible: if group.has_attr({'flu': 's'}): at = group.get_rel(Site.AT) n = at.get_pop_size() # total population at current location n_i = at.get_pop_size(GroupQry( attr={'flu': 'i'})) # infected population at current location p_infection = float(n_i) / float( n ) # changes every iteration (i.e., the source of the simulation dynamics) return [ GroupSplitSpec(p=p_infection, attr_set={'flu': 'i'}), GroupSplitSpec(p=1 - p_infection, attr_set={'flu': 's'}) ] # Infected: if group.has_attr({'flu': 'i'}): return [ GroupSplitSpec(p=0.2, attr_set={ 'flu': 'r' }), # group size after: 20% of before (recovered) GroupSplitSpec(p=0.8, attr_set={ 'flu': 'i' }) # group size after: 80% of before (still infected) ] # Recovered: if group.has_attr({'flu': 'r'}): return [ GroupSplitSpec(p=0.9, attr_set={'flu': 'r'}), GroupSplitSpec(p=0.1, attr_set={'flu': 's'}) ]
def apply(self, pop, group, iter, t): p = self.p_infection_min if group.has_rel({ Site.AT: group.get_rel('school') }): site = group.get_rel(Site.AT) na = site.get_pop_size(GroupQry(attr={ 'flu': 'i' })) ns = site.get_pop_size(GroupQry(attr={ 'flu': 'r' })) p = self.get_p_infection_site(na, ns) if group.get_attr('flu') == 's': return [ GroupSplitSpec(p=1 - p, attr_set={ 'flu': 's' }), GroupSplitSpec(p=p, attr_set={ 'flu': 'i' }) ] elif group.get_attr('flu') == 'i': return [ GroupSplitSpec(p=0.50, attr_set={ 'flu': 'i' }), GroupSplitSpec(p=0.50, attr_set={ 'flu': 's' }) ] elif group.get_attr('flu') == 'r': return [ GroupSplitSpec(p=0.10, attr_set={ 'flu': 's' }), GroupSplitSpec(p=0.90, attr_set={ 'flu': 'r' }) ] else: raise ValueError("Invalid value for attribute 'flu'")