-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutilities.py
More file actions
executable file
·299 lines (246 loc) · 9.78 KB
/
utilities.py
File metadata and controls
executable file
·299 lines (246 loc) · 9.78 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import numpy as np
import torch
from scipy.stats import norm
import matplotlib.pyplot as plt
def train(args, device, train_generator, model, criterion, optimizer):
"""
Train model
"""
model.train()
# Get inputs and labels
inputs, labels, _, mask, _ = train_generator.generate_batch()
# Send to device
inputs = torch.from_numpy(inputs).to(device)
labels = torch.from_numpy(labels).to(device)
mask = torch.from_numpy(mask).to(device)
# Initialize syn_x or hidden state
if args.model == 'STPNet' or args.model == 'STPRNN':
model.syn_x = model.init_syn_x(args.batch_size).to(device)
if args.model == 'RNN' or args.model == 'STPRNN':
model.hidden = model.init_hidden(args.batch_size).to(device)
optimizer.zero_grad()
output, hidden, _ = model(inputs)
# Convert to binary prediction
output = torch.sigmoid(output)
pred = torch.bernoulli(output).byte()
# Compute hit rate and false alarm rate
hit_rate = (pred * (labels == 1)).sum().float().item() / \
(labels == 1).sum().item()
fa_rate = (pred * (labels == -1)).sum().float().item() / \
(labels == -1).sum().item()
# Compute dprime
# dprime_true = dprime(hit_rate, fa_rate)
go = (labels == 1).sum().item()
catch = (labels == -1).sum().item()
num_trials = (labels != 0).sum().item()
assert (go + catch) == num_trials
# dprime_true = compute_dprime(hit_rate, fa_rate, go, catch, num_trials)
# dprime_old = dprime(hit_rate, fa_rate)
dprime_true = dprime(hit_rate, fa_rate)
# try:
# assert dprime_true == dprime_old
# except:
# print(hit_rate, fa_rate)
# print(dprime_true, dprime_old)
# Clamp to zero since we only want "go" labels
loss = criterion(output, labels.clamp(min=0))
# Apply mask and take mean
loss = (loss * mask).mean()
# L2 loss on hidden unit activations
L2_loss = hidden.pow(2).mean()
loss += args.l2_penalty * L2_loss
loss.backward()
optimizer.step()
return loss.item(), dprime_true.item()
def test(args, device, test_generator, model):
"""
Test model, get predictions and plot confusion matrix
"""
model.eval()
with torch.no_grad():
# Get inputs and labels
inputs, labels, image, _, omit = test_generator.generate_batch()
# Send to device
inputs = torch.from_numpy(inputs).to(device)
labels = torch.from_numpy(labels).to(device)
# Initialize syn_x or hidden state
if args.model == 'STPNet' or args.model == 'STPRNN':
model.syn_x = model.init_syn_x(args.batch_size).to(device)
if args.model == 'RNN' or args.model == 'STPRNN':
model.hidden = model.init_hidden(args.batch_size).to(device)
output, hidden, input_syn = model(inputs)
# output, hidden, inputs, input_syn = model(inputs) # for visualization below
# __import__("pdb").set_trace()
# Used to generate parts of Figure 2
# from matplotlib.ticker import FormatStrFormatter
# trial = 0
# # idx = [11, 4, 27]
# idx = [1, 6, 7] # RNN
# # extract inputs, synaptic efficacies, and image identities
# # inp_array = inputs[trial, :, idx].data.cpu().numpy().T
# inp_array = hidden[trial, :, idx].data.cpu().numpy().T # RNN
# syn_array = input_syn[trial, :, idx].data.cpu().numpy().T
# img_index = np.arange(0, 200, 3)
# img_array = image[trial, img_index]
# fig, ax = plt.subplots(3, 3, figsize=(16, 8))
# for i, (id, a, inp, syn) in enumerate(zip(idx, ax.T, inp_array, syn_array)):
# # plot
# a[0].scatter(img_index, np.ones_like(img_index)
# * 2.75, marker='.', c=img_array, cmap='viridis')
# a[0].plot(inp, color='mediumblue')
# a[1].plot(syn, color='k')
# # a[2].plot(inp*syn, color='mediumblue', alpha=0.7)
# a[2].plot(inp, color='mediumblue') # RNN
# # minor formatting
# a[0].set_xticklabels([])
# a[0].yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
# a[0].set_ylim([0, 3])
# a[1].set_xticklabels([])
# a[1].set_ylim([0, 1])
# # a[2].set_xticklabels([])
# a[2].set_xlabel('Time step', fontsize=14)
# # a[2].set_ylim([0, 2.5])
# a[2].yaxis.set_major_formatter(FormatStrFormatter('%.1f')) # RNN
# a[2].set_ylim([0, 5]) # RNN
# if i == 0:
# a[0].set_ylabel('Inp. activity (a.u.)', fontsize=14)
# a[1].set_ylabel('Syn. efficacy ($\it{x}$)', fontsize=14)
# # a[2].set_ylabel('Input * $\it{x}$ (a.u.)', fontsize=14)
# a[2].set_ylabel('Hid. activity (a.u.)', fontsize=14) # RNN
# else:
# a[0].set_yticklabels([])
# a[1].set_yticklabels([])
# a[2].set_yticklabels([])
# # turn off right and top spines
# for a in ax.flatten():
# a.spines['right'].set_visible(False)
# a.spines['top'].set_visible(False)
# a.tick_params(labelsize=12)
# # adjust whitespace
# plt.subplots_adjust(wspace=0.2, hspace=0.2)
# # plt.savefig('input_syn_v2.png', dpi=300, bbox_inches='tight')
# plt.savefig('input_rec.png', dpi=300, bbox_inches='tight') # RNN
# plt.show()
# Convert to binary prediction
output = torch.sigmoid(output)
pred = torch.bernoulli(output).byte()
# Compute hit rate and false alarm rate
hit_rate = (pred * (labels == 1)).sum().float().item() / \
(labels == 1).sum().item()
fa_rate = (pred * (labels == -1)).sum().float().item() / \
(labels == -1).sum().item()
# Compute dprime
# dprime_true = dprime(hit_rate, fa_rate)
go = (labels == 1).sum().item()
catch = (labels == -1).sum().item()
num_trials = (labels != 0).sum().item()
assert (go + catch) == num_trials
# dprime_true = compute_dprime(hit_rate, fa_rate, go, catch, num_trials)
# dprime_old = dprime(hit_rate, fa_rate)
dprime_true = dprime(hit_rate, fa_rate)
# try:
# assert dprime_true == dprime_old
# except:
# print(hit_rate, fa_rate)
# print(dprime_true, dprime_old)
return dprime_true.item(), hit_rate, fa_rate, input_syn, hidden, output, pred, image, labels, omit
def trial_number_limit(p, N):
if N == 0:
return np.nan
else:
p = np.max((p, 1. / (2 * N)))
p = np.min((p, 1 - 1. / (2 * N)))
return p
def compute_dprime(hit_rate, fa_rate, go_trials, catch_trials, total_trials):
""" calculates the d-prime for a given hit rate and false alarm rate
https://en.wikipedia.org/wiki/Sensitivity_index
Parameters
----------
hit_rate : float
rate of hits in the True class
fa_rate : float
rate of false alarms in the False class
go_trials: int
number of go trials
catch_trials: int
number of catch trials
total_trials: int
total number of trials
Returns
-------
d_prime
"""
limits = (1./total_trials, 1 - 1./total_trials)
assert limits[0] > 0.0, 'limits[0] must be greater than 0.0'
assert limits[1] < 1.0, 'limits[1] must be less than 1.0'
assert (go_trials + catch_trials) == total_trials
Z = norm.ppf
# Limit values in order to avoid d' infinity
hit_rate = np.clip(trial_number_limit(
hit_rate, go_trials), limits[0], limits[1])
fa_rate = np.clip(trial_number_limit(
fa_rate, catch_trials), limits[0], limits[1])
return Z(hit_rate) - Z(fa_rate)
def dprime(hit_rate, fa_rate, limits=(0.01, 0.99)):
""" calculates the d-prime for a given hit rate and false alarm rate
https://en.wikipedia.org/wiki/Sensitivity_index
Parameters
----------
hit_rate : float
rate of hits in the True class
fa_rate : float
rate of false alarms in the False class
limits : tuple, optional
limits on extreme values, which distort. default: (0.01,0.99)
Returns
-------
d_prime
"""
assert limits[0] > 0.0, 'limits[0] must be greater than 0.0'
assert limits[1] < 1.0, 'limits[1] must be less than 1.0'
Z = norm.ppf
# Limit values in order to avoid d' infinity
hit_rate = np.clip(hit_rate, limits[0], limits[1])
fa_rate = np.clip(fa_rate, limits[0], limits[1])
return Z(hit_rate) - Z(fa_rate)
def compute_confusion_matrix(num_images,
labels,
image,
output,
step=3,
plot=False,
image_ticks=None,
matrix_plot_save_path=None):
"""
Compute confusion matrix
Plot and save confusion matrix if needed
"""
# Initialize confusion matrix
response_matrix = np.zeros((num_images, num_images))
total_matrix = np.zeros((num_images, num_images))
switch = (labels != 0)
switch = switch.flatten()
image = image.flatten()
output = output.flatten()
for i in range(len(image)):
if switch[i] == 1: # if there is a switch
new_img = image[i]
old_img = image[i-step]
total_matrix[old_img, new_img] += 1
if (output[i] == 1): # need to modify to depend on target window
response_matrix[old_img, new_img] += 1
confusion_matrix = response_matrix / total_matrix
if plot:
plt.figure()
plt.imshow(confusion_matrix, cmap='magma', vmin=0, vmax=1)
plt.colorbar()
if image_ticks is not None:
plt.xticks(np.arange(8), image_ticks)
plt.yticks(np.arange(8), image_ticks)
plt.title('Response Probability Matrix')
plt.xlabel('Initial Image')
plt.ylabel('Change Image')
if matrix_plot_save_path is not None:
plt.savefig(matrix_plot_save_path + '.png', bbox_inches="tight")
# plt.close()
return response_matrix, total_matrix, confusion_matrix