def process(self, element): """Identifies instances of recidivism and non-recidivism. Sends the identifier the StateIncarcerationPeriods for a given StatePerson, which returns a list of ReleaseEvents for each year the individual was released from incarceration. Args: element: Tuple containing person_id and a dictionary with a StatePerson and a list of StateIncarcerationPeriods Yields: Tuple containing the StatePerson and a collection of ReleaseEvents. """ _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) release_events_by_cohort_year = identifier.find_release_events_by_cohort_year( **kwargs ) if not release_events_by_cohort_year: logging.info( "No valid release events identified for person with" "id: %d. Excluding them from the " "calculations.", person.person_id, ) else: yield person.person_id, (person, release_events_by_cohort_year)
def test_person_and_kwargs_for_identifier_no_person(self): assessment = StateAssessment.new_with_defaults(state_code='US_XX') arg_to_entities_map = { # There should never be two StatePerson entities with the same person_id. This should fail loudly. 'assessments': iter([assessment]) } with pytest.raises(ValueError): _ = person_and_kwargs_for_identifier(arg_to_entities_map)
def test_person_and_kwargs_for_identifier_two_people_same_id(self): person_input_1 = StatePerson.new_with_defaults(person_id=123) person_input_2 = StatePerson.new_with_defaults(person_id=123) assessment = StateAssessment.new_with_defaults(state_code='US_XX') arg_to_entities_map = { # There should never be two StatePerson entities with the same person_id. This should fail loudly. 'person': iter([person_input_1, person_input_2]), 'assessments': iter([assessment]) } with pytest.raises(ValueError): _ = person_and_kwargs_for_identifier(arg_to_entities_map)
def test_person_and_kwargs_for_identifier(self): person_input = StatePerson.new_with_defaults(person_id=123) assessment = StateAssessment.new_with_defaults(state_code='US_XX') arg_to_entities_map = { 'person': iter([person_input]), 'assessments': iter([assessment]) } person, kwargs = person_and_kwargs_for_identifier(arg_to_entities_map) expected_kwargs = {'assessments': [assessment]} self.assertEqual(person, person_input) self.assertEqual(expected_kwargs, kwargs)
def process(self, element): """Identifies instances of referrals to a program.""" _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) # Find the ProgramEvents from the StateProgramAssignments program_events = identifier.find_program_events(**kwargs) if not program_events: logging.info( "No valid program events for person with id: %d. Excluding them from the " "calculations.", person.person_id, ) else: yield person.person_id, (person, program_events)
def process(self, element): """Identifies instances of admission and release from incarceration.""" _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) # Find the IncarcerationEvents incarceration_events = identifier.find_incarceration_events(**kwargs) if not incarceration_events: logging.info( "No valid incarceration events for person with id: %d. Excluding them from the " "calculations.", person.person_id, ) else: yield person.person_id, (person, incarceration_events)
def process(self, element, ssvr_agent_associations, supervision_period_to_agent_associations): """Identifies various events related to supervision relevant to calculations.""" _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) # Add these arguments to the keyword args for the identifier kwargs['ssvr_agent_associations'] = ssvr_agent_associations kwargs['supervision_period_to_agent_associations'] = supervision_period_to_agent_associations # Find the SupervisionTimeBuckets from the supervision and incarceration # periods supervision_time_buckets = identifier.find_supervision_time_buckets(**kwargs) if not supervision_time_buckets: logging.info("No valid supervision time buckets for person with id: %d. Excluding them from the " "calculations.", person.person_id) else: yield person, supervision_time_buckets
def process(self, element): """Identifies various events related to supervision relevant to calculations.""" _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) # Find the SupervisionTimeBuckets from the supervision and incarceration # periods supervision_time_buckets = identifier.find_supervision_time_buckets( **kwargs) if not supervision_time_buckets: logging.info( "No valid supervision time buckets for person with id: %d. Excluding them from the " "calculations.", person.person_id, ) else: yield person.person_id, (person, supervision_time_buckets)
def process( self, element: Tuple[int, Dict[str, Iterable[Any]]], identifier: BaseIdentifier ) -> Generator[Tuple[Optional[int], Tuple[ entities.StatePerson, List[IdentifierEvent]]], None, None, ]: """Identifies various events relevant to calculations.""" _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) events = identifier.find_events(person, kwargs) if not events: logging.info( "No valid events for person with id: %d. Excluding them from the calculations.", person.person_id, ) else: yield person.person_id, (person, events)
def process(self, element, supervision_period_to_agent_associations): """Identifies instances of referrals to a program.""" _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) # Add this arguments to the keyword args for the identifier kwargs[ 'supervision_period_to_agent_associations'] = supervision_period_to_agent_associations # Find the ProgramEvents from the StateProgramAssignments program_events = \ identifier.find_program_events(**kwargs) if not program_events: logging.info( "No valid program events for person with id: %d. Excluding them from the " "calculations.", person.person_id) else: yield (person, program_events)
def process(self, element, person_id_to_county): """Identifies instances of recidivism and non-recidivism. Sends the identifier the StateIncarcerationPeriods for a given StatePerson, which returns a list of ReleaseEvents for each year the individual was released from incarceration. Args: element: Tuple containing person_id and a dictionary with a StatePerson and a list of StateIncarcerationPeriods Yields: Tuple containing the StatePerson and a collection of ReleaseEvents. """ _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) # Get the person's county of residence, if present person_id_to_county_fields = person_id_to_county.get( person.person_id, None) county_of_residence = (person_id_to_county_fields.get( 'county_of_residence', None) if person_id_to_county_fields else None) # Add this arguments to the keyword args for the identifier kwargs['county_of_residence'] = county_of_residence release_events_by_cohort_year = \ identifier.find_release_events_by_cohort_year(**kwargs) if not release_events_by_cohort_year: logging.info( "No valid release events identified for person with" "id: %d. Excluding them from the " "calculations.", person.person_id) else: yield person, release_events_by_cohort_year
def process(self, element, person_id_to_county): """Identifies instances of admission and release from incarceration.""" _, person_entities = element person, kwargs = person_and_kwargs_for_identifier(person_entities) # Get the person's county of residence, if present person_id_to_county_fields = person_id_to_county.get(person.person_id, None) county_of_residence = person_id_to_county_fields.get('county_of_residence', None) \ if person_id_to_county_fields else None # Add this arguments to the keyword args for the identifier kwargs['county_of_residence'] = county_of_residence # Find the IncarcerationEvents incarceration_events = identifier.find_incarceration_events(**kwargs) if not incarceration_events: logging.info("No valid incarceration events for person with id: %d. Excluding them from the " "calculations.", person.person_id) else: yield person, incarceration_events