-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_2.py
More file actions
77 lines (64 loc) · 2.32 KB
/
Copy pathexample_2.py
File metadata and controls
77 lines (64 loc) · 2.32 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
68
69
70
71
72
73
74
75
76
77
import numpy as np
import matplotlib.pyplot as plt
from scipy.sparse import csr_array
from scipy.spatial import Delaunay
from utilities import create_folder, save_data, load_data
from plot_utilities import set_font_size, set_cmap
import os
np.random.seed(42)
n = 2000
data_folder = "./data/example_2/"
create_folder(data_folder)
set_cmap("plasma")
set_font_size(18)
vertex_fig_size = (6.4, 4.4)
plots_folder = "./images/example_2/"
create_folder(plots_folder)
if os.path.exists(f"{data_folder}/points_{n}.pkl"):
points = load_data(f"{data_folder}/points_{n}.pkl")
else:
points = np.random.rand(n, 2) * 2 * np.pi
save_data(f"{data_folder}/points_{n}.pkl", points)
base_signals = [
0.5 * np.sin(5 * (points[:, 0] + points[:, 1])),
np.cos((points[:, 0] - points[:, 1])),
]
function_signal = sum(base_signals)
# Load or create adjacency matrix
if os.path.exists(f"{data_folder}/adjacency_{n}.pkl"):
adjacency = load_data(f"{data_folder}/adjacency_{n}.pkl")
else:
# Create sparse adjacency matrix
tri = Delaunay(points)
coords_dict = {}
for simplex in tri.simplices:
for i in range(len(simplex)):
j = i + 1
j = j % len(simplex)
dist = np.linalg.norm(points[simplex[i]] - points[simplex[j]])
val = 1 / dist
coords_dict[(simplex[i], simplex[j])] = val
coords_dict[(simplex[j], simplex[i])] = val
data = tuple(coords_dict.values())
row_inds, col_inds = zip(*coords_dict.keys())
adjacency = csr_array((data, (row_inds, col_inds)), shape=(n, n))
save_data(f"{data_folder}/adjacency_{n}.pkl", adjacency)
def plot_2D_graph(points, adjacency, c=None):
plt.scatter(points[:, 0], points[:, 1], c=c, zorder=1)
for row, col in zip(*adjacency.nonzero()):
if row >= col:
continue
x1, y1 = points[row]
x2, y2 = points[col]
plt.plot([x1, x2], [y1, y2], color="gray", linewidth=0.5, zorder=0)
plt.figure(figsize=vertex_fig_size)
plot_2D_graph(points, adjacency, c=function_signal)
plt.colorbar()
plt.tight_layout()
plt.savefig(f"{plots_folder}/signal.pdf")
for i, base_signal in enumerate(base_signals):
plt.figure(figsize=vertex_fig_size)
plot_2D_graph(points, adjacency, c=base_signal)
plt.colorbar()
plt.tight_layout()
plt.savefig(f"{plots_folder}/base_signal_{i}.pdf")