Example #1
0
def transform_to_2D(method, x_train, x_test):
    if method == 'gasf':
        gasf = GASF(image_size=x_train.shape[1] // 2,
                    overlapping=False,
                    scale=-1)
        x_tr = gasf.fit_transform(x_train)
        x_te = gasf.fit_transform(x_test)
        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)
        x_te = mtf.fit_transform(x_test)
        print('applying MTF')
    elif method == 'rp':
        rp = RecurrencePlots(dimension=3,
                             epsilon='percentage_points',
                             percentage=10)
        x_tr = rp.fit_transform(x_train)
        x_te = rp.fit_transform(x_test)
        print('applying RP')
    else:
        print('wrong method')
        x_tr = []
        x_te = []

    return x_tr, x_te
Example #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]
Example #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
Example #4
0
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)

# Show the results for the first time series
plt.figure(figsize=(8, 8))
plt.imshow(X_mtf[0], cmap='rainbow', origin='lower')
Example #5
0
#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:
        print(idx)
        #        X_train[idx,:len(val)] = val
        X_rp = rp.fit_transform(val.reshape(1, len(val)))
        plt.figure(frameon=False)
        fig, ax = plt.subplots(1)
        plt.imshow(X_rp[0], cmap='binary', origin='lower')
        plt.axis('off')
        ax.get_xaxis().set_visible(
            False)  # this removes the ticks and numbers for x axis
        ax.get_yaxis().set_visible(
            False)  # this removes the ticks and numbers for y axis
        fig.set_size_inches(2, 2)
        plt.savefig("{}/{}.png".format(folder + data_out_train[idx],
                                       file_list[idx]),
                    bbox_inches='tight',
                    pad_inches=0,
                    dpi=128)
        #        if data_out_train[idx]=='Directed':