Пример #1
0
    def transform_to_2D(self, method, x_train):
        if method == 'gasf':
            gasf = GASF(image_size=x_train.shape[1] // 2,
                        overlapping=False,
                        scale=-1)
            x_tr = gasf.fit_transform(x_train)
            print('applying GASF')
        elif method == 'mtf':
            mtf = MTF(image_size=x_train.shape[1],
                      n_bins=4,
                      quantiles='empirical',
                      overlapping=False)
            x_tr = mtf.fit_transform(x_train)
            print('applying MTF')
        elif method == 'rp':
            rp = RecurrencePlots(dimension=1,
                                 epsilon='percentage_points',
                                 percentage=10)
            x_tr = rp.fit_transform(x_train)
            print('applying RP')
        else:
            print('wrong method')
            x_tr = []

        return x_tr
Пример #2
0
def recurrence_plot(dataset, n_channels, seq_length, img_size=32, downsample=False):
    """
    function to transform the given dataset into recurrence plot images

    :param dataset: the raw data read from .csv or .npy file
    :type dataset: ndarray
    :param n_channels: number of channels
    :type n_channels: int
    :param seq_length: length of one data stream of a single sensor
    :type seq_length: int
    :param img_size: size of the recurrence plot image (if downsampling is enabled)
    :type img_size: int
    :param downsample: whether downsampling is applied or not
    :type downsample: bool
    :return: the flattened images, tupel holding the image size
    """
    rp = RecurrencePlots()
    if downsample:
        size_flat = img_size * img_size
    else:
        size_flat = seq_length * seq_length
    pics = np.zeros([dataset.shape[0], n_channels * size_flat])  # initialize array
    for ch in range(n_channels):  # loop over all channels
        pics_ch = rp.fit_transform(dataset[:, ch * seq_length:(ch + 1) * seq_length])   # calculate recurrence plot
        pics_ch = np.moveaxis(pics_ch, 0, 2)  # [samples, heigth, width] --> [heigth,width,samples]
        if downsample:
            pics_ch = transform.resize(pics_ch, [img_size, img_size], mode='constant', anti_aliasing=0.15,
                                       preserve_range=True)     # rescale (downsample)
        else:
            img_size = seq_length
        pics_ch = np.reshape(pics_ch, [size_flat, -1])  # [signal, samples]
        pics_ch = np.moveaxis(pics_ch, 0, 1)  # swap axes --> [samples, signal]
        pics[:, ch * size_flat:(ch + 1) * size_flat] = pics_ch
    return pics, [img_size, img_size]
Пример #3
0
def transform_ECG(x, method):
    # transform ECG sequence(s) to binary image(s)
    if method == 'gasf':
        gasf = GASF(image_size=x.shape[1] // 2, overlapping=False, scale=-1)
        x = gasf.fit_transform(x)
        # print('applying GASF')
    elif method == 'mtf':
        mtf = MTF(image_size=x.shape[1], n_bins=4, quantiles='empirical', overlapping=False)
        x = mtf.fit_transform(x)
        # print('applying MTF')
    elif method == 'rp':
        rp = RecurrencePlots(dimension=1, epsilon='percentage_points', percentage=10)
        x = rp.fit_transform(x)
        # print('applying RP')
    else:
        raise ValueError("Invalid method: " + str(method))

    return x
Пример #4
0
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pyts.image import GASF, GADF, RecurrencePlots, MTF

sig = pd.read_csv('sample_1.csv').iloc[0:10000, 4:7]

n_samples, n_features = 100, 1

rng = np.random.RandomState(41)

X = rng.randn(n_samples, n_features)

# Recurrence plot transformation
rp = RecurrencePlots(dimension=1,
                     epsilon='percentage_points',
                     percentage=30)

X_rp = rp.fit_transform(X)

plt.figure(figsize=(8, 8))
plt.imshow(X_rp[0], cmap='binary', origin='lower')
plt.show()



# MTF transformation
image_size = 1
mtf = MTF(image_size)
X_mtf = mtf.fit_transform(sig)
Пример #5
0
    y, sr = librosa.load(path)
    time = np.arange(0,
                     len(y) / sr, 1 / sr)
    total_data_train.append(y)
    total_data_time_train.append(time)

    if len(y) > max_size:
        max_size = len(y)

    if path.find('undir') < 0:
        data_out_train.append('Directed')
    else:
        data_out_train.append('Undirected')

X_train = np.empty([len(total_data_train), max_size])
rp = RecurrencePlots(dimension=1, epsilon='percentage_points', percentage=30)

folder = 'F:\data_for_avishek\LSTMGeneric2\RP_Images2\Train\\'
#folderD = 'F:\data_for_avishek\LSTMGeneric2\RP_Images2\Train\Directed\\'
#folderU = r'F:\data_for_avishek\LSTMGeneric2\RP_Images2\Train\Undirected\\'

#num = 78;

#fig = plt.figure(frameon=False)
#ax = plt.gca()
#im = ax.imshow(data_list[0],...)
#X_train[idx,:len(val)] = val
#X_rp = rp.fit_transform(val.reshape(1,len(val)))

for idx, val in enumerate(total_data_train):
    if idx < 10: