-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03-off_diag_errors.py
More file actions
411 lines (338 loc) · 12.4 KB
/
03-off_diag_errors.py
File metadata and controls
411 lines (338 loc) · 12.4 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# %%
# !%load_ext autoreload
# !%autoreload 2
from itertools import combinations
import matplotlib.pyplot as plt
import numpy as np
import scienceplots
from scipy.linalg import inv
from scipy.optimize import minimize
from statsmodels.tools.numdiff import approx_hess
from tqdm import tqdm
from dcsem.utils import stim_boxcar
from utils import (
add_noise,
add_underscore,
filter_params,
get_out_dir,
get_param_colors,
initialize_parameters,
set_style,
simulate_bold,
)
set_style()
# %%
def objective(param_vals, param_names, bold_observed):
# Map the parameter values to their names
params = dict(zip(param_names, param_vals))
# Simulate the estimated BOLD signal
bold_simulated = simulate_bold(params, time=time, u=u, num_rois=num_rois)
# Compute the mean squared error
loss = np.mean((bold_simulated - bold_observed) ** 2)
# Compute the sum of squared errors
return loss
def estimate_parameters(
initial_values,
param_names,
bounds=None,
normalize=True,
**kwargs,
):
# Perform the minimization
opt = minimize(
objective,
x0=initial_values,
args=(
param_names,
kwargs["bold_signal"],
),
bounds=bounds,
method="L-BFGS-B",
)
# Map the optimized parameter values back to their names
estimated_params = dict(zip(param_names, opt.x.tolist()))
# Compute the Hessian matrix using finite differences
hessian = approx_hess(opt.x, objective, args=(param_names, kwargs["bold_signal"]))
if normalize:
# Compute residuals and estimate the variance of the noise
residuals = kwargs["bold_signal"] - simulate_bold(
estimated_params,
time=kwargs["time"],
u=kwargs["u"],
num_rois=kwargs["num_rois"],
)
n = residuals.size # Total number of observations
p = len(opt.x) # Number of parameters
sig_est = np.sum(residuals**2) / (n - p) # Estimated variance of the residuals
# Compute the covariance matrix as the scaled inverse Hessian
cov_mat = inv(hessian) * sig_est
else:
cov_mat = inv(hessian)
std = np.sqrt(np.diag(cov_mat))
n = len(cov_mat)
if n > 1:
# Initialize lists to accumulate data
pairs = []
values = []
# Extract upper half elements and their indices
upper_half_mask = np.triu(np.ones((n, n), dtype=bool), k=1)
upper_half_elements = cov_mat[upper_half_mask]
row_indices, col_indices = np.where(upper_half_mask)
# Store pairs and values
for value, row, col in zip(upper_half_elements, row_indices, col_indices):
pair_label = rf"{add_underscore(param_names[row])} $\leftrightarrow$ {add_underscore(param_names[col])}"
pairs.append(pair_label)
values.append(value)
return estimated_params, hessian, cov_mat, std, pairs, values
def plot_bold_signals(time, bold_true, bold_noisy, bold_estimated):
"""
Plot the observed, true, and estimated BOLD signals for each region of
interest (ROI).
Args:
time (ndarray): A 1D array representing time points.
bold_true (ndarray): The ground truth BOLD signal, a 2D array where
each column corresponds to an ROI.
bold_noisy (ndarray): The observed BOLD signal with added noise,
in the same shape as bold_true.
bold_estimated (ndarray): The estimated BOLD signal, also a 2D array
with the same shape as bold_true.
"""
num_rois = bold_true.shape[1]
_, axs = plt.subplots(1, num_rois, figsize=(10, 4))
for i in range(num_rois):
axs[i].plot(time, bold_noisy[:, i], label="Observed", lw=2)
axs[i].plot(time, bold_true[:, i], label="Ground Truth", lw=2)
axs[i].plot(
time,
bold_estimated[:, i],
label="Estimated",
ls="--",
lw=2,
c="tomato",
)
axs[i].set_title(f"ROI {i}")
axs[i].set_xlabel("Time (s)")
axs[i].legend()
axs[0].set_ylabel("BOLD Signal")
plt.tight_layout()
plt.show()
def run_simulation(
true_params,
initial_values,
params_to_est,
snr,
bounds=None,
normalize=True,
plot=True,
verbose=True,
):
# Simulate observed data
bold_true = simulate_bold(
true_params,
time=time,
u=u,
num_rois=num_rois,
)
# Add noise to the observed data
bold_noisy = add_noise(bold_true, snr_db=snr)
# Filter the relevant bounds if they are provided
if bounds:
bounds = [(bounds[param]) for param in params_to_est]
# Estimate parameters
est_params, hessian, covariance, std, pairs, values = estimate_parameters(
initial_values,
params_to_est,
bounds,
normalize,
time=time,
u=u,
bold_signal=bold_noisy,
num_rois=num_rois,
)
# Compute the error between the true and estimated parameters
true_vals = np.array(list(filter_params(true_params, params_to_est).values()))
est_vals = np.array(list(est_params.values()))
err = true_vals - est_vals
# Simulate data using estimated parameters
bold_estimated = simulate_bold(est_params, time=time, u=u, num_rois=num_rois)
if plot:
# Plot results
plot_bold_signals(time, bold_true, bold_noisy, bold_estimated)
if verbose:
# Print results
print("\tTrue\tEstimated\tInitial Guess")
for i, param in enumerate(params_to_est):
print(f"{param}:\t", end="")
print(f"{true_params[param]:.2f}\t", end="")
print(f"{est_params[param]:.2f}\t\t", end="")
print(f"{initial_values[i]:.2f}")
print("\nHessian:")
print(hessian)
print("\nCovariance matrix:")
print(covariance)
print("\nVariances of the estimated parameters:")
print(np.diag(covariance))
print("\nStandard deviations of the estimated parameters:")
print(std, "\n\n")
return hessian, covariance, std, err, pairs, values
# %%
if __name__ == "__main__":
# ==================================================================================
# Specify the parameters for the simulation
# ==================================================================================
# Set up the time vector and stimulus function
time = np.arange(100)
u = stim_boxcar([[0, 30, 1]])
# Model parameters
num_rois = 2
num_layers = 1
# Set the colors for each parameter
param_colors = get_param_colors()
# Parameters to use in the simulation and estimation
params_to_set = ["a01", "a10", "c0", "c1"]
params_to_est = ["a01", "a10", "c0", "c1"]
# Generate all combinations of the parameters
all_combinations = []
for r in range(1, len(params_to_est) + 1):
combinations_r = combinations(params_to_est, r)
all_combinations.extend(combinations_r)
# Convert each combination to a list (optional)
all_combinations = [list(comb) for comb in all_combinations]
# Remove single parameter combinations
all_combinations = [comb for comb in all_combinations if len(comb) > 1]
# Ground truth parameter values
true_params = {
"a01": 0.4,
"a10": 0.4,
"c0": 0.5,
"c1": 0.5,
}
true_params = filter_params(true_params, params_to_set)
# Bounds for the parameters
bounds = {
"a01": (0, 1),
"a10": (0, 1),
"c0": (0, 1),
"c1": (0, 1),
}
bounds = filter_params(bounds, params_to_est)
random = True
# Signal-to-noise ratio
n_sims = 3 if random else 1
n_snrs = 20
min_snr, max_snr = 0.1, 50
snr_range = np.logspace(np.log10(min_snr), np.log10(max_snr), n_snrs)
snr_range = np.linspace(min_snr, max_snr, n_snrs)
IMG_DIR = get_out_dir(
type="img",
subfolder="wip",
extra_subfolders=["estimation", f"random-{random}"],
)
# ==================================================================================
# Run the simulation and estimation
# ==================================================================================
pair_list = []
all_vals = []
for comb in all_combinations:
tmp_init = np.zeros((n_sims, len(comb)))
tmp_stds = np.zeros((n_sims, len(comb)))
tmp_errs = np.zeros((n_sims, len(comb)))
tmp_vals = []
init_list = []
stds_list = []
errs_list = []
vals_list = []
# Run the simulation and estimation
for snr_db in tqdm(snr_range):
non_nan_found = False
while not non_nan_found:
for sim_i in range(n_sims):
# Random initialization of the parameters
initial_values = initialize_parameters(bounds, comb, random=random)
hess, cov, std, err, pairs, vals = run_simulation(
true_params=true_params,
initial_values=initial_values,
params_to_est=comb,
snr=snr_db,
bounds=bounds,
normalize=True,
plot=False,
verbose=False,
)
# Collect initial guesses, standard deviations, and errors
tmp_init[sim_i, :] = initial_values
tmp_stds[sim_i, :] = std
tmp_errs[sim_i, :] = err
tmp_vals.append(vals)
# Check if all simulations resulted in NaN
if (np.isnan(tmp_stds).all()) or (
np.isnan(np.sum(tmp_stds, axis=1)).all()
):
print(f"All simulations failed at SNR {snr_db} dB, retrying...")
tmp_init = np.zeros((n_sims, len(comb)))
tmp_stds = np.zeros((n_sims, len(comb)))
tmp_errs = np.zeros((n_sims, len(comb)))
else:
non_nan_found = True
# Get the best estimation results
best_run_idx = np.nanargmin(np.sum(tmp_stds, axis=1))
init_list.append(tmp_init[best_run_idx])
stds_list.append(tmp_stds[best_run_idx])
errs_list.append(tmp_errs[best_run_idx])
vals_list.append(tmp_vals[best_run_idx])
tmp_init = np.zeros((n_sims, len(comb)))
tmp_stds = np.zeros((n_sims, len(comb)))
tmp_errs = np.zeros((n_sims, len(comb)))
tmp_vals = []
all_vals.append(vals_list)
pair_list.append(pairs)
# Plot the results
stds_arr = np.array(stds_list)
errs_arr = np.array(errs_list)
vals_arr = np.array(vals_list)
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
axs[0].axhline(0, color="k", ls="--")
axs[1].axhline(0, color="k", ls="--")
for i, param in enumerate(comb):
axs[0].plot(
snr_range,
stds_arr[:, i],
"-x",
color=param_colors[param],
label=add_underscore(param),
)
axs[1].plot(
snr_range,
errs_arr[:, i],
"-x",
color=param_colors[param],
label=add_underscore(param),
)
axs[0].set_xlabel("Signal-to-Noise Ratio (dB)")
axs[0].set_ylabel("Standard Deviation")
axs[0].legend()
axs[1].set_xlabel("Signal-to-Noise Ratio (dB)")
axs[1].set_ylabel("Estimation Error")
axs[1].legend()
tmp_names = comb.copy()
tmp_names.sort()
fig.suptitle(
f"Parameter Estimation Results ({', '.join([add_underscore(name) for name in tmp_names])})"
)
plt.tight_layout()
plt.savefig(IMG_DIR / f"on_diag-{'_'.join(tmp_names)}.png")
plt.show()
for val, pairs, comb in zip(all_vals, pair_list, all_combinations):
tmp_val = np.array(val)
tmp_names = comb.copy()
tmp_names.sort()
plt.figure(figsize=(6, 4))
plt.xlabel("Signal-to-Noise Ratio (dB)")
plt.ylabel("Covariance")
plt.title("Off Diagonal Covariance")
for i, curr_off in enumerate(tmp_val.T):
plt.plot(curr_off, label=pairs[i])
plt.legend()
plt.savefig(IMG_DIR / f"off_diag-{'_'.join(tmp_names)}.png")
plt.show()
# %%