def test_plot_series_output_type(series_to_plot): """Tests whether plot_series returns plt.fig and plt.ax.""" _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt # Test output case where kwarg ax=None fig, ax = _plot_series(series_to_plot) is_fig_figure = isinstance(fig, plt.Figure) is_ax_axis = isinstance(ax, plt.Axes) assert is_fig_figure and is_ax_axis, "".join([ "plot_series with kwarg ax=None should return plt.Figure and plt.Axes,", f"but returned: {type(fig)} and {type(ax)}", ]) # Test output case where an existing plt.Axes object is passed to kwarg ax fig, ax = plt.subplots(1, figsize=plt.figaspect(0.25)) ax = _plot_series(series_to_plot, ax=ax) is_ax_axis = isinstance(ax, plt.Axes) assert is_ax_axis, "".join([ "plot_series with plt.Axes object passed to kwarg ax", f"should return plt.Axes, but returned: {type(ax)}", ])
def __init__( self, n_clusters: int = 8, init_algorithm: Union[str, np.ndarray] = "random", n_init: int = 10, max_iter: int = 300, tol: float = 1e-4, verbose: bool = False, random_state: Union[int, RandomState] = None, ): _check_soft_dependencies("tslearn", severity="error", object=self) self.init_algorithm = init_algorithm self.n_init = n_init self.max_iter = max_iter self.tol = tol self.verbose = verbose self.random_state = random_state self.cluster_centers_ = None self.labels_ = None self.inertia_ = None self.n_iter_ = 0 self._tslearn_k_shapes = None super(TimeSeriesKShapes, self).__init__(n_clusters=n_clusters)
def __init__( self, use_box_cox=None, box_cox_bounds=(0, 1), use_trend=None, use_damped_trend=None, sp=None, use_arma_errors=True, show_warnings=True, n_jobs=None, multiprocessing_start_method="spawn", context=None, ): _check_soft_dependencies("tbats", severity="error", object=self) self.use_box_cox = use_box_cox self.box_cox_bounds = box_cox_bounds self.use_trend = use_trend self.use_damped_trend = use_damped_trend self.sp = sp self.use_arma_errors = use_arma_errors self.show_warnings = show_warnings self.n_jobs = n_jobs self.multiprocessing_start_method = multiprocessing_start_method self.context = context # custom sktime args self._forecaster = None self._yname = None # .fit(y) -> y.name super(_TbatsAdapter, self).__init__()
def __init__( self, order=(1, 0, 0), seasonal_order=(0, 0, 0, 0), start_params=None, method="lbfgs", maxiter=50, suppress_warnings=False, out_of_sample_size=0, scoring="mse", scoring_args=None, trend=None, with_intercept=True, **sarimax_kwargs ): _check_soft_dependencies("pmdarima", severity="error", object=self) self.order = order self.seasonal_order = seasonal_order self.start_params = start_params self.method = method self.maxiter = maxiter self.suppress_warnings = suppress_warnings self.out_of_sample_size = out_of_sample_size self.scoring = scoring self.scoring_args = scoring_args self.trend = trend self.with_intercept = with_intercept self.sarimax_kwargs = sarimax_kwargs super(ARIMA, self).__init__()
def test_plot_series_runs_without_error(series_to_plot): """Test whether plot_series runs without error.""" _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt _plot_series(series_to_plot) plt.gcf().canvas.draw_idle()
def plot_series(X: TimeSeriesInstances): _check_soft_dependencies("matplotlib") import matplotlib.patches as mpatches import matplotlib.pyplot as plt if isinstance(X, pd.DataFrame): X = convert_to(X, "numpy3D") plt.figure(figsize=(5, 10)) plt.rcParams["figure.dpi"] = 100 fig, axes = plt.subplots(nrows=len(X), ncols=1) for i in range(len(X)): curr = X[i][0] curr_axes = axes[i] curr_axes.plot(curr, color="b") blue_patch = mpatches.Patch(color="blue", label="Series that belong to the cluster") plt.legend( handles=[blue_patch], loc="upper center", bbox_to_anchor=(0.5, -0.40), fancybox=True, shadow=True, ncol=5, ) plt.tight_layout() plt.show()
def __init__( self, n_clusters: int = 8, kernel: str = "gak", n_init: int = 10, max_iter: int = 300, tol: float = 1e-4, kernel_params: Union[dict, None] = None, verbose: bool = False, n_jobs: Union[int, None] = None, random_state: Union[int, RandomState] = None, ): _check_soft_dependencies("tslearn", severity="error", object=self) self.kernel = kernel self.n_init = n_init self.max_iter = max_iter self.tol = tol self.kernel_params = kernel_params self.verbose = verbose self.n_jobs = n_jobs self.random_state = random_state self.cluster_centers_ = None self.labels_ = None self.inertia_ = None self.n_iter_ = 0 self._tslearn_kernel_k_means = None super(TimeSeriesKernelKMeans, self).__init__(n_clusters=n_clusters)
def test_plot_lags_arguments(series_to_plot, lags, suptitle): """Tests whether plot_lags run with different input arguments.""" _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt plot_lags(series_to_plot, lags=lags, suptitle=suptitle) plt.gcf().canvas.draw_idle() plt.close()
def test_univariate_plots_run_without_error(series_to_plot, plot_func): """Tests whether plots that accept univariate series run without error.""" _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt plot_func(series_to_plot) plt.gcf().canvas.draw_idle() plt.close()
def test_check_soft_dependencies_raises_error(): """Test the _check_soft_dependencies() function.""" with pytest.raises(ModuleNotFoundError, match=r".* soft dependency .*"): _check_soft_dependencies("unavailable_module") with pytest.raises(ModuleNotFoundError, match=r".* soft dependency .*"): _check_soft_dependencies("unavailable_module_1", "unavailable_module_2")
def __init__( self, # Args due to wrapping freq=None, add_seasonality=None, add_country_holidays=None, # Args of fbprophet growth="linear", changepoints=None, n_changepoints=25, changepoint_range=0.8, yearly_seasonality="auto", weekly_seasonality="auto", daily_seasonality="auto", holidays=None, seasonality_mode="additive", seasonality_prior_scale=10.0, holidays_prior_scale=10.0, changepoint_prior_scale=0.05, mcmc_samples=0, alpha=DEFAULT_ALPHA, uncertainty_samples=1000, stan_backend=None, verbose=0, interval_width=0, ): _check_soft_dependencies("prophet", severity="error", object=self) self.freq = freq self.add_seasonality = add_seasonality self.add_country_holidays = add_country_holidays self.growth = growth self.changepoints = changepoints self.n_changepoints = n_changepoints self.changepoint_range = changepoint_range self.yearly_seasonality = yearly_seasonality self.weekly_seasonality = weekly_seasonality self.daily_seasonality = daily_seasonality self.holidays = holidays self.seasonality_mode = seasonality_mode self.seasonality_prior_scale = seasonality_prior_scale self.changepoint_prior_scale = changepoint_prior_scale self.holidays_prior_scale = holidays_prior_scale self.mcmc_samples = mcmc_samples self.alpha = alpha self.uncertainty_samples = uncertainty_samples self.stan_backend = stan_backend self.verbose = verbose self.interval_width = interval_width # import inside method to avoid hard dependency from prophet.forecaster import Prophet as _Prophet self._ModelClass = _Prophet super(Prophet, self).__init__()
def test_plot_series_uniform_treatment_of_int64_range_index_types(): """Test that int64 and range indices are treated the same without error.""" _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt y1 = pd.Series(np.arange(10)) y2 = pd.Series(np.random.normal(size=10)) y1.index = pd.Int64Index(y1.index) y2.index = pd.RangeIndex(y2.index) plot_series(y1, y2) plt.gcf().canvas.draw_idle()
def test_plot_series_uniform_treatment_of_int64_range_index_types(): # We test that int64 and range indices are treated uniformly and do not raise an # error of inconsistent index types _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt y1 = pd.Series(np.arange(10)) y2 = pd.Series(np.random.normal(size=10)) y1.index = pd.Int64Index(y1.index) y2.index = pd.RangeIndex(y2.index) plot_series(y1, y2) plt.gcf().canvas.draw_idle()
def test_plot_correlations_output_type(series_to_plot): _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt fig, ax = plot_correlations(series_to_plot) is_fig_figure = isinstance(fig, plt.Figure) is_ax_array = isinstance(ax, np.ndarray) is_ax_array_axis = all([isinstance(ax_, plt.Axes) for ax_ in ax]) assert is_fig_figure and is_ax_array and is_ax_array_axis, "".join([ "plot_correlations should return plt.Figure and array of plt.Axes,", f"but returned: {type(fig)} and {type(ax)}", ])
def test_univariate_plots_output_type(series_to_plot, plot_func): """Tests whether plots accepting univariate series have correct output types.""" _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt fig, ax = plot_func(series_to_plot) is_fig_figure = isinstance(fig, plt.Figure) is_ax_array = isinstance(ax, np.ndarray) is_ax_array_axis = all([isinstance(ax_, plt.Axes) for ax_ in ax]) assert is_fig_figure and is_ax_array and is_ax_array_axis, "".join([ f"{plot_func.__name__} should return plt.Figure and array of plt.Axes,", f"but returned: {type(fig)} and {type(ax)}", ])
def plot_time_series_with_change_points(ts_name, ts, true_cps, font_size=16): """Plot the time series with the known change points. Parameters ---------- ts_name: str the name of the time series (dataset) to be annotated ts: array-like, shape = [n] the univariate time series of length n to be annotated true_cps: array-like, dtype=int the known change points these are highlighted in the time series as vertical lines font_size: int for plotting Returns ------- fig : matplotlib.figure.Figure axes : np.ndarray Array of the figure's Axe objects """ # Checks availability of plotting libraries _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt ts = check_X(ts) fig = plt.figure(figsize=(20, 5)) segments = [0] + true_cps.tolist() + [ts.shape[0]] for idx in np.arange(0, len(segments) - 1): plt.plot( range(segments[idx], segments[idx + 1]), ts[segments[idx]:segments[idx + 1]], ) lim1 = plt.ylim()[0] lim2 = plt.ylim()[1] ax = plt.gca() for i, idx in enumerate(true_cps): ax.vlines(idx, lim1, lim2, linestyles="--", label=str(i) + "-th-CPT") plt.legend(loc="best") plt.title(ts_name, fontsize=font_size) return fig, ax
def test_plot_series_runs_without_error(series_to_plot): """Test whether plot_series runs without error.""" _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt _plot_series(series_to_plot) plt.gcf().canvas.draw_idle() # Test with labels specified if isinstance(series_to_plot, pd.Series): labels = ["Series 1"] elif isinstance(series_to_plot, tuple): labels = [f"Series {i+1}" for i in range(len(series_to_plot))] _plot_series(series_to_plot, labels=labels) plt.gcf().canvas.draw_idle() plt.close()
def plot_cluster_algorithm(model: BaseClusterer, predict_series: NumpyOrDF, k: int): """ Method that is used to plot a clustering algorithms output Parameters ---------- model: BaseClusterer Clustering model to plot predict_series: Numpy or Dataframe The series to predict the values for k: int Number of centers """ _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt import matplotlib.patches as mpatches if isinstance(predict_series, pd.DataFrame): predict_series = from_nested_to_2d_array(predict_series, return_numpy=True) plt.figure(figsize=(5, 10)) plt.rcParams["figure.dpi"] = 100 indexes = model.predict(predict_series) centers = model.get_centers() series_values = TimeSeriesLloydsPartitioning.get_cluster_values( indexes, predict_series, k) fig, axes = plt.subplots(nrows=k, ncols=1) for i in range(k): _plot(series_values[i], centers[i], axes[i]) blue_patch = mpatches.Patch(color="blue", label="Series that belong to the cluster") red_patch = mpatches.Patch(color="red", label="Cluster centers") plt.legend( handles=[red_patch, blue_patch], loc="upper center", bbox_to_anchor=(0.5, -0.40), fancybox=True, shadow=True, ncol=5, ) plt.tight_layout() plt.show()
def plot_cluster_algorithm(model: TimeSeriesLloyds, X: TimeSeriesInstances, k: int): """Plot the results from a univariate partitioning algorithm. Parameters ---------- model: BaseClusterer Clustering model to plot predict_series: np.ndarray or pd.Dataframe or List[pd.Dataframe] The series to predict the values for k: int Number of centers """ _check_soft_dependencies("matplotlib") import matplotlib.patches as mpatches import matplotlib.pyplot as plt if isinstance(X, pd.DataFrame): predict_series = convert_to(X, "numpy3D") plt.figure(figsize=(5, 10)) plt.rcParams["figure.dpi"] = 100 indexes = model.predict(predict_series) centers = model.cluster_centers_ series_values = _get_cluster_values(indexes, predict_series, k) fig, axes = plt.subplots(nrows=k, ncols=1) for i in range(k): _plot(series_values[i], centers[i], axes[i]) blue_patch = mpatches.Patch(color="blue", label="Series that belong to the cluster") red_patch = mpatches.Patch(color="red", label="Cluster centers") plt.legend( handles=[red_patch, blue_patch], loc="upper center", bbox_to_anchor=(0.5, -0.40), fancybox=True, shadow=True, ncol=5, ) plt.tight_layout() plt.show()
def __init__( self, window_name=None, window_depth=None, window_length=None, window_step=None, sig_tfm=None, sig_depth=None, rescaling=None, ): _check_soft_dependencies("esig", severity="error", object=self) super().__init__() self.window_name = window_name self.window_depth = window_depth self.window_length = window_length self.window_step = window_step self.sig_tfm = sig_tfm self.sig_depth = sig_depth self.rescaling = rescaling self.window = _window_getter( self.window_name, self.window_depth, self.window_length, self.window_step )
#!/usr/bin/env python3 -u # -*- coding: utf-8 -*- # copyright: sktime developers, BSD-3-Clause License (see LICENSE file) """Implements matrix profile transformation.""" __author__ = ["mloning"] __all__ = ["MatrixProfileTransformer"] from sktime.transformations.base import BaseTransformer from sktime.utils.validation._dependencies import _check_soft_dependencies _check_soft_dependencies("stumpy") import stumpy # noqa: E402 class MatrixProfileTransformer(BaseTransformer): """Calculate the matrix profile of a time series. Takes as input a single time series dataset and returns the matrix profile for that time series dataset. The matrix profile is a vector that stores the z-normalized Euclidean distance between any subsequence within a time series and its nearest neighbor. For more information on the matrix profile, see `stumpy's tutorial <https://stumpy.readthedocs.io/en/latest/Tutorial_The_Matrix_Profile.html>`_ Parameters ---------- window_length : int
#!/usr/bin/env python3 -u # -*- coding: utf-8 -*- # copyright: sktime developers, BSD-3-Clause License (see LICENSE file) __author__ = ["Martin Walter"] __all__ = ["BATS"] from sktime.utils.validation._dependencies import _check_soft_dependencies from sktime.forecasting.base.adapters import _TbatsAdapter _check_soft_dependencies("tbats") class BATS(_TbatsAdapter): """BATS estimator used to fit and select best performing model. BATS (Exponential smoothing state space model with Box-Cox transformation, ARMA errors, Trend and Seasonal components.) Model has been described in De Livera, Hyndman & Snyder (2011). Parameters ---------- use_box_cox: bool or None, optional (default=None) If Box-Cox transformation of original series should be applied. When None both cases shall be considered and better is selected by AIC. box_cox_bounds: tuple, shape=(2,), optional (default=(0, 1)) Minimal and maximal Box-Cox parameter values. use_trend: bool or None, optional (default=None) Indicates whether to include a trend or not. When None both cases shall be considered and better is selected by AIC. use_damped_trend: bool or None, optional (default=None) Indicates whether to include a damping parameter in the trend or not.
def __init__(self, window_length=3): _check_soft_dependencies("stumpy", severity="error", object=self) self.window_length = window_length super(MatrixProfileTransformer, self).__init__()
#!/usr/bin/env python3 -u # -*- coding: utf-8 -*- # copyright: sktime developers, BSD-3-Clause License (see LICENSE file) """Implements matrix profile transformation.""" __author__ = ["mloning"] __all__ = ["MatrixProfileTransformer"] from sktime.transformations.base import BaseTransformer from sktime.utils.validation._dependencies import _check_soft_dependencies _check_soft_dependencies("stumpy", severity="warning") class MatrixProfileTransformer(BaseTransformer): """Calculate the matrix profile of a time series. Takes as input a single time series dataset and returns the matrix profile for that time series dataset. The matrix profile is a vector that stores the z-normalized Euclidean distance between any subsequence within a time series and its nearest neighbor. For more information on the matrix profile, see `stumpy's tutorial <https://stumpy.readthedocs.io/en/latest/Tutorial_The_Matrix_Profile.html>`_ Parameters ---------- window_length : int Notes -----
import numpy as np import math from joblib import Parallel, delayed from sklearn.ensemble._forest import ForestClassifier from sklearn.tree import DecisionTreeClassifier from sklearn import clone from sklearn.utils import check_random_state from sklearn.utils.multiclass import class_distribution from sktime.utils.validation._dependencies import _check_soft_dependencies from sktime.utils.slope_and_trend import _slope from sktime.utils.validation.panel import check_X, check_X_y from sktime.classification.base import BaseClassifier _check_soft_dependencies("catch22") from sktime.transformations.panel.catch22_features import Catch22 # noqa: E402 class CanonicalIntervalForest(ForestClassifier, BaseClassifier): """Canonical Interval Forest Classifier. @article{middlehurst2020canonical, title={The Canonical Interval Forest {(CIF)} Classifier for Time Series Classification}, author={Middlehurst, Matthew and Large, James and Bagnall, Anthony}, journal={IEEE International Conference on Big Data}, year={2020} } Interval based forest making use of the catch22 feature set on randomly
#!/usr/bin/env python3 -u # -*- coding: utf-8 -*- # copyright: sktime developers, BSD-3-Clause License (see LICENSE file) """Implements Prophet forecaster by wrapping fbprophet.""" __author__ = ["Martin Walter"] __all__ = ["Prophet"] from sktime.forecasting.base._base import DEFAULT_ALPHA from sktime.forecasting.base.adapters import _ProphetAdapter from sktime.utils.validation._dependencies import _check_soft_dependencies _check_soft_dependencies("fbprophet") class Prophet(_ProphetAdapter): """Prophet forecaster by wrapping fbprophet. Parameters ---------- freq: String of DatetimeIndex frequency. See here for possible values: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html #timeseries-offset-aliases add_seasonality: Dict with args for Prophet.add_seasonality(). Dict can have the following keys/values: name: string name of the seasonality component. period: float number of days in one period. fourier_order: int number of Fourier components to use. prior_scale: optional float prior scale for this component. mode: optional 'additive' or 'multiplicative' condition_name: string name of the seasonality condition.
# -*- coding: utf-8 -*- """Standalone function to plot critical difference diagram.""" __author__ = ["Svea Meyer"] import numpy as np from scipy.stats import rankdata, find_repeats, distributions import math from sktime.utils.validation._dependencies import _check_soft_dependencies _check_soft_dependencies("matplotlib") import matplotlib.pyplot as plt def _check_friedman(n_strategies, n_datasets, ranked_data, alpha): """ Check whether Friedman test is significant. Larger parts of code copied from scipy. Arguments --------- n_strategies : int number of strategies to evaluate n_datasets : int number of datasets classified per strategy ranked_data : np.array (shape: n_strategies * n_datasets) rank of strategy on dataset Returns -------
# -*- coding: utf-8 -*- """Time series kshapes.""" from typing import Union import numpy as np from numpy.random import RandomState from sktime.clustering.base import BaseClusterer, TimeSeriesInstances from sktime.utils.validation._dependencies import _check_soft_dependencies _check_soft_dependencies("tslearn", severity="warning") class TimeSeriesKShapes(BaseClusterer): """Kshape algorithm wrapper tslearns implementation. Parameters ---------- n_clusters: int, defaults = 8 The number of clusters to form as well as the number of centroids to generate. init_algorithm: str or np.ndarray, defaults = 'random' Method for initializing cluster centers. Any of the following are valid: ['random']. Or a np.ndarray of shape (n_clusters, ts_size, d) and gives the initial centers. n_init: int, defaults = 10 Number of times the k-means algorithm will be run with different centroid seeds. The final result will be the best output of n_init consecutive runs in terms of inertia. max_iter: int, defaults = 30 Maximum number of iterations of the k-means algorithm for a single
# -*- coding: utf-8 -*- __author__ = "Angus Dempster" __all__ = ["Rocket"] import numpy as np import pandas as pd from sktime.transformations.base import _PanelToTabularTransformer from sktime.utils.validation._dependencies import _check_soft_dependencies from sktime.utils.validation.panel import check_X _check_soft_dependencies("numba") from numba import njit # noqa: E402 from numba import prange # noqa: E402 class Rocket(_PanelToTabularTransformer): """ROCKET RandOm Convolutional KErnel Transform @article{dempster_etal_2019, author = {Dempster, Angus and Petitjean, Francois and Webb, Geoffrey I}, title = {ROCKET: Exceptionally fast and accurate time series classification using random convolutional kernels}, year = {2019}, journal = {arXiv:1910.13051} } Parameters
# -*- coding: utf-8 -*- # copyright: sktime developers, BSD-3-Clause License (see LICENSE file) __author__ = ["Ayushmaan Seth", "Markus Löning", "Alwin Wang"] __all__ = ["TSFreshFeatureExtractor", "TSFreshRelevantFeatureExtractor"] from warnings import warn from sktime.transformations.base import _PanelToTabularTransformer from sktime.utils.validation._dependencies import _check_soft_dependencies from sktime.utils.data_processing import from_nested_to_long from sktime.utils.validation import check_n_jobs from sktime.utils.validation.panel import check_X from sktime.utils.validation.panel import check_X_y _check_soft_dependencies("tsfresh") class _TSFreshFeatureExtractor(_PanelToTabularTransformer): """Base adapter class for tsfresh transformations""" def __init__( self, default_fc_parameters="efficient", kind_to_fc_parameters=None, chunksize=None, n_jobs=1, show_warnings=True, disable_progressbar=False, impute_function=None, profiling=None, profiling_filename=None,