Esempio n. 1
0
    def _validate_metrics_map(self):
        """Ensure `metrics_map` input parameter is properly formatted and yields callable functions for all metrics"""
        if not (isinstance(self.metrics_map, dict)
                or isinstance(self.metrics_map, list)):
            raise TypeError(
                'metrics_map must be one of: [dict, list]. Received type: {}.'.
                format(type(self.metrics_map)))

        #################### If metrics_map is list, convert to dict with None values ####################
        if isinstance(self.metrics_map, list):
            self.metrics_map = {_: None for _ in self.metrics_map}

        for _m_key, _m_val in self.metrics_map.items():
            if not isinstance(_m_key, str):
                raise TypeError(
                    'metrics_map ids must be strings. Received type {}: {}'.
                    format(type(_m_key), _m_key))
            if not any(
                [callable(_m_val),
                 isinstance(_m_val, str), _m_val is None]):
                raise TypeError(
                    'metrics_map values must be one of: [callable, str, None]. Received {}'
                    .format(type(_m_val)))

            #################### Check sklearn.metrics for: _m_val if str, or _m_key if _m_val is None ####################
            if not callable(_m_val):
                try:
                    self.metrics_map[_m_key] = sk_metrics.__getattribute__(
                        _m_key if _m_val is None else _m_val)
                except AttributeError:
                    raise AttributeError(
                        '"sklearn.metrics" has no attribute "{}".'.format(
                            _m_key if _m_val is None else _m_val))
Esempio n. 2
0
    def _set_metric_function(self, f):
        """Ensure provided `f` is a valid callable

        Parameters
        ----------
        f: Callable, string, None
            See `metric_function` documentation of :meth:`Metric.__init__`

        Returns
        -------
        Callable
            A function derived from `f` if `f` was not already callable. Else `f`"""
        if not callable(f):
            try:
                return sk_metrics.__getattribute__(self.name if f is None else f)
            except AttributeError:
                raise AttributeError(f"`sklearn.metrics` has no attribute: {f or self.name}")
        return f
    def _validate_metrics_map(self):
        """Ensure `metrics_map` input parameter is properly formatted and yields callable functions
        for all metrics"""
        if not (isinstance(self.metrics_map, dict) or isinstance(self.metrics_map, list)):
            raise TypeError(f"metrics_map must be a dict, or list, not {type(self.metrics_map)}")

        # If metrics_map is list, convert to dict with None values
        if isinstance(self.metrics_map, list):
            self.metrics_map = {_: None for _ in self.metrics_map}

        for _m_key, _m_val in self.metrics_map.items():
            if not isinstance(_m_key, str):
                raise TypeError(f"`metrics_map` ids must be strings. Received {_m_key}")
            if not any([callable(_m_val), isinstance(_m_val, str), _m_val is None]):
                raise TypeError(f"metrics_map values must be callable, str, or None, not {_m_val}")

            # Check sklearn.metrics for: _m_val if str, or _m_key if _m_val is None
            if not callable(_m_val):
                try:
                    self.metrics_map[_m_key] = sk_metrics.__getattribute__(
                        _m_key if _m_val is None else _m_val
                    )
                except AttributeError:
                    raise AttributeError(f"`sklearn.metrics` has no attribute: {_m_val or _m_key}")
Esempio n. 4
0
def get_metric(metric_name):
    metric = metrics.__getattribute__(metric_name)
    return metric
Esempio n. 5
0
def get_metric(config: DictConfig):
    metric_config = config["metric"]
    metric_name = metric_config["name"]
    metric = skm.__getattribute__(metric_name)
    return metric