"""The 300-LIF inhibitory relay used in teaching script section 2.4.4. Run: python gating_example.py Outputs: gating-data.npz and gating-observations.json beside this script. The browser replays an archived run; running this file creates a new local run. """ from pathlib import Path import hashlib import json import nengo import numpy as np OUT = Path(__file__).resolve().parent def stimulus(t): return 0.65 if t < 1.2 else -0.55 def block_command(t): return float(0.4 <= t < 0.8 or 1.6 <= t < 2.0) def run(): with nengo.Network(seed=240, label="Inhibitory relay teaching example") as net: net.config[nengo.Ensemble].neuron_type = nengo.LIF() net.config[nengo.Ensemble].max_rates = nengo.dists.Uniform(80, 160) value = nengo.Node(stimulus) command = nengo.Node(block_command) source = nengo.Ensemble(100, 1, label="Source") relay = nengo.Ensemble(100, 1, label="Relay") block = nengo.Ensemble(100, 1, label="Block control", encoders=np.ones((100, 1)), intercepts=nengo.dists.Uniform(0.2, 0.8), eval_points=nengo.dists.Uniform(0, 1)) nengo.Connection(value, source, synapse=0.01) nengo.Connection(command, block, synapse=0.01) nengo.Connection(source, relay, synapse=0.01) nengo.Connection(block, relay.neurons, transform=-5.0 * np.ones((100, 1)), synapse=0.01) probes = {"input": nengo.Probe(value), "command": nengo.Probe(command)} for name, population in [("source", source), ("relay", relay), ("block", block)]: probes[name] = nengo.Probe(population, synapse=0.01) probes[name + "_spikes"] = nengo.Probe(population.neurons) with nengo.Simulator(net, dt=0.001, progress_bar=False) as sim: sim.run(2.4, progress_bar=False) data = {k: sim.data[v].copy() for k, v in probes.items()} data["time"] = sim.trange() np.savez_compressed(OUT / "gating-data.npz", **data) t = data["time"] windows = {"positive_open": (0.15, 0.35), "positive_closed": (0.6, 0.75), "positive_reopened": (1.0, 1.15), "negative_open": (1.4, 1.55), "negative_closed": (1.8, 1.95), "negative_reopened": (2.2, 2.35)} summaries = {} for name, (a, b) in windows.items(): mask = (t >= a) & (t < b) summaries[name] = { "interval_s": [a, b], **{k + "_mean": float(data[k][mask].mean()) for k in ["source", "relay", "block"]}, **{k + "_spike_count": int(np.rint(data[k + "_spikes"][mask].sum() * 0.001)) for k in ["source", "relay", "block"]}} # Local, prespecified observations of both signs; no statistical performance claim. for name, s in summaries.items(): target = 0.65 if name.startswith("positive") else -0.55 assert abs(s["source_mean"] - target) < 0.12 assert s["source_spike_count"] > 0 if name.endswith("closed"): assert abs(s["relay_mean"]) < 0.03 assert s["relay_spike_count"] == 0 assert s["block_spike_count"] > 0 else: assert abs(s["relay_mean"] - s["source_mean"]) < 0.12 assert s["relay_spike_count"] > 0 # Sustained 20-ms criteria on the observed relay output. markers = [] y = data["relay"][:, 0] for onset, mode in [(0.4, "close"), (0.8, "open"), (1.6, "close"), (2.0, "open")]: good = np.abs(y) < 0.1 if mode == "close" else np.abs(y) > 0.4 candidates = np.where((t >= onset) & (t < onset + 0.3))[0] index = next(i for i in candidates if good[i:i+20].all()) markers.append({"command_onset_s": onset, "mode": mode, "observed_crossing_s": float(t[index]), "delay_ms": round(float((t[index] - onset) * 1000), 3), "criterion": "abs(readout)<0.1" if mode == "close" else "abs(readout)>0.4", "sustained_ms": 20}) report = {"nengo": nengo.__version__, "seed": 240, "dt_s": 0.001, "duration_s": 2.4, "neurons": 300, "neurons_per_population": 100, "filter_s": 0.01, "inhibitory_transform": -5.0, "summaries": summaries, "local_markers": markers, "scope": "Isolated scalar teaching circuit; supplied value and block command; no learning or articulatory simulation.", "script_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "data_sha256": hashlib.sha256((OUT / "gating-data.npz").read_bytes()).hexdigest(), "checks_passed": ["Source active throughout all settled windows", "Both signal signs pass in open windows", "Relay silent in both settled closed windows", "Relay resumes transmission after both releases"]} (OUT / "gating-observations.json").write_text(json.dumps(report, indent=2) + "\n") print(json.dumps({"summaries": summaries, "local_markers": markers}, indent=2)) if __name__ == "__main__": run()