def apply(self, sim): ''' Introduce new infections with this strain ''' for ind in cvi.find_day(self.days, sim.t, interv=self, sim=sim): # Time to introduce strain susceptible_inds = cvu.true(sim.people.susceptible) rescale_factor = sim.rescale_vec[sim.t] if self.rescale else 1.0 n_imports = sc.randround(self.n_imports/rescale_factor) # Round stochastically to the nearest number of imports importation_inds = np.random.choice(susceptible_inds, n_imports) sim.people.infect(inds=importation_inds, layer='importation', strain=self.index) return
def apply(self, sim): ''' Introduce new infections with this variant ''' for ind in cvi.find_day(self.days, sim.t, interv=self, sim=sim): # Time to introduce variant susceptible_inds = cvu.true(sim.people.susceptible) rescale_factor = sim.rescale_vec[sim.t] if self.rescale else 1.0 scaled_imports = self.n_imports / rescale_factor n_imports = sc.randround( scaled_imports ) # Round stochastically to the nearest number of imports if self.n_imports > 0 and n_imports == 0 and sim['verbose']: msg = f'Warning: {self.n_imports:n} imported infections of {self.label} were specified on day {sim.t}, but given the rescale factor of {rescale_factor:n}, no agents were infected. Increase the number of imports or use more agents.' print(msg) importation_inds = np.random.choice( susceptible_inds, n_imports, replace=False ) # Can't use cvu.choice() since sampling from indices sim.people.infect(inds=importation_inds, layer='importation', variant=self.index) sim.results['n_imports'][sim.t] += n_imports return
def apply(self, sim): ''' Perform vaccination ''' t = sim.t if t < self.start_day: return elif self.end_day is not None and t > self.end_day: return # Check that there are still vaccines rel_t = t - self.start_day if rel_t < len(self.daily_vaccines): n_vaccines = sc.randround(self.daily_vaccines[rel_t] / sim.rescale_vec[t]) else: return vacc_probs = np.ones( sim.n ) # Begin by assigning equal vaccine weight (converted to a probability) to everyone if self.subtarget is not None: subtarget_inds, subtarget_vals = get_subtargets( self.subtarget, sim) vacc_probs[ subtarget_inds] = subtarget_vals # People being explicitly subtargeted # First dose # Don't give dose to people who have had at least one dose vacc_inds = cvu.true(self.vaccinations > 0) vacc_probs[vacc_inds] = 0.0 # Now choose who gets vaccinated and vaccinate them n_vaccines = min(n_vaccines, (vacc_probs != 0).sum()) all_vacc_inds = cvu.choose_w(probs=vacc_probs, n=n_vaccines, unique=True) # Choose who actually tests sim.results['new_doses'][t] += len(all_vacc_inds) self.vaccinations[all_vacc_inds] = 1 self.delay_days[all_vacc_inds] = self.delay self.first_dates[all_vacc_inds] = sim.t # Calculate the effect per person vacc_eff = self.cumulative[0] # Pull out corresponding effect sizes rel_sus_eff = 0.5 + 0.5 * self.rel_sus rel_symp_eff = 0.5 + 0.5 * self.rel_symp # Apply the vaccine to people #sim.people.rel_sus[all_vacc_inds] *= rel_sus_eff #sim.people.symp_prob[all_vacc_inds] *= rel_symp_eff sim.people.rel_sus[ all_vacc_inds] = self.orig_rel_sus[all_vacc_inds] * rel_sus_eff sim.people.symp_prob[ all_vacc_inds] = self.orig_symp_prob[all_vacc_inds] * rel_symp_eff all_vacc_inds2 = cvu.true( (self.vaccinations == 1) * (self.delay_days > 0) * (self.first_dates > 0) * (self.first_dates + self.delay_days == sim.t)) rel_sus_eff2 = self.rel_sus rel_symp_eff2 = self.rel_symp #sim.people.rel_sus[all_vacc_inds2] *= rel_sus_eff2 #sim.people.symp_prob[all_vacc_inds2] *= rel_symp_eff2 sim.people.rel_sus[ all_vacc_inds2] = self.orig_rel_sus[all_vacc_inds2] * rel_sus_eff2 sim.people.symp_prob[all_vacc_inds2] = self.orig_symp_prob[ all_vacc_inds2] * rel_symp_eff2 self.vaccinations[all_vacc_inds2] = 2 sim.results['new_doses'][t] += len(all_vacc_inds2)
def apply(self, sim): ''' Perform vaccination ''' t = sim.t if t < self.start_day: return elif self.end_day is not None and t > self.end_day: return # Check that there are still vaccines rel_t = t - self.start_day if rel_t < len(self.daily_vaccines): n_vaccines = sc.randround(self.daily_vaccines[rel_t]/sim.rescale_vec[t]) # Correct for scaling that may be applied by rounding to the nearest number of tests if not (n_vaccines and np.isfinite(n_vaccines)): # If there are no doses today, abort early return else: if sim.rescale_vec[t] != sim['pop_scale']: raise RuntimeError('bad rescale time') else: return vacc_probs = np.ones(sim.n) # Begin by assigning equal vaccine weight (converted to a probability) to everyone # Remove minors ppl = sim.people inds0 = cv.true(ppl.age <= 18) vacc_probs[inds0] *= 0 # add age priority inds1 = cv.true(ppl.age >= self.age_priority) vacc_probs[inds1] *= 10 # Handle scheduling # first dose: vacc_probs[self.vaccinations == 0] *= self.dose_priority[0] # time between first and second dose: no_dose = [sim.t < (d[0] + self.delay) if len(d) > 0 else False for d in self.vaccination_dates] vacc_probs[no_dose] *= 0 # time available for second dose: second_dose = [sim.t >= (d[0] + self.delay) if len(d) > 0 else False for d in self.vaccination_dates] vacc_probs[second_dose] *= self.dose_priority[1] # Don't give dose 2 people who have had more than 1 vacc_inds = cvu.true(self.vaccinations > 1) vacc_probs[vacc_inds] = 0.0 # Now choose who gets vaccinated and vaccinate them n_vaccines = min(n_vaccines, (vacc_probs!=0).sum()) # Don't try to vaccinate more people than have nonzero vaccination probability all_vacc_inds = cvu.choose_w(probs=vacc_probs, n=n_vaccines, unique=True) # Choose who actually tests sim.results['new_doses'][t] += len(all_vacc_inds) # Did the vaccine take? vacc_take_inds = all_vacc_inds[np.logical_or(cvu.n_binomial(self.take_prob, len(all_vacc_inds)) & (self.vaccinations[all_vacc_inds] == 0), self.vaccine_take[all_vacc_inds] & (self.vaccinations[all_vacc_inds] == 1))] self.vaccine_take[vacc_take_inds] = True # Calculate the effect per person vacc_doses = self.vaccinations[vacc_take_inds] # Calculate current doses eff_doses = np.minimum(vacc_doses, len(self.cumulative) - 1) # Convert to a valid index vacc_eff = self.cumulative[eff_doses] # Pull out corresponding effect sizes rel_trans_eff = (1.0 - vacc_eff) + vacc_eff * self.rel_trans rel_symp_eff = (1.0 - vacc_eff) + vacc_eff * self.rel_symp # Apply the vaccine to people sim.people.rel_trans[vacc_take_inds] = self.orig_rel_trans[vacc_take_inds]*rel_trans_eff sim.people.symp_prob[vacc_take_inds] = self.orig_symp_prob[vacc_take_inds]*rel_symp_eff self.vaccinations[all_vacc_inds] += 1 for v_ind in all_vacc_inds: self.vaccination_dates[v_ind].append(sim.t) return
legend_args={'loc': 'upper left'}, axis_args={'hspace': 0.4}, interval=50, n_cols=1) pl.axhline(1, linestyle='--', c=[0.8, 0.4, 0.4], alpha=1, lw=4) pl.axvline(sim.day('2021-05-01'), linestyle='--') pl.text(sim.day('2021-05-01') + 2, 8, 'ii.') pl.axvline(sim.day('2021-04-06'), linestyle='--') pl.text(sim.day('2021-04-06') + 2, 8, 'i.') pl.title('Scenario 6: Effective reproduction number') #%% total = [] for i in range(18): result = [] result.append(sc.randround(sims[i].results['cum_infections'][-1])) result.append(sc.randround(sims[i].results['cum_symptomatic'][-1])) result.append(sc.randround(sims[i].results['cum_deaths'][-1])) total.append(result) total #%% exposed = [] symptomatic = [] deaths = [] for i in range(18): exposed.append(sims[i].get_analyzer().hists[0][1].tolist()) symptomatic.append(sims[i].get_analyzer().hists[0][2].tolist()) deaths.append(sims[i].get_analyzer().hists[0][3].tolist())