Skip to content

classification_rule

Module for ClassificationRule class

!!! classes ClassificationRule

ClassificationRule (RuleBase, IMultiArrayBasedRule)

Implementation for the (multiple) classification rule

Source code in rules/classification_rule.py
class ClassificationRule(RuleBase, IMultiArrayBasedRule):
    """Implementation for the (multiple) classification rule"""

    def __init__(
        self,
        name: str,
        input_variable_names: List[str],
        criteria_table: Dict[str, List],
    ):
        super().__init__(name, input_variable_names)
        self._criteria_table = criteria_table

    @property
    def criteria_table(self) -> Dict:
        """Criteria property"""
        return self._criteria_table

    def execute(
        self, value_arrays: Dict[str, _xr.DataArray], logger: ILogger
    ) -> _xr.DataArray:
        """Determine the classification based on the table with criteria
        Args:
            values (Dict[str, float]): Dictionary holding the values
                                         for making the rule
        Returns:
            integer: classification
        """

        # Get all the headers in the criteria_table representing a value to be checked
        column_names = list(self._criteria_table.keys())
        column_names.remove("output")

        # Create an empty result_array to be filled
        result_array = _xr.zeros_like(value_arrays[column_names[0]])

        for row, out in reversed(list(enumerate(self._criteria_table["output"]))):
            criteria_comparison = _xr.full_like(value_arrays[column_names[0]], True)
            for column_name in column_names:
                # DataArray on which the criteria needs to be checked
                data = value_arrays[column_name]

                # Retrieving criteria and applying it in correct format (number,
                # range or comparison)
                criteria = self.criteria_table[column_name][row]
                comparison = self._get_comparison_for_criteria(criteria, data)
                if comparison is None:
                    comparison = True

                # Criteria_comparison == 1 -> to check where the value is True
                criteria_comparison = _xr.where(
                    comparison & (criteria_comparison == 1), True, False
                )
            # For the first row set the default to None, for all the other
            # rows use the already created dataarray
            default_val = None
            if row != len(self._criteria_table["output"]) - 1:
                default_val = result_array

            result_array = _xr.where(criteria_comparison, out, default_val)
        return result_array

    def _get_comparison_for_criteria(
        self, criteria: str, data: _xr.DataArray
    ) -> Optional[_xr.DataArray]:

        criteria_class = type_of_classification(criteria)

        comparison = None
        if criteria_class == "number":
            comparison = data == float(criteria)

        elif criteria_class == "range":
            begin, end = str_range_to_list(criteria)
            comparison = (data >= begin) & (data <= end)

        elif criteria_class == "larger_equal":
            comparison_val = read_str_comparison(criteria, ">=")
            comparison = data >= float(comparison_val)

        elif criteria_class == "smaller_equal":
            comparison_val = read_str_comparison(criteria, "<=")
            comparison = data <= float(comparison_val)

        elif criteria_class == "larger":
            comparison_val = read_str_comparison(criteria, ">")
            comparison = data > float(comparison_val)

        elif criteria_class == "smaller":
            comparison_val = read_str_comparison(criteria, "<")
            comparison = data < float(comparison_val)

        return comparison

criteria_table: Dict property readonly

Criteria property

execute(self, value_arrays, logger)

Determine the classification based on the table with criteria

Parameters:

Name Type Description Default
values Dict[str, float]

Dictionary holding the values for making the rule

required

Returns:

Type Description
integer

classification

Source code in rules/classification_rule.py
def execute(
    self, value_arrays: Dict[str, _xr.DataArray], logger: ILogger
) -> _xr.DataArray:
    """Determine the classification based on the table with criteria
    Args:
        values (Dict[str, float]): Dictionary holding the values
                                     for making the rule
    Returns:
        integer: classification
    """

    # Get all the headers in the criteria_table representing a value to be checked
    column_names = list(self._criteria_table.keys())
    column_names.remove("output")

    # Create an empty result_array to be filled
    result_array = _xr.zeros_like(value_arrays[column_names[0]])

    for row, out in reversed(list(enumerate(self._criteria_table["output"]))):
        criteria_comparison = _xr.full_like(value_arrays[column_names[0]], True)
        for column_name in column_names:
            # DataArray on which the criteria needs to be checked
            data = value_arrays[column_name]

            # Retrieving criteria and applying it in correct format (number,
            # range or comparison)
            criteria = self.criteria_table[column_name][row]
            comparison = self._get_comparison_for_criteria(criteria, data)
            if comparison is None:
                comparison = True

            # Criteria_comparison == 1 -> to check where the value is True
            criteria_comparison = _xr.where(
                comparison & (criteria_comparison == 1), True, False
            )
        # For the first row set the default to None, for all the other
        # rows use the already created dataarray
        default_val = None
        if row != len(self._criteria_table["output"]) - 1:
            default_val = result_array

        result_array = _xr.where(criteria_comparison, out, default_val)
    return result_array