def do_shedding_update(human_pop): # This is not optimal. Only optimize if it really helps global fertility if fertility is None: config = json.loads(open("nd.json").read()) fertility = config["Enable_Birth"] and config["Enable_Vital_Dynamics"] stat_pop = 0 for human in human_pop: hum_id = human["id"] mcw = gi.get_mcw(hum_id) #Can we make this conditional on if fertility: nd.update_node_stats( (mcw, 0, gi.is_possible_mother(hum_id), 0)) # mcw, infectiousness, is_poss_mom, is_infected """ if gi.is_infected(hum_id): if gi.has_latent_infection(hum_id): print( "{0} has latent infection.".format( hum_id ) ) else: print( "{0} has active infection (I guess).".format( hum_id ) ) else: print( "{0} has no infection.".format( hum_id ) ) """ stat_pop += mcw if gi.is_infected(hum_id): gi.update1(hum_id) # this should do shedding & vital-dynamics return stat_pop
def expose_callback(individual_id): """ This function is the callback for exposure. It is registered with the intrahost module and then called for each individual for each timestep. This is where you decide whether an individual should get infected or not. In the limit, you could just return False here and no-one would ever get infected. If you just returned True, everyone would be infected always. The expectation is that you write some code that uses the contagion shed and does some math. To be heterogeneous, you can use the individual id. The action and prob parameters can be ignored. """ # The following code is just to demo the use of TBHIV-specific getters if gi.is_infected(individual_id): #print( "Individual {0} is apparently already infected.".format( individual_id ) ) return 0 if gi.get_immunity(individual_id) == 0: return 0 global timestep #print( "timestep = {0}, outbreak_timestep = {1}.".format( timestep, outbreak_timestep ) ) #if timestep in outbreak_timesteps: # #if gi.get_immunity( individual_id ) == 1.0 and random.random() < 0.1: # let's infect some people at random (outbreaks) # if random.random() < outbreak_coverage: # let's infect some people at random (outbreaks) # print( "Let's force-infect (outbreak) uninfected, non-immune individual based on random draw." ) # return 1 if (timestep == 1) and (individual_id == 13): return 1 # force-infect individual 13 at time 0 else: if individual_id == 0: pdb.set_trace() global contagion_buckets #global contagion_bucket_homog #print( "Exposing individual {0} to contagion {1}.".format( individual_id, contagion_bucket ) ) #HINT-y code here contagion = contagion_buckets[CHILD] + contagion_buckets[ADULT] me = ADULT if gi.get_age( individual_id) >= ADULT_CHILD_AGE_THRESHOLD else CHILD my_factor_from_child = factors[me][CHILD] my_factor_from_adult = factors[me][ADULT] contagion = (contagion_buckets[CHILD] * my_factor_from_child) + ( contagion_buckets[ADULT] * my_factor_from_adult) contagion /= len(human_pop) #print( "HINT-y math: I am an {}, exposed to contagion {}.".format( "CHILD" if me == CHILD else "ADULT", contagion ) ) #contagion = contagion_bucket_homog if gi.should_infect((individual_id, contagion)): return 1 return 0
def update(self): self.time += 1 for h in self.humans: dgi.update(h) # Pull people who've changed out of human and into werewolves if dgi.is_infected(h) and not dgi.is_incubating(h): self.humans.remove(h) self.waiting_wolves.remove( h ) # See above, they are in two places and need to be removed self.werewolves.append(h) if self.debug: print(f"Individual {h} is a wolf!") if self.time % HALLOWEEN_DAY == 0: # It is october 31 if len(self.werewolves) == 0: # and there are no werewolves found_one = False possible_humans = len(self.humans) ages = [] min_age_exposure = self.min_age_werewolf_years * DAYS_YEAR while not found_one and possible_humans > 0: for h in self.humans: possible_humans -= 1 age = dgi.get_age(h) ages.append(age) if age > min_age_exposure: if age % DAYS_YEAR == HALLOWEEN_DAY: self.humans.remove(h) self.werewolves.append(h) found_one = True break if found_one: print("Found a new werewolf with a Halloween Birthday.") else: print("No cool birthdays, just taking someone.") future_wolf = None for h in self.humans: age = dgi.get_age(h) if not future_wolf and age > min_age_exposure: self.humans.remove(h) self.werewolves.append(h) future_wolf = h if future_wolf: print("Found someone old enough.") else: print("No one old enough! No outbreak!") print(ages) sys.exit() if self.enable_reporting: self.report_step()
def expose_callback( action, prob, individual_id ): """ This function is the callback for exposure. It is registered with the intrahost module and then called for each individual for each timestep. This is where you decide whether an individual should get infected or not. In the limit, you could just return False here and no-one would ever get infected. If you just returned True, everyone would be infected always. The expectation is that you write some code that uses the contagion shed and does some math. To be heterogeneous, you can use the individual id. The action and prob parameters can be ignored. """ # The following code is just to demo the use of TBHIV-specific getters if gi.is_infected( individual_id ): #print( "Individual {0} is apparently already infected.".format( individual_id ) ) return 0 if gi.get_immunity( individual_id ) == 0: return 0 global timestep #print( "timestep = {0}, outbreak_timestep = {1}.".format( timestep, outbreak_timestep ) ) if timestep in outbreak_timesteps: #if gi.get_immunity( individual_id ) == 1.0 and random.random() < 0.1: # let's infect some people at random (outbreaks) if random.random() < outbreak_coverage: # let's infect some people at random (outbreaks) print( "Let's force-infect (outbreak) uninfected, non-immune individual based on random draw." ) return 1 else: if individual_id == 0: pdb.set_trace() global contagion_bucket #print( "Exposing individual {0} to contagion {1}.".format( individual_id, contagion_bucket ) ) if gi.should_infect( ( individual_id, contagion_bucket ) ): return 1 return 0
def is_incubating(person): if gi.is_infected(person) and gi.get_infectiousness(person) == 0: return True else: return False
def do_vitaldynamics_update(human_pop, graveyard, contagion, census_cb=None, death_cb=None): num_infected = 0 num_incubating = 0 num_active = 0 num_suscept = 0 num_recover = 0 num_people = 0 num_deaths = 0 new_graveyard = [] for human in human_pop: hum_id = human["id"] if census_cb != None: census_cb(hum_id) gi.update2( hum_id ) # this should do exposure; possible optimization would be to skip this entirely if zero contagion mcw = gi.get_mcw(hum_id) if gi.is_dead(hum_id): # somebody died print("{0} is dead.".format(hum_id)) new_graveyard.append(human) num_deaths += mcw # can't use len(graveyard) coz mcw if death_cb != None: death_cb(hum_id) num_people += mcw global fertility if fertility: ipm = gi.is_possible_mother(hum_id) ip = gi.is_pregnant(hum_id) if hum_id == 0: pdb.set_trace() age = gi.get_age(hum_id) #print( "Calling cfp with {0}, {1}, {2}, and {3}.".format( str(ipm), str(ip), str(age), str(hum_id) ) ) # TBD: Optimization? I happen to know that this is only necessary for females of a # certain age. But technically that knowledge is for nd. nd.consider_for_pregnancy((ipm, ip, hum_id, age, 1.0)) #print( str( json.loads(gi.serialize( hum_id ))["individual"]["susceptibility"] ) ) if gi.is_infected(hum_id): if is_incubating(hum_id): num_incubating += mcw else: num_infected += mcw # TBD: use_mcw elif gi.get_immunity(hum_id) != 1.0: num_recover += mcw # TBD: use mcw else: num_suscept += mcw # TBD: use mcw #if gi.has_active_infection( hum_id ): # num_active += 1 # serialize seems to be broken when you had an intervention (or at least a SimpleVaccine) #serial_man = gi.serialize( hum_id ) #if hum_id == 1: #print( json.dumps( json.loads( serial_man ), indent=4 ) ) #print( "infectiousness: " + str( json.loads( serial_man )["individual"]["infectiousness"] ) ) #print( "Updating fertility for this timestep." ) for corpse in new_graveyard: if corpse in human_pop: human_pop.pop(human_pop.index(corpse)) else: print("Exception trying to remove individual from python list: " + str(ex)) graveyard.extend(new_graveyard) nd.update_fertility() exposeds.append(num_incubating) prevalence.append(num_infected) active_prevalence.append(num_active) susceptible.append(num_suscept) recovered.append(num_recover) disdeaths.append(num_deaths)
def do_vitaldynamics_update(human_pop, graveyard, census_cb=None, death_cb=None): num_infected = 0 num_incubating = 0 num_active = 0 num_suscept = 0 num_recover = 0 new_graveyard = [] for human in human_pop: hum_id = human["id"] if census_cb != None: census_cb(hum_id) gi.update2(hum_id) # this should do exposure if gi.is_dead(hum_id): # somebody died print("{0} is dead.".format(hum_id)) new_graveyard.append(human) if death_cb != None: death_cb(hum_id) # TESTING THIS ipm = gi.is_possible_mother(hum_id) ip = gi.is_pregnant(hum_id) if hum_id == 0: pdb.set_trace() age = gi.get_age(hum_id) #print( "Calling cfp with {0}, {1}, {2}, and {3}.".format( str(ipm), str(ip), str(age), str(hum_id) ) ) nd.consider_for_pregnancy((ipm, ip, hum_id, age, 1.0)) #print( str( json.loads(gi.serialize( hum_id ))["individual"]["susceptibility"] ) ) if gi.is_infected(hum_id): if is_incubating(hum_id): num_incubating += 1 else: num_infected += 1 # TBD: use_mcw elif gi.get_immunity(hum_id) != 1.0: num_recover += 1 # TBD: use mcw else: num_suscept += 1 # TBD: use mcw #if gi.has_active_infection( hum_id ): # num_active += 1 # serialize seems to be broken when you had an intervention (or at least a SimpleVaccine) #serial_man = gi.serialize( hum_id ) #if hum_id == 1: #print( json.dumps( json.loads( serial_man ), indent=4 ) ) #print( "infectiousness: " + str( json.loads( serial_man )["individual"]["infectiousness"] ) ) #print( "Updating fertility for this timestep." ) for corpse in new_graveyard: if corpse in human_pop: human_pop.pop(human_pop.index(corpse)) else: print("Exception trying to remove individual from python list: " + str(ex)) graveyard.extend(new_graveyard) nd.update_fertility() exposeds.append(num_incubating) prevalence.append(num_infected) active_prevalence.append(num_active) susceptible.append(num_suscept) recovered.append(num_recover) disdeaths.append(len(graveyard))
print("Forced infections.") for x in range(25): for person in people: dgi.update(person) pass pass incubating = 0 infected = 0 for person in people: if dgi.is_incubating(person): incubating += 1 pass if dgi.is_infected(person): infected += 1 pass pass print(f"25 days later... incubating: {incubating}\tinfected: {infected}") for x in range(5): for person in people: dgi.update(person) pass pass incubating = 0 infected = 0 for person in people: if dgi.is_incubating(person):