Skip to content

Commit c33d931

Browse files
committed
Refactor the structure of code
1 parent d17dc67 commit c33d931

File tree

7 files changed

+127
-97
lines changed

7 files changed

+127
-97
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The Academic License
22

3-
Copyright (c) [2020] [Saeed Hasani Borzadaran]
3+
Copyright (c) [2020] [Saeed Hasani]
44

55
This project includes academic-research code and documents under development.
66
You would be a fool to run any of the code.

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
# Recurrence Plot
22
[Recurrence Plot](https://en.wikipedia.org/wiki/Recurrence_plot) – A recurrence plot (RP) is an advanced technique of **nonlinear** data analysis. It is a visualisation (or a graph) of a square matrix, in which the matrix elements correspond to those times at which a state of a dynamical system recurs (columns and rows correspond then to a certain pair of times).
33

4-
# Result
4+
## Result
55
![](results/1D_to_2D.jpg)
66

7-
# Usage
7+
## Usage
88
**1. Install requirements:**
99

10+
Ensure you have Python 3 installed. Install the required dependencies using:
11+
12+
1013
pip install -r requirements.txt
1114

12-
**2. Run `main.py` script:**
15+
**2. Run `example.py` script:**
16+
17+
python3 example.py # or python example.py
18+
19+
This will:
20+
21+
- Generate a random signal.
22+
- Smooth the signal using a moving average filter.
23+
- Compute and visualize the recurrence plot.
24+
- Save the resulting plot as results/1D_to_2D.jpg.
25+
26+
----
27+
## How to Use the recurrence Package
28+
29+
If you want to use the recurrence package in your own projects:
30+
31+
1) **Import the Required Modules:**
32+
33+
For recurrence plot functions, import from recurrence.plotting.
34+
For signal processing utilities, import from recurrence.convolve.
35+
36+
Example:
37+
38+
```python
39+
from recurrence.plotting import setup_plot, save_plot
40+
from recurrence.convolve import calculate_convolve
41+
```
1342

14-
python3 main.py
43+
2) **Process Your Signal:**
44+
45+
Use `calculate_convolve` to smooth your input signal, then use the `setup_plot` and `save_plot` functions to generate and save recurrence plots.

example.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
from recurrence.plotting import setup_plot, save_plot
4+
from recurrence.convolve import calculate_convolve
5+
6+
if __name__ == "__main__":
7+
# Example usage
8+
fig = plt.figure(figsize=(8, 2))
9+
10+
# Configuration
11+
row, col = 1, 2
12+
size = f"{row}{col}"
13+
14+
# Generate raw signal
15+
raw_signal = np.random.uniform(-1, 1, 50)
16+
17+
# Process the signal
18+
convolved_signal = calculate_convolve(raw_signal)
19+
20+
# Plot signals and recurrence plot
21+
setup_plot(
22+
signal=convolved_signal,
23+
size=size,
24+
cell=1,
25+
signal_name='Input Signal',
26+
image_name='2D Image of Signal'
27+
)
28+
29+
# Save the plot
30+
save_plot(filepath='results/1D_to_2D.jpg')

main.py

Lines changed: 0 additions & 92 deletions
This file was deleted.

recurrence/__init__.py

Whitespace-only changes.
File renamed without changes.

recurrence/plotting.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
from scipy.spatial.distance import pdist, squareform
4+
5+
# ----------------- Plot config --------------------------------------- #
6+
SMALL = 8
7+
MEDIUM = 10
8+
BIGGER = 11
9+
10+
plt.rc('font', size=SMALL) # controls default text sizes
11+
plt.rc('axes', titlesize=SMALL) # font size of the axes title
12+
plt.rc('axes', labelsize=MEDIUM) # font size of the x and y labels
13+
plt.rc('xtick', labelsize=SMALL) # font size of the tick labels
14+
plt.rc('ytick', labelsize=SMALL) # font size of the tick labels
15+
plt.rc('legend', fontsize=SMALL) # legend font size
16+
plt.rc('figure', titlesize=BIGGER) # font size of the figure title
17+
18+
plt.rcParams["font.family"] = "Times", "Times New Roman", "serif"
19+
20+
# --------------------------------------------------------------------- #
21+
22+
def save_plot(filepath='results/1D_to_2D.jpg'):
23+
"""Adjust and save the plot."""
24+
left = 0.2
25+
right = 0.9
26+
bottom = 0.1
27+
top = 0.9
28+
wspace = 0.2
29+
hspace = 0.1
30+
31+
plt.subplots_adjust(left, bottom, right, top, wspace, hspace)
32+
plt.savefig(filepath, bbox_inches='tight')
33+
34+
def recurrence_plot(signal, eps=0.10, steps=3):
35+
"""Generate a recurrence plot from a 1D signal."""
36+
_2d_array = signal[:, None]
37+
distance = pdist(_2d_array)
38+
distance = np.floor(distance / eps)
39+
distance[distance > steps] = steps
40+
return squareform(distance)
41+
42+
def subplot(x, size, cell, is_signal=True, title=None, grid=True):
43+
"""Create a subplot for the signal or image."""
44+
ax = plt.subplot(int(f"{size}{cell}"))
45+
ax.grid(color='gray', linestyle='dotted', linewidth=0.5)
46+
ax.spines['top'].set_visible(False)
47+
ax.spines['right'].set_visible(False)
48+
49+
if is_signal:
50+
plt.plot(x, 'm', linewidth=1)
51+
else:
52+
plt.imshow(x)
53+
54+
plt.title(title)
55+
plt.grid(grid)
56+
57+
def setup_plot(signal, size, cell, signal_name='Raw Signal', image_name='2D Image'):
58+
"""Set up and display the plot for the signal and its recurrence plot."""
59+
subplot(signal, size=size, cell=cell, is_signal=True, title=signal_name)
60+
rp = recurrence_plot(signal)
61+
subplot(rp, size=size, cell=cell + 1, is_signal=False, title=image_name)

0 commit comments

Comments
 (0)