def test_map_incarceration_combinations_multiple_stays_one_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 = [ IncarcerationStayEvent( state_code="CA", event_date=date(2010, 3, 13), facility="FACILITY 33", county_of_residence=_COUNTY_OF_RESIDENCE, specialized_purpose_for_incarceration= StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON, ), IncarcerationStayEvent( state_code="CA", event_date=date(2010, 3, 14), facility="FACILITY 33", county_of_residence=_COUNTY_OF_RESIDENCE, specialized_purpose_for_incarceration= StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON, ), IncarcerationStayEvent( state_code="CA", event_date=date(2010, 3, 15), facility="FACILITY 33", county_of_residence=_COUNTY_OF_RESIDENCE, 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)
def test_map_incarceration_combinations_all_types(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( 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_ncic_code=_NCIC_CODE, most_serious_offense_statute=_STATUTE, specialized_purpose_for_incarceration= StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON, ), 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, supervision_type_at_release= StateSupervisionPeriodSupervisionType.PAROLE, ), ] 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)
def test_map_program_combinations_relevant_periods_multiple_supervisions( 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, 1, 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, 1, 11), 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=None, calculation_month_count=-1) relevant_periods = [36, 12] expected_combinations_count = expected_metric_combos_count( program_events, len(relevant_periods), duplicated_months_different_supervision_types=True) self.assertEqual(expected_combinations_count, len(program_combinations)) assert all(value == 1 for _combination, value in program_combinations) parole_combos = 0 probation_combos = 0 for combo, _ in program_combinations: if combo.get('supervision_type') \ and combo_has_enum_value_for_key( combo, 'methodology', MetricMethodologyType.PERSON): # Ensure that all person-based metrics are of type parole if combo.get('supervision_type') == \ StateSupervisionType.PAROLE: parole_combos += 1 elif combo.get('supervision_type') == \ StateSupervisionType.PROBATION: probation_combos += 1 # Assert that there are the same number of parole and probation # person-based combinations assert parole_combos == probation_combos
def test_for_characteristics_races_ethnicities_multiple_races_and_ethnicities( ): race_white = StatePersonRace.new_with_defaults(state_code='CA', race=Race.WHITE) race_black = StatePersonRace.new_with_defaults(state_code='CA', race=Race.BLACK) 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, race_black], [ethnicity_hispanic, ethnicity_not_hispanic], characteristics) assert combinations == [{}, { 'age': '<25' }, { 'race': Race.WHITE }, { 'age': '<25', 'race': Race.WHITE }, { 'race': Race.BLACK }, { 'age': '<25', 'race': Race.BLACK }, { '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 }, { 'race': Race.BLACK, 'ethnicity': Ethnicity.HISPANIC }, { 'age': '<25', 'race': Race.BLACK, 'ethnicity': Ethnicity.HISPANIC }, { 'race': Race.BLACK, 'ethnicity': Ethnicity.NOT_HISPANIC }, { 'age': '<25', 'race': Race.BLACK, 'ethnicity': Ethnicity.NOT_HISPANIC }]
def test_for_characteristics_races_ethnicities_one_race_one_ethnicity(): race_white = StatePersonRace.new_with_defaults(state_code='CA', race=Race.WHITE) ethnicity_hispanic = StatePersonEthnicity.new_with_defaults( state_code='CA', ethnicity=Ethnicity.HISPANIC) characteristics = {'gender': 'female', 'age': '<25'} combinations = calculator_utils.for_characteristics_races_ethnicities( [race_white], [ethnicity_hispanic], characteristics) assert combinations == [ {}, { 'gender': 'female' }, { 'age': '<25' }, { 'gender': 'female', 'age': '<25' }, { 'race': Race.WHITE }, { 'race': Race.WHITE, 'gender': 'female' }, { 'age': '<25', 'race': Race.WHITE }, { 'age': '<25', 'race': Race.WHITE, 'gender': 'female' }, { 'ethnicity': Ethnicity.HISPANIC }, { 'ethnicity': Ethnicity.HISPANIC, 'gender': 'female' }, { 'age': '<25', 'ethnicity': Ethnicity.HISPANIC }, { 'age': '<25', 'ethnicity': Ethnicity.HISPANIC, 'gender': 'female' }, { 'race': Race.WHITE, 'ethnicity': Ethnicity.HISPANIC }, { 'race': Race.WHITE, 'ethnicity': Ethnicity.HISPANIC, 'gender': 'female' }, { 'age': '<25', 'race': Race.WHITE, 'ethnicity': Ethnicity.HISPANIC }, { 'race': Race.WHITE, 'ethnicity': Ethnicity.HISPANIC, 'age': '<25', 'gender': 'female' }, ]
def test_map_program_combinations_multiple_supervision_types(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, 7) 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", ), ProgramReferralEvent( state_code="US_ND", event_date=date(2009, 10, 7), 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.PAROLE, ), 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, 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)