def process(self, element, calculation_month_limit, inclusions): """Produces various program metric combinations. Sends the calculator the StatePerson entity and their corresponding ProgramEvents for mapping all program combinations. Args: element: Tuple containing a StatePerson and their ProgramEvents calculation_month_limit: The number of months to limit the monthly calculation output to. inclusions: This should be a dictionary with values for the following keys: - age_bucket - gender - race - ethnicity Yields: Each program metric combination, tagged by metric type. """ person, program_events = element # Calculate program metric combinations for this person and their program events metric_combinations = \ calculator.map_program_combinations(person=person, program_events=program_events, inclusions=inclusions, calculation_month_limit=calculation_month_limit) # Return each of the program metric combinations for metric_combination in metric_combinations: metric_key, value = metric_combination metric_type = metric_key.get('metric_type') # Converting the metric key to a JSON string so it is hashable serializable_dict = json_serializable_metric_key(metric_key) json_key = json.dumps(serializable_dict, sort_keys=True) if metric_type == MetricType.REFERRAL.value: yield beam.pvalue.TaggedOutput('referrals', (json_key, value))
def test_map_program_combinations_calculation_month_count_1(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] 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 = [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=1) expected_combinations_count = expected_metric_combos_count( [included_event], len(calculator_utils.METRIC_PERIOD_MONTHS)) self.assertEqual(expected_combinations_count, len(program_combinations)) assert all(value == 1 for _combination, value in program_combinations)
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) 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)
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, person_metadata=_DEFAULT_PERSON_METADATA ) 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_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)