-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_figures.py
219 lines (161 loc) · 6 KB
/
example_figures.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# This script will generate many of the explanatory figures used in the paper
import matplotlib.pyplot as plt
import nengo.spa as spa
import seaborn as sns
import numpy as np
from utils import encode_point, spatial_dot, get_heatmap_vectors, make_good_unitary
import os
if not os.path.exists('figures'):
os.makedirs('figures')
plot_types = [
'Single Item',
'Two Items Decoded',
'Animal Icons',
'Sliding Objects',
]
seed = 13
dim = 512
limit = 5
res = 256
vmin=-1
vmax=1
cmap='plasma'
xs = np.linspace(-limit, limit, res)
ys = np.linspace(-limit, limit, res)
x_axis_sp = make_good_unitary(dim)
y_axis_sp = make_good_unitary(dim)
heatmap_vectors = get_heatmap_vectors(xs, ys, x_axis_sp, y_axis_sp)
def plt_heatmap(vec, heatmap_vectors, name='', vmin=-1, vmax=1, cmap='plasma'):
# vec has shape (dim) and heatmap_vectors have shape (xs, ys, dim) so the result will be (xs, ys)
# the output is transposed and flipped so that it is displayed intuitively on the image plot
vs = np.flip(np.tensordot(vec, heatmap_vectors, axes=([0], [2])).T, axis=0)
if cmap == 'diverging':
cmap = sns.diverging_palette(150, 275, s=80, l=55, as_cmap=True)
plt.imshow(vs, interpolation='none', extent=(xs[0], xs[-1], ys[0], ys[-1]), vmin=vmin, vmax=vmax, cmap=cmap)
plt.colorbar()
if name:
plt.suptitle(name)
def heatmap(vec, heatmap_vectors, ax, name='', vmin=-1, vmax=1, cmap='plasma'):
# vec has shape (dim) and heatmap_vectors have shape (xs, ys, dim) so the result will be (xs, ys)
# the output is transposed and flipped so that it is displayed intuitively on the image plot
vs = np.flip(np.tensordot(vec, heatmap_vectors, axes=([0], [2])).T, axis=0)
if cmap == 'diverging':
cmap = sns.diverging_palette(150, 275, s=80, l=55, as_cmap=True)
img = ax.imshow(vs, interpolation='none', extent=(xs[0], xs[-1], ys[0], ys[-1]), vmin=vmin, vmax=vmax, cmap=cmap)
ax.set_title(name)
return img
# Same as the other function, but using the plt interface so the colorbar can be used
def plt_plot_similarity(vec, xs, ys, x_axis_sp, y_axis_sp, name='', vmin=-1, vmax=1, cmap='plasma'):
# fig, ax = plt.subplots()
vs = spatial_dot(
vec=vec,
xs=xs,
ys=ys,
x_axis_sp=x_axis_sp,
y_axis_sp=y_axis_sp
)
if cmap == 'diverging':
cmap = sns.diverging_palette(150, 275, s=80, l=55, as_cmap=True)
plt.imshow(vs, interpolation='none', extent=(xs[0], xs[-1], ys[0], ys[-1]), vmin=vmin, vmax=vmax, cmap=cmap)
plt.colorbar()
if name:
plt.suptitle(name)
###############
# Single Item #
###############
if "Single Item" in plot_types:
fig, ax = plt.subplots(tight_layout=True, figsize=(4, 4))
coord_sp = encode_point(3, -2, x_axis_sp, y_axis_sp)
heatmap(
coord_sp.v,
heatmap_vectors,
ax,
name="Single Object",
vmin=vmin, vmax=vmax, cmap=cmap,
)
fig.savefig('figures/single_item.pdf', dpi=600, bbox_inches='tight')
#####################
# Two Items Decoded #
#####################
if "Two Items Decoded" in plot_types:
fig, ax = plt.subplots(tight_layout=True, figsize=(4, 4))
pos1 = encode_point(3, -2, x_axis_sp, y_axis_sp)
pos2 = encode_point(-.3, 1.5, x_axis_sp, y_axis_sp)
item1 = spa.SemanticPointer(dim)
item2 = spa.SemanticPointer(dim)
mem = pos1*item1 + pos2*item2
decode1 = mem *~ item1
decode2 = mem *~ item2
heatmap(
(decode1 + decode2).v,
heatmap_vectors,
ax,
name='',
vmin=vmin, vmax=vmax, cmap=cmap,
)
fig.savefig('figures/two_items.pdf', dpi=600, bbox_inches='tight')
if 'Sliding Objects' in plot_types:
fig, ax = plt.subplots(1, 3, sharey='row', tight_layout=True, figsize=(9, 3))
pos1 = encode_point(3, -2, x_axis_sp, y_axis_sp)
pos2 = encode_point(4, 1, x_axis_sp, y_axis_sp)
pos3 = encode_point(-1, 2, x_axis_sp, y_axis_sp)
mem = pos1 + pos2 + pos3
mem.normalize()
# sliding all objects
mem_moved = mem * encode_point(-2, 1, x_axis_sp, y_axis_sp)
title_font_size = 16
heatmap(
vec=mem.v,
heatmap_vectors=heatmap_vectors,
ax=ax[0],
name="Original Memory Contents",
vmin=vmin, vmax=vmax, cmap=cmap,
)
ax[0].set_title("Original Memory Contents", fontsize=title_font_size)
heatmap(
vec=mem_moved.v,
heatmap_vectors=heatmap_vectors,
ax=ax[2],
name="All Items Moved",
vmin=vmin, vmax=vmax, cmap=cmap,
)
ax[2].set_title("All Items Moved", fontsize=title_font_size)
# sliding single object
new_pos = pos3 * encode_point(-2, 1, x_axis_sp, y_axis_sp)
mem_single_moved = mem - pos3 + new_pos
img = heatmap(
vec=mem_single_moved.v,
heatmap_vectors=heatmap_vectors,
ax=ax[1],
name="Single Item Moved",
vmin=vmin, vmax=vmax, cmap=cmap,
)
ax[1].set_title("Single Item Moved", fontsize=title_font_size)
fig.savefig('figures/sliding_objects.pdf', dpi=600, bbox_inches='tight')
if 'Animal Icons' in plot_types:
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
def getImage(path, zoom=0.3):
return OffsetImage(plt.imread(path), zoom=zoom)
root = 'images'
paths = [
root + '/icons8-fox-96.png',
root + '/icons8-fox-96.png',
root + '/icons8-pug-96.png',
root + '/icons8-badger-96.png',
root + '/icons8-bear-96.png',
]
x = np.array([1.2, -3.4, 1.7, 4.1, 2.1])
y = np.array([1.3, -1.1, -1.1, 3.2, 2.4])
fig, ax = plt.subplots(tight_layout=True, figsize=(4, 4))
ax.scatter(y, -x)
ax.set_xlim([-5, 5])
ax.set_ylim([-5, 5])
ax.set_aspect('equal', 'box')
artists = []
for x0, y0, path in zip(x, y,paths):
ab = AnnotationBbox(getImage(path), (y0, -x0), frameon=False)
artists.append(ax.add_artist(ab))
ax.set_xlabel('x position')
ax.set_ylabel('y position')
ax.set_title('Memory Contents')
fig.savefig('figures/example_image.pdf', dpi=600, bbox_inches='tight')