def plot_recurrence_plots(x, dimension=1, epsilon=None, percentage=10, cmap='Greys', output_file=None): # Check parameters if not isinstance(dimension, int): raise TypeError("'dimension' must be an integer.") if dimension <= 0: raise ValueError("'dimension' must be greater than or equal to 1.") if (epsilon is not None) and \ (epsilon not in ['percentage_points', 'percentage_distance']) and \ (not isinstance(epsilon, (int, float))): raise TypeError("'epsilon' must be either None, 'percentage_points', " "'percentage_distance', a float or an integer.") if (isinstance(epsilon, (int, float))) and (epsilon < 0): raise ValueError("if 'epsilon' is a float or an integer," "'epsilon' must be greater than or equal to 0.") if not isinstance(percentage, (int, float)): raise TypeError("'percentage' must be a float or an integer.") if (percentage < 0) or (percentage > 100): raise ValueError("'percentage' must be between 0 and 100.") rp = recurrence_plot(x, dimension, epsilon, percentage) plt.imshow(rp, origin='lower', cmap=cmap) plt.axis('off') if output_file is not None: plt.savefig(output_file) # Show plot plt.show()
def test_recurrence_plot(): """Testing 'recurrence_plot'""" # Parameter size = 9 X = np.linspace(-1, 1, size) # Test 1 arr_actual = recurrence_plot(X, dimension=1, epsilon=None, percentage=10) arr_desired = np.empty((size, size)) for i in range(size): for j in range(size): arr_desired[i, j] = abs(X[i] - X[j]) np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.) # Test 2 percentage = 50 epsilon = 'percentage_distance' arr_actual = recurrence_plot(X, dimension=1, epsilon=epsilon, percentage=percentage) arr_desired = np.empty((size, size)) for i in range(size): for j in range(size): arr_desired[i, j] = abs(X[i] - X[j]) < percentage * (X.max() - X.min()) / 100 np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.) # Test 3 percentage = 50 epsilon = 'percentage_points' arr_actual = recurrence_plot(X, dimension=1, epsilon=epsilon, percentage=percentage) arr_desired = np.empty((size, size)) for i in range(size): for j in range(size): arr_desired[i, j] = abs(X[i] - X[j]) arr_desired = arr_desired < np.percentile(arr_desired, q=50) np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)
def plot_recurrence_plots(ts, dimension=1, epsilon=None, percentage=10, cmap='Greys', output_file=None, interpolation=None): """Plot the image obtained after Recurrence plots transformation. Parameters ---------- ts : np.array, shape = [n_features] time series to plot dimension : int (default = 1) dimension of the trajectory. epsilon : None or 'percentage_points' or 'percentage_distance' or float (default = None) threshold for the minimum distance percentage : float (default = 10) percentage of black points if epsilon == 'percentage_points' or percentage of maximum distance for threshold if epsilon == 'percentage_distance'. cmap : str (default = 'Greys') color map from matplotlib.pyplot output_file : str or None (default = None) if str, save the figure. interpolation : str or None (default = None) 'interpolation' parameter for matplotlib.pyplot.imshow """ # Check parameters if not isinstance(dimension, int): raise TypeError("'dimension' must be an integer.") if dimension <= 0: raise ValueError("'dimension' must be greater than or equal to 1.") if (epsilon is not None) and \ (epsilon not in ['percentage_points', 'percentage_distance']) and \ (not isinstance(epsilon, (int, float))): raise TypeError("'epsilon' must be either None, 'percentage_points', " "'percentage_distance', a float or an integer.") if (isinstance(epsilon, (int, float))) and (epsilon < 0): raise ValueError("if 'epsilon' is a float or an integer," "'epsilon' must be greater than or equal to 0.") if not isinstance(percentage, (int, float)): raise TypeError("'percentage' must be a float or an integer.") if (percentage < 0) or (percentage > 100): raise ValueError("'percentage' must be between 0 and 100.") rp = recurrence_plot(ts, dimension, epsilon, percentage) plt.imshow(rp, origin='lower', cmap=cmap, interpolation=interpolation) plt.axis('off') if output_file is not None: plt.savefig(output_file) # Show plot plt.show()