1 from datetime import datetime
6 import matplotlib.pyplot as plt
7 import matplotlib.ticker as mtick
13 exec(open("conf.py").read())
16 def fmt_num(num, width=18):
18 Format number to string.
21 return str(num)[:width].ljust(width)
24 def inputs_function(x):
28 def noise_decision_function(t):
29 return np.random.normal(0.0, noise_decision)
32 def noise_bias_function(t):
33 return np.random.normal(0.0, noise_wm)
37 return time_scale if t > t_cue else 0
40 def decision_function(x):
48 # return 1.0 if x[0] + x[1] > 0.0 else -1.0
53 Base class for alpha receptors. Not to be used directly.
58 self.y = 1 / (1 + (999 * np.exp(-0.1233 * (self.x / self.offset))))
63 for i in range(len(steps)):
64 self.gains.append(self.gaind * self.y[i] + 1)
65 self.biass.append(self.biasd * self.y[i] + 1)
68 out = f"./out/{self.__class__.__name__}"
70 title = "Norepinepherine Concentration vs Neuron Activity in " + \
72 logging.info("Plotting " + title)
74 plt.plot(self.x, self.y)
76 plt.xlabel("Norepinephrine concentration (nM)")
77 plt.ylabel("Activity (%)")
80 plt.vlines(self.ki, 0, 1, linestyles="dashed")
81 plt.text(1.1 * self.ki, 0.1, "Affinity")
83 plt.hlines(0.5, 0, 1000, linestyles="dashed")
84 plt.text(1, 0.51, "50%")
87 plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter())
90 plt.savefig(f"{out}-norep-activity.png")
92 #######################################################################
94 title = "Concentration vs Gain Scalar in" + self.pretty
95 logging.info("Plotting " + title)
97 plt.plot(self.x, self.gains)
99 plt.xlabel("Norepinephrine concentration (nM)")
106 plt.savefig(f"{out}-concentration-gain.png")
108 #######################################################################
110 title = "Concentration vs Bias scalar in " + self.pretty
111 logging.info("Plotting " + title)
113 plt.plot(self.x, self.biass)
117 plt.xlabel("Norepinephrine concentration (nM)")
122 plt.savefig(f"{out}-concentration-bias.png")
127 Subclass of Alpha representing an alpha1 receptor.
133 self.pretty = "α1 Receptor"
141 Subclass of Alpha representing an alpha2 receptor.
147 self.pretty = "α2 Receptor"
157 self.num_spikes = np.ones(len(steps))
158 self.out = np.ones(3)
161 # correctly perceived (not necessarily remembered) cues
162 self.perceived = np.ones(3)
163 rng = np.random.RandomState(seed=seed)
164 # whether the cues is on the left or right
165 self.cues = 2 * rng.randint(2, size=3)-1
166 for n in range(len(self.perceived)):
167 if rng.rand() < misperceive:
168 self.perceived[n] = 0
171 title = "Norepinephrine Concentration vs Spiking Rate"
172 logging.info("Plotting " + title)
174 plt.plot(steps, self.num_spikes)
178 plt.xlabel("Norepinephrine concentration (nM)")
179 plt.ylabel("Spiking rate (spikes/time step)")
183 plt.savefig("./out/concentration-spiking.png")
185 def cue_function(self, t):
186 if t < t_cue and self.perceived[self.trial] != 0:
187 return cue_scale * self.cues[self.trial]
195 with nengo.Network() as net:
197 cue_node = nengo.Node(output=self.cue_function)
198 time_node = nengo.Node(output=time_function)
199 noise_wm_node = nengo.Node(output=noise_bias_function)
200 noise_decision_node = nengo.Node(
201 output=noise_decision_function)
204 wm = nengo.Ensemble(neurons_wm, 2)
205 decision = nengo.Ensemble(neurons_decide, 2)
206 inputs = nengo.Ensemble(neurons_inputs, 2)
207 output = nengo.Ensemble(neurons_decide, 1)
210 nengo.Connection(cue_node, inputs[0], synapse=None)
211 nengo.Connection(time_node, inputs[1], synapse=None)
212 nengo.Connection(inputs, wm, synapse=tau_wm,
213 function=inputs_function)
214 wm_recurrent = nengo.Connection(wm, wm, synapse=tau_wm)
215 nengo.Connection(noise_wm_node, wm.neurons, synapse=tau_wm,
216 transform=np.ones((neurons_wm, 1)) * tau_wm)
217 wm_to_decision = nengo.Connection(
218 wm[0], decision[0], synapse=tau)
219 nengo.Connection(noise_decision_node,
220 decision[1], synapse=None)
221 nengo.Connection(decision, output, function=decision_function)
224 probes_wm = nengo.Probe(wm[0], synapse=0.01, sample_every=probe_dt)
225 probe_spikes = nengo.Probe(wm.neurons, sample_every=probe_dt)
226 probe_output = nengo.Probe(
227 output, synapse=None, sample_every=probe_dt)
230 with nengo.Simulator(net, dt=dt, progress_bar=False) as sim:
231 for i, _ in tqdm(enumerate(steps), total=len(steps)):
232 wm.gain = (self.a1.gains[i] + self.a2.gains[i]) * sim.data[wm].gain
233 wm.bias = (self.a1.biass[i] + self.a2.biass[i]) * sim.data[wm].bias
234 wm_recurrent.solver = MySolver(
235 sim.model.params[wm_recurrent].weights)
236 wm_to_decision.solver = MySolver(
237 sim.model.params[wm_to_decision].weights)
238 sim = nengo.Simulator(net, dt=dt, progress_bar=False)
239 for self.trial in range(3):
241 f"Simulating: trial: {self.trial}, gain: {fmt_num(wm.gain)}, bias: {fmt_num(wm.bias)}")
242 sim.run(t_cue + t_delay)
243 self.out[self.trial] = np.count_nonzero(
244 sim.data[probe_spikes])
245 self.num_spikes[i] = np.average(self.out)
247 with open(f"out/{datetime.now().isoformat()}-spikes.pkl", "wb") as pout:
248 pickle.dump(self, pout)
253 class MySolver(nengo.solvers.Solver):
254 def __init__(self, weights):
256 self.my_weights = weights
257 self._paramdict = dict()
259 def __call__(self, A, Y, rng=None, E=None):
260 return self.my_weights.T, dict()
264 logging.info("Initializing simulation")
265 plt.style.use("ggplot") # Nice looking and familiar style
268 data = open("simulation.pkl", "rb")
269 except FileNotFoundError:
272 pickle.load(data).plot()
275 if __name__ == "__main__":
278 except FileExistsError:
281 logging.basicConfig(filename=f"out/{datetime.now().isoformat()}.log",