pyRQA sandbox
Posted on Fri 01 June 2018 in Projects
In [39]:
import pandas as pd
import numpy as np
from pyrqa.time_series import TimeSeries
from pyrqa.settings import Settings
from pyrqa.computing_type import ComputingType
from pyrqa.neighbourhood import FixedRadius
from pyrqa.metric import EuclideanMetric
from pyrqa.computation import RQAComputation
from pyrqa.computation import RPComputation
from pyrqa.image_generator import ImageGenerator
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
Get Data¶
In [40]:
df = pd.read_csv('sea_ice.csv')
df.head()
Out[40]:
In [41]:
len(df)
Out[41]:
In [42]:
data_points = np.array(df.Arctic.values)
data_points[:5]
Out[42]:
In [43]:
# data_points = [0.1, 0.5, 1.3, 0.7, 0.8, 1.4, 1.6, 1.2, 0.4, 1.1, 0.8, 0.2, 1.3]
time_series = TimeSeries(data_points,
embedding_dimension=2,
time_delay=2)
settings = Settings(time_series,
computing_type=ComputingType.Classic,
neighbourhood=FixedRadius(0.65),
similarity_measure=EuclideanMetric,
theiler_corrector=1)
computation = RQAComputation.create(settings,
verbose=True)
result = computation.run()
result.min_diagonal_line_length = 2
result.min_vertical_line_length = 2
result.min_white_vertical_line_lelngth = 2
print(result)
In [44]:
computation = RPComputation.create(settings)
result = computation.run()
ImageGenerator.save_recurrence_plot(result.recurrence_matrix_reverse, 'recurrence_plot.png')
In [45]:
img=mpimg.imread('recurrence_plot.png')
imgplot = plt.imshow(img)
plt.show()
In [46]:
data_points = np.array(df.Antarctica.values)
data_points[:5]
Out[46]:
In [47]:
time_series = TimeSeries(data_points,
embedding_dimension=2,
time_delay=2)
settings = Settings(time_series,
computing_type=ComputingType.Classic,
neighbourhood=FixedRadius(0.65),
similarity_measure=EuclideanMetric,
theiler_corrector=1)
computation = RQAComputation.create(settings,
verbose=True)
result = computation.run()
result.min_diagonal_line_length = 2
result.min_vertical_line_length = 2
result.min_white_vertical_line_lelngth = 2
computation = RPComputation.create(settings)
result = computation.run()
ImageGenerator.save_recurrence_plot(result.recurrence_matrix_reverse, 'recurrence_plot2.png')
In [48]:
img1=mpimg.imread('recurrence_plot.png')
img2=mpimg.imread('recurrence_plot2.png')
f, ax = plt.subplots(1,2, figsize=(14,7))
ax[0].imshow(img1)
ax[1].imshow(img2)
Out[48]:
Get MOAR data, and then learn on it..¶
In [49]:
df = pd.read_csv('HAR/Activity Recognition from Single Chest-Mounted Accelerometer/1.csv', names=['t','x','y','z','sub'])
df.head()
Out[49]:
In [50]:
df.plot('t','x', figsize=(18,6))
plt.vlines([39000, 65000], df.x.min(), df.x.max())
plt.vlines([50000, 53000], df.x.min(), df.x.max(), linestyles='dotted')
plt.vlines([20000, 23000], df.x.min(), df.x.max(), linestyles='dashed')
Out[50]:
In [51]:
data_points = df.x.values
data_points
Out[51]:
In [52]:
len(data_points)
Out[52]:
In [53]:
df.loc[50000:53000].x.plot(figsize=(24,6), ylim=(1600,2200))
Out[53]:
In [54]:
data_points = data_points[50000:53000]
time_series = TimeSeries(data_points,
embedding_dimension=1,
time_delay=4)
settings = Settings(time_series,
computing_type=ComputingType.Classic,
neighbourhood=FixedRadius(15),
similarity_measure=EuclideanMetric,
theiler_corrector=1)
computation = RQAComputation.create(settings, verbose=False)
result = computation.run()
result.min_diagonal_line_length = 2
result.min_vertical_line_length = 2
result.min_white_vertical_line_lelngth = 2
computation = RPComputation.create(settings)
result = computation.run()
ImageGenerator.save_recurrence_plot(result.recurrence_matrix_reverse, 'recurrence_plot3.png')
img3=mpimg.imread('recurrence_plot3.png')
plt.figure(figsize=(9,9))
imgplot = plt.imshow(img3)
In [55]:
data_points = df.x.values
data_points = data_points[20000:23000]
In [56]:
df.loc[20000:23000].x.plot(figsize=(24,6), ylim=(1600,2200))
Out[56]:
In [57]:
time_series = TimeSeries(data_points,
embedding_dimension=1,
time_delay=4)
settings = Settings(time_series,
computing_type=ComputingType.Classic,
neighbourhood=FixedRadius(15),
similarity_measure=EuclideanMetric,
theiler_corrector=1)
computation = RQAComputation.create(settings, verbose=False)
result = computation.run()
result.min_diagonal_line_length = 2
result.min_vertical_line_length = 2
result.min_white_vertical_line_lelngth = 2
computation = RPComputation.create(settings)
result = computation.run()
ImageGenerator.save_recurrence_plot(result.recurrence_matrix_reverse, 'recurrence_plot4.png')
img4=mpimg.imread('recurrence_plot4.png')
plt.figure(figsize=(9,9))
imgplot = plt.imshow(img4)
CRQA¶
In [130]:
df.loc[30000:70000].plot(figsize=(24,6), ylim=(1500,2750))
Out[130]:
In [131]:
df.loc[50000:52000].plot(figsize=(24,6), ylim=(1500,2750))
Out[131]:
In [132]:
data_points_x = df.x.values
data_points_x = data_points_x[50000:52000]
data_points_y = df.y.values
data_points_y = data_points_y[50000:52000]
data_points_z = df.z.values
data_points_z = data_points_z[50000:52000]
In [133]:
time_series_x = TimeSeries(data_points_x,
embedding_dimension=1,
time_delay=0)
time_series_y = TimeSeries(data_points_z,
embedding_dimension=1,
time_delay=0)
time_series = (time_series_x,
time_series_y)
settings = Settings(time_series,
computing_type=ComputingType.Cross,
neighbourhood=FixedRadius(100),
similarity_measure=EuclideanMetric,
theiler_corrector=1)
computation = RQAComputation.create(settings,verbose=False)
result = computation.run()
result.min_diagonal_line_length = 1
result.min_vertical_line_length = 1
result.min_white_vertical_line_lelngth = 1
# print(result)
computation = RPComputation.create(settings)
result = computation.run()
ImageGenerator.save_recurrence_plot(result.recurrence_matrix_reverse,
'cross_recurrence_plot1.png')
img5=mpimg.imread('cross_recurrence_plot1.png')
plt.figure(figsize=(9,9))
imgplot = plt.imshow(img5)
"Subtract the mean!"¶
In [139]:
data_points_x = df.x.values
data_points_x = data_points_x[50000:52000]-data_points_x[50000:52000].mean()
data_points_y = df.y.values
data_points_y = data_points_y[50000:52000]-data_points_y[50000:52000].mean()
data_points_z = df.z.values
data_points_z = data_points_z[50000:52000]-data_points_z[50000:52000].mean()
plt.figure(figsize=(24,6))
plt.plot(data_points_x)
plt.plot(data_points_y)
plt.plot(data_points_z)
Out[139]:
In [142]:
time_series_x = TimeSeries(data_points_x,
embedding_dimension=1,
time_delay=0)
time_series_y = TimeSeries(data_points_z,
embedding_dimension=1,
time_delay=0)
time_series = (time_series_x,
time_series_y)
settings = Settings(time_series,
computing_type=ComputingType.Cross,
neighbourhood=FixedRadius(100),
similarity_measure=EuclideanMetric,
theiler_corrector=1)
computation = RQAComputation.create(settings,verbose=False)
result = computation.run()
result.min_diagonal_line_length = 1
result.min_vertical_line_length = 1
result.min_white_vertical_line_lelngth = 1
# print(result)
computation = RPComputation.create(settings)
result = computation.run()
ImageGenerator.save_recurrence_plot(result.recurrence_matrix_reverse,
'cross_recurrence_plot2.png')
img6=mpimg.imread('cross_recurrence_plot2.png')
plt.figure(figsize=(9,9))
imgplot = plt.imshow(img6)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: