def test_find_program_participation_events_not_actively_participating( self): program_assignment = StateProgramAssignment.new_with_defaults( state_code="US_XX", program_id="PG3", referral_date=date(2009, 10, 3), participation_status=StateProgramAssignmentParticipationStatus. DISCHARGED, program_location_id="LOCATION", start_date=date(2009, 11, 5), discharge_date=date(2009, 11, 8), ) supervision_period = StateSupervisionPeriod.new_with_defaults( supervision_period_id=111, status=StateSupervisionPeriodStatus.UNDER_SUPERVISION, state_code="US_XX", start_date=date(1990, 3, 5), supervision_type=StateSupervisionType.PAROLE, ) supervision_periods = [supervision_period] participation_events = self.identifier._find_program_participation_events( program_assignment, supervision_periods) expected_events = [ ProgramParticipationEvent( state_code=program_assignment.state_code, program_id=program_assignment.program_id, event_date=program_assignment.start_date, is_first_day_in_program=True, program_location_id=program_assignment.program_location_id, supervision_type=supervision_period.supervision_type, ), ProgramParticipationEvent( state_code=program_assignment.state_code, program_id=program_assignment.program_id, event_date=program_assignment.start_date + relativedelta(days=1), is_first_day_in_program=False, program_location_id=program_assignment.program_location_id, supervision_type=supervision_period.supervision_type, ), ProgramParticipationEvent( state_code=program_assignment.state_code, program_id=program_assignment.program_id, event_date=program_assignment.start_date + relativedelta(days=2), is_first_day_in_program=False, program_location_id=program_assignment.program_location_id, supervision_type=supervision_period.supervision_type, ), ] self.assertListEqual(expected_events, participation_events)
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))
def test_produce_program_metrics(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] 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" ), ] 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(program_events) self.assertEqual(expected_count, len(metrics))
def _find_program_participation_events( self, program_assignment: StateProgramAssignment, supervision_periods: List[StateSupervisionPeriod], ) -> List[ProgramParticipationEvent]: """Finds instances of actively participating in a program. Produces a ProgramParticipationEvent for each day that the person was actively participating in the program. If the program_assignment has a participation_status of IN_PROGRESS and has a set start_date, produces a ProgramParticipationEvent for every day between the start_date and today. If the program_assignment has a participation_status of DISCHARGED, produces a ProgramParticipationEvent for every day between the start_date and discharge_date, end date exclusive. Where possible, identifies what types of supervision the person is on on the date of the participation. If there are multiple overlapping supervision periods, returns one ProgramParticipationEvent for each supervision period that overlaps. Returns a list of ProgramReferralEvents. """ program_participation_events: List[ProgramParticipationEvent] = [] state_code = program_assignment.state_code participation_status = program_assignment.participation_status if participation_status not in ( StateProgramAssignmentParticipationStatus.IN_PROGRESS, StateProgramAssignmentParticipationStatus.DISCHARGED, ): return program_participation_events start_date = program_assignment.start_date if not start_date: return program_participation_events discharge_date = program_assignment.discharge_date if discharge_date is None: if (participation_status != StateProgramAssignmentParticipationStatus.IN_PROGRESS): logging.warning( "StateProgramAssignment with a DISCHARGED status but no discharge_date: %s", program_assignment, ) return program_participation_events # This person is actively participating in this program. Set the discharge_date for tomorrow. discharge_date = date.today() + relativedelta(days=1) program_id = (program_assignment.program_id if program_assignment.program_id else EXTERNAL_UNKNOWN_VALUE) program_location_id = (program_assignment.program_location_id if program_assignment.program_location_id else EXTERNAL_UNKNOWN_VALUE) participation_date = start_date while participation_date < discharge_date: overlapping_supervision_periods = ( self._find_supervision_periods_overlapping_with_date( participation_date, supervision_periods)) is_first_day_in_program = participation_date == start_date if overlapping_supervision_periods: for supervision_period in supervision_periods: program_participation_events.append( ProgramParticipationEvent( state_code=state_code, event_date=participation_date, is_first_day_in_program=is_first_day_in_program, program_id=program_id, program_location_id=program_location_id, # TODO(#2891): Use supervision_period_supervision_type supervision_type=supervision_period. supervision_type, )) else: program_participation_events.append( ProgramParticipationEvent( state_code=state_code, event_date=participation_date, is_first_day_in_program=is_first_day_in_program, program_id=program_id, program_location_id=program_location_id, )) participation_date = participation_date + relativedelta(days=1) return program_participation_events
def test_find_program_events(self): program_assignment = StateProgramAssignment.new_with_defaults( state_code="US_XX", program_id="PG3", referral_date=date(2020, 1, 3), participation_status=StateProgramAssignmentParticipationStatus. IN_PROGRESS, program_location_id="LOCATION X", start_date=date(2020, 1, 1), ) assessment = StateAssessment.new_with_defaults( state_code="US_XX", assessment_type=StateAssessmentType.ORAS, assessment_score=33, assessment_date=date(2019, 7, 10), ) supervision_period = StateSupervisionPeriod.new_with_defaults( supervision_period_id=999, status=StateSupervisionPeriodStatus.TERMINATED, state_code="US_XX", start_date=date(2019, 3, 5), termination_date=date(2020, 10, 1), supervision_type=StateSupervisionType.PAROLE, supervision_site="OFFICE_1", ) program_assignments = [program_assignment] assessments = [assessment] supervision_periods = [supervision_period] program_events = self.identifier._find_program_events( program_assignments, assessments, supervision_periods, DEFAULT_SUPERVISION_PERIOD_AGENT_ASSOCIATION_LIST, ) expected_events = [ ProgramReferralEvent( state_code=program_assignment.state_code, event_date=program_assignment.referral_date, program_id=program_assignment.program_id, supervision_type=supervision_period.supervision_type, participation_status=program_assignment.participation_status, assessment_score=assessment.assessment_score, assessment_type=assessment.assessment_type, supervising_officer_external_id="XXX", supervising_district_external_id="OFFICE_1", level_1_supervision_location_external_id="OFFICE_1", level_2_supervision_location_external_id=None, ), ProgramParticipationEvent( state_code=program_assignment.state_code, event_date=program_assignment.start_date, is_first_day_in_program=True, program_id=program_assignment.program_id, program_location_id=program_assignment.program_location_id, supervision_type=supervision_period.supervision_type, ), ProgramParticipationEvent( state_code=program_assignment.state_code, event_date=program_assignment.start_date + relativedelta(days=1), is_first_day_in_program=False, program_id=program_assignment.program_id, program_location_id=program_assignment.program_location_id, supervision_type=supervision_period.supervision_type, ), ] self.assertListEqual(program_events, expected_events)