-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_plot.py
105 lines (88 loc) · 3.14 KB
/
patch_plot.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from manifold_utils.mSVD import eigen_plot
from manifold_utils.iga import arccos_catch_nan
# Load landmark indices
indices = np.load("data/natural_images/indices.npy")
indices = indices.astype(int)
def vanilla_eigenplots():
# vanilla eigenplots
for ind in tqdm(indices):
radii = np.load("data/natural_images/radii_"+str(ind)+".npy")
eigvals = np.load("data/natural_images/eigvals_"+str(ind)+".npy")
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(radii, eigvals)
fig.savefig("figures/natural_images/eigen_plot_"+str(ind)+".png")
plt.close(fig)
def grassmann_distance(X, Y):
"""
Finds the Grassmann distance between two matrices whose rowspan
is the represented subspace, as is done in Neretin.
Remember: As opposed to computing "X.T @ Y", as is done in the
Neretin paper, this assumes these are transpose, and computes
"X @ Y.T"
"""
_, s, _ = np.linalg.svd(X @ Y.T)
jord_rad = arccos_catch_nan(s)
return np.sqrt(np.sum(np.square(jord_rad)))
def jordan_autocorrelation():
# Jordan "autocorrelation"
ind = indices[0]
radii = np.load("data/natural_images/radii_"+str(ind)+".npy")
eigvals = np.load("data/natural_images/eigvals_"+str(ind)+".npy")
eigvecs = np.load("data/natural_images/eigvecs_"+str(ind)+".npy")
eigvecs = eigvecs[:, :2, :]
delays = [1, 5, 25, 125]
fig = plt.figure()
for j, delay in enumerate(delays):
ax = fig.add_subplot(2, 2, j+1)
grass_dist = []
for k in range(eigvals.shape[0]):
try:
grass_dist.append(grassmann_distance(eigvecs[k, :, :], eigvecs[k + delay, :, :]))
except IndexError:
break
ax.plot(radii[:len(grass_dist)], grass_dist)
ax.set_title("delay = "+str(delay), fontsize=6)
fig.savefig("figures/natural_images/autocorr_"+str(ind)+".png")
plt.close(fig)
def jordan_matrix():
for ind in tqdm(indices):
radii = np.load("data/natural_images/radii_"+str(ind)+".npy")
eigvals = np.load("data/natural_images/eigvals_"+str(ind)+".npy")
eigvecs = np.load("data/natural_images/eigvecs_"+str(ind)+".npy")
eigvecs = eigvecs[:, :2, :]
n_hyperplanes = len(radii)
grass_dists = np.zeros((n_hyperplanes, n_hyperplanes))
for j in range(n_hyperplanes):
hyperplane_A = eigvecs[j, :, :]
for k in range(j, n_hyperplanes):
hyperplane_B = eigvecs[k, :, :]
grass_dist = grassmann_distance(hyperplane_A, hyperplane_B)
grass_dists[j, k] = grass_dist
grass_dists[k, j] = grass_dist
fig = plt.figure()
ax = fig.add_subplot(111)
cmap = plt.get_cmap("binary")
handle = ax.imshow(grass_dists, cmap=cmap)
ax.set_title("Point "+str(ind))
ticklabels = []
for xtick in ax.get_xticks():
try:
ticklabels.append(str(radii[int(xtick)])[:4])
except IndexError:
break
ax.set_xticklabels(ticklabels, rotation=45)
ax.set_yticklabels(ticklabels, rotation=45)
fig.colorbar(handle)
fig.savefig("figures/natural_images/autocorr_matrix_"+str(ind)+".png")
plt.close(fig)
fig_bleach = plt.figure()
ax = fig_bleach.add_subplot(111)
ax.imshow(grass_dists, cmap=cmap)
ax.set_xticks([])
ax.set_yticks([])
fig_bleach.savefig("figures/natural_images/autocorr_bleach_"+str(ind)+".png")
jordan_matrix()