-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfig_decode_error.py
More file actions
67 lines (57 loc) · 2.43 KB
/
fig_decode_error.py
File metadata and controls
67 lines (57 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# decode angle error with N and kappa
import numpy as np
import os
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
def encode_angle(phi, theta, kappa):
return np.exp(kappa * np.cos(phi - theta)) / np.exp(kappa)
def decode_angle(F, theta):
complex_sum = np.sum(F * np.exp(1j * theta))
return np.angle(complex_sum)
def angle_error_simulation(phi, kappas, N_list):
"""
Simulate angle error for a range of discrete point numbers N.
"""
errors = {}
for kappa in kappas:
error = []
for N in N_list:
pref_theta = np.linspace(0, 2 * np.pi, N, endpoint=False)
hat_phi = decode_angle(encode_angle(phi, pref_theta, kappa), pref_theta)
angle_error = np.abs(hat_phi - phi) % np.pi # Relative to phi
error.append(angle_error)
errors[kappa] = np.array(error)
return errors
if __name__ == "__main__":
# folder
project_folder = os.path.dirname(os.path.abspath(__file__))
data_folder = os.path.join(project_folder, 'data', 'method')
if not os.path.exists(data_folder):
os.makedirs(data_folder)
# Define extended ranges of kappa and phi
kappa_values = [0.000005, 0.1, 0.5, 1, 2, 4, 10, 50, 100] # Example kappa values to test concentration effects
phi_values = 7*np.pi/1000 # Different phi values
N_list = np.linspace(2, 82, 41) # Range of discrete point numbers
N_list = N_list.astype(int)
errors = angle_error_simulation(phi_values, kappa_values, N_list)
# Plot results for each combination of kappa and phi
plt.figure(figsize=(10, 8))
fontsize = 18
font = FontProperties()
font.set_size(fontsize)
font.set_weight('bold')
for i, (key, error) in enumerate(errors.items()):
color_value = plt.cm.viridis(i / len(kappa_values))
plt.plot(N_list, np.rad2deg(error), marker='o', color=color_value, label=f'$\kappa$ = {key}')
plt.xlabel('Number of Neuron ($N$)', fontsize=fontsize, fontweight='bold')
plt.ylabel('Encoded Angle Error (degree)', fontsize=fontsize, fontweight='bold')
plt.xticks(fontsize=fontsize-1, fontweight='bold')
plt.yticks(fontsize=fontsize-1, fontweight='bold')
plt.grid(alpha=0.2)
plt.legend(loc='upper right', prop=font)
# save
fts = {'.png', '.eps'}
for ft in fts:
filename = 'angle_error_vs_N' + ft
plt.savefig(os.path.join(data_folder, filename), dpi=600)
print('figure saved')