def test_for_characteristics_races_ethnicities_one_race_multiple_ethnicities():
    race_white = StatePersonRace.new_with_defaults(state_code='CA',
                                                   race=Race.WHITE)

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

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

    characteristics = {'age': '<25'}

    combinations = calculator_utils.for_characteristics_races_ethnicities(
        [race_white], [ethnicity_hispanic, ethnicity_not_hispanic],
        characteristics)

    assert combinations == [{}, {
        'age': '<25'
    }, {
        'race': Race.WHITE
    }, {
        'age': '<25',
        'race': Race.WHITE
    }, {
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'age': '<25',
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'ethnicity': Ethnicity.NOT_HISPANIC
    }, {
        'age': '<25',
        'ethnicity': Ethnicity.NOT_HISPANIC
    }, {
        'race': Race.WHITE,
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'age': '<25',
        'race': Race.WHITE,
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'race': Race.WHITE,
        'ethnicity': Ethnicity.NOT_HISPANIC
    }, {
        'age': '<25',
        'race': Race.WHITE,
        'ethnicity': Ethnicity.NOT_HISPANIC
    }]
Example #2
0
    def test_add_person_characteristics_RaceEthnicity(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(race=Race.ASIAN)],
            ethnicities=[
                StatePersonEthnicity.new_with_defaults(
                    ethnicity=Ethnicity.HISPANIC)
            ])

        event_date = date(2010, 9, 1)

        person_metadata = PersonMetadata(
            prioritized_race_or_ethnicity='HISPANIC')

        updated_characteristics = person_characteristics(
            person, event_date, person_metadata, 'pipeline')

        expected_output = {
            'age_bucket': '25-29',
            'race': [Race.ASIAN],
            'gender': Gender.FEMALE,
            'ethnicity': [Ethnicity.HISPANIC],
            'person_id': person.person_id,
            'prioritized_race_or_ethnicity': 'HISPANIC'
        }

        self.assertEqual(updated_characteristics, expected_output)
Example #3
0
    def test_add_person_characteristics_IncludeExternalId(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",
        }

        self.assertEqual(updated_characteristics, expected_output)
Example #4
0
    def test_characteristic_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',
            event_date=date(2018, 9, 13),
            facility='SAN QUENTIN',
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        combinations = calculator.characteristic_combinations(
            person, incarceration_event, ALL_INCLUSIONS_DICT,
            IncarcerationMetricType.ADMISSION)

        # 64 combinations of demographics + 1 person-level metric
        assert len(combinations) == 65
Example #5
0
    def test_map_incarceration_combinations_two_stays_same_month_facility(
            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_events = [
            IncarcerationStayEvent(
                state_code="CA",
                event_date=date(2010, 3, 31),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="CA",
                event_date=date(2010, 3, 31),
                facility="FACILITY 18",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
        ]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            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)
Example #6
0
    def test_characteristic_combinations_no_county(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(2018, 9, 13),
            facility='facility',
        )

        combinations = calculator.characteristic_combinations(
            person, incarceration_event, ALL_INCLUSIONS_DICT,
            IncarcerationMetricType.RELEASE)

        # 32 combinations of demographics + 1 person-level metric
        assert len(combinations) == 33
    def test_add_demographic_characteristics_RaceEthnicity(self):
        characteristics = {}

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

        event_date = date(2010, 9, 1)

        updated_characteristics = add_demographic_characteristics(
            characteristics, person, event_date)

        expected_output = {
            'age_bucket': '25-29',
            'race': [Race.ASIAN],
            'gender': Gender.FEMALE,
            'ethnicity': [Ethnicity.HISPANIC]
        }

        self.assertEqual(updated_characteristics, expected_output)
def test_for_characteristics_races_ethnicities_one_ethnicity():
    ethnicity_hispanic = StatePersonEthnicity.new_with_defaults(
        state_code='CA', ethnicity=Ethnicity.HISPANIC)

    characteristics = {'gender': 'female', 'age': '<25'}

    combinations = calculator_utils.for_characteristics_races_ethnicities(
        [], [ethnicity_hispanic], characteristics)

    assert combinations == [{}, {
        'gender': 'female'
    }, {
        'age': '<25'
    }, {
        'gender': 'female',
        'age': '<25'
    }, {
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'ethnicity': Ethnicity.HISPANIC,
        'gender': 'female'
    }, {
        'age': '<25',
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'age': '<25',
        'ethnicity': Ethnicity.HISPANIC,
        'gender': 'female'
    }]
    def test_build_person_metadata(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]

        person_metadata = build_person_metadata(
            person, self.state_race_ethnicity_population_counts)

        expected_person_metadata = PersonMetadata(
            prioritized_race_or_ethnicity=Race.WHITE.value)

        self.assertEqual(expected_person_metadata, person_metadata)
Example #10
0
    def test_add_person_characteristics_IncludeExternalId(self):
        person = StatePerson.new_with_defaults(
            state_code='US_MO',
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
            races=[StatePersonRace.new_with_defaults()],
            ethnicities=[StatePersonEthnicity.new_with_defaults()],
            external_ids=[
                StatePersonExternalId.new_with_defaults(external_id='SID1341',
                                                        id_type='US_MO_DOC',
                                                        state_code='US_MO')
            ])

        event_date = date(2010, 9, 1)

        person_metadata = PersonMetadata()

        updated_characteristics = person_characteristics(
            person, event_date, person_metadata, 'supervision')

        expected_output = {
            'age_bucket': '25-29',
            'gender': Gender.FEMALE,
            'person_id': person.person_id,
            'person_external_id': 'SID1341'
        }

        self.assertEqual(updated_characteristics, expected_output)
Example #11
0
    def test_map_incarceration_combinations_calculation_month_limit_exclude(
            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(1990, 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)

        self.assertEqual(0, len(incarceration_combinations))
Example #12
0
    def test_map_incarceration_combinations_all_types(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(
                admission_reason=StateIncarcerationPeriodAdmissionReason.
                PAROLE_REVOCATION,
                admission_reason_raw_text='NEW_ADMISSION',
                supervision_type_at_admission=
                StateSupervisionPeriodSupervisionType.PAROLE,
                state_code='CA',
                event_date=date(2000, 3, 31),
                facility='SAN QUENTIN',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_statute=_STATUTE,
            ),
            IncarcerationAdmissionEvent(
                state_code='CA',
                event_date=date(2000, 3, 12),
                facility='SAN QUENTIN',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                supervision_type_at_admission=
                StateSupervisionPeriodSupervisionType.PAROLE,
                admission_reason_raw_text='PAROLE_REVOCATION',
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON),
            IncarcerationReleaseEvent(
                state_code='CA',
                event_date=date(2003, 4, 12),
                facility='SAN QUENTIN',
                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)
Example #13
0
    def test_produce_incarceration_metrics_multiple_overlapping_stays(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]

        incarceration_events = [
            IncarcerationStayEvent(
                state_code="US_ND",
                event_date=date(2019, 11, 30),
                facility="JRCC",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="US_ND",
                event_date=date(2019, 11, 30),
                facility="JRCC",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="US_ND",
                event_date=date(2019, 11, 30),
                facility="JRCC",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
        ]

        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=-1,
            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))
Example #14
0
    def test_produce_incarceration_metrics_two_stays_same_month(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_events = [
            IncarcerationStayEvent(
                state_code="US_XX",
                event_date=date(2010, 3, 31),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="US_XX",
                event_date=date(2010, 3, 31),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
        ]

        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=-1,
            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))
Example #15
0
    def test_produce_incarceration_metrics_secondary_person_external_id(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",
                                                  race=Race.WHITE)
            ],
            ethnicities=[
                StatePersonEthnicity.new_with_defaults(
                    state_code="US_XX", ethnicity=Ethnicity.NOT_HISPANIC)
            ],
            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"),
            ],
        )

        incarceration_events = [
            IncarcerationCommitmentFromSupervisionAdmissionEvent(
                state_code="US_XX",
                event_date=date(2000, 3, 12),
                facility="FACILITY X",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
                supervision_type=StateSupervisionPeriodSupervisionType.PAROLE,
            ),
        ]

        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=-1,
            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:
            self.assertEqual("DOC1341", metric.person_external_id)

            if isinstance(metric,
                          IncarcerationCommitmentFromSupervisionMetric):
                self.assertEqual("SID9889",
                                 metric.secondary_person_external_id)
Example #16
0
    def test_map_incarceration_combinations_calculation_month_count_include_one(
            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_include = IncarcerationAdmissionEvent(
            state_code="CA",
            event_date=date(2000, 3, 12),
            facility="SAN QUENTIN",
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

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

        incarceration_events = [
            incarceration_event_include,
            incarceration_event_exclude,
        ]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=36,
            person_metadata=_DEFAULT_PERSON_METADATA,
        )

        expected_combinations_count = expected_metric_combos_count(
            [incarceration_event_include])

        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
Example #17
0
    def test_produce_incarceration_metrics_calculation_month_count_include_one(
            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_include = IncarcerationStandardAdmissionEvent(
            state_code="US_XX",
            event_date=date(2000, 3, 12),
            facility="FACILITY X",
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_event_exclude = IncarcerationStandardAdmissionEvent(
            state_code="US_XX",
            event_date=date(1994, 3, 12),
            facility="FACILITY X",
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [
            incarceration_event_include,
            incarceration_event_exclude,
        ]

        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=36,
            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_event_include])

        self.assertEqual(expected_count, len(metrics))

        for metric in metrics:
            assert metric.year == 2000
Example #18
0
    def test_map_incarceration_combinations_relevant_periods_revocations(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(2010, 10, 2),
                facility='SAN QUENTIN',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION),
            IncarcerationAdmissionEvent(
                state_code='CA',
                event_date=date(2010, 10, 19),
                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,
            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
        for combo, _ in incarceration_combinations:
            if combo.get('admission_reason')\
                    and combo_has_enum_value_for_key(
                            combo, 'metric_type', MetricMethodologyType.PERSON):
                # Ensure that all person-based metrics have the parole
                # revocation admission reason on them
                assert combo.get('admission_reason') == \
                       AdmissionReason.PAROLE_REVOCATION
Example #19
0
    def test_map_program_combinations_full_info(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]

        event_date = date(2009, 10, 31)

        program_events = [
            ProgramReferralEvent(
                state_code="US_ND",
                event_date=event_date,
                program_id="XXX",
                supervision_type=StateSupervisionType.PAROLE,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id="OFFICERZ",
                supervising_district_external_id="135",
            ),
            ProgramParticipationEvent(
                state_code="US_ND",
                event_date=event_date,
                program_id="XXX",
                program_location_id="YYY",
                supervision_type=StateSupervisionType.PAROLE,
            ),
        ]

        program_combinations = calculator.map_program_combinations(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month="2009-10",
            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)
Example #20
0
    def test_produce_incarceration_metrics_commitment_from_supervision(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_events = [
            IncarcerationCommitmentFromSupervisionAdmissionEvent(
                state_code="US_XX",
                event_date=date(2000, 3, 12),
                facility="FACILITY X",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
                supervision_type=StateSupervisionPeriodSupervisionType.PAROLE,
            ),
        ]

        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=-1,
            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)

        # Assert that the IncarcerationCommitmentFromSupervisionAdmissionEvent produces both an
        # INCARCERATION_ADMISSION and INCARCERATION_COMMITMENT_FROM_SUPERVISION metric
        self.assertTrue(IncarcerationMetricType.INCARCERATION_ADMISSION in
                        [metric.metric_type for metric in metrics])
        self.assertTrue(
            IncarcerationMetricType.INCARCERATION_COMMITMENT_FROM_SUPERVISION
            in [metric.metric_type for metric in metrics])

        self.assertEqual(expected_count, len(metrics))
Example #21
0
    def test_produce_program_metrics_full_info(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]

        event_date = date(2009, 10, 31)

        program_events = [
            ProgramReferralEvent(
                state_code="US_ND",
                event_date=event_date,
                program_id="XXX",
                supervision_type=StateSupervisionType.PAROLE,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id="OFFICERZ",
                supervising_district_external_id="135",
            ),
            ProgramParticipationEvent(
                state_code="US_ND",
                event_date=event_date,
                program_id="XXX",
                program_location_id="YYY",
                supervision_type=StateSupervisionType.PAROLE,
            ),
        ]

        metrics = self.metric_producer.produce_metrics(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month="2009-10",
            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(program_events)

        self.assertEqual(expected_count, len(metrics))
Example #22
0
    def test_map_incarceration_combinations_includes_statute_output(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='CA',
                                   event_date=date(2010, 3, 31),
                                   facility='FACILITY 33',
                                   county_of_residence=_COUNTY_OF_RESIDENCE,
                                   most_serious_offense_ncic_code=_NCIC_CODE,
                                   most_serious_offense_statute=_STATUTE),
            IncarcerationStayEvent(state_code='CA',
                                   event_date=date(2010, 4, 30),
                                   facility='FACILITY 33',
                                   county_of_residence=_COUNTY_OF_RESIDENCE,
                                   most_serious_offense_ncic_code=_NCIC_CODE,
                                   most_serious_offense_statute=_STATUTE),
            IncarcerationStayEvent(state_code='CA',
                                   event_date=date(2010, 5, 31),
                                   facility='FACILITY 33',
                                   county_of_residence=_COUNTY_OF_RESIDENCE,
                                   most_serious_offense_ncic_code=_NCIC_CODE,
                                   most_serious_offense_statute=_STATUTE)
        ]

        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)
        assert all(
            combo.get('most_serious_offense_statute') is not None
            for combo, value in incarceration_combinations
            if combo.get('person_id') is not None)
Example #23
0
    def test_map_incarceration_combinations_calculation_month_limit_include_one(
            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_include = IncarcerationAdmissionEvent(
            state_code='CA',
            event_date=date(2000, 3, 12),
            facility='SAN QUENTIN',
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_event_exclude = IncarcerationAdmissionEvent(
            state_code='CA',
            event_date=date(1994, 3, 12),
            facility='SAN QUENTIN',
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [
            incarceration_event_include, incarceration_event_exclude
        ]

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

        expected_combinations_count = expected_metric_combos_count(
            person, [incarceration_event_include],
            ALL_INCLUSIONS_DICT,
            num_relevant_periods_admissions=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
Example #24
0
    def test_map_incarceration_combinations_two_stays_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 = [
            IncarcerationStayEvent(
                state_code='CA',
                event_date=date(2010, 3, 31),
                facility='FACILITY 33',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text='PAROLE_REVOCATION',
            ),
            IncarcerationStayEvent(
                state_code='CA',
                event_date=date(2010, 3, 31),
                facility='FACILITY 33',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text='PAROLE_REVOCATION',
            )
        ]

        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)
Example #25
0
    def test_map_incarceration_combinations(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",
            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,
            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
Example #26
0
    def test_map_program_combinations_relevant_periods_duplicates(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'
            ),
            ProgramReferralEvent(
                state_code='US_ND',
                event_date=date(2007, 12, 11),
                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)
Example #27
0
    def test_map_incarceration_combinations_two_releases_same_month(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_events = [
            IncarcerationReleaseEvent(
                state_code="CA",
                event_date=date(2010, 3, 12),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
            ),
            IncarcerationReleaseEvent(
                state_code="CA",
                event_date=date(2010, 3, 24),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
            ),
        ]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            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)
Example #28
0
    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'),
            ProgramParticipationEvent(
                state_code='US_ND',
                event_date=event_date,
                program_id='XXX',
                program_location_id='YYY',
                supervision_type=StateSupervisionType.PROBATION,
            )
        ]

        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)
Example #29
0
    def setUpClass(cls):
        """Initialize the test person for the characteristics dict."""
        cls.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)

        cls.person.races = [race]

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

        cls.person.ethnicities = [ethnicity]
Example #30
0
    def setUp(self) -> None:
        self.person = StatePerson.new_with_defaults(person_id=12345,
                                                    birthdate=date(1984, 8, 31),
                                                    gender=Gender.FEMALE)

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

        self.person.races = [race_white, race_black]

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

        self.person.ethnicities = [ethnicity]