def get_revocation_violation_type_analysis_metrics(
        supervision_time_bucket: RevocationReturnSupervisionTimeBucket,
        characteristic_combo: Dict[str, Any],
        calculation_month_upper_bound: date,
        calculation_month_lower_bound: Optional[date],
        all_buckets_sorted: List[SupervisionTimeBucket],
        periods_and_buckets: Dict[int, List[SupervisionTimeBucket]],
        include_metric_period_output: bool) -> List[Tuple[Dict[str, Any], Any]]:
    """Produces metrics of the type REVOCATION_VIOLATION_TYPE_ANALYSIS. For each violation type list in the bucket's
    violation_type_frequency_counter, produces metrics for each violation type in the list, and one with a
    violation_count_type of 'VIOLATION' to keep track of the overall number of violations."""
    metrics = []
    if supervision_time_bucket.violation_type_frequency_counter:
        for violation_type_list in supervision_time_bucket.violation_type_frequency_counter:
            violation_type_augment_values = {'violation_count_type': 'VIOLATION'}

            violation_count_characteristic_combo = augment_combination(characteristic_combo,
                                                                       violation_type_augment_values)

            revocation_analysis_metrics_violation_count = map_metric_combinations(
                violation_count_characteristic_combo,
                supervision_time_bucket,
                calculation_month_upper_bound,
                calculation_month_lower_bound,
                all_buckets_sorted,
                periods_and_buckets,
                SupervisionMetricType.REVOCATION_VIOLATION_TYPE_ANALYSIS,
                include_metric_period_output
            )

            metrics.extend(revocation_analysis_metrics_violation_count)

            for violation_type_string in violation_type_list:

                violation_type_augment_values = {'violation_count_type': violation_type_string}

                violation_type_characteristic_combo = augment_combination(characteristic_combo,
                                                                          violation_type_augment_values)

                revocation_analysis_metrics_violation_type = map_metric_combinations(
                    violation_type_characteristic_combo,
                    supervision_time_bucket,
                    calculation_month_upper_bound,
                    calculation_month_lower_bound,
                    all_buckets_sorted,
                    periods_and_buckets,
                    SupervisionMetricType.REVOCATION_VIOLATION_TYPE_ANALYSIS,
                    include_metric_period_output
                )

                metrics.extend(revocation_analysis_metrics_violation_type)

    return metrics
def person_level_augmented_combo(combo: Dict[str, Any], event: ReleaseEvent,
                                 methodology: MetricMethodologyType,
                                 period: Optional[int]) -> Dict[str, Any]:
    """Returns a dictionary that has been augmented with all of the parameters that apply to the given event.

    Args:
        combo: the base combo to be augmented with methodology and period
        event: the ReleaseEvent from which the combo was derived
        methodology: the MetricMethodologyType to add to each combo
        period: the follow_up_period value to add to each combo

    Returns:
        The augmented combination dictionary.
    """
    parameters: Dict[str, Any] = {
        'state_code': event.state_code,
        'methodology': methodology
    }

    if period:
        parameters['follow_up_period'] = period

    if isinstance(event, RecidivismReleaseEvent):
        parameters['return_type'] = event.return_type
        parameters['source_violation_type'] = event.source_violation_type
        parameters['from_supervision_type'] = event.from_supervision_type

    return augment_combination(combo, parameters)
Exemple #3
0
def test_augment_combination():
    combo = {"age": "<25", "race": "black", "gender": "female"}

    parameters = {"A": "a", "B": 9}

    augmented = calculator_utils.augment_combination(combo, parameters)

    assert augmented == {
        "age": "<25",
        "A": "a",
        "B": 9,
        "race": "black",
        "gender": "female",
    }
    assert augmented != combo
Exemple #4
0
def test_augment_combination():
    combo = {'age': '<25', 'race': 'black', 'gender': 'female'}

    parameters = {'A': 'a', 'B': 9}

    augmented = calculator_utils.augment_combination(combo, parameters)

    assert augmented == {
        'age': '<25',
        'A': 'a',
        'B': 9,
        'race': 'black',
        'gender': 'female'
    }
    assert augmented != combo
Exemple #5
0
def augmented_combo_list(combo: Dict[str, Any], event: ReleaseEvent,
                         methodology: MetricMethodologyType,
                         period: Optional[int]) -> List[Dict[str, Any]]:
    """Returns a list of combo dictionaries that have been augmented with necessary parameters.

    Each combo is augmented with the given methodology and follow-up period. Then, combos are added with relevant
    pairings of return_type and from_supervision_type as well.

    Args:
        combo: the base combo to be augmented with methodology and period
        event: the ReleaseEvent from which the combo was derived
        methodology: the MetricMethodologyType to add to each combo
        period: the follow_up_period value to add to each combo

    Returns: a list of combos augmented with various parameters
    """

    if combo.get('person_id') is not None:
        return [
            person_level_augmented_combo(combo, event, methodology, period)
        ]

    combos = []
    parameters: Dict[str, Any] = {
        'state_code': event.state_code,
        'methodology': methodology
    }

    if period:
        parameters['follow_up_period'] = period

    base_combo = augment_combination(combo, parameters)
    combos.append(base_combo)

    new_admission_parameters = parameters.copy()
    new_admission_parameters[
        'return_type'] = ReincarcerationReturnType.NEW_ADMISSION

    combos.append(augment_combination(base_combo, new_admission_parameters))

    revocation_parameters = parameters.copy()
    revocation_parameters['return_type'] = ReincarcerationReturnType.REVOCATION
    combo_revocation = augment_combination(base_combo, revocation_parameters)
    combos.append(combo_revocation)

    for violation_type in StateSupervisionViolationType:
        violation_parameters = revocation_parameters.copy()
        violation_parameters['source_violation_type'] = violation_type
        combos.append(
            augment_combination(combo_revocation, violation_parameters))

    parole_parameters = revocation_parameters.copy()
    parole_parameters[
        'from_supervision_type'] = ReincarcerationReturnFromSupervisionType.PAROLE
    parole_combo = augment_combination(combo_revocation, parole_parameters)
    combos.append(parole_combo)

    for violation_type in StateSupervisionViolationType:
        violation_parameters = parole_parameters.copy()
        violation_parameters['source_violation_type'] = violation_type
        combos.append(augment_combination(parole_combo, violation_parameters))

    probation_parameters = revocation_parameters.copy()
    probation_parameters['from_supervision_type'] = \
        ReincarcerationReturnFromSupervisionType.PROBATION
    probation_combo = augment_combination(combo_revocation,
                                          probation_parameters)
    combos.append(probation_combo)

    for violation_type in StateSupervisionViolationType:
        violation_parameters = probation_parameters.copy()
        violation_parameters['source_violation_type'] = violation_type
        combos.append(
            augment_combination(probation_combo, violation_parameters))

    return combos