-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize_multi.py
executable file
·221 lines (181 loc) · 7.04 KB
/
optimize_multi.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
219
220
221
"""
Script to optimize performance of multi-scale edge model(s)
@author: Lynn Schmittwilken
Last update: June 2024
"""
import numpy as np
import pandas as pd
import pickle
from time import time
import sys
from scipy.optimize import minimize
from functions import create_loggabors, compute_performance, get_lapse_rate, \
grid_optimize, load_all_data, log_likelihood, reformat_data, \
save_filter_outputs, load_filter_outputs
sys.path.append('../experiment')
from params import stim_params as sparams
np.random.seed(10) # random seed for reproducibility
####################################
# Parameters #
####################################
results_file = "results_multi.pickle"
# Model params
fos = [0.5, 3., 9.] # center SFs of log-Gabor filters
sigma_fo = 0.5945 # from Schütt & Wichmann (2017)
sigma_angleo = 0.2965 # from Schütt & Wichmann (2017)
n_trials = 30 # average performance over n-trials
noiseVar = 1. # magnitude of internal noise
gain = None # None, global, channel, local, spatial
sameNoise = True # use same or different noise instances?
outDir = "./outputs_same" # directory to save filter outputs
n_filters = len(fos)
ppd = sparams["ppd"]
fac = int(ppd*2) # padding to avoid border artefacts
sparams["n_masks"] = n_trials # use same noise masks everytime
####################################
# Read data #
####################################
print("Loading psychophysical data and computing lapse rates ... (takes a moment)")
vps = ["ls", "mm", "jv", "ga", "sg", "fd"]
n_vps = len(vps)
# Load data
datadir = "../experiment/results/"
data = load_all_data(datadir, vps)
# Reformat data
noise_conds = sparams["noise_types"]
edge_conds = sparams["edge_widths"]
df_list = []
for n in noise_conds:
for e in edge_conds:
contrasts, ncorrect, ntrials = reformat_data(data, n, e)
lamb = get_lapse_rate(contrasts, ncorrect, ntrials)
df = pd.DataFrame({
"noise": [n,]*len(contrasts),
"edge": [e,]*len(contrasts),
"contrasts": contrasts,
"ncorrect": ncorrect,
"ntrials": ntrials,
"lambda": [lamb,]*len(contrasts),
})
df_list.append(df)
df = pd.concat(df_list).reset_index(drop=True)
####################################
# Preparations #
####################################
# Calculate spatial frequency axis in cpd:
nX = int(sparams["stim_size"] * ppd)
fs = np.fft.fftshift(np.fft.fftfreq(int(nX), d=1./ppd))
fx, fy = np.meshgrid(fs, fs)
# Create loggabor filters
loggabors = create_loggabors(fx, fy, fos, sigma_fo, 0., sigma_angleo)
# Constant model params
mparams = {"n_filters": n_filters,
"fos": fos,
"sigma_fo": sigma_fo,
"sigma_angleo": sigma_angleo,
"loggabors": loggabors,
"fac": fac,
"nX": nX,
"n_trials": n_trials,
"gain": gain,
"outDir": outDir,
"sameNoise": sameNoise,
"noiseVar": noiseVar,
}
adict = {"model_params": mparams,
"stim_params": sparams}
def get_loss(params):
# Read params from dict / list
if type(params) is dict:
beta, eta, kappa = params["beta"], params["eta"], params["kappa"]
alphas = [value for key, value in params.items() if "alpha" in key.lower()]
else:
beta, eta, kappa = params[0], params[1], params[2]
alphas = params[3::]
# Infinite loss if all alphas are zero
if sum(alphas) == 0:
return np.inf
# Infinite loss if alphas are negative
if any(x < 0 for x in alphas):
return np.inf
if any(x < 0 for x in [beta, eta, kappa]):
return np.inf
if kappa > 6:
return np.inf
if beta == 0:
beta = 1e-10 # beta should not be 0 to avoid division by zero in Naka-Rushton
# Run model for each contrast in each condition
LLs = []
for n in noise_conds:
for e in edge_conds:
df_cond = df[(df["noise"]==n) & (df["edge"]==e)]
contrasts = df_cond["contrasts"].to_numpy()
ncorrect = df_cond["ncorrect"].to_numpy()
ntrials = df_cond["ntrials"].to_numpy()
lamb = np.unique(df_cond["lambda"].to_numpy())[0]
for i, c in enumerate(contrasts):
pc = np.zeros(n_trials)
for t in range(n_trials):
# Load filter outputs
name = outDir + "/%s_%.3f_%i_%i.pickle" % (n, e, i, t)
mout1, mout2 = load_filter_outputs(name)
pc[t] = compute_performance(mout1, mout2, mparams, alphas, beta, eta, kappa, lamb)
# Compute log-likelihood
LLs.append(log_likelihood(y=ncorrect[i], n=ntrials[i], p=pc.mean()))
return -sum(LLs)
####################################
# Simulations #
####################################
start = time()
# Filter outputs dont change with model params, so save+load to reduce runtime drastically
print('------------------------------------------------')
print('Creating all filter outputs and save them to memory for computational efficiency')
save_filter_outputs(sparams, mparams, df, outDir)
# Initial parameter guesses for grid search
alpha = np.linspace(0., 5., 3)
params_dict = {
"beta": np.linspace(0.5, 2.5, 3),
"eta": np.linspace(0., 1., 3),
"kappa": np.linspace(0., 4., 3),
"alpha": alpha,
"alpha2": alpha,
"alpha3": alpha,
}
# Run / Continue optimization
print('------------------------------------------------')
print('Starting optimization:', results_file)
print('------------------------------------------------')
bparams, bloss = grid_optimize(results_file, params_dict, get_loss, adict)
print()
print("Best params:", bparams)
print("Best loss:", bloss)
print('------------------------------------------------')
print('Elapsed time: %.2f minutes' % ((time()-start) / 60.))
# Use best params from grid search for initial guess for optimizer
automatic_grid = True
if automatic_grid:
start = time()
res = minimize(get_loss,
list(bparams.values()),
method='Nelder-Mead', # Nelder-Mead (=Simplex)
options={"maxiter": 500},
)
# Save final results to pickle
with open(results_file, 'rb') as handle:
data_pickle = pickle.load(handle)
data_pickle["best_loss_auto"] = res.fun
data_pickle["best_params_auto"] = {
"beta": res.x[0],
"eta": res.x[1],
"kappa": res.x[2],
"alpha": res.x[3],
"alpha2": res.x[4],
"alpha3": res.x[5],
}
data_pickle["overview_auto"] = res
with open(results_file, 'wb') as handle:
pickle.dump(data_pickle, handle)
print('------------------------------------------------')
print(res)
print('------------------------------------------------')
print('Elapsed time: %.2f minutes' % ((time()-start) / 60.))