def process(self, element, *args, **kwargs):
        """Produces various recidivism metric combinations.

        Sends the calculator the StatePerson entity and their corresponding
        ReleaseEvents for mapping all recidivism combinations.

        Args:
            element: Tuple containing a StatePerson and their ReleaseEvents
            **kwargs: This should be a dictionary with values for the
                following keys:
                    - age_bucket
                    - gender
                    - stay_length_bucket
                    - release_facility
                    - race
                    - ethnicity
        Yields:
            Each recidivism metric combination, tagged by metric type.
        """
        person, release_events = element

        # Calculate recidivism metric combinations for this person and events
        metric_combinations = \
            calculator.map_recidivism_combinations(person,
                                                   release_events, kwargs)

        # Return each of the recidivism metric combinations
        for metric_combination in metric_combinations:
            metric_key, value = metric_combination
            metric_type = metric_key.get('metric_type')

            # Converting the metric key to a JSON string so it is hashable
            serializable_dict = json_serializable_metric_key(metric_key)
            json_key = json.dumps(serializable_dict, sort_keys=True)

            if metric_type == MetricType.RATE:
                yield beam.pvalue.TaggedOutput('rates',
                                               (json_key, value))
            elif metric_type == MetricType.COUNT:
                yield beam.pvalue.TaggedOutput('counts',
                                               (json_key, value))
            elif metric_type == MetricType.LIBERTY:
                yield beam.pvalue.TaggedOutput('liberties',
                                               (json_key, value))
Beispiel #2
0
    def process(self, element, metric_inclusions):
        """Produces various recidivism metric combinations.

        Sends the calculator the StatePerson entity and their corresponding ReleaseEvents for mapping all recidivism
        combinations.

        Args:
            element: Tuple containing a StatePerson and their ReleaseEvents
            metric_inclusions: A dictionary where the keys are each ReincarcerationRecidivismMetricType, and the values
                are boolean flags for whether or not to include that metric type in the calculations
        Yields:
            Each recidivism metric combination.
        """
        person, release_events = element

        # Calculate recidivism metric combinations for this person and events
        metric_combinations = calculator.map_recidivism_combinations(person, release_events, metric_inclusions)

        # Return each of the recidivism metric combinations
        for metric_combination in metric_combinations:
            yield metric_combination