def test_find_release_events_by_cohort_year_no_recid_cond_release(self):
        """Tests the find_release_events_by_cohort_year function when the person
        does not have any StateIncarcerationPeriods after their first, and they
        were released conditionally."""
        only_incarceration_period = \
            StateIncarcerationPeriod.new_with_defaults(
                incarceration_period_id=1111,
                incarceration_type=StateIncarcerationType.STATE_PRISON,
                status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY,
                state_code='TX',
                admission_date=date(2000, 1, 9),
                admission_reason=AdmissionReason.NEW_ADMISSION,
                release_date=date(2003, 12, 8),
                release_reason=ReleaseReason.CONDITIONAL_RELEASE)

        release_events_by_cohort = \
            identifier.find_release_events_by_cohort_year(
                [only_incarceration_period], _COUNTY_OF_RESIDENCE)

        assert len(release_events_by_cohort) == 1

        assert release_events_by_cohort[2003] == [
            NonRecidivismReleaseEvent(
                state_code='TX',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                original_admission_date=only_incarceration_period.
                admission_date,
                release_date=only_incarceration_period.release_date,
                release_facility=None)
        ]
Beispiel #2
0
    def test_for_last_incarceration_period(self):
        state_code = 'CA'
        admission_date = date(2000, 12, 1)
        status = StateIncarcerationPeriodStatus.NOT_IN_CUSTODY
        release_date = date(2005, 3, 8)
        release_facility = 'Facility'

        for release_reason in ReleaseReason:
            if release_reason in _SHOULD_BE_FILTERED_OUT_IN_VALIDATION_RELEASE:
                with pytest.raises(ValueError) as e:
                    _ = identifier.for_last_incarceration_period(
                        state_code, admission_date, status, release_date,
                        release_reason, release_facility, _COUNTY_OF_RESIDENCE)
                    assert str(e) == ("validate_sort_and_collapse_"
                                      "incarceration_periods is "
                                      "not effectively filtering."
                                      " Found unexpected release_reason"
                                      f" of: {release_reason}")
            else:
                # Will raise ValueError if enum case isn't handled
                event = identifier.for_last_incarceration_period(
                    state_code, admission_date, status, release_date,
                    release_reason, release_facility, _COUNTY_OF_RESIDENCE)

                if release_reason in [
                        ReleaseReason.COMMUTED, ReleaseReason.COMPASSIONATE,
                        ReleaseReason.CONDITIONAL_RELEASE,
                        ReleaseReason.EXTERNAL_UNKNOWN,
                        ReleaseReason.SENTENCE_SERVED
                ]:
                    assert event == NonRecidivismReleaseEvent(
                        state_code, admission_date, release_date,
                        release_facility, _COUNTY_OF_RESIDENCE)
                else:
                    assert not event
Beispiel #3
0
def for_incarceration_period_no_return(
        state_code: str,
        admission_date: date,
        release_date: date,
        release_facility: Optional[str],
        county_of_residence: Optional[str]) \
        -> Optional[ReleaseEvent]:
    """Builds a NonRecidivismReleaseEvent from the attributes of the release from incarceration.

    Returns any non-recidivism event relevant to the person's StateIncarcerationPeriod.

        Args:
            state_code: the state code on the StateIncarcerationPeriod
            admission_date: when this StateIncarcerationPeriod started
            release_date: when they were released from this StateIncarcerationPeriod
            release_facility: the facility they were released from on this StateIncarcerationPeriod
            county_of_residence: the county that the incarcerated person lives in (prior to incarceration).

        Returns:
            A non-recidivism event.
    """
    return NonRecidivismReleaseEvent(state_code=state_code,
                                     original_admission_date=admission_date,
                                     release_date=release_date,
                                     release_facility=release_facility,
                                     county_of_residence=county_of_residence)
Beispiel #4
0
    def test_find_release_events_by_cohort_year_invalid_open_period(self):
        """Tests the find_release_events_by_cohort_year function where the person has an open IN_CUSTODY period that is
        invalid because the person was released elsewhere after the admission to the period."""
        invalid_open_incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1111,
            incarceration_type=StateIncarcerationType.STATE_PRISON,
            status=StateIncarcerationPeriodStatus.IN_CUSTODY,
            state_code='TX',
            admission_date=date(2008, 11, 20),
            admission_reason=AdmissionReason.NEW_ADMISSION)

        closed_incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1111,
            incarceration_type=StateIncarcerationType.STATE_PRISON,
            status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY,
            state_code='TX',
            admission_date=date(2008, 11, 20),
            admission_reason=AdmissionReason.NEW_ADMISSION,
            release_date=date(2009, 3, 14),
            release_reason=ReleaseReason.SENTENCE_SERVED)

        release_events_by_cohort = \
            identifier.find_release_events_by_cohort_year(
                [invalid_open_incarceration_period, closed_incarceration_period],
                _COUNTY_OF_RESIDENCE)

        assert release_events_by_cohort[2009] == [
            NonRecidivismReleaseEvent(
                state_code='TX',
                county_of_residence=_COUNTY_OF_RESIDENCE,
                original_admission_date=closed_incarceration_period.admission_date,
                release_date=closed_incarceration_period.release_date,
                release_facility=None)]
Beispiel #5
0
    def test_find_release_events_by_cohort_year_sentence_served_prob_rev(self):
        """Tests the find_release_events_by_cohort_year function path where the
        person served their first sentence, then later returned on a probation
        revocation."""

        initial_incarceration_period = \
            StateIncarcerationPeriod.new_with_defaults(
                incarceration_period_id=1111,
                incarceration_type=StateIncarcerationType.STATE_PRISON,
                status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY,
                state_code='TX',
                admission_date=date(2008, 11, 20),
                admission_reason=AdmissionReason.NEW_ADMISSION,
                release_date=date(2010, 12, 4),
                release_reason=ReleaseReason.SENTENCE_SERVED)

        first_reincarceration_period = \
            StateIncarcerationPeriod.new_with_defaults(
                incarceration_period_id=2222,
                incarceration_type=StateIncarcerationType.STATE_PRISON,
                status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY,
                state_code='TX',
                admission_date=date(2011, 4, 5),
                admission_reason=AdmissionReason.PROBATION_REVOCATION,
                release_date=date(2014, 4, 14),
                release_reason=ReleaseReason.SENTENCE_SERVED)

        incarceration_periods = [initial_incarceration_period,
                                 first_reincarceration_period]

        release_events_by_cohort = \
            identifier.find_release_events_by_cohort_year(
                incarceration_periods=incarceration_periods,
                county_of_residence=_COUNTY_OF_RESIDENCE)

        assert len(release_events_by_cohort) == 2

        assert release_events_by_cohort[2010] == [RecidivismReleaseEvent(
            state_code='TX',
            original_admission_date=initial_incarceration_period.admission_date,
            release_date=initial_incarceration_period.release_date,
            release_facility=None,
            reincarceration_date=first_reincarceration_period.admission_date,
            reincarceration_facility=None,
            county_of_residence=_COUNTY_OF_RESIDENCE,
            return_type=ReincarcerationReturnType.REVOCATION,
            from_supervision_type=StateSupervisionPeriodSupervisionType.
            PROBATION)]

        assert release_events_by_cohort[2014] == [
            NonRecidivismReleaseEvent(
                state_code='TX',
                original_admission_date=first_reincarceration_period.
                admission_date,
                release_date=first_reincarceration_period.release_date,
                county_of_residence=_COUNTY_OF_RESIDENCE,
                release_facility=None)]
Beispiel #6
0
    def test_find_release_events_by_cohort_year_cond_release_new_admit(self):
        """Tests the find_release_events_by_cohort_year function path where the
        person was conditionally released on parole but returned as a new
         admission."""

        initial_incarceration_period = \
            StateIncarcerationPeriod.new_with_defaults(
                incarceration_period_id=1111,
                incarceration_type=StateIncarcerationType.STATE_PRISON,
                status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY,
                state_code='TX',
                admission_date=date(2008, 11, 20),
                admission_reason=AdmissionReason.NEW_ADMISSION,
                release_date=date(2010, 12, 4),
                release_reason=ReleaseReason.CONDITIONAL_RELEASE)

        first_reincarceration_period = \
            StateIncarcerationPeriod.new_with_defaults(
                incarceration_period_id=2222,
                incarceration_type=StateIncarcerationType.STATE_PRISON,
                status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY,
                state_code='TX',
                admission_date=date(2011, 4, 5),
                admission_reason=AdmissionReason.NEW_ADMISSION,
                release_date=date(2014, 4, 14),
                release_reason=ReleaseReason.SENTENCE_SERVED)

        incarceration_periods = [initial_incarceration_period,
                                 first_reincarceration_period]

        release_events_by_cohort = \
            identifier.find_release_events_by_cohort_year(
                incarceration_periods=incarceration_periods,
                county_of_residence=_COUNTY_OF_RESIDENCE)

        assert len(release_events_by_cohort) == 2

        assert release_events_by_cohort[2010] == [RecidivismReleaseEvent(
            state_code='TX',
            original_admission_date=initial_incarceration_period.admission_date,
            release_date=initial_incarceration_period.release_date,
            release_facility=None,
            reincarceration_date=first_reincarceration_period.admission_date,
            reincarceration_facility=None,
            county_of_residence=_COUNTY_OF_RESIDENCE,
            return_type=ReincarcerationReturnType.NEW_ADMISSION)]

        assert release_events_by_cohort[2014] == [
            NonRecidivismReleaseEvent(
                state_code='TX',
                original_admission_date=first_reincarceration_period.
                admission_date,
                release_date=first_reincarceration_period.release_date,
                county_of_residence=_COUNTY_OF_RESIDENCE,
                release_facility=None)]
    def test_find_release_events_by_cohort_year_transfer_no_recidivism(self):
        """Tests the find_release_events_by_cohort_year function path where the
        person was transferred between two incarceration periods."""

        initial_incarceration_period = \
            StateIncarcerationPeriod.new_with_defaults(
                incarceration_period_id=1111,
                incarceration_type=StateIncarcerationType.STATE_PRISON,
                status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY,
                state_code='TX',
                admission_date=date(2008, 11, 20),
                admission_reason=AdmissionReason.NEW_ADMISSION,
                release_date=date(2010, 12, 4),
                release_reason=ReleaseReason.TRANSFER)

        first_reincarceration_period = \
            StateIncarcerationPeriod.new_with_defaults(
                incarceration_period_id=2222,
                incarceration_type=StateIncarcerationType.STATE_PRISON,
                status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY,
                state_code='TX',
                admission_date=date(2010, 12, 4),
                admission_reason=AdmissionReason.TRANSFER,
                release_date=date(2014, 4, 14),
                release_reason=ReleaseReason.SENTENCE_SERVED)

        incarceration_periods = [
            initial_incarceration_period, first_reincarceration_period
        ]

        release_events_by_cohort = \
            identifier.find_release_events_by_cohort_year(
                incarceration_periods=incarceration_periods,
                county_of_residence=_COUNTY_OF_RESIDENCE)

        assert len(release_events_by_cohort) == 1

        assert release_events_by_cohort[2014] == [
            NonRecidivismReleaseEvent(
                state_code='TX',
                original_admission_date=initial_incarceration_period.
                admission_date,
                release_date=first_reincarceration_period.release_date,
                county_of_residence=_COUNTY_OF_RESIDENCE,
                release_facility=None)
        ]
Beispiel #8
0
def for_last_incarceration_period(
        state_code: str,
        admission_date: date,
        status: StateIncarcerationPeriodStatus,
        release_date: Optional[date],
        release_reason: Optional[ReleaseReason],
        release_facility: Optional[str],
        county_of_residence: Optional[str]) \
        -> Optional[ReleaseEvent]:
    """Looks at the last StateIncarcerationPeriod for a non-recidivism event.

    If the person has been released from their last StateIncarcerationPeriod,
    there is an instance of non-recidivism to count. If they are still
    incarcerated, or they were released for some circumstances, e.g. because
    they died, there is nothing to count. Returns any non-recidivism event
    relevant to the person's last StateIncarcerationPeriod.

        Args:
            state_code: the state code on the StateIncarcerationPeriod
            admission_date: when this StateIncarcerationPeriod started
            status: status of this StateIncarcerationPeriod
            release_date: when they were released from this
                 StateIncarcerationPeriod
            release_reason: reason they were released from this
                StateIncarcerationPeriod
            release_facility: the facility they were released from on this
                StateIncarcerationPeriod
            county_of_residence: the county that the incarcerated person lives
                in (prior to incarceration).


        Returns:
            A non-recidivism event if released legitimately from this
                StateIncarcerationPeriod. None otherwise.
    """

    # If the person is still in custody, there is nothing to track.
    if status == StateIncarcerationPeriodStatus.IN_CUSTODY:
        return None

    if not release_date:
        # If the person is not in custody, there should be a release_date.
        # This should not happen after validation. Throw error.
        raise ValueError("release_date is not set where it should be.")

    if release_reason in [ReleaseReason.DEATH, ReleaseReason.EXECUTION]:
        # If the person was released from this incarceration period because they
        # died or were executed, do not include them in the release cohort.
        return None
    if release_reason in (ReleaseReason.ESCAPE,
                          ReleaseReason.RELEASED_IN_ERROR):
        # If the person was released from this incarceration period but was not
        # meant to be released, do not include them in the release cohort.
        return None
    if release_reason in (ReleaseReason.TRANSFER,
                          ReleaseReason.TRANSFERRED_OUT_OF_STATE):
        # If the person was released from this incarceration period because they
        # were transferred elsewhere, do not include them in the release cohort.
        return None
    if release_reason == ReleaseReason.COURT_ORDER:
        # If the person was released from this incarceration period on a court
        # order, do not include them in the release cohort.
        return None
    if release_reason in [
            ReleaseReason.COMMUTED, ReleaseReason.COMPASSIONATE,
            ReleaseReason.CONDITIONAL_RELEASE, ReleaseReason.EXTERNAL_UNKNOWN,
            ReleaseReason.SENTENCE_SERVED
    ]:

        return NonRecidivismReleaseEvent(
            state_code=state_code,
            original_admission_date=admission_date,
            release_date=release_date,
            release_facility=release_facility,
            county_of_residence=county_of_residence)

    if release_reason == ReleaseReason.RELEASED_FROM_TEMPORARY_CUSTODY:
        # This should never happen. Should have been filtered by
        # validate_sort_and_collapse_incarceration_periods function. Throw error
        raise ValueError("validate_sort_and_collapse_incarceration_periods is "
                         "not effectively filtering. Found unexpected "
                         f"release_reason of: {release_reason}")

    raise ValueError("Enum case not handled for "
                     "StateIncarcerationPeriodReleaseReason of type:"
                     f" {release_reason}.")