Skip to content

Commit aa276ab

Browse files
committed
refactor: create multipurpose RecurrencePlot
1 parent 4227a61 commit aa276ab

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

main.py

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,58 @@
11
from __future__ import division, print_function
22
import numpy as np
3-
import pylab
3+
import pylab as plt
44
from scipy.spatial.distance import pdist, squareform
55

6+
from utils import calculate_convolve
67

7-
def calculate_convolve(signal, n=5):
8-
return np.convolve(signal, np.ones((n,)) / n, mode='valid')
98

9+
# ----------------- Plot config -------------------------------------------
10+
SMALL = 8
11+
MEDIUM = 10
12+
BIGGER = 11
1013

11-
def recurrence_plot(signal, eps=0.10, steps=3):
12-
_2d_array = signal[:, None]
13-
# Pairwise distances
14-
distance = pdist(_2d_array)
15-
print(distance)
16-
distance = np.floor(distance / eps)
17-
print(distance)
18-
distance[distance > steps] = steps
19-
print(distance.shape)
20-
return squareform(distance)
14+
plt.rc('font', size=SMALL) # controls default text sizes
15+
plt.rc('axes', titlesize=SMALL) # font size of the axes title
16+
plt.rc('axes', labelsize=MEDIUM) # font size of the x and y labels
17+
plt.rc('xtick', labelsize=SMALL) # font size of the tick labels
18+
plt.rc('ytick', labelsize=SMALL) # font size of the tick labels
19+
plt.rc('legend', fontsize=SMALL) # legend font size
20+
plt.rc('figure', titlesize=BIGGER) # font size of the figure title
21+
# -------------------------------------------------------------------------
2122

2223

23-
if __name__ == "__main__":
24-
raw_signal = np.random.uniform(-1, 1, 1000)
25-
convolved_signal = calculate_convolve(raw_signal)
24+
class RecurrencePlot(object):
25+
def __init__(self, signal, row=2, col=2):
26+
self.signal = signal
27+
self.size = '%d%d' % (row, col)
28+
29+
def recurrence_plot(self, eps=0.10, steps=3):
30+
_2d_array = self.signal[:, None]
31+
32+
# Pairwise distances
33+
distance = pdist(_2d_array)
34+
distance = np.floor(distance / eps)
35+
distance[distance > steps] = steps
36+
return squareform(distance)
2637

27-
pylab.title("Raw Signal")
28-
pylab.subplot(211)
29-
pylab.plot(convolved_signal)
38+
def subplot(self, x, is_signal=True, index=1, title=None, grid=True):
39+
plt.subplot(int('%s%d' % (self.size, index)))
40+
plt.plot(x) if is_signal else plt.imshow(x)
41+
plt.title(title)
42+
plt.grid(grid)
3043

31-
# pylab.title("2D Image")
32-
pylab.subplot(212)
44+
def save(self):
45+
# plot with various axes scales
46+
plt.figure()
47+
self.subplot(self.signal, index=1, title='Raw Signal')
48+
self.subplot(self.recurrence_plot(), is_signal=False, index=2, title='2D Image')
49+
self.subplot(self.signal, index=3, title='Raw Signal')
50+
self.subplot(self.recurrence_plot(), is_signal=False, index=4, title='2D Image')
51+
plt.savefig('result.jpg')
3352

34-
pylab.imshow(recurrence_plot(convolved_signal))
35-
pylab.savefig('result.jpg')
53+
54+
if __name__ == "__main__":
55+
raw_signal = np.random.uniform(-1, 1, 500)
56+
convolved_signal = calculate_convolve(raw_signal)
57+
rp = RecurrencePlot(convolved_signal)
58+
rp.save()

result.jpg

27.5 KB
Loading

0 commit comments

Comments
 (0)