def find_program_events( program_assignments: List[StateProgramAssignment], assessments: List[StateAssessment], supervision_periods: List[StateSupervisionPeriod], supervision_period_to_agent_association: List[Dict[str, Any]], ) -> List[ProgramEvent]: """Finds instances of interaction with a program. Identifies instances of being referred to a program and actively participating in a program. Args: - program_assignments: All of the person's StateProgramAssignments - assessments: All of the person's recorded StateAssessments - supervision_periods: All of the person's supervision_periods - supervision_period_to_agent_associations: dictionary associating StateSupervisionPeriod ids to information about the corresponding StateAgent Returns: A list of ProgramEvents for the person. """ # TODO(#2855): Bring in supervision and incarceration sentences to infer the supervision type on supervision # periods that don't have a set supervision type program_events: List[ProgramEvent] = [] if not program_assignments: return program_events supervision_period_to_agent_associations = list_of_dicts_to_dict_with_keys( supervision_period_to_agent_association, StateSupervisionPeriod.get_class_id_name(), ) state_code = get_single_state_code(program_assignments) should_drop_federal_and_other_country = ( filter_out_federal_and_other_country_supervision_periods(state_code) ) supervision_periods = prepare_supervision_periods_for_calculations( supervision_periods, drop_federal_and_other_country_supervision_periods=should_drop_federal_and_other_country, ) for program_assignment in program_assignments: program_referrals = find_program_referrals( program_assignment, assessments, supervision_periods, supervision_period_to_agent_associations, ) program_events.extend(program_referrals) program_participation_events = find_program_participation_events( program_assignment, supervision_periods ) program_events.extend(program_participation_events) return program_events
def _find_incarceration_events( self, sentence_groups: List[StateSentenceGroup], assessments: List[StateAssessment], violation_responses: List[StateSupervisionViolationResponse], incarceration_period_judicial_district_association: List[Dict[str, Any]], persons_to_recent_county_of_residence: List[Dict[str, Any]], supervision_period_to_agent_association: List[Dict[str, Any]], ) -> List[IncarcerationEvent]: """Finds instances of various events related to incarceration. Transforms the person's StateIncarcerationPeriods, which are connected to their StateSentenceGroups, into IncarcerationAdmissionEvents, IncarcerationStayEvents, and IncarcerationReleaseEvents, representing admissions, stays in, and releases from incarceration in a state prison. Args: - sentence_groups: All of the person's StateSentenceGroups - incarceration_period_judicial_district_association: A list of dictionaries with information connecting StateIncarcerationPeriod ids to the judicial district responsible for the period of incarceration - persons_to_recent_county_of_residence: Reference table rows containing the county that the incarcerated person lives in (prior to incarceration). Returns: A list of IncarcerationEvents for the person. """ incarceration_events: List[IncarcerationEvent] = [] incarceration_sentences: List[StateIncarcerationSentence] = [] supervision_sentences: List[StateSupervisionSentence] = [] for sentence_group in sentence_groups: incarceration_sentences.extend(sentence_group.incarceration_sentences) supervision_sentences.extend(sentence_group.supervision_sentences) ( incarceration_periods, supervision_periods, ) = self._get_unique_periods_from_sentence_groups_and_add_backedges( sentence_groups ) if not incarceration_periods: return incarceration_events county_of_residence: Optional[str] = extract_county_of_residence_from_rows( persons_to_recent_county_of_residence ) state_code: str = get_single_state_code(incarceration_periods) # Convert the list of dictionaries into one dictionary where the keys are the # incarceration_period_id values incarceration_period_to_judicial_district: Dict[ Any, Dict[str, Any] ] = list_of_dicts_to_dict_with_keys( incarceration_period_judicial_district_association, key=StateIncarcerationPeriod.get_class_id_name(), ) supervision_period_to_agent_associations = list_of_dicts_to_dict_with_keys( supervision_period_to_agent_association, StateSupervisionPeriod.get_class_id_name(), ) sorted_violation_responses = prepare_violation_responses_for_calculations( violation_responses=violation_responses, pre_processing_function=state_specific_violation_response_pre_processing_function( state_code=state_code ), ) ( ip_pre_processing_manager, sp_pre_processing_manager, ) = pre_processing_managers_for_calculations( state_code=state_code, incarceration_periods=incarceration_periods, supervision_periods=supervision_periods, violation_responses=sorted_violation_responses, ) if not ip_pre_processing_manager or not sp_pre_processing_manager: raise ValueError( "Expected both pre-processed IPs and SPs for this pipeline." ) incarceration_events.extend( self._find_all_stay_events( ip_pre_processing_manager=ip_pre_processing_manager, incarceration_period_to_judicial_district=incarceration_period_to_judicial_district, county_of_residence=county_of_residence, ) ) incarceration_events.extend( self._find_all_admission_release_events( incarceration_sentences=incarceration_sentences, supervision_sentences=supervision_sentences, ip_pre_processing_manager=ip_pre_processing_manager, sp_pre_processing_manager=sp_pre_processing_manager, assessments=assessments, sorted_violation_responses=sorted_violation_responses, supervision_period_to_agent_associations=supervision_period_to_agent_associations, county_of_residence=county_of_residence, ) ) return incarceration_events
def _find_program_events( self, program_assignments: List[StateProgramAssignment], assessments: List[StateAssessment], supervision_periods: List[StateSupervisionPeriod], supervision_period_to_agent_association: List[Dict[str, Any]], ) -> List[ProgramEvent]: """Finds instances of interaction with a program. Identifies instances of being referred to a program and actively participating in a program. Args: - program_assignments: All of the person's StateProgramAssignments - assessments: All of the person's recorded StateAssessments - supervision_periods: All of the person's supervision_periods - supervision_period_to_agent_associations: dictionary associating StateSupervisionPeriod ids to information about the corresponding StateAgent Returns: A list of ProgramEvents for the person. """ # TODO(#2855): Bring in supervision and incarceration sentences to infer the supervision type on supervision # periods that don't have a set supervision type program_events: List[ProgramEvent] = [] if not program_assignments: return program_events state_code = get_single_state_code(program_assignments) supervision_period_to_agent_associations = list_of_dicts_to_dict_with_keys( supervision_period_to_agent_association, StateSupervisionPeriod.get_class_id_name(), ) ( _, sp_pre_processing_manager, ) = pre_processing_managers_for_calculations( state_code=state_code, # SP pre-processing doesn't rely on StateIncarcerationPeriod entities, # and this pipeline doesn't require StateIncarcerationPeriods incarceration_periods=None, supervision_periods=supervision_periods, # Note: This pipeline cannot be run for any state that relies on # StateSupervisionViolationResponse entities in IP pre-processing violation_responses=None, ) if not sp_pre_processing_manager: raise ValueError("Expected pre-processed SPs for this pipeline.") supervision_periods_for_calculations = ( sp_pre_processing_manager. pre_processed_supervision_period_index_for_calculations( ).supervision_periods) for program_assignment in program_assignments: program_referrals = self._find_program_referrals( program_assignment, assessments, supervision_periods_for_calculations, supervision_period_to_agent_associations, ) program_events.extend(program_referrals) program_participation_events = self._find_program_participation_events( program_assignment, supervision_periods_for_calculations) program_events.extend(program_participation_events) return program_events