def test_basic_population(self): popSize = 100 numYears = 3 pop = NHANESDirectSamplePopulation(popSize, 1999) for i in range(1, numYears): pop.advance(1) self.assertEqual(popSize, len(pop._people)) self.assertEqual(numYears, len(pop._people.iloc[0]._age))
def test_people_from_population(self): test_population = NHANESDirectSamplePopulation( n=self.test_n, year=2015, random_seed=self.pandas_seed ) test_people = test_population._people test_ages = [x._age[0] for x in test_people] self.assertAlmostEqual(np.mean(test_ages), self.test_sample.age.mean(), delta=0.000001)
def testAdvanceAfterRollbackWorksOnWholePopulation(self): oftenMIPop = NHANESDirectSamplePopulation(self.popSize, 2001) oftenMIPop._outcome_model_repository = TestOftenMIModelRepository( 0.2, 0.2, 0.2) # this requires that we rollback a lot of events. oftenMIPop.set_bp_treatment_strategy(addABPMedMILargeEffectSize()) oftenMIPop.advance_vectorized(5) ageLength = pd.Series( [len(person._age) for i, person in oftenMIPop._people.iteritems()]) dead = pd.Series( [person.is_dead() for i, person in oftenMIPop._people.iteritems()]) numberWithFullFollowup = pd.Series([ person.is_dead() or len(person._age) == 6 for i, person in oftenMIPop._people.iteritems() ]).sum() # some people were getting "lost" when they had events to rollback of if the had non CV daeths... # this way everybody either is clearly marekd as dead or has compelte follow up self.assertEqual(self.popSize, numberWithFullFollowup)
def testRollbackFatalEventsRollsBackDeath(self): neverMIPop = NHANESDirectSamplePopulation(self.popSize, 2001) neverMIPop._outcome_model_repository = TestOftenMIModelRepository( 1.0, 1.0) neverMIPop.advance_vectorized(1) # the whole popuulation should have MIs at baseline numberOfMIsInBasePopulation = pd.Series([ person.has_mi_during_simulation() for i, person in neverMIPop._people.iteritems() ]).sum() self.assertEqual(self.popSize, numberOfMIsInBasePopulation) numberOfFatalMIsInBasePopulation = pd.Series([ person.has_mi_during_simulation() & person.is_dead() for i, person in neverMIPop._people.iteritems() ]).sum() self.assertEqual(self.popSize, numberOfFatalMIsInBasePopulation) neverMIPop = NHANESDirectSamplePopulation(self.popSize, 2001) neverMIPop._outcome_model_repository = TestOftenMIModelRepository( 1.0, 1.0) # this requires that we rollback a lot of events. neverMIPop.set_bp_treatment_strategy(addABPMedMILargeEffectSize()) neverMIPop.advance_vectorized(1) numberOfMIsAfterRecalibration = pd.Series([ person.has_mi_during_simulation() for i, person in neverMIPop._people.iteritems() ]).sum() numberOfFatalMIsAfterRecalibration = pd.Series([ person.has_mi_during_simulation() & person.is_dead() for i, person in neverMIPop._people.iteritems() ]).sum() self.assertGreater(numberOfFatalMIsInBasePopulation, numberOfFatalMIsAfterRecalibration)
def testRecalibrationReducesMIsWhenEffectSizeIsClincallyLargerButNumericallySmaller( self): neverMIPop = NHANESDirectSamplePopulation(self.popSize, 2001) neverMIPop._outcome_model_repository = TestOftenMIModelRepository(0.5) neverMIPop.advance_vectorized(1) # abou thalf of hte population has an MI at baseline numberOfMIsInBasePopulation = pd.Series([ person.has_mi_during_simulation() for i, person in neverMIPop._people.iteritems() ]).sum() # set a treatment strategy on teh population neverMIPop = NHANESDirectSamplePopulation(self.popSize, 2001) neverMIPop._outcome_model_repository = TestOftenMIModelRepository(0.5) neverMIPop.set_bp_treatment_strategy(addABPMedMILargeEffectSize()) neverMIPop.advance_vectorized(1) numberOfMIsInRecalibratedPopulation = pd.Series([ person.has_mi_during_simulation() for i, person in neverMIPop._people.iteritems() ]).sum() self.assertGreater(numberOfMIsInBasePopulation, numberOfMIsInRecalibratedPopulation)
def testRecalibrationReducesStrokesWhenEffectSizeIsClincallyLargerButNumericallySmaller( self): alwaysStrokePop = NHANESDirectSamplePopulation(self.popSize, 2001) alwaysStrokePop._outcome_model_repository = TestOftenStrokeModelRepository( 0.5) alwaysStrokePop.advance_vectorized(1) # about half of people shoudl have strokes numberOfStrokesInBasePopulation = pd.Series([ person.has_stroke_during_simulation() for i, person in alwaysStrokePop._people.iteritems() ]).sum() # set a treatment strategy on teh population alwaysStrokePop = NHANESDirectSamplePopulation(self.popSize, 2001) alwaysStrokePop._outcome_model_repository = TestOftenStrokeModelRepository( 0.5) alwaysStrokePop.set_bp_treatment_strategy( addABPMedStrokeLargeEffectSize()) alwaysStrokePop.advance_vectorized(1) numberOfStrokesInRecalibratedPopulation = pd.Series([ person.has_stroke_during_simulation() for i, person in alwaysStrokePop._people.iteritems() ]).sum() self.assertGreater(numberOfStrokesInBasePopulation, numberOfStrokesInRecalibratedPopulation)
def testRecalibrationIncreasesStrokesWhenEffectSizeIsClincallySmallerButNumericallyLarger( self, ): alwaysStrokePop = NHANESDirectSamplePopulation(self.popSize, 2001) alwaysStrokePop._outcome_model_repository = TestOftenStrokeModelRepository( 0.5) alwaysStrokePop.advance_vectorized(1) # about half of the people should have a stroke...at baseline numberOfStrokesInBasePopulation = pd.Series([ person.has_stroke_during_simulation() for i, person in alwaysStrokePop._people.iteritems() ]).sum() # set a treatment strategy on teh population alwaysStrokePop = NHANESDirectSamplePopulation(self.popSize, 2001) alwaysStrokePop._outcome_model_repository = TestOftenStrokeModelRepository( 0.5) # on average, treatment will have an RR round 0.95 for the BP lowering effect applied # so, we're going to recalibrate to a RR of 1.5...that will lead to many MORE strokes alwaysStrokePop.set_bp_treatment_strategy(addABPMedStrokeHarm()) alwaysStrokePop.advance_vectorized(1) numberOfStrokesInRecalibratedPopulation = pd.Series([ person.has_stroke_during_simulation() for i, person in alwaysStrokePop._people.iteritems() ]).sum() self.assertLess(numberOfStrokesInBasePopulation, numberOfStrokesInRecalibratedPopulation)
def testRecalibrationIncreasesSIsWhenEffectSizeIsClincallySmallerButNumericallyLarger( self): alwaysMIPop = NHANESDirectSamplePopulation(self.popSize, 2001) alwaysMIPop._outcome_model_repository = TestOftenMIModelRepository(0.5) alwaysMIPop.advance(1) # about half of people have an MI at baseline numberOfMIsInBasePopulation = pd.Series([ person.has_mi_during_simulation() for i, person in alwaysMIPop._people.iteritems() ]).sum() # set a treatment strategy on teh population alwaysMIPop = NHANESDirectSamplePopulation(self.popSize, 2001) alwaysMIPop._outcome_model_repository = TestOftenMIModelRepository(0.5) alwaysMIPop.set_bp_treatment_strategy(addABPMedMIHarm) alwaysMIPop.advance(1) numberOfMIsInRecalibratedPopulation = pd.Series([ person.has_mi_during_simulation() for i, person in alwaysMIPop._people.iteritems() ]).sum() self.assertLess(numberOfMIsInBasePopulation, numberOfMIsInRecalibratedPopulation)