def find_incarceration_events( sentence_groups: List[StateSentenceGroup], incarceration_period_judicial_district_association: List[Dict[str, Any]], county_of_residence: Optional[str]) -> List[IncarcerationEvent]: """Finds instances of admission or release from 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 - county_of_residence: The person's most recent county of residence Returns: A list of IncarcerationEvents for the person. """ incarceration_events: List[IncarcerationEvent] = [] incarceration_sentences = [] supervision_sentences = [] for sentence_group in sentence_groups: incarceration_sentences.extend(sentence_group.incarceration_sentences) supervision_sentences.extend(sentence_group.supervision_sentences) incarceration_periods, _supervision_periods = get_unique_periods_from_sentence_groups_and_add_backedges( sentence_groups) if not incarceration_periods: return incarceration_events state_code = 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 = list_of_dicts_to_dict_with_keys( incarceration_period_judicial_district_association, key=StateIncarcerationPeriod.get_class_id_name()) incarceration_events.extend(find_all_stay_events( state_code, incarceration_sentences, supervision_sentences, incarceration_periods, incarceration_period_to_judicial_district, county_of_residence)) incarceration_events.extend(find_all_admission_release_events( state_code, incarceration_sentences, supervision_sentences, incarceration_periods, county_of_residence)) return incarceration_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