Esempio n. 1
0
    def test_person_external_id_to_include_no_results(self):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        person_external_id = StatePersonExternalId.new_with_defaults(
            external_id="SID10928", id_type="US_ND_SID", state_code="US_ND")

        person.external_ids = [person_external_id]

        for pipeline_type in INCLUDED_PIPELINES:
            external_id = calculator_utils.person_external_id_to_include(
                pipeline_type, person_external_id.state_code, person)
            self.assertIsNone(external_id)
Esempio n. 2
0
    def test_map_incarceration_combinations_calculation_month_count(self):
        person = StatePerson.new_with_defaults(
            state_code="CA",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="CA",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="CA", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_event = IncarcerationAdmissionEvent(
            state_code="CA",
            event_date=date(2000, 3, 12),
            facility="SAN QUENTIN",
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [incarceration_event]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month="2000-03",
            calculation_month_count=1,
            person_metadata=_DEFAULT_PERSON_METADATA,
        )

        expected_combinations_count = expected_metric_combos_count(
            incarceration_events)

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)
        for combo, _ in incarceration_combinations:
            assert combo.get("year") == 2000
Esempio n. 3
0
    def test_map_incarceration_combinations(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='CA',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='CA', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_event = IncarcerationAdmissionEvent(
            state_code='CA',
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            NEW_ADMISSION,
            admission_reason_raw_text='NEW_ADMISSION',
            supervision_type_at_admission=StateSupervisionPeriodSupervisionType
            .PROBATION,
            event_date=date(2000, 3, 12),
            facility='SAN QUENTIN',
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [incarceration_event]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            inclusions=ALL_INCLUSIONS_DICT,
            calculation_month_limit=-1)

        expected_combinations_count = expected_metric_combos_count(
            person, incarceration_events, ALL_INCLUSIONS_DICT)

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)

        for combo, _ in incarceration_combinations:
            assert combo.get('year') == 2000
Esempio n. 4
0
    def test_produce_program_metrics_calculation_month_count_1(self) -> None:
        person = StatePerson.new_with_defaults(
            state_code="US_ND",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_ND", race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_ND", ethnicity=Ethnicity.NOT_HISPANIC
        )

        person.ethnicities = [ethnicity]

        included_event = ProgramReferralEvent(
            state_code="US_ND", event_date=date(2012, 11, 10), program_id="XXX"
        )

        not_included_event = ProgramReferralEvent(
            state_code="US_ND", event_date=date(2000, 2, 2), program_id="ZZZ"
        )

        program_events: List[ProgramEvent] = [
            included_event,
            not_included_event,
        ]

        metrics = self.metric_producer.produce_metrics(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=1,
            person_metadata=_DEFAULT_PERSON_METADATA,
            pipeline_job_id=PIPELINE_JOB_ID,
            pipeline_type=self.pipeline_config.pipeline_type,
        )

        expected_count = expected_metrics_count([included_event])

        self.assertEqual(expected_count, len(metrics))
Esempio n. 5
0
    def test_produce_incarceration_metrics_calculation_month_count_include_monthly(
        self, ):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_XX",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_XX", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_event = IncarcerationStandardAdmissionEvent(
            state_code="US_XX",
            event_date=date(2007, 12, 12),
            facility="FACILITY X",
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [incarceration_event]

        metrics = self.metric_producer.produce_metrics(
            person=person,
            identifier_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=37,
            person_metadata=_DEFAULT_PERSON_METADATA,
            pipeline_job_id=PIPELINE_JOB_ID,
            pipeline_type=self.pipeline_config.pipeline_type,
        )

        expected_count = self.expected_metrics_count(incarceration_events)

        self.assertEqual(expected_count, len(metrics))

        for metric in metrics:
            assert metric.year == 2007
Esempio n. 6
0
    def test_determine_prioritized_race_or_ethnicity_unsupported_state(self):
        person = StatePerson.new_with_defaults(
            state_code="US_NOT_SUPPORTED",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race_white = StatePersonRace.new_with_defaults(
            state_code="US_NOT_SUPPORTED", race=Race.WHITE)
        race_black = StatePersonRace.new_with_defaults(
            state_code="US_NOT_SUPPORTED", race=Race.BLACK)

        person.races = [race_white, race_black]

        with pytest.raises(ValueError):
            _ = determine_prioritized_race_or_ethnicity(
                person, self.state_race_ethnicity_population_counts)
    def test_add_demographic_characteristics_EmptyRaceEthnicity(self):
        characteristics = {}

        person = StatePerson.new_with_defaults(
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
            races=[StatePersonRace.new_with_defaults()],
            ethnicities=[StatePersonEthnicity.new_with_defaults()])

        event_date = date(2010, 9, 1)

        updated_characteristics = add_demographic_characteristics(
            characteristics, person, event_date)

        expected_output = {'age_bucket': '25-29', 'gender': Gender.FEMALE}

        self.assertEqual(updated_characteristics, expected_output)
    def testCalculateProgramMetricCombinations(self):
        """Tests the CalculateProgramMetricCombinations DoFn."""

        fake_person = StatePerson.new_with_defaults(
            person_id=123,
            gender=Gender.MALE,
            birthdate=date(1970, 1, 1),
            residency_status=ResidencyStatus.PERMANENT)

        program_events = [
            ProgramReferralEvent(state_code='US_TX',
                                 event_date=date(2011, 4, 3),
                                 program_id='program')
        ]

        # Get the number of combinations of person-event characteristics.
        num_combinations = len(
            calculator.characteristic_combinations(fake_person,
                                                   program_events[0],
                                                   ALL_INCLUSIONS_DICT))
        assert num_combinations > 0

        # Each characteristic combination will be tracked for each of the
        # months and the two methodology types
        expected_population_metric_count = \
            num_combinations * len(program_events) * 2

        expected_combination_counts = \
            {'referrals': expected_population_metric_count}

        test_pipeline = TestPipeline()

        output = (test_pipeline
                  | beam.Create([(fake_person, program_events)])
                  | 'Calculate Program Metrics' >> beam.ParDo(
                      pipeline.CalculateProgramMetricCombinations(), -1,
                      ALL_INCLUSIONS_DICT).with_outputs('referrals'))

        assert_that(
            output.referrals,
            AssertMatchers.count_combinations(expected_combination_counts),
            'Assert number of metrics is expected value')

        test_pipeline.run()
Esempio n. 9
0
    def test_map_incarceration_combinations_admission_relevant_periods(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='CA',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='CA', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_event = IncarcerationAdmissionEvent(
            state_code='CA',
            event_date=date(2000, 3, 12),
            facility='SAN QUENTIN',
            county_of_residence=_COUNTY_OF_RESIDENCE,
            admission_reason=AdmissionReason.NEW_ADMISSION,
            specialized_purpose_for_incarceration=
            StateSpecializedPurposeForIncarceration.SHOCK_INCARCERATION)

        incarceration_events = [incarceration_event]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            inclusions=ALL_INCLUSIONS_DICT,
            calculation_month_limit=-1)

        expected_combinations_count = expected_metric_combos_count(
            person, incarceration_events, ALL_INCLUSIONS_DICT,
            len(calculator_utils.METRIC_PERIOD_MONTHS))

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)
        for combo, _ in incarceration_combinations:
            assert combo.get('year') == 2000
            if combo.get('person_id') is not None:
                assert combo.get('admission_date') is not None
Esempio n. 10
0
    def test_map_program_combinations_calculation_month_count_36_include(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_ND',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_ND', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        same_month_event = ProgramReferralEvent(state_code='US_ND',
                                                event_date=date(2012, 12, 10),
                                                program_id='XXX')

        old_month_event = ProgramReferralEvent(state_code='US_ND',
                                               event_date=date(2010, 1, 10),
                                               program_id='ZZZ')

        program_events = [same_month_event, old_month_event]

        program_combinations = calculator.map_program_combinations(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=36)

        expected_combinations_count = expected_metric_combos_count(
            [same_month_event], len(calculator_utils.METRIC_PERIOD_MONTHS))

        expected_combinations_count += expected_metric_combos_count(
            [old_month_event], 1)

        # Subtract the duplicated count for the metric_period_months = 36 person-based dict
        expected_combinations_count -= 1

        self.assertEqual(expected_combinations_count,
                         len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Esempio n. 11
0
    def test_map_program_combinations_calculation_month_count_36(self):
        person = StatePerson.new_with_defaults(
            state_code="US_ND",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_ND",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_ND", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        included_event = ProgramReferralEvent(state_code="US_ND",
                                              event_date=date(2012, 12, 10),
                                              program_id="XXX")

        not_included_event = ProgramReferralEvent(state_code="US_ND",
                                                  event_date=date(
                                                      2009, 12, 10),
                                                  program_id="ZZZ")

        program_events = [included_event, not_included_event]

        program_combinations = calculator.map_program_combinations(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=36,
            person_metadata=_DEFAULT_PERSON_METADATA,
        )

        expected_combinations_count = expected_metric_combos_count(
            [included_event])

        self.assertEqual(expected_combinations_count,
                         len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Esempio n. 12
0
    def test_map_incarceration_combinations_two_admissions_same_month(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='CA',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='CA', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_events = [
            IncarcerationAdmissionEvent(
                state_code='CA',
                event_date=date(2000, 3, 12),
                facility='SAN QUENTIN',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.NEW_ADMISSION),
            IncarcerationAdmissionEvent(
                state_code='CA',
                event_date=date(2000, 3, 17),
                facility='SAN QUENTIN',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.NEW_ADMISSION)
        ]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            inclusions=ALL_INCLUSIONS_DICT,
            calculation_month_limit=-1)

        expected_combinations_count = expected_metric_combos_count(
            person, incarceration_events, ALL_INCLUSIONS_DICT)

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)
Esempio n. 13
0
    def test_person_has_external_ids_from_multiple_states(self):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        person_external_id_1 = StatePersonExternalId.new_with_defaults(
            external_id="SID10928", id_type="US_ND_SID", state_code="US_ND")

        person_external_id_2 = StatePersonExternalId.new_with_defaults(
            external_id="SID1341", id_type="US_MO_DOC", state_code="US_MO")

        person.external_ids = [person_external_id_1, person_external_id_2]

        with self.assertRaises(ValueError):
            _ = calculator_utils.person_external_id_to_include(
                INCLUDED_PIPELINES[0], person_external_id_2.state_code, person)
Esempio n. 14
0
    def test_determine_prioritized_race_or_ethnicity_race(self):
        person = StatePerson.new_with_defaults(state_code='US_XX',
                                               person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race_white = StatePersonRace.new_with_defaults(state_code='US_XX',
                                                       race=Race.WHITE)
        race_black = StatePersonRace.new_with_defaults(state_code='US_XX',
                                                       race=Race.BLACK)

        person.races = [race_white, race_black]

        prioritized_race_ethnicity = determine_prioritized_race_or_ethnicity(
            person, self.state_race_ethnicity_population_counts)

        expected_output = Race.BLACK.value

        self.assertEqual(expected_output, prioritized_race_ethnicity)
Esempio n. 15
0
    def test_map_incarceration_combinations_multiple_overlapping_stays(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='CA',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='CA', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_events = [
            IncarcerationStayEvent(state_code='US_ND',
                                   event_date=date(2019, 11, 30),
                                   facility='JRCC',
                                   county_of_residence=_COUNTY_OF_RESIDENCE),
            IncarcerationStayEvent(state_code='US_ND',
                                   event_date=date(2019, 11, 30),
                                   facility='JRCC',
                                   county_of_residence=_COUNTY_OF_RESIDENCE),
            IncarcerationStayEvent(state_code='US_ND',
                                   event_date=date(2019, 11, 30),
                                   facility='JRCC',
                                   county_of_residence=_COUNTY_OF_RESIDENCE)
        ]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            inclusions=ALL_INCLUSIONS_DICT,
            calculation_month_limit=-1)

        expected_combinations_count = expected_metric_combos_count(
            person, incarceration_events, ALL_INCLUSIONS_DICT)

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)
Esempio n. 16
0
    def test_determine_prioritized_race_or_ethnicity_ethnicity(self):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_XX", ethnicity=Ethnicity.HISPANIC)

        person.ethnicities = [ethnicity]

        prioritized_race_ethnicity = determine_prioritized_race_or_ethnicity(
            person, self.state_race_ethnicity_population_counts)

        expected_output = Ethnicity.HISPANIC.value

        self.assertEqual(expected_output, prioritized_race_ethnicity)
    def test_person_external_id_to_include_multiple(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        person_external_id_exclude = StatePersonExternalId.new_with_defaults(
            external_id='SID10928', id_type='US_ND_SID', state_code='US_ND')

        person_external_id_include = StatePersonExternalId.new_with_defaults(
            external_id='SID1341', id_type='US_MO_DOC', state_code='US_MO')

        person.external_ids = [
            person_external_id_exclude, person_external_id_include
        ]

        external_id = calculator_utils.person_external_id_to_include(
            INCLUDED_PIPELINES[0], person)

        self.assertEqual(external_id, person_external_id_include.external_id)
Esempio n. 18
0
    def testCalculateProgramMetricCombinations(self):
        """Tests the CalculateProgramMetricCombinations DoFn."""

        fake_person = StatePerson.new_with_defaults(
            person_id=123,
            gender=Gender.MALE,
            birthdate=date(1970, 1, 1),
            residency_status=ResidencyStatus.PERMANENT)

        program_events = [
            ProgramReferralEvent(
                state_code='US_TX',
                event_date=date(2011, 4, 3),
                program_id='program',
                participation_status=StateProgramAssignmentParticipationStatus.
                IN_PROGRESS),
            ProgramParticipationEvent(state_code='US_TX',
                                      event_date=date(2011, 6, 3),
                                      program_id='program')
        ]

        # Each event will be have an output for each methodology type
        expected_metric_count = 2

        expected_combination_counts = \
            {'referrals': expected_metric_count,
             'participation': expected_metric_count}

        test_pipeline = TestPipeline()

        output = (test_pipeline
                  | beam.Create([(fake_person, program_events)])
                  | 'Calculate Program Metrics' >> beam.ParDo(
                      pipeline.CalculateProgramMetricCombinations(), None, -1,
                      ALL_METRIC_INCLUSIONS_DICT))

        assert_that(
            output,
            AssertMatchers.count_combinations(expected_combination_counts),
            'Assert number of metrics is expected value')

        test_pipeline.run()
    def test_map_program_combinations_full_info_probation(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_ND',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_ND', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        event_date = date(2009, 10, 31)

        program_events = [
            ProgramReferralEvent(
                state_code='US_ND',
                event_date=event_date,
                program_id='XXX',
                supervision_type=StateSupervisionType.PROBATION,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id='OFFICERZ',
                supervising_district_external_id='135')
        ]

        program_combinations = calculator.map_program_combinations(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month='2009-10',
            calculation_month_count=-1)

        expected_combinations_count = expected_metric_combos_count(
            program_events)

        self.assertEqual(expected_combinations_count,
                         len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Esempio n. 20
0
    def test_getTotalEntitiesOfCls(self):
        supervision_sentence = StateSupervisionSentence.new_with_defaults()
        supervision_sentence_2 = attr.evolve(supervision_sentence)
        supervision_sentence_3 = attr.evolve(supervision_sentence)
        sentence_group = StateSentenceGroup.new_with_defaults(
            supervision_sentences=[
                supervision_sentence, supervision_sentence_2
            ])
        sentence_group_2 = StateSentenceGroup.new_with_defaults(
            supervision_sentences=[
                supervision_sentence_2, supervision_sentence_3
            ])
        person = StatePerson.new_with_defaults(
            sentence_groups=[sentence_group, sentence_group_2])

        self.assertEqual(
            3, get_total_entities_of_cls([person], StateSupervisionSentence))
        self.assertEqual(
            2, get_total_entities_of_cls([person], StateSentenceGroup))
        self.assertEqual(1, get_total_entities_of_cls([person], StatePerson))
Esempio n. 21
0
    def testClassifyIncarcerationEvents_NoSentenceGroups(self):
        """Tests the ClassifyIncarcerationEvents DoFn when the person has no sentence groups."""
        fake_person = StatePerson.new_with_defaults(
            person_id=123,
            gender=Gender.MALE,
            birthdate=date(1970, 1, 1),
            residency_status=ResidencyStatus.PERMANENT)

        person_periods = {'person': [fake_person], 'sentence_groups': []}

        test_pipeline = TestPipeline()

        output = (test_pipeline
                  | beam.Create([(fake_person.person_id, person_periods)])
                  | 'Identify Incarceration Events' >> beam.ParDo(
                      pipeline.ClassifyIncarcerationEvents(), {}))

        assert_that(output, equal_to([]))

        test_pipeline.run()
Esempio n. 22
0
    def setUp(self) -> None:
        self.state_code = "US_ND"
        self.person = StatePerson.new_with_defaults(
            state_code=self.state_code,
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        self.race = StatePersonRace.new_with_defaults(
            state_code=self.state_code, race=Race.WHITE)

        self.person.races = [self.race]

        self.ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code=self.state_code, ethnicity=Ethnicity.NOT_HISPANIC)

        self.person.ethnicities = [self.ethnicity]
        self.metric_producer = metric_producer.ViolationMetricProducer()
        self.pipeline_config = pipeline.ViolationPipeline().pipeline_config
Esempio n. 23
0
    def test_map_incarceration_combinations_release_relevant_periods(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='CA',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='CA', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_event = IncarcerationReleaseEvent(
            state_code='CA',
            event_date=date(2010, 10, 2),
            facility='SAN QUENTIN',
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [incarceration_event]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            inclusions=ALL_INCLUSIONS_DICT,
            calculation_month_limit=-1)

        expected_combinations_count = expected_metric_combos_count(
            person, incarceration_events, ALL_INCLUSIONS_DICT, 0,
            len(calculator_utils.METRIC_PERIOD_MONTHS))

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)

        for combo, _ in incarceration_combinations:
            assert combo.get('year') == 2010
Esempio n. 24
0
    def test_map_program_combinations_relevant_periods(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_ND',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_ND',
            ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        program_events = [
            ProgramReferralEvent(
                state_code='US_ND',
                event_date=date(2007, 12, 7),
                program_id='XXX',
                supervision_type=StateSupervisionType.PAROLE,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id='OFFICERZ',
                supervising_district_external_id='135'
            )
        ]

        program_combinations = calculator.map_program_combinations(
            person, program_events, ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA
        )

        expected_combinations_count = expected_metric_combos_count(
            program_events, len(calculator_utils.METRIC_PERIOD_MONTHS))

        self.assertEqual(expected_combinations_count, len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Esempio n. 25
0
    def test_person_external_id_to_include(self):
        person = StatePerson.new_with_defaults(
            state_code="US_MO",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        person_external_id = StatePersonExternalId.new_with_defaults(
            external_id="SID1341",
            id_type="US_MO_DOC",
            state_code="US_MO",
        )

        person.external_ids = [person_external_id]

        for pipeline_type in INCLUDED_PIPELINES:
            external_id = calculator_utils.person_external_id_to_include(
                pipeline_type, person_external_id.state_code, person)

            self.assertEqual(external_id, person_external_id.external_id)
Esempio n. 26
0
    def testCalculateProgramMetricCombinations_NoReferrals(self):
        """Tests the CalculateProgramMetricCombinations when there are
        no supervision months. This should never happen because any person
        without program events is dropped entirely from the pipeline."""
        fake_person = StatePerson.new_with_defaults(
            person_id=123,
            gender=Gender.MALE,
            birthdate=date(1970, 1, 1),
            residency_status=ResidencyStatus.PERMANENT)

        test_pipeline = TestPipeline()

        output = (test_pipeline
                  | beam.Create([(fake_person, [])])
                  | 'Calculate Program Metrics' >> beam.ParDo(
                      pipeline.CalculateProgramMetricCombinations(), None, -1,
                      ALL_METRIC_INCLUSIONS_DICT))

        assert_that(output, equal_to([]))

        test_pipeline.run()
Esempio n. 27
0
    def testCalculateProgramMetricCombinations_NoReferrals(self):
        """Tests the CalculateProgramMetricCombinations when there are
        no supervision months. This should never happen because any person
        without program events is dropped entirely from the pipeline."""
        fake_person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=123,
            gender=Gender.MALE,
            birthdate=date(1970, 1, 1),
            residency_status=ResidencyStatus.PERMANENT,
        )

        test_pipeline = TestPipeline()

        inputs = [
            (
                self.fake_person_id,
                {
                    "person_events": [(fake_person, [])],
                    "person_metadata": [self.person_metadata],
                },
            )
        ]

        output = (
            test_pipeline
            | beam.Create(inputs)
            | beam.ParDo(ExtractPersonEventsMetadata())
            | "Calculate Program Metrics"
            >> beam.ParDo(
                pipeline.CalculateProgramMetricCombinations(),
                None,
                -1,
                ALL_METRIC_INCLUSIONS_DICT,
            )
        )

        assert_that(output, equal_to([]))

        test_pipeline.run()
Esempio n. 28
0
    def test_map_program_combinations(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_ND',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_ND',
            ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        program_events = [
            ProgramReferralEvent(
                state_code='US_ND',
                event_date=date(2019, 10, 10),
                program_id='XXX'
            ),
            ProgramParticipationEvent(
                state_code='US_ND',
                event_date=date(2019, 2, 2),
                program_id='ZZZ'
            )
        ]

        program_combinations = calculator.map_program_combinations(
            person, program_events, ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA
        )

        expected_combinations_count = expected_metric_combos_count(program_events)

        self.assertEqual(expected_combinations_count, len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Esempio n. 29
0
    def test_add_person_characteristics_IncludeSecondaryExternalId(self):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
            races=[StatePersonRace.new_with_defaults(state_code="US_XX", )],
            ethnicities=[
                StatePersonEthnicity.new_with_defaults(state_code="US_XX", )
            ],
            external_ids=[
                StatePersonExternalId.new_with_defaults(external_id="DOC1341",
                                                        id_type="US_XX_DOC",
                                                        state_code="US_XX"),
                StatePersonExternalId.new_with_defaults(external_id="SID9889",
                                                        id_type="US_XX_SID",
                                                        state_code="US_XX"),
            ],
        )

        event_date = date(2010, 9, 1)

        person_metadata = PersonMetadata()

        updated_characteristics = person_characteristics(
            person,
            event_date,
            person_metadata,
            "test_pipeline",
        )

        expected_output = {
            "age_bucket": "25-29",
            "gender": Gender.FEMALE,
            "person_id": person.person_id,
            "person_external_id": "DOC1341",
            "secondary_person_external_id": "SID9889",
        }

        self.assertEqual(updated_characteristics, expected_output)
    def test_generateChildEntitiesWithAncestorChain(self):
        fine = StateFine.new_with_defaults(fine_id=_ID)
        fine_another = StateFine.new_with_defaults(fine_id=_ID_2)
        person = StatePerson.new_with_defaults(person_id=_ID)
        sentence_group = StateSentenceGroup.new_with_defaults(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_STATE_CODE,
            fines=[fine, fine_another],
            person=[person],
            sentence_group_id=_ID)
        sentence_group_tree = EntityTree(entity=sentence_group,
                                         ancestor_chain=[person])

        expected_child_trees = [
            EntityTree(entity=fine, ancestor_chain=[person, sentence_group]),
            EntityTree(entity=fine_another,
                       ancestor_chain=[person, sentence_group]),
        ]

        self.assertEqual(
            expected_child_trees,
            generate_child_entity_trees('fines', [sentence_group_tree]))