Ejemplo n.º 1
0
    def test_get_month_supervision_type_no_dates_on_sentence(self):
        any_date_in_month = date(2018, 4, 13)

        supervision_period = \
            StateSupervisionPeriod.new_with_defaults(
                supervision_period_id=111,
                external_id='sp1',
                status=StateSupervisionPeriodStatus.TERMINATED,
                state_code='US_ND',
                start_date=date(2018, 3, 5),
                termination_date=date(2018, 5, 19),
                termination_reason=StateSupervisionPeriodTerminationReason.DISCHARGE,
                supervision_type=None
            )

        supervision_sentence = \
            StateSupervisionSentence.new_with_defaults(
                supervision_sentence_id=111,
                external_id='ss1',
                status=StateSentenceStatus.COMPLETED,
                supervision_periods=[supervision_period]
            )

        supervision_sentences = [supervision_sentence]
        incarceration_sentences = []

        supervision_type = get_month_supervision_type(any_date_in_month,
                                                      supervision_sentences,
                                                      incarceration_sentences,
                                                      supervision_period)

        self.assertEqual(
            StateSupervisionPeriodSupervisionType.INTERNAL_UNKNOWN,
            supervision_type)
Ejemplo n.º 2
0
    def test_get_month_supervision_type_parole(self):
        any_date_in_month = date(2018, 4, 13)

        supervision_period = \
            StateSupervisionPeriod.new_with_defaults(
                supervision_period_id=111,
                external_id='sp1',
                status=StateSupervisionPeriodStatus.TERMINATED,
                state_code='US_ND',
                start_date=date(2018, 3, 5),
                termination_date=date(2018, 5, 19),
                termination_reason=StateSupervisionPeriodTerminationReason.DISCHARGE,
                supervision_type=None
            )

        incarceration_sentence = StateIncarcerationSentence.new_with_defaults(
            incarceration_sentence_id=123,
            external_id='is1',
            start_date=date(2018, 3, 1),
            completion_date=date(2018, 5, 30),
            supervision_periods=[supervision_period])

        supervision_sentences = []
        incarceration_sentences = [incarceration_sentence]

        supervision_type = get_month_supervision_type(any_date_in_month,
                                                      supervision_sentences,
                                                      incarceration_sentences,
                                                      supervision_period)

        self.assertEqual(StateSupervisionPeriodSupervisionType.PAROLE,
                         supervision_type)
Ejemplo n.º 3
0
    def test_get_month_supervision_type_probation_options(self):
        any_date_in_month = date(2018, 4, 13)

        supervision_period = \
            StateSupervisionPeriod.new_with_defaults(
                supervision_period_id=111,
                external_id='sp1',
                status=StateSupervisionPeriodStatus.TERMINATED,
                state_code='US_ND',
                start_date=date(2018, 3, 5),
                termination_date=date(2018, 5, 19),
                termination_reason=StateSupervisionPeriodTerminationReason.DISCHARGE,
                supervision_type=None
            )

        supervision_sentence = \
            StateSupervisionSentence.new_with_defaults(
                supervision_sentence_id=111,
                external_id='ss1',
                start_date=date(2018, 3, 1),
                completion_date=date(2018, 5, 30),
                status=StateSentenceStatus.COMPLETED,
                projected_completion_date=date(2018, 5, 19),
                supervision_periods=[supervision_period]
            )

        supervision_sentences = [supervision_sentence]
        incarceration_sentences = []

        probation_types = [
            StateSupervisionType.PRE_CONFINEMENT,
            StateSupervisionType.POST_CONFINEMENT,
            StateSupervisionType.HALFWAY_HOUSE,
            StateSupervisionType.CIVIL_COMMITMENT
        ]

        for sup_type in probation_types:
            supervision_sentence.supervision_type = sup_type

            supervision_type = get_month_supervision_type(
                any_date_in_month, supervision_sentences,
                incarceration_sentences, supervision_period)

            self.assertEqual(StateSupervisionPeriodSupervisionType.PROBATION,
                             supervision_type)
Ejemplo n.º 4
0
    def test_get_month_supervision_type_test_all_enums(self):
        any_date_in_month = date(2018, 4, 13)

        supervision_period = \
            StateSupervisionPeriod.new_with_defaults(
                supervision_period_id=111,
                external_id='sp1',
                status=StateSupervisionPeriodStatus.TERMINATED,
                state_code='US_ND',
                start_date=date(2018, 3, 5),
                termination_date=date(2018, 5, 19),
                termination_reason=StateSupervisionPeriodTerminationReason.DISCHARGE,
                supervision_type=None
            )

        supervision_sentence = \
            StateSupervisionSentence.new_with_defaults(
                supervision_sentence_id=111,
                external_id='ss1',
                start_date=date(2018, 3, 1),
                completion_date=date(2018, 5, 30),
                status=StateSentenceStatus.COMPLETED,
                projected_completion_date=date(2018, 5, 19),
                supervision_type=StateSupervisionType.INTERNAL_UNKNOWN,
                supervision_periods=[supervision_period]
            )

        supervision_sentences = [supervision_sentence]
        incarceration_sentences = []

        for sup_type in StateSupervisionType:
            supervision_sentence.supervision_type = sup_type

            # Tests that all cases are covered
            _ = get_month_supervision_type(any_date_in_month,
                                           supervision_sentences,
                                           incarceration_sentences,
                                           supervision_period)
Ejemplo n.º 5
0
def find_supervision_termination_bucket(
    supervision_sentences: List[StateSupervisionSentence],
    incarceration_sentences: List[StateIncarcerationSentence],
    supervision_period: StateSupervisionPeriod,
    indexed_supervision_periods: Dict[int, Dict[int,
                                                List[StateSupervisionPeriod]]],
    assessments: List[StateAssessment],
    supervision_period_to_agent_associations: Dict[int, Dict[Any, Any]]
) -> Optional[SupervisionTimeBucket]:
    """Identifies an instance of supervision termination. If the given supervision_period has a valid start_date and
    termination_date, then returns a SupervisionTerminationBucket with the details of the termination.

    Calculates the change in assessment score from the beginning of supervision to the termination of supervision.
    This is done by identifying the first reassessment (or, the second score) and the last score during a
    supervision period, and taking the difference between the last score and the first reassessment score.

    If a person has multiple supervision periods that end in a given month, the the earliest start date and the latest
    termination date of the periods is used to estimate the start and end dates of the supervision. These dates
    are then used to determine what the second and last assessment scores are.

    If this supervision does not have a termination_date, then None is returned.
    """
    if supervision_period.start_date is not None and supervision_period.termination_date is not None:
        start_date = supervision_period.start_date
        termination_date = supervision_period.termination_date

        termination_year = termination_date.year
        termination_month = termination_date.month

        periods_terminated_in_year = indexed_supervision_periods.get(
            termination_year)

        if periods_terminated_in_year:
            periods_terminated_in_month = periods_terminated_in_year.get(
                termination_month)

            if periods_terminated_in_month:
                for period in periods_terminated_in_month:
                    if period.start_date and period.start_date < start_date:
                        start_date = period.start_date

                    if period.termination_date and period.termination_date > termination_date:
                        termination_date = period.termination_date

        assessment_score_change, \
            end_assessment_score, \
            end_assessment_level, \
            end_assessment_type = \
            find_assessment_score_change(
                start_date, termination_date, assessments)

        supervising_officer_external_id, supervising_district_external_id = \
            _get_supervising_officer_and_district(supervision_period, supervision_period_to_agent_associations)

        case_type = _identify_most_severe_case_type(supervision_period)
        supervision_type = get_month_supervision_type(termination_date,
                                                      supervision_sentences,
                                                      incarceration_sentences,
                                                      supervision_period)

        return SupervisionTerminationBucket(
            state_code=supervision_period.state_code,
            year=termination_date.year,
            month=termination_date.month,
            supervision_type=supervision_type,
            case_type=case_type,
            assessment_score=end_assessment_score,
            assessment_level=end_assessment_level,
            assessment_type=end_assessment_type,
            termination_reason=supervision_period.termination_reason,
            assessment_score_change=assessment_score_change,
            supervising_officer_external_id=supervising_officer_external_id,
            supervising_district_external_id=supervising_district_external_id)

    return None
Ejemplo n.º 6
0
def find_time_buckets_for_supervision_period(
    supervision_sentences: List[StateSupervisionSentence],
    incarceration_sentences: List[StateIncarcerationSentence],
    supervision_period: StateSupervisionPeriod,
    incarceration_periods_by_admission_month: Dict[int, Dict[
        int, List[StateIncarcerationPeriod]]],
    months_fully_incarcerated: Set[Tuple[int, int]],
    assessments: List[StateAssessment],
    violation_responses: List[StateSupervisionViolationResponse],
    supervision_period_to_agent_associations: Dict[int, Dict[Any, Any]]
) -> List[SupervisionTimeBucket]:
    """Finds months that this person was on supervision for the given StateSupervisionPeriod, where the person was not
    incarcerated for the full month and did not have a revocation admission that month.

    Args:
        - supervision_period: The supervision period the person was on
        - incarceration_periods_by_admission_month: A dictionary mapping years and months of admissions to prison to the
            StateIncarcerationPeriods that started in that month.
        - months_fully_incarcerated: A set of tuples in the format (year, month) for each month of which this person has
            been incarcerated for the full month.
        - ssvr_agent_associations: dictionary associating StateSupervisionViolationResponse ids to information about the
            corresponding StateAgent on the response
    Returns
        - A set of unique SupervisionTimeBuckets for the person for the given StateSupervisionPeriod.
    """
    supervision_month_buckets: List[SupervisionTimeBucket] = []

    start_date = supervision_period.start_date
    termination_date = supervision_period.termination_date

    if termination_date is None:
        termination_date = date.today()

    if start_date is None:
        return supervision_month_buckets

    start_of_month = first_day_of_month(start_date)

    while start_of_month <= termination_date:
        if month_is_non_revocation_supervision_bucket(
                start_of_month, termination_date, months_fully_incarcerated,
                incarceration_periods_by_admission_month):

            supervision_type = get_month_supervision_type(
                start_of_month, supervision_sentences, incarceration_sentences,
                supervision_period)
            end_of_month = last_day_of_month(start_of_month)

            assessment_score, assessment_level, assessment_type = find_most_recent_assessment(
                end_of_month, assessments)

            supervising_officer_external_id, supervising_district_external_id = \
                _get_supervising_officer_and_district(supervision_period, supervision_period_to_agent_associations)

            case_type = _identify_most_severe_case_type(supervision_period)

            end_of_violation_window = end_of_month if end_of_month < termination_date else termination_date
            violation_history = get_violation_and_response_history(
                end_of_violation_window, violation_responses)

            supervision_month_buckets.append(
                NonRevocationReturnSupervisionTimeBucket(
                    state_code=supervision_period.state_code,
                    year=start_of_month.year,
                    month=start_of_month.month,
                    supervision_type=supervision_type,
                    case_type=case_type,
                    assessment_score=assessment_score,
                    assessment_level=assessment_level,
                    assessment_type=assessment_type,
                    most_severe_violation_type=violation_history.
                    most_severe_violation_type,
                    most_severe_violation_type_subtype=violation_history.
                    most_severe_violation_type_subtype,
                    response_count=violation_history.response_count,
                    supervising_officer_external_id=
                    supervising_officer_external_id,
                    supervising_district_external_id=
                    supervising_district_external_id,
                    supervision_level=supervision_period.supervision_level,
                    supervision_level_raw_text=supervision_period.
                    supervision_level_raw_text))

        start_of_month = start_of_month + relativedelta(months=1)

    return supervision_month_buckets