def apply(self, sim: cv.Sim): ''' Perform testing ''' t = sim.t if self.n_tests[t]: # Compute weights for people who would test positive or negative positive_tests = np.zeros((sim.n, )) for i, person in enumerate(sim.people.values()): if person.infectious: positive_tests[i] = 1 negative_tests = 1 - positive_tests # Select the people to test in each category positive_inds = cv.choose_weighted(probs=positive_tests, n=min(sum(positive_tests), self.n_positive[t]), normalize=True) negative_inds = cv.choose_weighted( probs=negative_tests, n=min(sum(negative_tests), self.n_tests[t] - len(positive_inds)), normalize=True) # Todo - assess performance and optimize e.g. to reduce dict indexing for ind in positive_inds: person = sim.get_person(ind) person.test( t, test_sensitivity=1.0 ) # Sensitivity is 1 because the person is guaranteed to test positive sim.results['new_diagnoses'][t] += 1 for ind in negative_inds: person = sim.get_person(ind) person.test(t, test_sensitivity=1.0) sim.results['new_tests'][t] += self.n_tests[t] return
def apply(self, sim: cv.Sim): t = sim.t # Check that there are still tests if t < len(self.daily_tests): n_tests = self.daily_tests[t] # Number of tests for this day sim.results['new_tests'][t] += n_tests else: return # If there are no tests today, abort early if not (n_tests and pl.isfinite(n_tests)): return test_probs = np.ones(sim.n) for i, person in enumerate(sim.people.values()): # Adjust testing probability based on what's happened to the person # NB, these need to be separate if statements, because a person can be both diagnosed and infectious/symptomatic if person.symptomatic: test_probs[i] *= self.sympt_test # They're symptomatic if person.known_contact: test_probs[ i] *= self.trace_test # They've had contact with a known positive if person.diagnosed: test_probs[i] = 0.0 test_inds = cv.choose_weighted(probs=test_probs, n=n_tests, normalize=True) for test_ind in test_inds: person = sim.get_person(test_ind) person.test(t, self.sensitivity) if person.diagnosed: sim.results['new_diagnoses'][t] += 1 return