-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotebook_08.py
More file actions
413 lines (364 loc) · 13.6 KB
/
Copy pathNotebook_08.py
File metadata and controls
413 lines (364 loc) · 13.6 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
412
413
# %% [raw]
# ---
# title: "Robustness to Measurement Noise"
# author: "Allier, Lappalainen, Saalfeld"
# categories:
# - FlyVis
# - GNN
# - Measurement Noise
# execute:
# echo: false
# image: "graphs_data/fly/flyvis_noise_005_010/activity_traces_noisy.png"
# description: "Train the GNN on voltage observations corrupted by additive Gaussian measurement noise at five levels. Evaluate whether the model can still recover synaptic weights, biophysical parameters, and neuron-type identity from noisy observations."
# ---
# %% [markdown]
# ## Robustness to Measurement Noise
#
# In all previous experiments, the GNN received clean voltage traces
# from the simulator (up to intrinsic dynamics noise). In a real
# experimental setting, however, voltage recordings are corrupted by
# **measurement noise** --- instrument noise, shot noise, or
# fluorescence noise in calcium imaging. This notebook investigates
# how measurement noise degrades the GNN's ability to recover the
# circuit.
#
# ### Distinction Between Intrinsic and Measurement Noise
#
# Recall the simulated dynamics
# ([Notebook 00](Notebook_00.html)):
#
# $$\tau_i\frac{dv_i(t)}{dt} = -v_i(t) + V_i^{\text{rest}}
# + \sum_{j\in\mathcal{N}_i} \mathbf{W}_{ij}\,
# \text{ReLU}\!\big(v_j(t)\big) + I_i(t)
# + \sigma_{\text{dyn}}\,\xi_i(t),$$
#
# where $\sigma_{\text{dyn}}\,\xi_i(t)$ with
# $\xi_i(t) \sim \mathcal{N}(0,1)$ is **intrinsic dynamics noise**
# that drives stochastic fluctuations *within* the ODE. This noise
# is part of the true dynamics and produces voltage trajectories
# that genuinely differ from the deterministic solution.
#
# **Measurement noise** is fundamentally different: it corrupts the
# *observations* of the voltage, not the dynamics themselves. The
# GNN receives
#
# $$\tilde{v}_i(t) = v_i(t) + \sigma_{\text{meas}}\,\eta_i(t),
# \qquad \eta_i(t) \sim \mathcal{N}(0,1),$$
#
# and the derivative targets become
#
# $$\frac{d\widetilde{v}}{dt} \approx
# \frac{\tilde{v}_i(t+\Delta t) - \tilde{v}_i(t)}{\Delta t},$$
#
# which amplifies the measurement noise by a factor
# $\sim 1/\Delta t$. Both the input voltage and the derivative
# targets are noisy, making this a harder inverse problem than
# intrinsic noise alone.
#
# ### Experimental Setup
#
# We fix the intrinsic dynamics noise at
# $\sigma_{\text{dyn}} = 0.05$ (the same as the baseline
# [Notebook 04](Notebook_04.html) low-noise condition) and vary the
# measurement noise level across five conditions:
#
# | Config | $\sigma_{\text{meas}}$ | Voltage SNR | Derivative SNR |
# |:--|:--|:--|:--|
# | `flyvis_noise_005_002` | 0.02 | high | moderate |
# | `flyvis_noise_005_004` | 0.04 | moderate | low |
# | `flyvis_noise_005_006` | 0.06 | moderate | low |
# | `flyvis_noise_005_008` | 0.08 | low | very low |
# | `flyvis_noise_005_010` | 0.10 | low | very low |
#
# To change the intrinsic noise level, edit `noise_model_level` in
# the respective config files under `config/fly/`.
# %%
#| output: false
import glob
import os
import warnings
from IPython.display import Image, Markdown, display
sys_path = os.path.dirname(os.path.abspath(__file__)) if '__file__' in dir() else '.'
import sys
sys.path.insert(0, sys_path)
from GNN_PlotFigure import data_plot
from flyvis_gnn.config import NeuralGraphConfig
from flyvis_gnn.generators.graph_data_generator import data_generate
from flyvis_gnn.models.graph_trainer import data_test, data_train
from flyvis_gnn.plot import plot_loss_from_file
from flyvis_gnn.utils import set_device, add_pre_folder, graphs_data_path, log_path
warnings.filterwarnings("ignore", message="pkg_resources is deprecated as an API")
warnings.filterwarnings("ignore", category=FutureWarning)
def display_image(path, width=700):
"""Display a full-resolution image; width controls inline size (px)."""
if os.path.isfile(path):
display(Image(filename=path, width=width))
else:
display(Markdown(f"*Image not found: `{os.path.basename(path)}`*"))
# %% [markdown]
# ## Results
#
# Each config shares the same intrinsic dynamics noise
# ($\sigma_{\text{dyn}} = 0.05$) and GNN architecture. Only the
# measurement noise level $\sigma_{\text{meas}}$ varies.
#
# ### Optimized Training Hyperparameters
#
# A systematic LLM-driven exploration on the
# $\sigma_{\text{meas}} = 0.04$ condition (36 iterations across
# 5 intervention categories) identified noise-robust
# hyperparameters that significantly improve connectivity
# recovery under measurement noise:
#
# | Parameter | Default | Optimized | Rationale |
# |:--|:--|:--|:--|
# | `batch_size` | 4 | **6** | Larger batches average out noisy gradients |
# | `data_augmentation_loop` | 35 | **30** | Balances noise averaging with convergence |
# | `coeff_g_phi_diff` | 750 | **1200** | Stronger monotonicity constraint stabilizes $g_\phi$ |
#
# These parameters are applied uniformly across all five
# measurement noise conditions. The exploration also established
# that **noise averaging (batch size $\times$ augmentation loop)
# is the dominant lever** --- all other interventions tested
# (LR scheduling, recurrent training, derivative smoothing,
# stronger L1/L2 regularization, $f_\theta$ message monotonicity)
# either degraded or catastrophically broke training.
# %%
#| output: false
datasets = [
('flyvis_noise_005_002', '0.02', r'$\sigma_{\text{meas}} = 0.02$'),
('flyvis_noise_005_004', '0.04', r'$\sigma_{\text{meas}} = 0.04$'),
('flyvis_noise_005_006', '0.06', r'$\sigma_{\text{meas}} = 0.06$'),
('flyvis_noise_005_008', '0.08', r'$\sigma_{\text{meas}} = 0.08$'),
('flyvis_noise_005_010', '0.10', r'$\sigma_{\text{meas}} = 0.10$'),
]
config_root = "./config"
configs = {}
graphs_dirs = {}
for config_name, table_label, label in datasets:
config_file, pre_folder = add_pre_folder(config_name)
config = NeuralGraphConfig.from_yaml(f"{config_root}/{config_file}.yaml")
config.dataset = pre_folder + config.dataset
config.config_file = pre_folder + config_name
configs[config_name] = config
graphs_dirs[config_name] = graphs_data_path(pre_folder + config_name)
device = set_device(configs[datasets[0][0]].training.device)
print()
print("=" * 80)
print("GENERATE - Simulating fly visual system (measurement noise variants)")
print("=" * 80)
for config_name, table_label, label in datasets:
config = configs[config_name]
graphs_dir = graphs_dirs[config_name]
print()
print(f"--- {label} ---")
data_exists = os.path.isdir(os.path.join(graphs_dir, 'x_list_train'))
if data_exists:
print(f" data already exists at {graphs_dir}/")
print(" skipping simulation...")
else:
print(f" generating data at {graphs_dir}/")
data_generate(
config,
device=device,
visualize=False,
run_vizualized=0,
style="color",
alpha=1,
erase=False,
save=True,
step=100,
)
print()
print("=" * 80)
print("TRAIN - GNN on fly visual system (measurement-noise variants)")
print("=" * 80)
for config_name, table_label, label in datasets:
config = configs[config_name]
log_dir = log_path(config.config_file)
model_dir = os.path.join(log_dir, "models")
model_exists = os.path.isdir(model_dir) and any(
f.startswith("best_model") for f in os.listdir(model_dir)
) if os.path.isdir(model_dir) else False
print()
print(f"--- {label} ---")
if model_exists:
print(f" trained model already present in {model_dir}/")
print(" skipping training. To retrain, delete the log folder:")
print(f" rm -rf {log_dir}")
else:
print(f" training on {config.simulation.n_frames} frames")
print(f" {config.training.n_epochs} epochs, batch_size={config.training.batch_size}")
print()
data_train(config, device=device)
# %%
def parse_plot_results(log_dir):
"""Extract key metrics from data_plot metrics.txt."""
metrics = {}
path = os.path.join(log_dir, "results", "metrics.txt")
if not os.path.isfile(path):
return metrics
with open(path) as f:
for line in f:
line = line.strip()
if ':' in line:
key, val = line.split(':', 1)
metrics[key.strip()] = val.strip()
return metrics
header = "| Metric | " + " | ".join(tl for _, tl, _ in datasets) + " |"
sep = "|:--|" + "|".join(":--:" for _ in datasets) + "|"
rows = [header, sep]
metric_keys = [
('W_corrected_R2', '$W$ corrected $R^2$'),
('W_corrected_slope', '$W$ corrected slope'),
('tau_R2', r'$\tau$ $R^2$'),
('V_rest_R2', r'$V^{\text{rest}}$ $R^2$'),
('clustering_accuracy', 'Clustering accuracy'),
]
for key, display_name in metric_keys:
cells = []
for config_name, _, _ in datasets:
log_dir = log_path(configs[config_name].config_file)
m = parse_plot_results(log_dir)
cells.append(m.get(key, '\u2014'))
rows.append(f"| {display_name} | " + " | ".join(cells) + " |")
display(Markdown("\n".join(rows)))
# %% [markdown]
# ## Activity Traces
#
# Each figure shows 20 sampled neuron traces over a 500-frame
# window. The **left panel** displays the clean voltage from the
# ODE simulation (intrinsic dynamics noise
# $\sigma_{\text{dyn}} = 0.05$ only).
#
# The **right panel** shows the noisy observations
# $\tilde{v}_i(t) = v_i(t) + \sigma_{\text{meas}}\,\eta_i(t)$
# that the GNN actually receives during training. As
# $\sigma_{\text{meas}}$ increases, the high-frequency measurement
# noise becomes clearly visible.
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"### {label}"))
gdir = graphs_dirs[config_name]
display_image(os.path.join(gdir, "activity_traces_noisy.png"), width=850)
# %% [markdown]
# ## Generate Analysis Plots
#
# For each measurement noise condition, `data_plot` loads the best
# model checkpoint and generates the full suite of results
# visualizations.
# %%
#| echo: true
#| output: false
print()
print("=" * 80)
print("ANALYSIS - Generating results plots for all measurement noise conditions")
print("=" * 80)
for config_name, table_label, label in datasets:
config = configs[config_name]
print(f"\n--- {label} ---")
data_plot(
config=config,
config_file=config.config_file,
epoch_list=['best'],
style='color',
extended='plots',
device=device,
)
# %% [markdown]
# ## Connectivity Recovery
#
# The scatter plots below compare the learned (corrected) synaptic
# weights $W_{ij}^{\text{corr}}$ against the ground-truth connectome
# weights for all 434,112 edges. As measurement noise increases,
# the derivative targets become noisier and the weight recovery
# degrades.
# %%
#| output: false
def show_result(filename, config_name, width=600):
log_dir = log_path(configs[config_name].config_file)
config_indices = config_name.replace('flyvis_', '')
path = os.path.join(log_dir, "results", filename.format(idx=config_indices))
if os.path.isfile(path):
display_image(path, width=width)
def show_mlp(mlp_name, config_name, suffix=""):
log_dir = log_path(configs[config_name].config_file)
config_indices = config_name.replace('flyvis_', '')
path = os.path.join(log_dir, "results", f"{mlp_name}_{config_indices}{suffix}.png")
if os.path.isfile(path):
display_image(path, width=700)
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"### {label}"))
show_result("weights_comparison_corrected.png", config_name)
# %% [markdown]
# ## Neural Embeddings
#
# Each neuron is assigned a learned embedding $\mathbf{a}_i$ that
# captures its functional identity. Tight clustering by cell type
# indicates that the GNN discovers neuron-type identity despite
# measurement noise.
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"### {label}"))
show_result("embedding_{idx}.png", config_name)
# %% [markdown]
# ## UMAP Projections
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"### {label}"))
show_result("embedding_augmented_{idx}.png", config_name)
# %% [markdown]
# ## Learned Functions
#
# The domain-restricted plots show the true ground-truth function
# (left panel) alongside the learned function (right panel) for
# each measurement noise condition. As noise increases, the
# learned functions deviate more from the ground truth, but the
# overall shape is often preserved.
# %% [markdown]
# ### $f_\theta$ (MLP$_0$): Neuron Update Function
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"#### {label}"))
show_mlp("MLP0", config_name, "_domain")
# %% [markdown]
# ### Time Constants ($\tau$)
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"#### {label}"))
show_result("tau_comparison_{idx}.png", config_name, width=500)
# %% [markdown]
# ### Resting Potentials ($V^{\text{rest}}$)
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"#### {label}"))
show_result("V_rest_comparison_{idx}.png", config_name, width=500)
# %% [markdown]
# ### $g_\phi$ (MLP$_1$): Edge Message Function
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"#### {label}"))
show_mlp("MLP1", config_name, "_domain")
# %% [markdown]
# ### Neural Embeddings
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"#### {label}"))
show_result("embedding_{idx}.png", config_name, width=500)
# %% [markdown]
# ## Spectral Analysis
# %%
#| lightbox: true
for config_name, table_label, label in datasets:
display(Markdown(f"### {label}"))
show_result("eigen_comparison.png", config_name, width=900)