Esempio n. 1
0
def encode_dataset(batch_size,
                   downscale_factor,
                   dataset,
                   pooling_function,
                   number_of_bins=15):
    """ 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

    mtf = MarkovTransitionField(image_size=1.,
                                n_bins=number_of_bins,
                                strategy='uniform',
                                overlapping=False)

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

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

        else:
            X_mtf = mtf.transform(X[batches[p - 1]:batches[p], :])

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

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

    return X_mtf_red
Esempio n. 2
0
def test_parameter_check(params, error, err_msg):
    """Test parameter validation."""
    mtf = MarkovTransitionField(**params)
    with pytest.raises(error, match=re.escape(err_msg)):
        mtf.transform(X)
Esempio n. 3
0
    divider = make_axes_locatable(ax2)
    cax = divider.append_axes("right", size="5%", pad=0.2)
    plt.colorbar(cax=cax)

    ax3 = plt.subplot(133)
    plt.imshow(patchimage[k])
    plt.title('patch average')
    divider = make_axes_locatable(ax3)
    cax = divider.append_axes("right", size="5%", pad=0.2)
    plt.colorbar(cax=cax)
    plt.show()

    ## call API
    X, _, _, _ = load_gunpoint(return_X_y=True)
    mtf = MarkovTransitionField()
    fullimage = mtf.transform(X)

    # downscale MTF of the time series (without paa) through mean operation
    batch = int(len(X[0]) / s)
    patch = []
    for p in range(s):
        for q in range(s):
            patch.append(
                np.mean(fullimage[0][p * batch:(p + 1) * batch,
                                     q * batch:(q + 1) * batch]))
    # reshape
    patchimage = np.array(patch).reshape(s, s)

    plt.figure()
    plt.suptitle('gunpoint_index_' + str(k))
    ax1 = plt.subplot(121)
def main():
    # sine
    sin = np.vectorize(sin_f) 
    series = sin(samples)

    # cos
    #cos = np.vectorize(cos_f) 
    #series = cos(samples)

    # tan
    #tan = np.vectorize(tan_f) 
    #series = tan(samples)    

    # normal
    #series = np.random.normal(loc=6, scale=0.1, size=(N,1))
    # binomial
    #series = np.random.binomial(n=10, p=0.7, size=(N,1))
    # Poisson
    #series = np.random.poisson(lam=2, size=(N,1))
    # Uniform
    #series = np.random.uniform(size=(N, 1))
    # Logistic
    #series = np.random.logistic(loc=1, scale=2, size=(N, 1))
    # Multi
    #series = np.random.multinomial(n=6, pvals=[1/6, 1/6, 1/6, 1/6, 1/6, 1/6])
    # Exponential
    #series = np.random.exponential(scale=2, size=(N, 1))
    # Chi
    #series = np.random.chisquare(df=2, size=(N, 1))
    # Rayleigh
    #series = np.random.rayleigh(scale=2, size=(N, 1))
    # Pareto
    #series = np.random.pareto(a=2, size=(N, 1))  
    # Zipf
    #series = np.random.zipf(a=2, size=(N, 1))
    
    # rotate data and aggregate
    X = list()
    s_list = list(series)
    d_list = deque(s_list)
    i = 0
    for _ in range(len(series)):
        d_list.rotate(i)
        X.append(list(d_list))
        i = i + 1
    
    # MarkovTransitionField plot transformation
    X_num = np.squeeze(np.array(X))
    transformer = MarkovTransitionField()
    X_new = transformer.transform(X_num)      
    
    plt.figure(1)
    plt.plot(series)
    plt.show()
    
    plt.figure(2)
    for idx, _ in enumerate(series):
        plt.clf()
        plt.subplot(211)
        plt.imshow(X_new[idx], cmap='gist_gray', origin='lower')
        plt.title('MarkovTransitionField Plot ' + str(idx), fontsize=16)
        plt.tight_layout()    
        plt.subplot(212)
        plt.plot(series)
        plt.scatter(idx,series[idx])
        plt.tight_layout()  
        plt.show()
        time.sleep(0.1)
    
    plt.figure(3)
    plt.hist(series)
    plt.show()