Example #1
0
def plot_gadf(ts, image_size, overlapping=False, scale='-1',
              cmap='rainbow', output_file=None, interpolation=None):
    """Plot the image obtained after GADF transformation.

    Parameters
    ----------
    ts : np.array, shape = [n_features]
        time series to plot

    image_size : int (default = 32)
        determines the shapes of the output image :
        image_size x image_size

    overlapping : bool (default = False)
        if True, reducing the size of the time series with PAA is
        done with possible overlapping windows.

    scale : str (default = '-1')
        the lower bound of the scaled time series. Possible values:

            - '-1' : the time series are scaled in [-1,1]
            - '0' : the time series are scaled in [0,1]

    output_file : str or None (default = None)
        if str, save the figure.
    """

    # Check input data
    if not (isinstance(ts, np.ndarray) and ts.ndim == 1):
        raise ValueError("'ts' must be a 1-dimensional np.ndarray.")

    # Size of ts
    ts_size = ts.size

    # Check parameters
    if not isinstance(image_size, int):
        raise TypeError("'image_size' must be an integer.")
    if image_size < 2:
        raise ValueError("'image_size' must be greater or equal than 2.")
    if image_size > ts_size:
        raise ValueError("'image_size' must be lower or equal than the size of 'ts'.")
    if not isinstance(overlapping, (float, int)):
        raise TypeError("'overlapping' must be a boolean.")
    if scale not in ['0', '-1']:
        raise ValueError("'scale' must be either '0' or '-1'.")

    image_gadf = gaf(ts, ts_size, image_size, overlapping, 'd', scale)

    plt.imshow(image_gadf, cmap=cmap, interpolation=interpolation)
    plt.axis('off')

    if output_file is not None:
        plt.savefig(output_file)

    # Show plot
    plt.show()
Example #2
0
def test_gaf():
    """Testing 'gaf'"""

    # Parameter
    size = 9
    ts = np.linspace(-1, 1, size)
    overlapping = False
    ones = np.ones(size)

    # Test 1
    arr_actual = gaf(ts, size, size, overlapping, method='s', scale='-1')
    arr_desired = np.outer(ts, ts) - np.outer(np.sqrt(ones - ts**2),
                                              np.sqrt(ones - ts**2))
    np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)

    # Test 2
    arr_actual = gaf(ts, size, size, overlapping, method='d', scale='-1')
    arr_desired = np.outer(np.sqrt(ones - ts**2), ts) - np.outer(
        ts, np.sqrt(ones - ts**2))
    np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)