def _all_estimators(self):
     """Retrieve list of all estimator classes of type self.estimator_type_filter."""
     return all_estimators(
         estimator_types=getattr(self, "estimator_type_filter", None),
         return_names=False,
         exclude_estimators=EXCLUDE_ESTIMATORS,
     )
Exemple #2
0
def test_all_estimators_return_tags(return_tags, return_names):
    """Test ability to return estimator value of passed tags."""
    estimators = all_estimators(
        return_tags=return_tags,
        return_names=return_names,
    )
    # Helps us keep track of estimator index within the tuple:
    ESTIMATOR_INDEX = 1 if return_names else 0
    TAG_START_INDEX = ESTIMATOR_INDEX + 1

    assert isinstance(estimators[0], tuple)
    # check length of tuple is what we expect:
    if isinstance(return_tags, str):
        assert len(estimators[0]) == TAG_START_INDEX + 1
    else:
        assert len(estimators[0]) == len(return_tags) + TAG_START_INDEX

    # check that for each estimator the value for that tag is correct:
    for est_tuple in estimators:
        est = est_tuple[ESTIMATOR_INDEX]
        if isinstance(return_tags, str):
            assert est.get_class_tag(return_tags) == est_tuple[TAG_START_INDEX]
        else:
            for tag_index, tag in enumerate(return_tags):
                assert est.get_class_tag(tag) == est_tuple[TAG_START_INDEX +
                                                           tag_index]
Exemple #3
0
def test_all_estimators_exclude_estimators(exclude_estimators):
    estimators = all_estimators(return_names=True,
                                exclude_estimators=exclude_estimators)
    assert isinstance(estimators, list)
    assert len(estimators) > 0
    names, estimators = list(zip(*estimators))

    if not isinstance(exclude_estimators, list):
        exclude_estimators = [exclude_estimators]
    for estimator in exclude_estimators:
        assert estimator not in names
Exemple #4
0
def test_all_estimators_filter_estimator_types(estimator_type):
    estimators = all_estimators(estimator_types=estimator_type,
                                return_names=False)
    assert isinstance(estimators, list)
    # assert len(estimators) > 0

    if isinstance(estimator_type, str):
        estimator_type = VALID_ESTIMATOR_BASE_TYPE_LOOKUP[estimator_type]

    assert all(
        [issubclass(estimator, estimator_type) for estimator in estimators])
Exemple #5
0
def test_all_estimators_return_names(return_names):
    estimators = all_estimators(return_names=return_names)
    assert isinstance(estimators, list)
    assert len(estimators) > 0

    if return_names:
        assert all([isinstance(estimator, tuple) for estimator in estimators])
        names, estimators = list(zip(*estimators))
        assert all([isinstance(name, str) for name in names])
        assert all([
            name == estimator.__name__
            for name, estimator in zip(names, estimators)
        ])

    assert all([isinstance(estimator, type) for estimator in estimators])
Exemple #6
0
def test_all_estimators_by_scitype(estimator_scitype, return_names):
    """Check that all_estimators return argument has correct type."""
    estimators = all_estimators(
        estimator_types=estimator_scitype,
        return_names=return_names,
    )

    estimator_classes = _get_type_tuple(estimator_scitype)

    assert isinstance(estimators, list)
    # there should be at least one estimator returned
    assert len(estimators) > 0

    # checks return type specification (see docstring)
    if return_names:
        for estimator in estimators:
            assert isinstance(estimator, tuple) and len(estimator) == 2
            assert isinstance(estimator[0], str)
            assert issubclass(estimator[1], estimator_classes)
            assert estimator[0] == estimator[1].__name__
    else:
        for estimator in estimators:
            assert issubclass(estimator, estimator_classes)
# -*- coding: utf-8 -*-
"""Tests for pairwise transformer."""

import numpy as np
import pytest

from sktime.registry import all_estimators
from sktime.utils._testing.panel import make_transformer_problem

PAIRWISE_TRANSFORMERS = all_estimators(estimator_types="transformer-pairwise",
                                       return_names=False)
PAIRWISE_TRANSFORMERS_PANEL = all_estimators(
    estimator_types="transformer-pairwise-panel", return_names=False)

EXPECTED_SHAPE = (4, 5)

X1_tab = make_transformer_problem(
    n_instances=4,
    n_columns=4,
    n_timepoints=5,
    random_state=1,
    return_numpy=True,
    panel=False,
)
X2_tab = make_transformer_problem(
    n_instances=5,
    n_columns=5,
    n_timepoints=5,
    random_state=2,
    return_numpy=True,
    panel=False,
from sktime.registry import all_estimators

# List of columns in the table
df_columns = [
    "Classifier Category",
    "Classifier Name",
    "multivariate",
    "unequal_length",
    "missing_values",
    "train_estimate",
    "contractable",
]
# creates dataframe as df
df = pd.DataFrame([], columns=df_columns)
# Loop through all the classifiers
for classiName, classiClass in all_estimators(estimator_types="classifier"):
    category = str(classiClass).split(".")[2]
    try:
        # capabilites of each of the classifier classifier
        cap_dict = classiClass.capabilities
        multivariate = str(cap_dict["multivariate"])
        unequal_length = str(cap_dict["unequal_length"])
        missing_values = str(cap_dict["missing_values"])
        train_estimate = str(cap_dict["train_estimate"])
        contractable = str(cap_dict["contractable"])
        # Adding capabilites for each classifier in the table
        df = df.append(
            {
                "Classifier Category": category,
                "Classifier Name": classiName,
                "multivariate": multivariate,
Exemple #9
0
# -*- coding: utf-8 -*-
# copyright: sktime developers, BSD-3-Clause License (see LICENSE file)
"""Tests for BaseAligner API points."""

__author__ = ["fkiraly"]

import pandas as pd
import pytest

from sktime.datatypes._check import check_raise
from sktime.registry import all_estimators
from sktime.utils._testing.series import _make_series

# get all aligners
ALIGNERS = all_estimators(estimator_types="aligner", return_names=False)
INVALID_X_INPUT_TYPES = [list(), tuple()]
INVALID_y_INPUT_TYPES = [list(), tuple()]


@pytest.mark.parametrize("Aligner", ALIGNERS)
def test_get_alignment(Aligner):
    """Test that get_alignment returns an alignment (iloc)."""
    f = Aligner.create_test_instance()

    X = [_make_series(n_columns=2), _make_series(n_columns=2)]
    align = f.fit(X).get_alignment()

    check_raise(align, mtype="alignment", scitype="Alignment")


@pytest.mark.parametrize("Aligner", ALIGNERS)
Exemple #10
0
#!/usr/bin/env python3 -u
# -*- coding: utf-8 -*-
# copyright: sktime developers, BSD-3-Clause License (see LICENSE file)

__author__ = ["Markus Löning"]
__all__ = ["test_estimator"]

import pytest

from sktime.tests._config import EXCLUDE_ESTIMATORS
from sktime.tests._config import EXCLUDED_TESTS
from sktime.registry import all_estimators
from sktime.utils._testing.estimator_checks import check_estimator

ALL_ESTIMATORS = all_estimators(return_names=False,
                                exclude_estimators=EXCLUDE_ESTIMATORS)


@pytest.mark.parametrize("Estimator", ALL_ESTIMATORS)
def test_estimator(Estimator):
    # We run a number of basic checks on all estimators to ensure correct
    # implementation of our framework and compatibility with scikit-learn.
    check_estimator(Estimator, EXCLUDED_TESTS.get(Estimator.__name__, []))
from sktime.tests._config import VALID_TRANSFORMER_TYPES
from sktime.transformations.base import (
    BaseTransformer,
    _PanelToPanelTransformer,
    _PanelToTabularTransformer,
    _SeriesToPrimitivesTransformer,
    _SeriesToSeriesTransformer,
)
from sktime.utils._testing.estimator_checks import (
    _assert_array_almost_equal,
    _construct_instance,
    _has_capability,
    _make_args,
)

ALL_TRANSFORMERS = all_estimators(estimator_types="transformer",
                                  return_names=False)


@pytest.mark.parametrize("Estimator", ALL_TRANSFORMERS)
def test_all_transformers(Estimator):
    check_transformer(Estimator)


def check_transformer(Estimator):
    for check in _yield_transformer_checks(Estimator):
        check(Estimator)


def _construct_fit_transform(Estimator, **kwargs):
    estimator = _construct_instance(Estimator)
Exemple #12
0
# -*- coding: utf-8 -*-
import numpy as np
from sktime.dists_kernels.compose_tab_to_panel import AggrDist
from sktime.dists_kernels.scipy_dist import ScipyDist
from sktime.utils._testing.panel import make_transformer_problem
from sktime.registry import all_estimators

PAIRWISE_TRANSFORMERS_TAB = all_estimators(
    estimator_types="transformer-pairwise", return_names=False
)

AGGFUNCS = [
    np.mean,
    np.std,
    np.var,
    np.sum,
    np.prod,
    np.min,
    np.max,
    np.argmin,
    np.argmax,
    np.any,
]


X1_num_pan = make_transformer_problem(
    n_instances=4, n_columns=4, n_timepoints=5, random_state=1, return_numpy=True
)
X2_num_pan = make_transformer_problem(
    n_instances=5, n_columns=5, n_timepoints=5, random_state=2, return_numpy=True
)
Exemple #13
0
]

import numpy as np
import pytest

from sktime.registry import all_estimators
from sktime.series_as_features.tests._config import ACCEPTED_OUTPUT_TYPES
from sktime.tests._config import EXCLUDE_ESTIMATORS, NON_STATE_CHANGING_METHODS
from sktime.transformations.base import (
    _PanelToPanelTransformer,
    _PanelToTabularTransformer,
)
from sktime.utils._testing.estimator_checks import _construct_instance, _make_args

CLASSIFIERS = all_estimators("classifier",
                             return_names=False,
                             exclude_estimators=EXCLUDE_ESTIMATORS)
REGRESSORS = all_estimators("regressor",
                            return_names=False,
                            exclude_estimators=EXCLUDE_ESTIMATORS)

PANEL_TRANSFORMERS = all_estimators(
    estimator_types=[_PanelToPanelTransformer, _PanelToTabularTransformer],
    return_names=False,
    exclude_estimators=EXCLUDE_ESTIMATORS,
)

PANEL_ESTIMATORS = CLASSIFIERS + REGRESSORS + PANEL_TRANSFORMERS

# We here only check the ouput for a single number of classes
N_CLASSES = 3
# -*- coding: utf-8 -*-
import pandas as pd

import pytest
from sktime.utils._testing.estimator_checks import _construct_instance, _make_args
from sktime.registry import all_estimators

ALL_ANNOTATORS = all_estimators(estimator_types="series-annotator", return_names=False)


@pytest.mark.parametrize("Estimator", ALL_ANNOTATORS)
def test_output_type(Estimator):
    estimator = _construct_instance(Estimator)

    args = _make_args(estimator, "fit")
    estimator.fit(*args)
    args = _make_args(estimator, "predict")
    y_pred = estimator.predict(*args)
    assert isinstance(y_pred, pd.Series)
__all__ = [
    "test_regressor_output",
    "test_multivariate_input",
    "test_3d_numpy_input",
]

import numpy as np
import pandas as pd
import pytest

from sktime.registry import all_estimators
from sktime.tests._config import EXCLUDE_ESTIMATORS, NON_STATE_CHANGING_METHODS
from sktime.utils._testing.estimator_checks import _has_capability, _make_args

REGRESSORS = all_estimators("regressor",
                            return_names=False,
                            exclude_estimators=EXCLUDE_ESTIMATORS)

ACCEPTED_OUTPUT_TYPES = (np.ndarray, pd.Series)

# We here only check the ouput for a single number of classes
N_CLASSES = 3


@pytest.mark.parametrize("Estimator", REGRESSORS)
def test_3d_numpy_input(Estimator):
    """Test classifiers handle 3D numpy input correctly."""
    estimator = Estimator.create_test_instance()
    fit_args = _make_args(estimator, "fit", return_numpy=True)
    estimator.fit(*fit_args)
Exemple #16
0
    "test_3d_numpy_input",
]

import pytest

from sktime.registry import all_estimators
from sktime.tests._config import EXCLUDE_ESTIMATORS, NON_STATE_CHANGING_METHODS
from sktime.transformations.base import (
    _PanelToPanelTransformer,
    _PanelToTabularTransformer,
)
from sktime.utils._testing.estimator_checks import _has_capability, _make_args

PANEL_TRANSFORMERS = all_estimators(
    estimator_types=[_PanelToPanelTransformer, _PanelToTabularTransformer],
    return_names=False,
    exclude_estimators=EXCLUDE_ESTIMATORS,
)

PANEL_ESTIMATORS = PANEL_TRANSFORMERS

# We here only check the ouput for a single number of classes
N_CLASSES = 3


@pytest.mark.parametrize("Estimator", PANEL_TRANSFORMERS)
def test_3d_numpy_input(Estimator):
    """Test classifiers handle 3D numpy input correctly."""
    estimator = Estimator.create_test_instance()
    fit_args = _make_args(estimator, "fit", return_numpy=True)
    estimator.fit(*fit_args)
Exemple #17
0
)
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
from sktime.registry import all_estimators
from sktime.utils._testing.estimator_checks import _construct_instance
from sktime.utils._testing.forecasting import (
    _assert_correct_pred_time_index,
    _get_expected_index_for_update_predict,
    _get_n_columns,
    _make_fh,
    make_forecasting_problem,
)
from sktime.utils._testing.series import _make_series
from sktime.utils.validation.forecasting import check_fh

# get all forecasters
FORECASTERS = all_estimators(estimator_types="forecaster", return_names=False)
FH0 = 1
INVALID_X_INPUT_TYPES = [list(), tuple()]
INVALID_y_INPUT_TYPES = [list(), tuple()]

# testing data
y = make_forecasting_problem()
y_train, y_test = temporal_train_test_split(y, train_size=0.75)


@pytest.mark.parametrize("Forecaster", FORECASTERS)
def test_get_fitted_params(Forecaster):
    """Test get_fitted_params."""
    f = _construct_instance(Forecaster)
    columns = _get_n_columns(f.get_tag("scitype:y"))
    for n_columns in columns:
Exemple #18
0
__author__ = ["mloning"]

import numpy as np
import pytest

from sktime.forecasting.base._sktime import _BaseWindowForecaster
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.registry import all_estimators
from sktime.utils._testing.forecasting import _get_n_columns, make_forecasting_problem
from sktime.utils._testing.series import _make_series

FH0 = 1

WINDOW_FORECASTERS = [
    forecaster
    for (name, forecaster) in all_estimators(estimator_types="forecaster")
    if issubclass(forecaster, _BaseWindowForecaster)
]

# testing data
y = make_forecasting_problem()
y_train, y_test = temporal_train_test_split(y, train_size=0.75)


@pytest.mark.parametrize("Forecaster", WINDOW_FORECASTERS)
def test_last_window(Forecaster):
    """Test window forecaster common API points."""
    f = Forecaster.create_test_instance()
    n_columns_list = _get_n_columns(f.get_tag("scitype:y"))

    for n_columns in n_columns_list:
Exemple #19
0
def test_all_estimators_return_tags_bad_arg(return_tags):
    """Test ability to catch bad arguments of return_tags."""
    with pytest.raises(TypeError):
        _ = all_estimators(return_tags=return_tags)
Exemple #20
0
def _make_estimator_overview(app):
    """Make estimator overview table."""
    import pandas as pd

    from sktime.registry import all_estimators

    def _process_author_info(author_info):
        """
        Process author information from source code files.

        Parameters
        ----------
        author_info : str
            Author information string from source code files.

        Returns
        -------
        author_info : str
            Preprocessed author information.

        Notes
        -----
        A list of author names is turned into a string.
        Multiple author names will be separated by a comma,
        with the final name always preceded by "&".
        """
        if isinstance(author_info, list):
            if len(author_info) > 1:
                return ", ".join(author_info[:-1]) + " & " + author_info[-1]
            else:
                return author_info[0]
        else:
            return author_info

    def _does_not_start_with_underscore(input_string):
        return not input_string.startswith("_")

    # creates dataframe as df
    COLNAMES = ["Class Name", "Estimator Type", "Authors"]

    df = pd.DataFrame([], columns=COLNAMES)

    for modname, modclass in all_estimators():
        algorithm_type = "::".join(str(modclass).split(".")[1:-2])
        try:
            author_info = _process_author_info(modclass.__author__)
        except AttributeError:
            try:
                author_info = _process_author_info(
                    import_module(modclass.__module__).__author__)
            except AttributeError:
                author_info = "no author info"

        # includes part of class string
        modpath = str(modclass)[8:-2]
        path_parts = modpath.split(".")
        # joins strings excluding starting with '_'
        clean_path = ".".join(
            list(filter(_does_not_start_with_underscore, path_parts)))
        # adds html link reference
        modname = str(
            '<a href="https://www.sktime.org/en/latest/api_reference' +
            "/auto_generated/" + clean_path + '.html">' + modname + "</a>")

        df = df.append(
            pd.Series([modname, algorithm_type, author_info], index=COLNAMES),
            ignore_index=True,
        )
    with open("estimator_overview_table.md", "w") as file:
        df.to_markdown(file, index=False)
Exemple #21
0
__author__ = ["mloning", "TonyBagnall"]
__all__ = [
    "test_classifier_output",
    "test_multivariate_input",
    "test_3d_numpy_input",
]

import numpy as np
import pytest

from sktime.registry import all_estimators
from sktime.tests._config import EXCLUDE_ESTIMATORS, NON_STATE_CHANGING_METHODS
from sktime.utils._testing.estimator_checks import _has_capability, _make_args

CLASSIFIERS = all_estimators("classifier",
                             return_names=False,
                             exclude_estimators=EXCLUDE_ESTIMATORS)
N_CLASSES = 3


@pytest.mark.parametrize("Estimator", CLASSIFIERS)
def test_3d_numpy_input(Estimator):
    """Test classifiers handle 3D numpy input correctly."""
    estimator = Estimator.create_test_instance()
    fit_args = _make_args(estimator, "fit", return_numpy=True)
    estimator.fit(*fit_args)

    for method in NON_STATE_CHANGING_METHODS:
        if _has_capability(estimator, method):

            # try if methods can handle 3d numpy input data