Ejemplo n.º 1
0
    def _call_feature_methods(
            self,
            remove_nan: bool = True,
            verbose: bool = False,
            # enable_parallel: bool = False,
            suppress_warnings: bool = False,
            **kwargs) -> t.Tuple[t.List, ...]:
        """Invoke feature methods/functions loaded in the model and gather
        results.

        The returned values are already summarized if needed.

        For more information, check ``extract`` method documentation for
        in-depth information about arguments and return value.
        """
        metafeat_vals = []  # type: t.List[t.Union[int, float, t.Sequence]]
        metafeat_names = []  # type: t.List[str]
        metafeat_times = []  # type: t.List[float]

        for ft_mtd_name, ft_mtd_callable, ft_mtd_args in self._metadata_mtd_ft:

            if verbose:
                print("Extracting {} feature...".format(ft_mtd_name))

            ft_name_without_prefix = _internal.remove_prefix(
                value=ft_mtd_name, prefix=_internal.MTF_PREFIX)

            ft_mtd_args_pack = _internal.build_mtd_kwargs(
                mtd_name=ft_name_without_prefix,
                mtd_args=ft_mtd_args,
                user_custom_args=kwargs.get(ft_name_without_prefix),
                inner_custom_args=self._custom_args_ft,
                precomp_args=self._precomp_args_ft,
                suppress_warnings=suppress_warnings)

            features, time_ft = _internal.timeit(
                _internal.get_feat_value, ft_mtd_name, ft_mtd_args_pack,
                ft_mtd_callable, suppress_warnings)

            ft_has_length = isinstance(features,
                                       (np.ndarray, collections.Sequence))

            if ft_has_length and self._timeopt_type_is_avg():
                time_ft /= len(features)

            if self._metadata_mtd_sm and ft_has_length:
                sm_ret = self._call_summary_methods(
                    feature_values=features,
                    feature_name=ft_name_without_prefix,
                    remove_nan=remove_nan,
                    verbose=verbose,
                    suppress_warnings=suppress_warnings,
                    **kwargs)

                summarized_names, summarized_vals, times_sm = sm_ret

                metafeat_vals += summarized_vals
                metafeat_names += summarized_names
                metafeat_times += self._combine_time(time_ft, times_sm)

            else:
                metafeat_vals.append(features)
                metafeat_names.append(ft_name_without_prefix)
                metafeat_times.append(time_ft)

            if verbose:
                print("Done with {} feature.".format(ft_mtd_name))

        return metafeat_names, metafeat_vals, metafeat_times
Ejemplo n.º 2
0
    def _call_summary_methods(
            self,
            feature_values: t.Sequence[_internal.TypeNumeric],
            feature_name: str,
            remove_nan: bool = True,
            verbose: bool = False,
            suppress_warnings: bool = False,
            **kwargs
    ) -> t.Tuple[t.List[str], t.List[t.Union[float, t.Sequence]], t.
                 List[float]]:
        """Invoke summary functions loaded in the model on given feature
        values.

        Parameters
        ----------
        feature_values : :obj:`sequence` of numerics
            Sequence containing values from feature-extraction methods.

        feature_name : :obj:`str`
            Name of the feature method used for produce the ``feature_value.``

        remove_nan : :obj:`bool`, optional
            If True, all non-numeric values are removed from ``feature_values``
            before calling each summary method. Note that the summary method
            itself may still remove non-numeric values and, in this case, the
            user must suppress these warnings using some built-in argument of
            the summary method using the kwargs argument, if possible.

        verbose : :obj:`bool`, optional
            If True, then messages about the summarization process may be
            printed. Note that there is no relation between this argument and
            warnings (see ``suppress_warnings`` argument below).

        suppress_warnings : :obj:`bool`, optional
            If True, ignore all warnings invoked before and after summary
            method calls. Note that, as seen in the ``remove_nan`` argument,
            the summary callables may still invoke warnings by itself and the
            user need to ignore them, if possible, via kwargs.

        kwargs:
            User-defined arguments for the summary callables.

        Returns
        -------
        :obj:`tuple`(:obj:`list`, :obj:`list`, :obj:`list`)
            A tuple containing three lists.

            The first field is the identifiers of each summarized value in the
            form ``feature_name.summary_mtd_name`` (i.e., the feature
            extraction name concatenated by the summary method name, separated
            by a dot). If the summary function return more than one value
            (cardinality greater than 1), then each value name have an extra
            concatenated id starting from 0 to differ between values (i.e.
            ``feature_name.summary_mtd_name.id``).

            The second field is the summarized values. Both lists have a 1-1
            correspondence by the index of each element (i.e., the value at
            index ``i`` in the second list has its identifier at the same index
            in the first list and vice-versa).

            The third field is a list with measured time wasted by each summary
            function. If the cardinality of the summary function is greater
            than 1, then the correspondent measured time is kept only in the
            first correspondent field, and the extra fields are filled with 0
            to keep the consistency of the size between all lists.

                Example:
                    ([``attr_ent.mean``, ``attr_ent.sd``], [0.98346, 0.34436])
                    is the return value for the feature `attr_end` summarized
                    by both ``mean`` and ``sd`` (standard deviation), giving
                    the values ``0.98347`` and ``0.34436``, respectively.
        """
        metafeat_vals = []  # type: t.List[t.Union[int, float, t.Sequence]]
        metafeat_names = []  # type: t.List[str]
        metafeat_times = []  # type: t.List[float]

        for sm_mtd_name, sm_mtd_callable, sm_mtd_args in self._metadata_mtd_sm:

            if verbose:
                print(
                    "  Summarizing {0} feature with {1} summary"
                    " function...".format(feature_name, sm_mtd_name),
                    end=" ")

            sm_mtd_args_pack = _internal.build_mtd_kwargs(
                mtd_name=sm_mtd_name,
                mtd_args=sm_mtd_args,
                user_custom_args=kwargs.get(sm_mtd_name),
                inner_custom_args=self._custom_args_sum,
                suppress_warnings=suppress_warnings)

            summarized_val, time_sm = _internal.timeit(
                _internal.summarize, feature_values, sm_mtd_callable,
                sm_mtd_args_pack, remove_nan)

            if not suppress_warnings:
                _internal.check_summary_warnings(
                    value=summarized_val,
                    name_feature=feature_name,
                    name_summary=sm_mtd_name)

            if isinstance(summarized_val, np.ndarray):
                summarized_val = summarized_val.flatten().tolist()

            if (isinstance(summarized_val, collections.Sequence)
                    and not isinstance(summarized_val, str)):
                metafeat_vals += summarized_val
                metafeat_names += [
                    ".".join((feature_name, sm_mtd_name, str(i)))
                    for i in range(len(summarized_val))
                ]
                metafeat_times += ([time_sm] + (
                    (len(summarized_val) - 1) * [0.0]))

            else:
                metafeat_vals.append(summarized_val)
                metafeat_names.append(".".join((feature_name, sm_mtd_name)))
                metafeat_times.append(time_sm)

            if verbose:
                print("Done.")

        return metafeat_names, metafeat_vals, metafeat_times