Example #1
0
def getPAA(y, window_length):
    """Getter function for the PAA representation"""
    transformer = PiecewiseAggregateApproximation(window_size=window_length)
    paa = transformer.transform(y.reshape(1, -1))
    paaX = []
    linpaa = []
    paa_graph = []
    paa_graphX = []
    for i in range(len(paa[0])):
        if i == 0:
            paa_graph += [paa[0][i]]
            paa_graph += [paa[0][i + 1]]
            curX = i * window_length
            paa_graphX += [curX]
            paa_graphX += [curX + window_length]
        elif i != len(paa[0]) - 1:
            paa_graph += [paa[0][i + 1]]
            paa_graph += [paa[0][i + 1]]
            curX = i * window_length
            paa_graphX += [curX]
            paa_graphX += [curX + window_length]
        paaX += [i * window_length]
    for i in range(len(paa[0])):
        for j in range(window_length):
            linpaa += [paa[0][i]]
    return paa_graphX, paa_graph, linpaa[:len(y)]
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
    def _paa(self):
        # Todo: rework docstring
        """
        Takes a Numpy array (ndarray) and apply the Piecewise Aggregate Approximation
        algorithm (PAA) on it.
        This is a wrapper around the PiecewiseAggregateApproximation() class from the pyts
        package.
        :return: nothing, all objects are stored internally in the class.
        """
        paa = PiecewiseAggregateApproximation(window_size=self.window_size)

        self.paa_data = paa.fit_transform(self.data)
Example #4
0
def test_actual_results(params, arr_desired):
    """Test that the actual results are the expected ones."""
    arr_actual = PiecewiseAggregateApproximation(**params).fit_transform(X)
    np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)
Example #5
0
def test_parameter_check(params, error, err_msg):
    """Test parameter validation."""
    paa = PiecewiseAggregateApproximation(**params)
    with pytest.raises(error, match=re.escape(err_msg)):
        paa.transform(X)

#%%


train_list= [train_list[i].reset_index(drop=True)for i in range(len(train_list))]
val_list= [val_list[i].reset_index(drop=True) for i in range(len(val_list))]

train_list= [train_list[i].to_numpy() for i in range(len(train_list))]
val_list= [val_list[i].to_numpy() for i in range(len(val_list))]

train_list= [train_list[i].reshape(1,-1)for i in range(len(train_list))]
val_list= [val_list[i].reshape(1,-1) for i in range(len(val_list))]


transformer = PiecewiseAggregateApproximation(window_size=4)


train_list= [transformer.transform(train_list[i]) for i in range(len(train_list))]
val_list = [transformer.transform(val_list[i]) for i in range(len(val_list))]


train_list= [train_list[i].transpose()for i in range(len(train_list))]
val_list= [val_list[i].transpose() for i in range(len(val_list))]




#%%

def feature_scaling(ts):
Example #7
0
# License: BSD-3-Clause

import numpy as np
import matplotlib.pyplot as plt
from pyts.approximation import PiecewiseAggregateApproximation

# Parameters
n_samples, n_timestamps = 100, 48

# Toy dataset
rng = np.random.RandomState(41)
X = rng.randn(n_samples, n_timestamps)

# PAA transformation
window_size = 6
paa = PiecewiseAggregateApproximation(window_size=window_size)
X_paa = paa.transform(X)

# Show the results for the first time series
plt.figure(figsize=(6, 4))
plt.plot(X[0], 'o--', ms=4, label='Original')
plt.plot(np.arange(window_size // 2,
                   n_timestamps + window_size // 2,
                   window_size), X_paa[0], 'o--', ms=4, label='PAA')
plt.vlines(np.arange(0, n_timestamps, window_size) - 0.5,
           X[0].min(), X[0].max(), color='g', linestyles='--', linewidth=0.5)
plt.legend(loc='best', fontsize=10)
plt.xlabel('Time', fontsize=12)
plt.title('Piecewise Aggregate Approximation', fontsize=16)
plt.show()

# set the window size and alphabet size in the following variables
window = 24
alphabets = 7

input_path = "../path/of/input/file..."

mindist_with_target = []

df = pd.read_csv(input_path)
column_names = list(df.columns)
df = df.T
N = df.shape[1]
df_to_numpy = df.values
ppa_transformer = PiecewiseAggregateApproximation(window_size=window)
paa_output = ppa_transformer.transform(df_to_numpy)

sax_transformer = SymbolicAggregateApproximation(n_bins=alphabets,
                                                 strategy='normal')
sax_output = sax_transformer.transform(paa_output)
target = sax_output[-1]

sax = SAX_trans(window, alphabets)

for i in range(len(sax_output) - 1):
    mindist = sax.compare_strings(sax_output[i], target)
    mindist_with_target.append(mindist)

MinDistances = np.asarray(mindist_with_target)
ranking = np.argsort(MinDistances)
Example #9
0
from pyts.approximation import PiecewiseAggregateApproximation
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

batch = 1
bolt = 20

file = '~/kuka-ml-threading/dataset/dataset_new_iros21/new_dataset_with_linear_error/data_insertion/data_insertion_batch_'+\
                   str(batch).zfill(4) +'_bolt_' + str(bolt).zfill(2)

data = pd.read_csv(file + '.csv')

wrench = ['fx', 'fy', 'fz', 'mx', 'my', 'mz']

window = 15

paa = PiecewiseAggregateApproximation(window_size=window)

t, fz = paa.transform(X=data[['time', 'fz']].values.T)

plt.plot(data['time'], data['fz'])
plt.plot(t, fz)
plt.show()
Example #10
0
def PAA(timeseries, window_size):
    transformer = PiecewiseAggregateApproximation(window_size=window_size)
    return transformer.transform(np.array([timeseries]))[0]
Example #11
0
timestamp_for_plot = pd.to_datetime(time)

# In[97]:

# finding the frequency / daily case
timestamp_in_date = pd.to_datetime(time).date
counting_the_date = np.unique(timestamp_in_date, return_counts=True)
freq = np.max(counting_the_date[1])

# In[98]:

# data compression
n = 10
w = np.int(freq / n)

paa = PiecewiseAggregateApproximation(window_size=n)
data_compressed = paa.transform(data.reshape(1, -1))[0]

# In[99]:

true_sub_id = np.array([0])
for i in counting_the_date[1]:
    true_sub_id = np.append(true_sub_id, np.max(true_sub_id) + i)

sub_id = (true_sub_id / n).astype(int)

# In[100]:

# trend removal
res = seasonal_decompose(data_compressed,
                         model='additive',