Example #1
0
def encode_dataset(dataset,
                   batch_size,
                   downscale_factor,
                   pooling_function,
                   dimension=1,
                   time_delay=1,
                   threshold=None):
    """ Computation of encodings has to be done in batches due to the large size of the dataset.
        Otherwise the kernel will die!
        
        For downscaling pick np.mean (average pooling) or np.max (max pooling) respectively.
        If downscaling is not required choose downscale_factor=1.
        Keep in mind the network expects an input image size of 64x64.
        
        The function returns a 3D matrix.
        The new 3D matrix contains several 2D matrices, which correspond to the time series encodings/images.
        The order of the objects does not change, which means for example that the 23rd slice of the 
        input dataset corresponds to the 23rd encoding in the 3D Matrix."""

    n, l = np.shape(dataset)
    f = downscale_factor
    n_batches = n // batch_size
    batches = np.linspace(1, n_batches, n_batches, dtype=int) * batch_size

    rp = RecurrencePlot(dimension=dimension,
                        time_delay=time_delay,
                        threshold=threshold)

    print('Encoding started...')
    for p in range(n_batches):
        if p == 0:
            X_rp = rp.transform(dataset[0:batches[p], :])
            sample = block_reduce(X_rp[0],
                                  block_size=(f, f),
                                  func=pooling_function)
            l_red = sample.shape[0]
            X_rp_red = np.zeros((n, l_red, l_red))
            print('output 3D Matrix shape: ', np.shape(X_rp_red))

            j = 0
            for i in range(0, batches[p]):
                X_rp_red[i] = block_reduce(X_rp[j],
                                           block_size=(f, f),
                                           func=pooling_function)
                j += 1

        else:
            X_rp = rp.transform(X[batches[p - 1]:batches[p], :])

            j = 0
            for i in range(batches[p - 1], batches[p]):
                X_rp_red[i] = block_reduce(X_rp[j],
                                           block_size=(f, f),
                                           func=pooling_function)
                j += 1

    print('Encoding successful!')
    print('#####################################')

    return X_rp_red
Example #2
0
    def load_data(self):
        """
        Prepare numpy array X with shape (num_instances, img_size, img_size, num_variables)
        and y with shape (num_instances, num_variables)
        """

        X, Y = np.empty((self.num_instances, self.img_size, self.img_size, self.num_variables)), \
               np.empty((self.num_instances, self.num_variables))
        print(X.shape)

        # Initialize PAA transformer
        paa = PiecewiseAggregateApproximation(window_size=None,
                                              output_size=self.img_size,
                                              overlapping=False)
        rp = RecurrencePlot()

        # For all instance
        start = time.time()
        for idx, row in enumerate(self.data.iterrows()):
            for i in range(self.num_variables):
                # Get current variable's series
                # Apply linear interpolation on missing values
                s = row[1][i].interpolate(
                    limit_direction='both').to_numpy()[:self.ts_length]
                # Apply PAA and RP
                X[idx, :, :, i] = rp.transform(
                    paa.transform(np.expand_dims(s[:-1], axis=0)))[0]
                Y[idx, i] = s[-1]
        end = time.time()
        print(f"Data loaded in {end - start} seconds")

        return X, Y
Example #3
0
# Author: Johann Faouzi <*****@*****.**>
# License: BSD-3-Clause

import numpy as np
import matplotlib.pyplot as plt
from pyts.image import RecurrencePlot


# Create a toy time series using the sine function
time_points = np.linspace(0, 4 * np.pi, 1000)
x = np.sin(time_points)
X = np.array([x])

# Recurrence plot transformation
rp = RecurrencePlot(threshold=np.pi/18)
X_rp = rp.transform(X)

# Plot the time series and its recurrence plot
fig = plt.figure(figsize=(6, 6))

gs = fig.add_gridspec(2, 2,  width_ratios=(2, 7), height_ratios=(2, 7),
                      left=0.1, right=0.9, bottom=0.1, top=0.9,
                      wspace=0.05, hspace=0.05)

# Define the ticks and their labels for both axes
time_ticks = np.linspace(0, 4 * np.pi, 9)
time_ticklabels = [r'$0$', r'$\frac{\pi}{2}$', r'$\pi$',
                   r'$\frac{3\pi}{2}$', r'$2\pi$', r'$\frac{5\pi}{2}$',
                   r'$3\pi$', r'$\frac{7\pi}{2}$', r'$4\pi$']
value_ticks = [-1, 0, 1]
reversed_value_ticks = value_ticks[::-1]
Example #4
0
def test_parameter_check(params, error, err_msg):
    """Test parameter validation."""
    recurrence = RecurrencePlot(**params)
    with pytest.raises(error, match=re.escape(err_msg)):
        recurrence.transform(X)
    Pandas: 1.0.3
    matplotlib: 3.2.1
"""

import pickle
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.axes_grid1 import make_axes_locatable
from pyts.datasets import load_gunpoint
from pyts.image import RecurrencePlot

if __name__ == '__main__':
    X, _, _, _ = load_gunpoint(return_X_y=True)
    rp = RecurrencePlot(dimension=3, time_delay=3)
    X_new = rp.transform(X)
    rp2 = RecurrencePlot(dimension=3, time_delay=10)
    X_new2 = rp2.transform(X)
    plt.figure()
    plt.suptitle('gunpoint_index_0')
    ax1 = plt.subplot(121)
    plt.imshow(X_new[0])
    plt.title('Recurrence plot, dimension=3, time_delay=3')
    divider = make_axes_locatable(ax1)
    cax = divider.append_axes("right", size="5%", pad=0.2)
    plt.colorbar(cax=cax)

    ax1 = plt.subplot(122)
    plt.imshow(X_new2[0])
    plt.title('Recurrence plot, dimension=3, time_delay=10')
    divider = make_axes_locatable(ax1)