def test_upper_bounds(self):
     highBPPerson = Person(
         age=75,
         gender=0,
         raceEthnicity=1,
         sbp=500,
         ldl=90,
         trig=150,
         dbp=80,
         a1c=6.5,
         hdl=50,
         totChol=210,
         bmi=22,
         waist=50,
         anyPhysicalActivity=0,
         education=Education.COLLEGEGRADUATE,
         smokingStatus=1,
         alcohol=AlcoholCategory.NONE,
         antiHypertensiveCount=0,
         statin=0,
         otherLipidLoweringMedicationCount=0,
         creatinine=0,
         initializeAfib=initializeAfib,
     )
     highBPPerson.advance_risk_factors(self._risk_model_repository)
     self.assertEqual(300, highBPPerson._sbp[-1])
class TestNHANESLinearRiskFactorModel(unittest.TestCase):
    def setUp(self):
        self._test_person = Person(
            age=75,
            gender=0,
            raceEthnicity=1,
            sbp=140,
            dbp=80,
            a1c=6.5,
            hdl=50,
            totChol=210,
            ldl=90,
            trig=150,
            bmi=22,
            waist=50,
            anyPhysicalActivity=0,
            education=Education.COLLEGEGRADUATE,
            smokingStatus=1,
            alcohol=AlcoholCategory.NONE,
            antiHypertensiveCount=0,
            statin=0,
            otherLipidLoweringMedicationCount=0,
            creatinine=0,
            initializeAfib=initializeAfib,
        )

        self._risk_model_repository = TestRiskModelRepository()

    def test_sbp_model(self):
        self._test_person.advance_risk_factors(self._risk_model_repository)
        expectedSBP = 75 * 1 + 140 * 0.5 + 80
        self.assertEqual(expectedSBP, self._test_person._sbp[-1])

    def test_upper_bounds(self):
        highBPPerson = Person(
            age=75,
            gender=0,
            raceEthnicity=1,
            sbp=500,
            ldl=90,
            trig=150,
            dbp=80,
            a1c=6.5,
            hdl=50,
            totChol=210,
            bmi=22,
            waist=50,
            anyPhysicalActivity=0,
            education=Education.COLLEGEGRADUATE,
            smokingStatus=1,
            alcohol=AlcoholCategory.NONE,
            antiHypertensiveCount=0,
            statin=0,
            otherLipidLoweringMedicationCount=0,
            creatinine=0,
            initializeAfib=initializeAfib,
        )
        highBPPerson.advance_risk_factors(self._risk_model_repository)
        self.assertEqual(300, highBPPerson._sbp[-1])
예제 #3
0
class TestPersonAdvanceOutcomes(unittest.TestCase):
    def setUp(self):
        self.joe = Person(
            42,
            NHANESGender.MALE,
            NHANESRaceEthnicity.NON_HISPANIC_BLACK,
            140,
            90,
            5.5,
            50,
            200,
            25,
            90,
            150,
            45,
            0,
            Education.COLLEGEGRADUATE,
            SmokingStatus.NEVER,
            AlcoholCategory.NONE,
            0,
            0,
            0,
            0,
            initializeAFib,
        )

        self.joe_with_mi = copy.deepcopy(self.joe)
        self.joe_with_mi.add_outcome_event(Outcome(OutcomeType.MI, False))

        self.joe_with_stroke = copy.deepcopy(self.joe)
        self.joe_with_stroke.add_outcome_event(Outcome(OutcomeType.STROKE, False))

        self._population_dataframe = init_vectorized_population_dataframe(
            [self.joe, self.joe_with_mi, self.joe_with_stroke],
            with_base_gcp=True,
        )

        self._always_positive_repository = AlwaysPositiveOutcomeRepository()
        self._always_negative_repository = AlwaysNegativeOutcomeRepository()
        self.cvDeterminer = CVOutcomeDetermination(self._always_positive_repository)

    def test_dead_is_dead_advance_year(self):
        self.joe._alive[-1] = False
        with self.assertRaises(RuntimeError, msg="Person is dead. Can not advance year"):
            self.joe.advance_year(None, None)

    def test_dead_is_dead_advance_risk_factors(self):
        self.joe._alive[-1] = False
        with self.assertRaises(RuntimeError, msg="Person is dead. Can not advance risk factors"):
            self.joe.advance_risk_factors(None)

    def test_dead_is_dead_advance_outcomes(self):
        self.joe._alive[-1] = False
        with self.assertRaises(RuntimeError, msg="Person is dead. Can not advance outcomes"):
            self.joe.advance_outcomes(None)

    def test_will_have_fatal_mi(self):
        joe_data = self._population_dataframe.iloc[0]

        is_max_prob_mi_fatal = self.cvDeterminer._will_have_fatal_mi(
            joe_data,
            vectorized=True,
            overrideMIProb=1.0,
        )
        is_min_prob_mi_fatal = self.cvDeterminer._will_have_fatal_mi(
            joe_data,
            vectorized=True,
            overrideMIProb=0.0,
        )

        self.assertTrue(is_max_prob_mi_fatal)
        self.assertFalse(is_min_prob_mi_fatal)

    def test_fatal_mi_secondary_prob(self):
        self.cvDeterminer.mi_secondary_case_fatality = 1.0
        self.cvDeterminer.mi_case_fatality = 0.0
        joe_data = self._population_dataframe.iloc[0]
        joe_with_mi_data = self._population_dataframe.iloc[1]  # same as joe_data plus 1 MI

        will_have_fatal_first_mi = self.cvDeterminer._will_have_fatal_mi(
            joe_data,
            vectorized=True,
            overrideMIProb=0.0,
        )
        will_have_fatal_second_mi = self.cvDeterminer._will_have_fatal_mi(
            joe_with_mi_data,
            vectorized=True,
            overrideMIProb=0.0,
        )

        self.assertFalse(will_have_fatal_first_mi)
        # even though the passed fatality rate is zero, it should be overriden by the
        # secondary rate given that joe had a prior MI
        self.assertTrue(will_have_fatal_second_mi)

    def test_fatal_stroke_secondary_prob(self):
        self.cvDeterminer.stroke_secondary_case_fatality = 1.0
        self.cvDeterminer.stroke_case_fatality = 0.0
        joe_data = self._population_dataframe.iloc[0]
        joe_with_stroke_data = self._population_dataframe.iloc[2]  # same as joe_data plus 1 stroke

        will_have_fatal_first_stroke = self.cvDeterminer._will_have_fatal_stroke(
            joe_data,
            vectorized=True,
            overrideStrokeProb=0.0,
        )
        will_have_fatal_second_stroke = self.cvDeterminer._will_have_fatal_stroke(
            joe_with_stroke_data,
            vectorized=True,
            overrideStrokeProb=0.0,
        )

        self.assertFalse(will_have_fatal_first_stroke)
        # even though the passed fatality rate is zero, it shoudl be overriden by the
        # secondary rate given that joeclone had a prior stroke
        self.assertTrue(will_have_fatal_second_stroke)

    def test_will_have_fatal_stroke(self):
        joe_data = self._population_dataframe.iloc[0]

        is_max_prob_stroke_fatal = self.cvDeterminer._will_have_fatal_stroke(
            joe_data,
            vectorized=True,
            overrideStrokeProb=1.0,
        )
        is_min_prob_stroke_fatal = self.cvDeterminer._will_have_fatal_stroke(
            joe_data,
            vectorized=True,
            overrideStrokeProb=0.0,
        )

        self.assertTrue(is_max_prob_stroke_fatal)
        self.assertFalse(is_min_prob_stroke_fatal)

    def test_has_mi_vs_stroke(self):
        joe_data = self._population_dataframe.iloc[0]

        has_mi_max_manual_prob = self.cvDeterminer._will_have_mi(
            joe_data,
            outcome_model_repository=None,
            vectorized=False,
            manualMIProb=1.0,
        )
        has_mi_min_manual_prob = self.cvDeterminer._will_have_mi(
            joe_data,
            outcome_model_repository=None,
            vectorized=False,
            manualMIProb=0.0,
        )

        self.assertTrue(has_mi_max_manual_prob)
        self.assertFalse(has_mi_min_manual_prob)

    def test_advance_outcomes_fatal_mi(self):
        self._always_positive_repository.stroke_case_fatality = 1.0
        self._always_positive_repository.mi_case_fatality = 1.0
        self._always_positive_repository.manualStrokeMIProbability = 1.0

        self.joe.advance_outcomes(self._always_positive_repository)
        self.assertTrue(self.joe.has_mi_during_simulation())
        self.assertFalse(self.joe.has_stroke_during_simulation())
        self.assertTrue(self.joe.is_dead())

    def test_advance_outcomes_fatal_stroke(self):
        self._always_positive_repository.stroke_case_fatality = 1.0
        self._always_positive_repository.mi_case_fatality = 1.0
        self._always_positive_repository.manualStrokeMIProbability = 0.0

        self.joe.advance_outcomes(self._always_positive_repository)
        self.assertFalse(self.joe.has_mi_during_simulation())
        self.assertTrue(self.joe.has_stroke_during_simulation())
        self.assertTrue(self.joe.is_dead())

    def test_advance_outcomes_nonfatal_mi(self):
        self.assertEqual(0, self.joe.is_dead())
        self._always_positive_repository.stroke_case_fatality = 0.0
        self._always_positive_repository.mi_case_fatality = 0.0
        self._always_positive_repository.manualStrokeMIProbability = 1.0

        self.joe.advance_outcomes(self._always_positive_repository)
        self.assertTrue(self.joe.has_mi_during_simulation())
        self.assertFalse(self.joe.has_stroke_during_simulation())

    def test_advance_outcomes_nonfatal_stroke(self):
        self._always_positive_repository.stroke_case_fatality = 0.0
        self._always_positive_repository.mi_case_fatality = 0.0
        self._always_positive_repository.manualStrokeMIProbability = 0.0

        self.joe.advance_outcomes(self._always_positive_repository)
        self.assertFalse(self.joe.has_mi_during_simulation())
        self.assertTrue(self.joe.has_stroke_during_simulation())
예제 #4
0
class TestPersonAdvanceOutcomes(unittest.TestCase):
    def setUp(self):
        self.joe = Person(42, NHANESGender.MALE,
                          NHANESRaceEthnicity.NON_HISPANIC_BLACK, 140, 90, 5.5,
                          50, 200, 25, 90, 150, 45, 0,
                          Education.COLLEGEGRADUATE, SmokingStatus.NEVER,
                          AlcoholCategory.NONE, 0, 0, 0, initializeAFib)
        self._always_positive_repository = AlwaysPositiveOutcomeRepository()
        self._always_negative_repository = AlwaysNegativeOutcomeRepository()
        self.cvDeterminer = CVOutcomeDetermination(
            self._always_positive_repository)

    def test_dead_is_dead_advance_year(self):
        self.joe._alive[-1] = False
        with self.assertRaises(RuntimeError,
                               msg="Person is dead. Can not advance year"):
            self.joe.advance_year(None, None)

    def test_dead_is_dead_advance_risk_factors(self):
        self.joe._alive[-1] = False
        with self.assertRaises(
                RuntimeError,
                msg="Person is dead. Can not advance risk factors"):
            self.joe.advance_risk_factors(None)

    def test_dead_is_dead_advance_outcomes(self):
        self.joe._alive[-1] = False
        with self.assertRaises(RuntimeError,
                               msg="Person is dead. Can not advance outcomes"):
            self.joe.advance_outcomes(None)

    def test_will_have_fatal_mi(self):
        self.assertEqual(self.cvDeterminer._will_have_fatal_mi(self.joe, 1.0),
                         1)
        self.assertEqual(self.cvDeterminer._will_have_fatal_mi(self.joe, 0.0),
                         0)

    def test_fatal_mi_secondary_prob(self):
        self.cvDeterminer.mi_secondary_case_fatality = 1.0
        self.cvDeterminer.mi_case_fatality = 0.0

        joeClone = copy.deepcopy(self.joe)

        self.assertEqual(self.cvDeterminer._will_have_fatal_mi(joeClone, 0.0),
                         0)

        joeClone._outcomes[OutcomeType.MI] = (joeClone._age,
                                              Outcome(OutcomeType.MI, False))
        # even though the passed fatality rate is zero, it shoudl be overriden by the
        # secondary rate given that joeclone had a prior MI
        self.assertEqual(self.cvDeterminer._will_have_fatal_mi(joeClone, 0.0),
                         1)

    def test_fatal_stroke_secondary_prob(self):
        self.cvDeterminer.stroke_secondary_case_fatality = 1.0
        self.cvDeterminer.stroke_case_fatality = 0.0

        joeClone = copy.deepcopy(self.joe)

        self.assertEqual(
            self.cvDeterminer._will_have_fatal_stroke(joeClone, 0.0), 0)

        joeClone._outcomes[OutcomeType.STROKE] = (joeClone._age,
                                                  Outcome(
                                                      OutcomeType.STROKE,
                                                      False))
        # even though the passed fatality rate is zero, it shoudl be overriden by the
        # secondary rate given that joeclone had a prior stroke
        self.assertEqual(
            self.cvDeterminer._will_have_fatal_stroke(joeClone, 0.0), 1)

    def test_will_have_fatal_stroke(self):
        self.assertEqual(
            self.cvDeterminer._will_have_fatal_stroke(self.joe, 1.0), 1)
        self.assertEqual(
            self.cvDeterminer._will_have_fatal_stroke(self.joe, 0.0), 0)

    def test_has_mi_vs_stroke(self):
        self.assertEqual(self.cvDeterminer._will_have_mi(self.joe, None, 1.0),
                         1)
        self.assertEqual(self.cvDeterminer._will_have_mi(self.joe, None, 0.0),
                         0)

    def test_advance_outcomes_fatal_mi(self):
        self._always_positive_repository.stroke_case_fatality = 1.0
        self._always_positive_repository.mi_case_fatality = 1.0
        self._always_positive_repository.manualStrokeMIProbability = 1.0

        self.joe.advance_outcomes(self._always_positive_repository)
        self.assertTrue(self.joe.has_mi_during_simulation())
        self.assertFalse(self.joe.has_stroke_during_simulation())
        self.assertTrue(self.joe.is_dead())

    def test_advance_outcomes_fatal_stroke(self):
        self._always_positive_repository.stroke_case_fatality = 1.0
        self._always_positive_repository.mi_case_fatality = 1.0
        self._always_positive_repository.manualStrokeMIProbability = 0.0

        self.joe.advance_outcomes(self._always_positive_repository)
        self.assertFalse(self.joe.has_mi_during_simulation())
        self.assertTrue(self.joe.has_stroke_during_simulation())
        self.assertTrue(self.joe.is_dead())

    def test_advance_outcomes_nonfatal_mi(self):
        self.assertEqual(0, self.joe.is_dead())
        self._always_positive_repository.stroke_case_fatality = 0.0
        self._always_positive_repository.mi_case_fatality = 0.0
        self._always_positive_repository.manualStrokeMIProbability = 1.0

        self.joe.advance_outcomes(self._always_positive_repository)
        self.assertTrue(self.joe.has_mi_during_simulation())
        self.assertFalse(self.joe.has_stroke_during_simulation())

    def test_advance_outcomes_nonfatal_stroke(self):
        self._always_positive_repository.stroke_case_fatality = 0.0
        self._always_positive_repository.mi_case_fatality = 0.0
        self._always_positive_repository.manualStrokeMIProbability = 0.0

        self.joe.advance_outcomes(self._always_positive_repository)
        self.assertFalse(self.joe.has_mi_during_simulation())
        self.assertTrue(self.joe.has_stroke_during_simulation())
예제 #5
0
class TestTreatmentStrategy(unittest.TestCase):
    def initializeAfib(person):
        return None

    def setUp(self):
        self.baselineSBP = 140
        self.baselineDBP = 80
        self._test_person = Person(
            age=75,
            gender=0,
            raceEthnicity=1,
            sbp=self.baselineSBP,
            dbp=self.baselineDBP,
            a1c=6.5,
            hdl=50,
            totChol=210,
            ldl=90,
            trig=150,
            bmi=22,
            waist=50,
            anyPhysicalActivity=0,
            education=Education.COLLEGEGRADUATE,
            smokingStatus=1,
            alcohol=0,
            antiHypertensiveCount=0,
            statin=0,
            otherLipidLoweringMedicationCount=0,
            initializeAfib=TestTreatmentStrategy.initializeAfib)
        self._risk_model_repository = TestRiskModelRepository()
        # setup so that the SBP  always stays the same
        self._risk_model_repository._repository['sbp']._params['age'] = 0
        self._risk_model_repository._repository['sbp']._params['sbp'] = 1.0
        self._risk_model_repository._repository['sbp']._params['intercept'] = 0
        self._risk_model_repository._repository['dbp']._params['age'] = 0
        self._risk_model_repository._repository['dbp']._params['dbp'] = 1.0
        self._risk_model_repository._repository['dbp']._params['sbp'] = 0
        self._risk_model_repository._repository['dbp']._params['intercept'] = 0
        # setup so that the anti-hypertensive count stays at zero
        self._risk_model_repository._repository[
            'antiHypertensiveCount']._params['age'] = 0
        self._risk_model_repository._repository[
            'antiHypertensiveCount']._params['sbp'] = 0
        self._risk_model_repository._repository[
            'antiHypertensiveCount']._params['intercept'] = 0

    def add_a_single_blood_pressure_medication_strategy(person):
        return {'_antiHypertensiveCount': 1}, {'_sbp': -5, '_dbp': -3}, {}

    def testSimpleBPTreatmentStrategy(self):
        self._test_person.advance_treatment(self._risk_model_repository)
        self._test_person.advance_risk_factors(self._risk_model_repository)

        self.assertEqual(self.baselineSBP, self._test_person._sbp[1])
        self.assertEqual(self.baselineDBP, self._test_person._dbp[1])
        self.assertEqual(0, self._test_person._antiHypertensiveCount[1])

        self._test_person._bpTreatmentStrategy = TestTreatmentStrategy.add_a_single_blood_pressure_medication_strategy

        self._test_person.advance_treatment(self._risk_model_repository)
        self._test_person.advance_risk_factors(self._risk_model_repository)

        self.assertEqual(self.baselineSBP - 5, self._test_person._sbp[2])
        self.assertEqual(self.baselineDBP - 3, self._test_person._dbp[2])
        self.assertEqual(1, self._test_person._antiHypertensiveCount[2])