def map_metric_combinations( characteristic_combo: Dict[str, Any], incarceration_event: IncarcerationEvent, calculation_month_upper_bound: date, calculation_month_lower_bound: Optional[date], all_incarceration_events: List[IncarcerationEvent], periods_and_events: Dict[int, List[IncarcerationEvent]], metric_type: IncarcerationMetricType, include_metric_period_output: bool) -> \ List[Tuple[Dict[str, Any], Any]]: """Maps the given time bucket and characteristic combinations to a variety of metrics that track incarceration admission and release counts. All values will be 1 for these count metrics, because the presence of an IncarcerationEvent for a given month implies that the person was counted towards the admission or release for that month. Args: characteristic_combo: A dictionary containing the characteristics of the person and event incarceration_event: The incarceration event from which the combination was derived. calculation_month_upper_bound: The year and month of the last month for which metrics should be calculated. calculation_month_lower_bound: The date of the first month to be included in the monthly calculations all_incarceration_events: All of the person's IncarcerationEvents periods_and_events: A dictionary mapping metric period month values to the corresponding relevant IncarcerationEvents metric_type: The metric type to set on each combination include_metric_period_output: Whether or not to include metrics for the various metric periods before the current month. If False, will still include metric_period_months = 1 for the current month. Returns: A list of key-value tuples representing specific metric combinations and the metric value corresponding to that metric. """ metrics = [] characteristic_combo['metric_type'] = metric_type if include_in_historical_metrics(incarceration_event.event_date.year, incarceration_event.event_date.month, calculation_month_upper_bound, calculation_month_lower_bound): # IncarcerationPopulationMetrics are point-in-time counts for the date of the event, all other # IncarcerationMetrics are counts based on the month of the event is_daily_metric = metric_type == IncarcerationMetricType.INCARCERATION_POPULATION # All other IncarcerationMetrics are counts based on the month of the event metrics.extend( combination_incarceration_metrics(characteristic_combo, incarceration_event, all_incarceration_events, is_daily_metric)) if include_metric_period_output and metric_type != IncarcerationMetricType.INCARCERATION_POPULATION: metrics.extend( combination_incarceration_metric_period_metrics( characteristic_combo, incarceration_event, calculation_month_upper_bound, periods_and_events)) return metrics
def test_include_in_monthly_metrics_after_end_date(self): calculation_month_upper_bound = date(2000, 1, 31) include = calculator_utils.include_in_historical_metrics( year=2000, month=2, calculation_month_upper_bound=calculation_month_upper_bound, calculation_month_lower_bound=None) self.assertFalse(include)
def test_include_in_monthly_metrics(self): calculation_month_upper_bound = date(2000, 1, 31) include = calculator_utils.include_in_historical_metrics( year=1999, month=11, calculation_month_upper_bound=calculation_month_upper_bound, calculation_month_lower_bound=None) self.assertTrue(include)
def map_metric_combinations( characteristic_combo: Dict[str, Any], supervision_time_bucket: SupervisionTimeBucket, calculation_month_upper_bound: date, calculation_month_lower_bound: Optional[date], all_supervision_time_buckets: List[SupervisionTimeBucket], periods_and_buckets: Dict[int, List[SupervisionTimeBucket]], metric_type: SupervisionMetricType, include_metric_period_output: bool) -> \ List[Tuple[Dict[str, Any], Any]]: """Maps the given time bucket and characteristic combinations to a variety of metrics that track supervision population and revocation counts. Args: characteristic_combo: A dictionary describing the person and supervision_time_bucket. supervision_time_bucket: The time bucket on supervision from which the combination was derived. calculation_month_upper_bound: The year and month of the last month for which metrics should be calculated. calculation_month_lower_bound: The date of the first month to be included in the monthly calculations all_supervision_time_buckets: All of the person's SupervisionTimeBuckets periods_and_buckets: Dictionary mapping metric period month lengths to the SupervisionTimeBuckets that fall in that period. metric_type: The metric type to set on each combination. include_metric_period_output: Whether or not to include metrics for the various metric periods before the current month. If False, will still include metric_period_months = 0 or 1 for the current month. Returns: A list of key-value tuples representing specific metric combinations and the metric value corresponding to that metric. """ metrics = [] characteristic_combo['metric_type'] = metric_type if include_in_historical_metrics( supervision_time_bucket.year, supervision_time_bucket.month, calculation_month_upper_bound, calculation_month_lower_bound): # SupervisionPopulationMetrics and SupervisionCaseComplianceMetrics are point-in-time metrics, all # other SupervisionMetrics are metrics based on the month of the event is_daily_metric = metric_type in (SupervisionMetricType.SUPERVISION_POPULATION, SupervisionMetricType.SUPERVISION_COMPLIANCE) metrics.extend(combination_supervision_monthly_metrics( characteristic_combo, supervision_time_bucket, all_supervision_time_buckets, metric_type, is_daily_metric)) if include_metric_period_output: metrics.extend(combination_supervision_metric_period_metrics( characteristic_combo, supervision_time_bucket, calculation_month_upper_bound, periods_and_buckets, metric_type )) return metrics
def map_metric_combinations( characteristic_combo: Dict[str, Any], program_event: ProgramEvent, calculation_month_upper_bound: date, calculation_month_lower_bound: Optional[date], all_program_events: List[ProgramEvent], periods_and_events: Dict[int, List[ProgramEvent]], metric_type: ProgramMetricType, include_metric_period_output: bool) -> \ List[Tuple[Dict[str, Any], Any]]: """Maps the given program event and characteristic combinations to a variety of metrics that track program interactions. All values will be 1 for these count metrics, because the presence of a ProgramEvent for a given event implies that the person interacted with the program in the way being described. Args: characteristic_combo: A dictionary describing the person and event. program_event: The program event from which the combination was derived. calculation_month_upper_bound: The year and month of the last month for which metrics should be calculated. calculation_month_lower_bound: The date of the first month to be included in the monthly calculations all_program_events: All of the person's ProgramEvents periods_and_events: A dictionary mapping metric period month values to the corresponding relevant ProgramEvents metric_type: The metric type to set on each combination include_metric_period_output: Whether or not to include metrics for the various metric periods before the current month. If False, will still include metric_period_months = 1 for the current month. Returns: A list of key-value tuples representing specific metric combinations and the metric value corresponding to that metric. """ metrics = [] all_referral_events = [ event for event in all_program_events if isinstance(event, ProgramReferralEvent) ] if metric_type == ProgramMetricType.REFERRAL and isinstance(program_event, ProgramReferralEvent): characteristic_combo['metric_type'] = metric_type if include_in_historical_metrics( program_event.event_date.year, program_event.event_date.month, calculation_month_upper_bound, calculation_month_lower_bound): metrics.extend( combination_referral_monthly_metrics(characteristic_combo, program_event, all_referral_events)) if include_metric_period_output: metrics.extend(combination_referral_metric_period_metrics(characteristic_combo, program_event, calculation_month_upper_bound, periods_and_events)) return metrics
def test_include_in_monthly_metrics_one_month_run_exclude(self): calculation_month_upper_bound = date(1999, 12, 31) calculation_month_lower_bound = date(1999, 12, 1) include = calculator_utils.include_in_historical_metrics( year=2000, month=1, calculation_month_upper_bound=calculation_month_upper_bound, calculation_month_lower_bound=calculation_month_lower_bound) self.assertFalse(include)
def test_include_in_monthly_metrics_before_lower_bound(self): calculation_month_upper_bound = date(2000, 1, 31) calculation_month_lower_bound = date(1999, 10, 1) include = calculator_utils.include_in_historical_metrics( year=1990, month=4, calculation_month_upper_bound=calculation_month_upper_bound, calculation_month_lower_bound=calculation_month_lower_bound) self.assertFalse(include)
def test_include_in_monthly_metrics_month_of_end_date(self): calculation_month_upper_bound = date(2000, 1, 31) calculation_month_lower_bound = date(1999, 10, 1) include = calculator_utils.include_in_historical_metrics( year=calculation_month_upper_bound.year, month=calculation_month_upper_bound.month, calculation_month_upper_bound=calculation_month_upper_bound, calculation_month_lower_bound=calculation_month_lower_bound) self.assertTrue(include)