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]:
Time Arctic Antarctica
0 1990M01 12.72 3.27
1 1990M02 13.33 2.15
2 1990M03 13.44 2.71
3 1990M04 12.16 5.10
4 1990M05 10.84 7.37
In [41]:
len(df)
Out[41]:
255
In [42]:
data_points = np.array(df.Arctic.values)
data_points[:5]
Out[42]:
array([12.72, 13.33, 13.44, 12.16, 10.84])
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)
[Platform 'Intel(R) OpenCL']
Vendor: Intel(R) Corporation
Version: OpenCL 1.2 
Profile: FULL_PROFILE
Extensions: cl_intel_dx9_media_sharing cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_d3d11_sharing cl_khr_depth_images cl_khr_dx9_media_sharing cl_khr_gl_sharing cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_icd cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_spir


[Device 'Intel(R) HD Graphics 4400']
Vendor: Intel(R) Corporation
Type: 4
Version: OpenCL 1.2 
Profile: FULL_PROFILE
Max Clock Frequency: 1000
Global Mem Size: 1708759450
Address Bits: 64
Max Compute Units: 20
Max Work Group Size: 512
Max Work Item Dimensions: 3
Max Work Item Sizes: [512, 512, 512]
Local Mem Size: 65536
Max Mem Alloc Size: 427189862
Extensions: cl_intel_accelerator cl_intel_advanced_motion_estimation cl_intel_ctz cl_intel_d3d11_nv12_media_sharing cl_intel_dx9_media_sharing cl_intel_motion_estimation cl_intel_simultaneous_sharing cl_intel_subgroups cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_d3d10_sharing cl_khr_d3d11_sharing cl_khr_depth_images cl_khr_dx9_media_sharing cl_khr_gl_depth_images cl_khr_gl_event cl_khr_gl_msaa_sharing cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_gl_sharing cl_khr_icd cl_khr_image2d_from_buffer cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_spir 


RQA Result:
===========

Minimum diagonal line length (L_min): 2
Minimum vertical line length (V_min): 2
Minimum white vertical line length (W_min): 2

Recurrence rate (RR): 0.038916
Determinism (DET): 0.880250
Average diagonal line length (L): 6.042945
Longest diagonal line length (L_max): 33
Divergence (DIV): 0.030303
Entropy diagonal lines (L_entr): 2.359358
Laminarity (LAM): 0.012846
Trapping time (TT): 2.000000
Longest vertical line length (V_max): 2
Entropy vertical lines (V_entr): 0.000000
Average white vertical line length (W): 22.886863
Longest white vertical line length (W_max): 235
Longest white vertical line length inverse (W_div): 0.004255
Entropy white vertical lines (W_entr): 2.001658

Ratio determinism / recurrence rate (DET/RR): 22.619003
Ratio laminarity / determinism (LAM/DET): 0.014594

C:\Users\Devin\AppData\Local\Programs\Python\Python37\lib\site-packages\pyopencl\__init__.py:231: CompilerWarning: Built kernel retrieved from cache. Original from-source build had warnings:
Build on <pyopencl.Device 'Intel(R) HD Graphics 4400' on 'Intel(R) OpenCL' at 0x1ad625990b0> succeeded, but said:

fcl build 1 succeeded.
fcl build 2 succeeded.
bcl build succeeded.

  warn(text, CompilerWarning)
In [44]:
computation = RPComputation.create(settings)
result = computation.run()
ImageGenerator.save_recurrence_plot(result.recurrence_matrix_reverse, 'recurrence_plot.png')
C:\Users\Devin\AppData\Local\Programs\Python\Python37\lib\site-packages\pyopencl\__init__.py:235: CompilerWarning: Non-empty compiler output encountered. Set the environment variable PYOPENCL_COMPILER_OUTPUT=1 to see more.
  "to see more.", CompilerWarning)
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]:
array([3.27, 2.15, 2.71, 5.1 , 7.37])
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')
[Platform 'Intel(R) OpenCL']
Vendor: Intel(R) Corporation
Version: OpenCL 1.2 
Profile: FULL_PROFILE
Extensions: cl_intel_dx9_media_sharing cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_d3d11_sharing cl_khr_depth_images cl_khr_dx9_media_sharing cl_khr_gl_sharing cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_icd cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_spir


[Device 'Intel(R) HD Graphics 4400']
Vendor: Intel(R) Corporation
Type: 4
Version: OpenCL 1.2 
Profile: FULL_PROFILE
Max Clock Frequency: 1000
Global Mem Size: 1708759450
Address Bits: 64
Max Compute Units: 20
Max Work Group Size: 512
Max Work Item Dimensions: 3
Max Work Item Sizes: [512, 512, 512]
Local Mem Size: 65536
Max Mem Alloc Size: 427189862
Extensions: cl_intel_accelerator cl_intel_advanced_motion_estimation cl_intel_ctz cl_intel_d3d11_nv12_media_sharing cl_intel_dx9_media_sharing cl_intel_motion_estimation cl_intel_simultaneous_sharing cl_intel_subgroups cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_d3d10_sharing cl_khr_d3d11_sharing cl_khr_depth_images cl_khr_dx9_media_sharing cl_khr_gl_depth_images cl_khr_gl_event cl_khr_gl_msaa_sharing cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_gl_sharing cl_khr_icd cl_khr_image2d_from_buffer cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_spir 


C:\Users\Devin\AppData\Local\Programs\Python\Python37\lib\site-packages\pyopencl\__init__.py:231: CompilerWarning: Built kernel retrieved from cache. Original from-source build had warnings:
Build on <pyopencl.Device 'Intel(R) HD Graphics 4400' on 'Intel(R) OpenCL' at 0x1ad625990b0> succeeded, but said:

fcl build 1 succeeded.
fcl build 2 succeeded.
bcl build succeeded.

  warn(text, CompilerWarning)
C:\Users\Devin\AppData\Local\Programs\Python\Python37\lib\site-packages\pyopencl\__init__.py:235: CompilerWarning: Non-empty compiler output encountered. Set the environment variable PYOPENCL_COMPILER_OUTPUT=1 to see more.
  "to see more.", CompilerWarning)
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]:
<matplotlib.image.AxesImage at 0x1ad73043be0>

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]:
t x y z sub
0 0.0 1502 2215 2153 1
1 1.0 1667 2072 2047 1
2 2.0 1611 1957 1906 1
3 3.0 1601 1939 1831 1
4 4.0 1643 1965 1879 1
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]:
<matplotlib.collections.LineCollection at 0x1ad732dcfd0>
In [51]:
data_points = df.x.values
data_points
Out[51]:
array([1502, 1667, 1611, ..., 1929, 1926, 1922], dtype=int64)
In [52]:
len(data_points)
Out[52]:
162501
In [53]:
df.loc[50000:53000].x.plot(figsize=(24,6), ylim=(1600,2200))
Out[53]:
<matplotlib.axes._subplots.AxesSubplot at 0x1ad730896d8>
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)
C:\Users\Devin\AppData\Local\Programs\Python\Python37\lib\site-packages\pyopencl\__init__.py:235: CompilerWarning: Non-empty compiler output encountered. Set the environment variable PYOPENCL_COMPILER_OUTPUT=1 to see more.
  "to see more.", CompilerWarning)
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]:
<matplotlib.axes._subplots.AxesSubplot at 0x1ad7384d748>
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)
C:\Users\Devin\AppData\Local\Programs\Python\Python37\lib\site-packages\pyopencl\__init__.py:235: CompilerWarning: Non-empty compiler output encountered. Set the environment variable PYOPENCL_COMPILER_OUTPUT=1 to see more.
  "to see more.", CompilerWarning)

CRQA

In [130]:
df.loc[30000:70000].plot(figsize=(24,6), ylim=(1500,2750))
Out[130]:
<matplotlib.axes._subplots.AxesSubplot at 0x1ad6eeff7b8>
In [131]:
df.loc[50000:52000].plot(figsize=(24,6), ylim=(1500,2750))
Out[131]:
<matplotlib.axes._subplots.AxesSubplot at 0x1ad0c2df278>
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)
C:\Users\Devin\AppData\Local\Programs\Python\Python37\lib\site-packages\pyopencl\__init__.py:235: CompilerWarning: Non-empty compiler output encountered. Set the environment variable PYOPENCL_COMPILER_OUTPUT=1 to see more.
  "to see more.", CompilerWarning)

"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]:
[<matplotlib.lines.Line2D at 0x1ad0a2deac8>]
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)
C:\Users\Devin\AppData\Local\Programs\Python\Python37\lib\site-packages\pyopencl\__init__.py:235: CompilerWarning: Non-empty compiler output encountered. Set the environment variable PYOPENCL_COMPILER_OUTPUT=1 to see more.
  "to see more.", CompilerWarning)
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: