johannfaouzi / pyts

A Python package for time series classification

Home Page:https://pyts.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recurrence plot example

toncho11 opened this issue · comments

Hi,

Please add the following code to the documentation. For me it was not clear that the input is multiple time series in the form of 2D array. I thought it is a plot image of the function. It turned up to be completely false. It is indeed different epochs/time series for each line of this 2D ... let's call it "input". Below is an example of a single time series (the output of a sin with 126 values) and the resulting single reccurence plot.

import numpy as np
import matplotlib.pyplot as plt
import datetime
import sys


from pyts.image import RecurrencePlot
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas


sin_func = np.arange(0,4*np.pi,0.1)   # start,stop,step
y = np.sin(sin_func)

X = np.reshape(y,(1, y.size)) #1 because we have only one input

# Recurrence plot transformation
rp = RecurrencePlot(threshold='point', percentage=20)
X_rp = rp.fit_transform(X)

plt.subplot(211)
plt.plot(y)
plt.subplot(212)
plt.imshow(X_rp[0],  cmap='binary', origin='lower')

Hi,

Thank you for your suggestion. Do you think that the following illustration is more clear? I could add this example to show a single recurrence plot and update the existing example to display several recurrence plots in order to make it clear that it is a data set of univariate time series that are independently transformed into recurrence plots.

recurrence plot

Here is the following code:

import numpy as np
import matplotlib.pyplot as plt
from pyts.image import RecurrencePlot


# Create a toy time series using the sine function
time_points = np.arange(0, 4 * np.pi, 0.01)
x = np.sin(time_points)
X = np.array([x])

# Recurrence plot transformation
rp = RecurrencePlot(threshold=np.pi/18)
X_rp = rp.fit_transform(X)

# Plot the time series and its recurrence plot
fig = plt.figure(figsize=(6, 6))

gs = fig.add_gridspec(2, 2,  width_ratios=(2, 7), height_ratios=(2, 7),
                      left=0.1, right=0.9, bottom=0.1, top=0.9,
                      wspace=0.05, hspace=0.05)

# Define the ticks and their labels for both axes
time_ticks = np.linspace(0, 4 * np.pi, 9)
time_ticklabels = [r'$0$', r'$\frac{\pi}{2}$', r'$\pi$',
                   r'$\frac{3\pi}{2}$', r'$2\pi$', r'$\frac{5\pi}{2}$',
                   r'$3\pi$', r'$\frac{7\pi}{2}$', r'$4\pi$']
value_ticks = [-1, 0, 1]
reversed_value_ticks = value_ticks[::-1]

# Plot the time series on the left with inverted axes
ax_left = fig.add_subplot(gs[1, 0])
ax_left.plot(x, time_points)
ax_left.set_xticks(reversed_value_ticks)
ax_left.set_xticklabels(reversed_value_ticks, rotation=90)
ax_left.set_yticks(time_ticks)
ax_left.set_yticklabels(time_ticklabels, rotation=90)
ax_left.invert_xaxis()

# Plot the time series on the top
ax_top = fig.add_subplot(gs[0, 1])
ax_top.plot(time_points, x)
ax_top.set_xticks(time_ticks)
ax_top.set_xticklabels(time_ticklabels)
ax_top.set_yticks(value_ticks)
ax_top.set_yticklabels(value_ticks)
ax_top.xaxis.tick_top()

# Plot the recurrence plot on the bottom right
ax = fig.add_subplot(gs[1, 1])
ax.imshow(X_rp[0], cmap='binary', origin='lower', extent=[0, 4 * np.pi, 0, 4 * np.pi])
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Looks very good! :)
Thanks!!!
In the comments you can set "Here X contains a single time series (sin over a specified range) and that is why there is a single output recurrence plot X_rp[0]".