Ejemplo n.º 1
0
def test_shuffle_make_cylinder_bell_funnel(params):
    """Test that shuffling works as expected."""
    _, y = make_cylinder_bell_funnel(**params)
    arr_desired_no_shuffle = np.repeat(np.arange(3), 10)
    if 'shuffle' in params.keys() and (not params['shuffle']):
        np.testing.assert_array_equal(y, arr_desired_no_shuffle)
    else:
        assert not np.array_equal(y, arr_desired_no_shuffle)
Ejemplo n.º 2
0
def test_return_params_make_cylinder_bell_funnel(params):
    """Test the return objects."""
    res = make_cylinder_bell_funnel(**params)
    assert isinstance(res[0], np.ndarray)
    assert isinstance(res[1], np.ndarray)
    if 'return_params' in params.keys() and params['return_params']:
        parameters = res[2]
        assert isinstance(parameters, dict)
        assert isinstance(parameters['a'], (int, np.integer))
        assert isinstance(parameters['b'], (int, np.integer))
        assert isinstance(parameters['eta'], (float, np.floating))
        assert isinstance(parameters['epsilon'], np.ndarray)
        if 'n_samples' in params:
            assert parameters['epsilon'].shape == (params['n_samples'], 128)
        else:
            assert parameters['epsilon'].shape == (30, 128)
Ejemplo n.º 3
0
It is implemented as :class:`pyts.decomposition.SingularSpectrumAnalysis`.
"""

# Author: Lucas Plagwitz <*****@*****.**>
# License: BSD-3-Clause

import numpy as np
import matplotlib.pyplot as plt
from pyts.decomposition import SingularSpectrumAnalysis
from pyts.datasets import make_cylinder_bell_funnel

# Parameters
n_samples, n_timestamps = 3, 128

X_cbf, y = make_cylinder_bell_funnel(n_samples=10,
                                     random_state=42,
                                     shuffle=False)
X_period = 3 * np.sin(np.arange(n_timestamps))

X = X_cbf[:, :n_timestamps] + X_period

# We decompose the time series into three subseries
window_size = 20
# Singular Spectrum Analysis
ssa = SingularSpectrumAnalysis(window_size=window_size, groups="auto")
X_ssa = ssa.fit_transform(X)

# Show the results for different frequency-parameters
plt.figure(figsize=(16, 12))

ax1 = plt.subplot(221)
Ejemplo n.º 4
0
def rolling_window(a, window, step_size):
    shape = (np.int((a.shape[-1] - window) / step_size) + 1, window)
    rolled = np.empty(shape)

    for i in np.arange(shape[0]):
        rolled[i, :] = a[i * step_size:i * step_size + window]

    return rolled


# %% Cylinder_bell_funnel data

number_of_samples = 12

data, labels = make_cylinder_bell_funnel(n_samples=number_of_samples,
                                         random_state=0,
                                         shuffle=True,
                                         weights=(1 / 2, 1 / 2))

fs = 1
length_of_window = 128

timeseries = np.hstack(data)

np.savetxt('cylinderbellfunneltimeseries.csv',
           np.concatenate((np.reshape(np.arange(timeseries.size),
                                      (timeseries.size, 1)),
                           np.reshape(timeseries, (timeseries.size, 1))),
                          axis=1),
           delimiter=',')

windowed_data = rolling_window(timeseries, length_of_window, 1)
Ejemplo n.º 5
0
def test_parameter_check_make_cylinder_bell_funnel(params, error, err_msg):
    """Test parameter validation."""
    with pytest.raises(error, match=re.escape(err_msg)):
        make_cylinder_bell_funnel(**params)
Ejemplo n.º 6
0
def test_class_balance_make_cylinder_bell_funnel(params,
                                                 class_balance_desired):
    """Test that the class balance is the expected one."""
    X, y = make_cylinder_bell_funnel(**params)
    class_balance_actual = np.bincount(y)
    np.testing.assert_array_equal(class_balance_actual, class_balance_desired)
Ejemplo n.º 7
0
    return rolled


# %% Cylinder_bell_funnel data --> CHOOSE WHETHER YOU WANT TWO OR THREE CLUSTERS

number_of_samples = 12

proportion = (1 / 2, 1 / 2)  #For only cylinder and bell shapes
amount_of_clusters = 2

#proportion = (1/3,1/3)      #For all three shapes
#amount_of_clusters = 3

data, labels = make_cylinder_bell_funnel(n_samples=number_of_samples,
                                         shuffle=True,
                                         weights=proportion)

fs = 1
length_of_window = 128

timeseries = np.hstack(data)

np.savetxt('cylinderbellfunneltimeseries.csv',
           np.concatenate((np.reshape(np.arange(timeseries.size),
                                      (timeseries.size, 1)),
                           np.reshape(timeseries, (timeseries.size, 1))),
                          axis=1),
           delimiter=',')

windowed_data = rolling_window(timeseries, length_of_window, 1)
Ejemplo n.º 8
0
"""
=====================================
Making a Cylinder-Bell-Funnel dataset
=====================================

This example shows how to generate a Cylinder-Bell-Funnel dataset. This
simulated dataset was introduced by N. Saito in his Ph.D. thesis entitled
"Local feature extraction and its application". It is one of the most
well-known datasets in time series classification.
It is implemented as :func:`pyts.datasets.make_cylinder_bell_funnel`.
"""

# Author: Johann Faouzi <*****@*****.**>
# License: BSD-3-Clause

import matplotlib.pyplot as plt
from pyts.datasets import make_cylinder_bell_funnel

X, y = make_cylinder_bell_funnel(n_samples=12, random_state=42)

plt.figure(figsize=(12, 9))
for i, classe in enumerate(['cylinder', 'bell', 'funnel']):
    plt.subplot(3, 1, i + 1)
    for x in X[y == i]:
        plt.plot(x, color='C0', linewidth=0.9)
    plt.title('Class: {}'.format(classe), fontsize=16)

plt.tight_layout()
plt.subplots_adjust(hspace=0.4)
plt.show()
Ejemplo n.º 9
0
                               color=color,
                               **kwargs)
        return self


if __name__ == '__main__':
    from pyts.datasets import make_cylinder_bell_funnel
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import accuracy_score

    # warnings.filterwarnings("ignore")

    random_state = 0
    dataset_name = "cbf"

    X, y = make_cylinder_bell_funnel(n_samples=100, random_state=random_state)

    print("DATASET INFO:")
    print("X SHAPE: ", X.shape)
    print("y SHAPE: ", y.shape)
    unique, counts = np.unique(y, return_counts=True)
    print("\nCLASSES BALANCE")
    for i, label in enumerate(unique):
        print(label, ": ", round(counts[i] / sum(counts), 2))

    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.3, stratify=y, random_state=random_state)

    print("\nSHAPES:")
    print("TRAINING SET: ", X_train.shape)
    print("TEST SET: ", X_test.shape)
Ejemplo n.º 10
0
def build_cbf(n_samples=600, random_state=0, verbose=True):
    X_all, y_all = make_cylinder_bell_funnel(n_samples=n_samples, random_state=random_state)
    X_all = X_all[:, :, np.newaxis]

    if verbose:
        print("DATASET INFO:")
        print("X SHAPE: ", X_all.shape)
        print("y SHAPE: ", y_all.shape)
        unique, counts = np.unique(y_all, return_counts=True)
        print("\nCLASSES BALANCE")
        for i, label in enumerate(unique):
            print(label, ": ", round(counts[i] / sum(counts), 2))

    # BLACKBOX/EXPLANATION SETS SPLIT
    X_train, X_exp, y_train, y_exp = train_test_split(
        X_all,
        y_all,
        test_size=0.3,
        stratify=y_all, random_state=random_state
    )

    # BLACKBOX TRAIN/TEST SETS SPLIT
    X_train, X_test, y_train, y_test = train_test_split(
        X_train,
        y_train,
        test_size=0.2,
        stratify=y_train,
        random_state=random_state
    )

    # BLACKBOX TRAIN/VALIDATION SETS SPLIT
    X_train, X_val, y_train, y_val = train_test_split(
        X_train,
        y_train,
        test_size=0.2,
        stratify=y_train,
        random_state=random_state
    )

    # EXPLANATION TRAIN/TEST SETS SPLIT
    X_exp_train, X_exp_test, y_exp_train, y_exp_test = train_test_split(
        X_exp,
        y_exp,
        test_size=0.2,
        stratify=y_exp,
        random_state=random_state
    )

    # EXPLANATION TRAIN/VALIDATION SETS SPLIT
    X_exp_train, X_exp_val, y_exp_train, y_exp_val = train_test_split(
        X_exp_train, y_exp_train,
        test_size=0.2,
        stratify=y_exp_train,
        random_state=random_state
    )

    if verbose:
        print("\nSHAPES:")
        print("BLACKBOX TRAINING SET: ", X_train.shape)
        print("BLACKBOX VALIDATION SET: ", X_val.shape)
        print("BLACKBOX TEST SET: ", X_test.shape)
        print("EXPLANATION TRAINING SET: ", X_exp_train.shape)
        print("EXPLANATION VALIDATION SET: ", X_exp_val.shape)
        print("EXPLANATION TEST SET: ", X_exp_test.shape)

    return (X_train, y_train, X_val, y_val, X_test, y_test, X_exp_train,
            y_exp_train, X_exp_val, y_exp_val, X_exp_test, y_exp_test)
Ejemplo n.º 11
0
def build_multivariate_cbf(n_samples=600, n_features=3, random_state=0, verbose=True):
    X_all = [[], [], []]
    y_all = []
    for i in range(n_features):
        X, y = make_cylinder_bell_funnel(n_samples=n_samples, random_state=random_state + i)
        X = X[:, :, np.newaxis]
        for label in range(3):
            X_all[label].append(X[np.nonzero(y == label)])
    for i in range(len(X_all)):
        X_all[i] = np.concatenate(X_all[i], axis=2)
    for label in range(3):
        y_all.extend(label for i in range(len(X_all[label])))
    X_all = np.concatenate(X_all, axis=0)
    y_all = np.array(y_all)

    if verbose:
        print("DATASET INFO:")
        print("X SHAPE: ", X_all.shape)
        print("y SHAPE: ", y_all.shape)
        unique, counts = np.unique(y_all, return_counts=True)
        print("\nCLASSES BALANCE")
        for i, label in enumerate(unique):
            print(label, ": ", round(counts[i] / sum(counts), 2))

    # BLACKBOX/EXPLANATION SETS SPLIT
    X_train, X_exp, y_train, y_exp = train_test_split(
        X_all,
        y_all,
        test_size=0.3,
        stratify=y_all, random_state=random_state
    )

    # BLACKBOX TRAIN/TEST SETS SPLIT
    X_train, X_test, y_train, y_test = train_test_split(
        X_train,
        y_train,
        test_size=0.2,
        stratify=y_train,
        random_state=random_state
    )

    # BLACKBOX TRAIN/VALIDATION SETS SPLIT
    X_train, X_val, y_train, y_val = train_test_split(
        X_train,
        y_train,
        test_size=0.2,
        stratify=y_train,
        random_state=random_state
    )

    # EXPLANATION TRAIN/TEST SETS SPLIT
    X_exp_train, X_exp_test, y_exp_train, y_exp_test = train_test_split(
        X_exp,
        y_exp,
        test_size=0.2,
        stratify=y_exp,
        random_state=random_state
    )

    # EXPLANATION TRAIN/VALIDATION SETS SPLIT
    X_exp_train, X_exp_val, y_exp_train, y_exp_val = train_test_split(
        X_exp_train, y_exp_train,
        test_size=0.2,
        stratify=y_exp_train,
        random_state=random_state
    )

    if verbose:
        print("\nSHAPES:")
        print("BLACKBOX TRAINING SET: ", X_train.shape)
        print("BLACKBOX VALIDATION SET: ", X_val.shape)
        print("BLACKBOX TEST SET: ", X_test.shape)
        print("EXPLANATION TRAINING SET: ", X_exp_train.shape)
        print("EXPLANATION VALIDATION SET: ", X_exp_val.shape)
        print("EXPLANATION TEST SET: ", X_exp_test.shape)

    return (X_train, y_train, X_val, y_val, X_test, y_test, X_exp_train,
            y_exp_train, X_exp_val, y_exp_val, X_exp_test, y_exp_test)