| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Introduction to Brian part 3: Simulations\n", |
| 8 |
"\n", |
| 9 |
"If you haven’t yet read parts 1 and 2 on Neurons and Synapses, go read them first.\n", |
| 10 |
"\n", |
| 11 |
"This tutorial is about managing the slightly more complicated tasks that crop up in research problems, rather than the toy examples we've been looking at so far. So we cover things like inputting sensory data, modelling experimental conditions, etc.\n", |
| 12 |
"\n", |
| 13 |
"As before we start by importing the Brian package and setting up matplotlib for IPython:" |
| 14 |
] |
| 15 |
}, |
| 16 |
{ |
| 17 |
"cell_type": "code", |
| 18 |
"execution_count": null, |
| 19 |
"metadata": { |
| 20 |
"collapsed": true |
| 21 |
}, |
| 22 |
"outputs": [], |
| 23 |
"source": [ |
| 24 |
"from brian2 import *\n", |
| 25 |
"%matplotlib inline" |
| 26 |
] |
| 27 |
}, |
| 28 |
{ |
| 29 |
"cell_type": "markdown", |
| 30 |
"metadata": {}, |
| 31 |
"source": [ |
| 32 |
"## Multiple runs\n", |
| 33 |
"\n", |
| 34 |
"Let's start by looking at a very common task: doing multiple runs of a simulation with some parameter that changes. Let's start off with something very simple, how does the firing rate of a leaky integrate-and-fire neuron driven by Poisson spiking neurons change depending on its membrane time constant? Let's set that up." |
| 35 |
] |
| 36 |
}, |
| 37 |
{ |
| 38 |
"cell_type": "code", |
| 39 |
"execution_count": null, |
| 40 |
"metadata": {}, |
| 41 |
"outputs": [], |
| 42 |
"source": [ |
| 43 |
"# remember, this is here for running separate simulations in the same notebook\n", |
| 44 |
"start_scope() \n", |
| 45 |
"# Parameters\n", |
| 46 |
"num_inputs = 100\n", |
| 47 |
"input_rate = 10*Hz\n", |
| 48 |
"weight = 0.1\n", |
| 49 |
"# Range of time constants\n", |
| 50 |
"tau_range = linspace(1, 10, 30)*ms\n", |
| 51 |
"# Use this list to store output rates\n", |
| 52 |
"output_rates = []\n", |
| 53 |
"# Iterate over range of time constants\n", |
| 54 |
"for tau in tau_range:\n", |
| 55 |
" # Construct the network each time\n", |
| 56 |
" P = PoissonGroup(num_inputs, rates=input_rate)\n", |
| 57 |
" eqs = '''\n", |
| 58 |
" dv/dt = -v/tau : 1\n", |
| 59 |
" '''\n", |
| 60 |
" G = NeuronGroup(1, eqs, threshold='v>1', reset='v=0', method='exact')\n", |
| 61 |
" S = Synapses(P, G, on_pre='v += weight')\n", |
| 62 |
" S.connect()\n", |
| 63 |
" M = SpikeMonitor(G)\n", |
| 64 |
" # Run it and store the output firing rate in the list\n", |
| 65 |
" run(1*second)\n", |
| 66 |
" output_rates.append(M.num_spikes/second)\n", |
| 67 |
"# And plot it\n", |
| 68 |
"plot(tau_range/ms, output_rates)\n", |
| 69 |
"xlabel(r'$\\tau$ (ms)')\n", |
| 70 |
"ylabel('Firing rate (sp/s)');" |
| 71 |
] |
| 72 |
}, |
| 73 |
{ |
| 74 |
"cell_type": "markdown", |
| 75 |
"metadata": {}, |
| 76 |
"source": [ |
| 77 |
"Now if you're running the notebook, you'll see that this was a little slow to run. The reason is that for each loop, you're recreating the objects from scratch. We can improve that by setting up the network just once. We store a copy of the state of the network before the loop, and restore it at the beginning of each iteration." |
| 78 |
] |
| 79 |
}, |
| 80 |
{ |
| 81 |
"cell_type": "code", |
| 82 |
"execution_count": null, |
| 83 |
"metadata": {}, |
| 84 |
"outputs": [], |
| 85 |
"source": [ |
| 86 |
"start_scope() \n", |
| 87 |
"num_inputs = 100\n", |
| 88 |
"input_rate = 10*Hz\n", |
| 89 |
"weight = 0.1\n", |
| 90 |
"tau_range = linspace(1, 10, 30)*ms\n", |
| 91 |
"output_rates = []\n", |
| 92 |
"# Construct the network just once\n", |
| 93 |
"P = PoissonGroup(num_inputs, rates=input_rate)\n", |
| 94 |
"eqs = '''\n", |
| 95 |
"dv/dt = -v/tau : 1\n", |
| 96 |
"'''\n", |
| 97 |
"G = NeuronGroup(1, eqs, threshold='v>1', reset='v=0', method='exact')\n", |
| 98 |
"S = Synapses(P, G, on_pre='v += weight')\n", |
| 99 |
"S.connect()\n", |
| 100 |
"M = SpikeMonitor(G)\n", |
| 101 |
"# Store the current state of the network\n", |
| 102 |
"store()\n", |
| 103 |
"for tau in tau_range:\n", |
| 104 |
" # Restore the original state of the network\n", |
| 105 |
" restore()\n", |
| 106 |
" # Run it with the new value of tau\n", |
| 107 |
" run(1*second)\n", |
| 108 |
" output_rates.append(M.num_spikes/second)\n", |
| 109 |
"plot(tau_range/ms, output_rates)\n", |
| 110 |
"xlabel(r'$\\tau$ (ms)')\n", |
| 111 |
"ylabel('Firing rate (sp/s)');" |
| 112 |
] |
| 113 |
}, |
| 114 |
{ |
| 115 |
"cell_type": "markdown", |
| 116 |
"metadata": {}, |
| 117 |
"source": [ |
| 118 |
"That's a very simple example of using store and restore, but you can use it in much more complicated situations. For example, you might want to run a long training run, and then run multiple test runs afterwards. Simply put a store after the long training run, and a restore before each testing run.\n", |
| 119 |
"\n", |
| 120 |
"You can also see that the output curve is very noisy and doesn't increase monotonically like we'd expect. The noise is coming from the fact that we run the Poisson group afresh each time. If we only wanted to see the effect of the time constant, we could make sure that the spikes were the same each time (although note that really, you ought to do multiple runs and take an average). We do this by running just the Poisson group once, recording its spikes, and then creating a new `SpikeGeneratorGroup` that will output those recorded spikes each time." |
| 121 |
] |
| 122 |
}, |
| 123 |
{ |
| 124 |
"cell_type": "code", |
| 125 |
"execution_count": null, |
| 126 |
"metadata": {}, |
| 127 |
"outputs": [], |
| 128 |
"source": [ |
| 129 |
"start_scope() \n", |
| 130 |
"num_inputs = 100\n", |
| 131 |
"input_rate = 10*Hz\n", |
| 132 |
"weight = 0.1\n", |
| 133 |
"tau_range = linspace(1, 10, 30)*ms\n", |
| 134 |
"output_rates = []\n", |
| 135 |
"# Construct the Poisson spikes just once\n", |
| 136 |
"P = PoissonGroup(num_inputs, rates=input_rate)\n", |
| 137 |
"MP = SpikeMonitor(P)\n", |
| 138 |
"# We use a Network object because later on we don't\n", |
| 139 |
"# want to include these objects\n", |
| 140 |
"net = Network(P, MP)\n", |
| 141 |
"net.run(1*second)\n", |
| 142 |
"# And keep a copy of those spikes\n", |
| 143 |
"spikes_i = MP.i\n", |
| 144 |
"spikes_t = MP.t\n", |
| 145 |
"# Now construct the network that we run each time\n", |
| 146 |
"# SpikeGeneratorGroup gets the spikes that we created before\n", |
| 147 |
"SGG = SpikeGeneratorGroup(num_inputs, spikes_i, spikes_t)\n", |
| 148 |
"eqs = '''\n", |
| 149 |
"dv/dt = -v/tau : 1\n", |
| 150 |
"'''\n", |
| 151 |
"G = NeuronGroup(1, eqs, threshold='v>1', reset='v=0', method='exact')\n", |
| 152 |
"S = Synapses(SGG, G, on_pre='v += weight')\n", |
| 153 |
"S.connect()\n", |
| 154 |
"M = SpikeMonitor(G)\n", |
| 155 |
"# Store the current state of the network\n", |
| 156 |
"net = Network(SGG, G, S, M)\n", |
| 157 |
"net.store()\n", |
| 158 |
"for tau in tau_range:\n", |
| 159 |
" # Restore the original state of the network\n", |
| 160 |
" net.restore()\n", |
| 161 |
" # Run it with the new value of tau\n", |
| 162 |
" net.run(1*second)\n", |
| 163 |
" output_rates.append(M.num_spikes/second)\n", |
| 164 |
"plot(tau_range/ms, output_rates)\n", |
| 165 |
"xlabel(r'$\\tau$ (ms)')\n", |
| 166 |
"ylabel('Firing rate (sp/s)');" |
| 167 |
] |
| 168 |
}, |
| 169 |
{ |
| 170 |
"cell_type": "markdown", |
| 171 |
"metadata": {}, |
| 172 |
"source": [ |
| 173 |
"You can see that now there is much less noise and it increases monotonically because the input spikes are the same each time, meaning we're seeing the effect of the time constant, not the random spikes.\n", |
| 174 |
"\n", |
| 175 |
"Note that in the code above, we created `Network` objects. The reason is that in the loop, if we just called `run` it would try to simulate all the objects, including the Poisson neurons ``P``, and we only want to run that once. We use `Network` to specify explicitly which objects we want to include.\n", |
| 176 |
"\n", |
| 177 |
"The techniques we've looked at so far are the conceptually most simple way to do multiple runs, but not always the most efficient. Since there's only a single output neuron in the model above, we can simply duplicate that output neuron and make the time constant a parameter of the group." |
| 178 |
] |
| 179 |
}, |
| 180 |
{ |
| 181 |
"cell_type": "code", |
| 182 |
"execution_count": null, |
| 183 |
"metadata": {}, |
| 184 |
"outputs": [], |
| 185 |
"source": [ |
| 186 |
"start_scope() \n", |
| 187 |
"num_inputs = 100\n", |
| 188 |
"input_rate = 10*Hz\n", |
| 189 |
"weight = 0.1\n", |
| 190 |
"tau_range = linspace(1, 10, 30)*ms\n", |
| 191 |
"num_tau = len(tau_range)\n", |
| 192 |
"P = PoissonGroup(num_inputs, rates=input_rate)\n", |
| 193 |
"# We make tau a parameter of the group\n", |
| 194 |
"eqs = '''\n", |
| 195 |
"dv/dt = -v/tau : 1\n", |
| 196 |
"tau : second\n", |
| 197 |
"'''\n", |
| 198 |
"# And we have num_tau output neurons, each with a different tau\n", |
| 199 |
"G = NeuronGroup(num_tau, eqs, threshold='v>1', reset='v=0', method='exact')\n", |
| 200 |
"G.tau = tau_range\n", |
| 201 |
"S = Synapses(P, G, on_pre='v += weight')\n", |
| 202 |
"S.connect()\n", |
| 203 |
"M = SpikeMonitor(G)\n", |
| 204 |
"# Now we can just run once with no loop\n", |
| 205 |
"run(1*second)\n", |
| 206 |
"output_rates = M.count/second # firing rate is count/duration\n", |
| 207 |
"plot(tau_range/ms, output_rates)\n", |
| 208 |
"xlabel(r'$\\tau$ (ms)')\n", |
| 209 |
"ylabel('Firing rate (sp/s)');" |
| 210 |
] |
| 211 |
}, |
| 212 |
{ |
| 213 |
"cell_type": "markdown", |
| 214 |
"metadata": {}, |
| 215 |
"source": [ |
| 216 |
"You can see that this is much faster again! It's a little bit more complicated conceptually, and it's not always possible to do this trick, but it can be much more efficient if it's possible.\n", |
| 217 |
"\n", |
| 218 |
"Let's finish with this example by having a quick look at how the mean and standard deviation of the interspike intervals depends on the time constant." |
| 219 |
] |
| 220 |
}, |
| 221 |
{ |
| 222 |
"cell_type": "code", |
| 223 |
"execution_count": null, |
| 224 |
"metadata": {}, |
| 225 |
"outputs": [], |
| 226 |
"source": [ |
| 227 |
"trains = M.spike_trains()\n", |
| 228 |
"isi_mu = full(num_tau, nan)*second\n", |
| 229 |
"isi_std = full(num_tau, nan)*second\n", |
| 230 |
"for idx in range(num_tau):\n", |
| 231 |
" train = diff(trains[idx])\n", |
| 232 |
" if len(train)>1:\n", |
| 233 |
" isi_mu[idx] = mean(train)\n", |
| 234 |
" isi_std[idx] = std(train)\n", |
| 235 |
"errorbar(tau_range/ms, isi_mu/ms, yerr=isi_std/ms)\n", |
| 236 |
"xlabel(r'$\\tau$ (ms)')\n", |
| 237 |
"ylabel('Interspike interval (ms)');" |
| 238 |
] |
| 239 |
}, |
| 240 |
{ |
| 241 |
"cell_type": "markdown", |
| 242 |
"metadata": {}, |
| 243 |
"source": [ |
| 244 |
"Notice that we used the ``spike_trains()`` method of `SpikeMonitor`. This is a dictionary with keys being the indices of the neurons and values being the array of spike times for that neuron.\n", |
| 245 |
"\n", |
| 246 |
"## Changing things during a run\n", |
| 247 |
"\n", |
| 248 |
"Imagine an experiment where you inject current into a neuron, and change the amplitude randomly every 10 ms. Let's see if we can model that using a Hodgkin-Huxley type neuron." |
| 249 |
] |
| 250 |
}, |
| 251 |
{ |
| 252 |
"cell_type": "code", |
| 253 |
"execution_count": null, |
| 254 |
"metadata": {}, |
| 255 |
"outputs": [], |
| 256 |
"source": [ |
| 257 |
"start_scope()\n", |
| 258 |
"# Parameters\n", |
| 259 |
"area = 20000*umetre**2\n", |
| 260 |
"Cm = 1*ufarad*cm**-2 * area\n", |
| 261 |
"gl = 5e-5*siemens*cm**-2 * area\n", |
| 262 |
"El = -65*mV\n", |
| 263 |
"EK = -90*mV\n", |
| 264 |
"ENa = 50*mV\n", |
| 265 |
"g_na = 100*msiemens*cm**-2 * area\n", |
| 266 |
"g_kd = 30*msiemens*cm**-2 * area\n", |
| 267 |
"VT = -63*mV\n", |
| 268 |
"# The model\n", |
| 269 |
"eqs_HH = '''\n", |
| 270 |
"dv/dt = (gl*(El-v) - g_na*(m*m*m)*h*(v-ENa) - g_kd*(n*n*n*n)*(v-EK) + I)/Cm : volt\n", |
| 271 |
"dm/dt = 0.32*(mV**-1)*(13.*mV-v+VT)/\n", |
| 272 |
" (exp((13.*mV-v+VT)/(4.*mV))-1.)/ms*(1-m)-0.28*(mV**-1)*(v-VT-40.*mV)/\n", |
| 273 |
" (exp((v-VT-40.*mV)/(5.*mV))-1.)/ms*m : 1\n", |
| 274 |
"dn/dt = 0.032*(mV**-1)*(15.*mV-v+VT)/\n", |
| 275 |
" (exp((15.*mV-v+VT)/(5.*mV))-1.)/ms*(1.-n)-.5*exp((10.*mV-v+VT)/(40.*mV))/ms*n : 1\n", |
| 276 |
"dh/dt = 0.128*exp((17.*mV-v+VT)/(18.*mV))/ms*(1.-h)-4./(1+exp((40.*mV-v+VT)/(5.*mV)))/ms*h : 1\n", |
| 277 |
"I : amp\n", |
| 278 |
"'''\n", |
| 279 |
"group = NeuronGroup(1, eqs_HH,\n", |
| 280 |
" threshold='v > -40*mV',\n", |
| 281 |
" refractory='v > -40*mV',\n", |
| 282 |
" method='exponential_euler')\n", |
| 283 |
"group.v = El\n", |
| 284 |
"statemon = StateMonitor(group, 'v', record=True)\n", |
| 285 |
"spikemon = SpikeMonitor(group, variables='v')\n", |
| 286 |
"figure(figsize=(9, 4))\n", |
| 287 |
"for l in range(5):\n", |
| 288 |
" group.I = rand()*50*nA\n", |
| 289 |
" run(10*ms)\n", |
| 290 |
" axvline(l*10, ls='--', c='k')\n", |
| 291 |
"axhline(El/mV, ls='-', c='lightgray', lw=3)\n", |
| 292 |
"plot(statemon.t/ms, statemon.v[0]/mV, '-b')\n", |
| 293 |
"plot(spikemon.t/ms, spikemon.v/mV, 'ob')\n", |
| 294 |
"xlabel('Time (ms)')\n", |
| 295 |
"ylabel('v (mV)');" |
| 296 |
] |
| 297 |
}, |
| 298 |
{ |
| 299 |
"cell_type": "markdown", |
| 300 |
"metadata": {}, |
| 301 |
"source": [ |
| 302 |
"In the code above, we used a loop over multiple runs to achieve this. That's fine, but it's not the most efficient way to do it because each time we call ``run`` we have to do a lot of initialisation work that slows everything down. It also won't work as well with the more efficient standalone mode of Brian. Here's another way." |
| 303 |
] |
| 304 |
}, |
| 305 |
{ |
| 306 |
"cell_type": "code", |
| 307 |
"execution_count": null, |
| 308 |
"metadata": {}, |
| 309 |
"outputs": [], |
| 310 |
"source": [ |
| 311 |
"start_scope()\n", |
| 312 |
"group = NeuronGroup(1, eqs_HH,\n", |
| 313 |
" threshold='v > -40*mV',\n", |
| 314 |
" refractory='v > -40*mV',\n", |
| 315 |
" method='exponential_euler')\n", |
| 316 |
"group.v = El\n", |
| 317 |
"statemon = StateMonitor(group, 'v', record=True)\n", |
| 318 |
"spikemon = SpikeMonitor(group, variables='v')\n", |
| 319 |
"# we replace the loop with a run_regularly\n", |
| 320 |
"group.run_regularly('I = rand()*50*nA', dt=10*ms)\n", |
| 321 |
"run(50*ms)\n", |
| 322 |
"figure(figsize=(9, 4))\n", |
| 323 |
"# we keep the loop just to draw the vertical lines\n", |
| 324 |
"for l in range(5):\n", |
| 325 |
" axvline(l*10, ls='--', c='k')\n", |
| 326 |
"axhline(El/mV, ls='-', c='lightgray', lw=3)\n", |
| 327 |
"plot(statemon.t/ms, statemon.v[0]/mV, '-b')\n", |
| 328 |
"plot(spikemon.t/ms, spikemon.v/mV, 'ob')\n", |
| 329 |
"xlabel('Time (ms)')\n", |
| 330 |
"ylabel('v (mV)');" |
| 331 |
] |
| 332 |
}, |
| 333 |
{ |
| 334 |
"cell_type": "markdown", |
| 335 |
"metadata": {}, |
| 336 |
"source": [ |
| 337 |
"We've replaced the loop that had multiple ``run`` calls with a ``run_regularly``. This makes the specified block of code run every ``dt=10*ms``. The ``run_regularly`` lets you run code specific to a single `NeuronGroup`, but sometimes you might need more flexibility. For this, you can use `network_operation` which lets you run arbitrary Python code (but won't work with the standalone mode)." |
| 338 |
] |
| 339 |
}, |
| 340 |
{ |
| 341 |
"cell_type": "code", |
| 342 |
"execution_count": null, |
| 343 |
"metadata": {}, |
| 344 |
"outputs": [], |
| 345 |
"source": [ |
| 346 |
"start_scope()\n", |
| 347 |
"group = NeuronGroup(1, eqs_HH,\n", |
| 348 |
" threshold='v > -40*mV',\n", |
| 349 |
" refractory='v > -40*mV',\n", |
| 350 |
" method='exponential_euler')\n", |
| 351 |
"group.v = El\n", |
| 352 |
"statemon = StateMonitor(group, 'v', record=True)\n", |
| 353 |
"spikemon = SpikeMonitor(group, variables='v')\n", |
| 354 |
"# we replace the loop with a network_operation\n", |
| 355 |
"@network_operation(dt=10*ms)\n", |
| 356 |
"def change_I():\n", |
| 357 |
" group.I = rand()*50*nA\n", |
| 358 |
"run(50*ms)\n", |
| 359 |
"figure(figsize=(9, 4))\n", |
| 360 |
"for l in range(5):\n", |
| 361 |
" axvline(l*10, ls='--', c='k')\n", |
| 362 |
"axhline(El/mV, ls='-', c='lightgray', lw=3)\n", |
| 363 |
"plot(statemon.t/ms, statemon.v[0]/mV, '-b')\n", |
| 364 |
"plot(spikemon.t/ms, spikemon.v/mV, 'ob')\n", |
| 365 |
"xlabel('Time (ms)')\n", |
| 366 |
"ylabel('v (mV)');" |
| 367 |
] |
| 368 |
}, |
| 369 |
{ |
| 370 |
"cell_type": "markdown", |
| 371 |
"metadata": {}, |
| 372 |
"source": [ |
| 373 |
"Now let's extend this example to run on multiple neurons, each with a different capacitance to see how that affects the behaviour of the cell." |
| 374 |
] |
| 375 |
}, |
| 376 |
{ |
| 377 |
"cell_type": "code", |
| 378 |
"execution_count": null, |
| 379 |
"metadata": {}, |
| 380 |
"outputs": [], |
| 381 |
"source": [ |
| 382 |
"start_scope()\n", |
| 383 |
"N = 3\n", |
| 384 |
"eqs_HH_2 = '''\n", |
| 385 |
"dv/dt = (gl*(El-v) - g_na*(m*m*m)*h*(v-ENa) - g_kd*(n*n*n*n)*(v-EK) + I)/C : volt\n", |
| 386 |
"dm/dt = 0.32*(mV**-1)*(13.*mV-v+VT)/\n", |
| 387 |
" (exp((13.*mV-v+VT)/(4.*mV))-1.)/ms*(1-m)-0.28*(mV**-1)*(v-VT-40.*mV)/\n", |
| 388 |
" (exp((v-VT-40.*mV)/(5.*mV))-1.)/ms*m : 1\n", |
| 389 |
"dn/dt = 0.032*(mV**-1)*(15.*mV-v+VT)/\n", |
| 390 |
" (exp((15.*mV-v+VT)/(5.*mV))-1.)/ms*(1.-n)-.5*exp((10.*mV-v+VT)/(40.*mV))/ms*n : 1\n", |
| 391 |
"dh/dt = 0.128*exp((17.*mV-v+VT)/(18.*mV))/ms*(1.-h)-4./(1+exp((40.*mV-v+VT)/(5.*mV)))/ms*h : 1\n", |
| 392 |
"I : amp\n", |
| 393 |
"C : farad\n", |
| 394 |
"'''\n", |
| 395 |
"group = NeuronGroup(N, eqs_HH_2,\n", |
| 396 |
" threshold='v > -40*mV',\n", |
| 397 |
" refractory='v > -40*mV',\n", |
| 398 |
" method='exponential_euler')\n", |
| 399 |
"group.v = El\n", |
| 400 |
"# initialise with some different capacitances\n", |
| 401 |
"group.C = array([0.8, 1, 1.2])*ufarad*cm**-2*area\n", |
| 402 |
"statemon = StateMonitor(group, variables=True, record=True)\n", |
| 403 |
"# we go back to run_regularly\n", |
| 404 |
"group.run_regularly('I = rand()*50*nA', dt=10*ms)\n", |
| 405 |
"run(50*ms)\n", |
| 406 |
"figure(figsize=(9, 4))\n", |
| 407 |
"for l in range(5):\n", |
| 408 |
" axvline(l*10, ls='--', c='k')\n", |
| 409 |
"axhline(El/mV, ls='-', c='lightgray', lw=3)\n", |
| 410 |
"plot(statemon.t/ms, statemon.v.T/mV, '-')\n", |
| 411 |
"xlabel('Time (ms)')\n", |
| 412 |
"ylabel('v (mV)');" |
| 413 |
] |
| 414 |
}, |
| 415 |
{ |
| 416 |
"cell_type": "markdown", |
| 417 |
"metadata": {}, |
| 418 |
"source": [ |
| 419 |
"So that runs, but something looks wrong! The injected currents look like they're different for all the different neurons! Let's check:" |
| 420 |
] |
| 421 |
}, |
| 422 |
{ |
| 423 |
"cell_type": "code", |
| 424 |
"execution_count": null, |
| 425 |
"metadata": {}, |
| 426 |
"outputs": [], |
| 427 |
"source": [ |
| 428 |
"plot(statemon.t/ms, statemon.I.T/nA, '-')\n", |
| 429 |
"xlabel('Time (ms)')\n", |
| 430 |
"ylabel('I (nA)');" |
| 431 |
] |
| 432 |
}, |
| 433 |
{ |
| 434 |
"cell_type": "markdown", |
| 435 |
"metadata": {}, |
| 436 |
"source": [ |
| 437 |
"Sure enough, it's different each time. But why? We wrote ``group.run_regularly('I = rand()*50*nA', dt=10*ms)`` which seems like it should give the same value of I for each neuron. But, like threshold and reset statements, ``run_regularly`` code is interpreted as being run separately for each neuron, and because I is a parameter, it can be different for each neuron. We can fix this by making I into a *shared* variable, meaning it has the same value for each neuron." |
| 438 |
] |
| 439 |
}, |
| 440 |
{ |
| 441 |
"cell_type": "code", |
| 442 |
"execution_count": null, |
| 443 |
"metadata": {}, |
| 444 |
"outputs": [], |
| 445 |
"source": [ |
| 446 |
"start_scope()\n", |
| 447 |
"N = 3\n", |
| 448 |
"eqs_HH_3 = '''\n", |
| 449 |
"dv/dt = (gl*(El-v) - g_na*(m*m*m)*h*(v-ENa) - g_kd*(n*n*n*n)*(v-EK) + I)/C : volt\n", |
| 450 |
"dm/dt = 0.32*(mV**-1)*(13.*mV-v+VT)/\n", |
| 451 |
" (exp((13.*mV-v+VT)/(4.*mV))-1.)/ms*(1-m)-0.28*(mV**-1)*(v-VT-40.*mV)/\n", |
| 452 |
" (exp((v-VT-40.*mV)/(5.*mV))-1.)/ms*m : 1\n", |
| 453 |
"dn/dt = 0.032*(mV**-1)*(15.*mV-v+VT)/\n", |
| 454 |
" (exp((15.*mV-v+VT)/(5.*mV))-1.)/ms*(1.-n)-.5*exp((10.*mV-v+VT)/(40.*mV))/ms*n : 1\n", |
| 455 |
"dh/dt = 0.128*exp((17.*mV-v+VT)/(18.*mV))/ms*(1.-h)-4./(1+exp((40.*mV-v+VT)/(5.*mV)))/ms*h : 1\n", |
| 456 |
"I : amp (shared) # everything is the same except we've added this shared\n", |
| 457 |
"C : farad\n", |
| 458 |
"'''\n", |
| 459 |
"group = NeuronGroup(N, eqs_HH_3,\n", |
| 460 |
" threshold='v > -40*mV',\n", |
| 461 |
" refractory='v > -40*mV',\n", |
| 462 |
" method='exponential_euler')\n", |
| 463 |
"group.v = El\n", |
| 464 |
"group.C = array([0.8, 1, 1.2])*ufarad*cm**-2*area\n", |
| 465 |
"statemon = StateMonitor(group, 'v', record=True)\n", |
| 466 |
"group.run_regularly('I = rand()*50*nA', dt=10*ms)\n", |
| 467 |
"run(50*ms)\n", |
| 468 |
"figure(figsize=(9, 4))\n", |
| 469 |
"for l in range(5):\n", |
| 470 |
" axvline(l*10, ls='--', c='k')\n", |
| 471 |
"axhline(El/mV, ls='-', c='lightgray', lw=3)\n", |
| 472 |
"plot(statemon.t/ms, statemon.v.T/mV, '-')\n", |
| 473 |
"xlabel('Time (ms)')\n", |
| 474 |
"ylabel('v (mV)');" |
| 475 |
] |
| 476 |
}, |
| 477 |
{ |
| 478 |
"cell_type": "markdown", |
| 479 |
"metadata": {}, |
| 480 |
"source": [ |
| 481 |
"Ahh, that's more like it!\n", |
| 482 |
"\n", |
| 483 |
"## Adding input\n", |
| 484 |
"\n", |
| 485 |
"Now let's think about a neuron being driven by a sinusoidal input. Let's go back to a leaky integrate-and-fire to simplify the equations a bit." |
| 486 |
] |
| 487 |
}, |
| 488 |
{ |
| 489 |
"cell_type": "code", |
| 490 |
"execution_count": null, |
| 491 |
"metadata": {}, |
| 492 |
"outputs": [], |
| 493 |
"source": [ |
| 494 |
"start_scope()\n", |
| 495 |
"A = 2.5\n", |
| 496 |
"f = 10*Hz\n", |
| 497 |
"tau = 5*ms\n", |
| 498 |
"eqs = '''\n", |
| 499 |
"dv/dt = (I-v)/tau : 1\n", |
| 500 |
"I = A*sin(2*pi*f*t) : 1\n", |
| 501 |
"'''\n", |
| 502 |
"G = NeuronGroup(1, eqs, threshold='v>1', reset='v=0', method='euler')\n", |
| 503 |
"M = StateMonitor(G, variables=True, record=True)\n", |
| 504 |
"run(200*ms)\n", |
| 505 |
"plot(M.t/ms, M.v[0], label='v')\n", |
| 506 |
"plot(M.t/ms, M.I[0], label='I')\n", |
| 507 |
"xlabel('Time (ms)')\n", |
| 508 |
"ylabel('v')\n", |
| 509 |
"legend(loc='best');" |
| 510 |
] |
| 511 |
}, |
| 512 |
{ |
| 513 |
"cell_type": "markdown", |
| 514 |
"metadata": {}, |
| 515 |
"source": [ |
| 516 |
"So far, so good and the sort of thing we saw in the first tutorial. Now, what if that input current were something we had recorded and saved in a file? In that case, we can use `TimedArray`. Let's start by reproducing the picture above but using `TimedArray`." |
| 517 |
] |
| 518 |
}, |
| 519 |
{ |
| 520 |
"cell_type": "code", |
| 521 |
"execution_count": null, |
| 522 |
"metadata": {}, |
| 523 |
"outputs": [], |
| 524 |
"source": [ |
| 525 |
"start_scope()\n", |
| 526 |
"A = 2.5\n", |
| 527 |
"f = 10*Hz\n", |
| 528 |
"tau = 5*ms\n", |
| 529 |
"# Create a TimedArray and set the equations to use it\n", |
| 530 |
"t_recorded = arange(int(200*ms/defaultclock.dt))*defaultclock.dt\n", |
| 531 |
"I_recorded = TimedArray(A*sin(2*pi*f*t_recorded), dt=defaultclock.dt)\n", |
| 532 |
"eqs = '''\n", |
| 533 |
"dv/dt = (I-v)/tau : 1\n", |
| 534 |
"I = I_recorded(t) : 1\n", |
| 535 |
"'''\n", |
| 536 |
"G = NeuronGroup(1, eqs, threshold='v>1', reset='v=0', method='exact')\n", |
| 537 |
"M = StateMonitor(G, variables=True, record=True)\n", |
| 538 |
"run(200*ms)\n", |
| 539 |
"plot(M.t/ms, M.v[0], label='v')\n", |
| 540 |
"plot(M.t/ms, M.I[0], label='I')\n", |
| 541 |
"xlabel('Time (ms)')\n", |
| 542 |
"ylabel('v')\n", |
| 543 |
"legend(loc='best');" |
| 544 |
] |
| 545 |
}, |
| 546 |
{ |
| 547 |
"cell_type": "markdown", |
| 548 |
"metadata": {}, |
| 549 |
"source": [ |
| 550 |
"Note that for the example where we put the ``sin`` function directly in the equations, we had to use the ``method='euler'`` argument because the exact integrator wouldn't work here (try it!). However, ``TimedArray`` is considered to be constant over its time step and so the linear integrator can be used. This means you won't get the same behaviour from these two methods for two reasons. Firstly, the numerical integration methods ``exact`` and ``euler`` give slightly different results. Secondly, ``sin`` is not constant over a timestep whereas ``TimedArray`` is.\n", |
| 551 |
"\n", |
| 552 |
"Now just to show that ``TimedArray`` works for arbitrary currents, let's make a weird \"recorded\" current and run it on that." |
| 553 |
] |
| 554 |
}, |
| 555 |
{ |
| 556 |
"cell_type": "code", |
| 557 |
"execution_count": null, |
| 558 |
"metadata": {}, |
| 559 |
"outputs": [], |
| 560 |
"source": [ |
| 561 |
"start_scope()\n", |
| 562 |
"A = 2.5\n", |
| 563 |
"f = 10*Hz\n", |
| 564 |
"tau = 5*ms\n", |
| 565 |
"# Let's create an array that couldn't be\n", |
| 566 |
"# reproduced with a formula\n", |
| 567 |
"num_samples = int(200*ms/defaultclock.dt)\n", |
| 568 |
"I_arr = zeros(num_samples)\n", |
| 569 |
"for _ in range(100):\n", |
| 570 |
" a = randint(num_samples)\n", |
| 571 |
" I_arr[a:a+100] = rand()\n", |
| 572 |
"I_recorded = TimedArray(A*I_arr, dt=defaultclock.dt)\n", |
| 573 |
"eqs = '''\n", |
| 574 |
"dv/dt = (I-v)/tau : 1\n", |
| 575 |
"I = I_recorded(t) : 1\n", |
| 576 |
"'''\n", |
| 577 |
"G = NeuronGroup(1, eqs, threshold='v>1', reset='v=0', method='exact')\n", |
| 578 |
"M = StateMonitor(G, variables=True, record=True)\n", |
| 579 |
"run(200*ms)\n", |
| 580 |
"plot(M.t/ms, M.v[0], label='v')\n", |
| 581 |
"plot(M.t/ms, M.I[0], label='I')\n", |
| 582 |
"xlabel('Time (ms)')\n", |
| 583 |
"ylabel('v')\n", |
| 584 |
"legend(loc='best');" |
| 585 |
] |
| 586 |
}, |
| 587 |
{ |
| 588 |
"cell_type": "markdown", |
| 589 |
"metadata": {}, |
| 590 |
"source": [ |
| 591 |
"Finally, let's finish on an example that actually reads in some data from a file. See if you can work out how this example works." |
| 592 |
] |
| 593 |
}, |
| 594 |
{ |
| 595 |
"cell_type": "code", |
| 596 |
"execution_count": null, |
| 597 |
"metadata": {}, |
| 598 |
"outputs": [], |
| 599 |
"source": [ |
| 600 |
"start_scope()\n", |
| 601 |
"from matplotlib.image import imread\n", |
| 602 |
"img = (1-imread('brian.png'))[::-1, :, 0].T\n", |
| 603 |
"num_samples, N = img.shape\n", |
| 604 |
"ta = TimedArray(img, dt=1*ms) # 228\n", |
| 605 |
"A = 1.5\n", |
| 606 |
"tau = 2*ms\n", |
| 607 |
"eqs = '''\n", |
| 608 |
"dv/dt = (A*ta(t, i)-v)/tau+0.8*xi*tau**-0.5 : 1\n", |
| 609 |
"'''\n", |
| 610 |
"G = NeuronGroup(N, eqs, threshold='v>1', reset='v=0', method='euler')\n", |
| 611 |
"M = SpikeMonitor(G)\n", |
| 612 |
"run(num_samples*ms)\n", |
| 613 |
"plot(M.t/ms, M.i, '.k', ms=3)\n", |
| 614 |
"xlim(0, num_samples)\n", |
| 615 |
"ylim(0, N)\n", |
| 616 |
"xlabel('Time (ms)')\n", |
| 617 |
"ylabel('Neuron index');" |
| 618 |
] |
| 619 |
} |
| 620 |
], |
| 621 |
"metadata": { |
| 622 |
"hide_input": false, |
| 623 |
"kernelspec": { |
| 624 |
"display_name": "Python 3", |
| 625 |
"language": "python", |
| 626 |
"name": "python3" |
| 627 |
}, |
| 628 |
"language_info": { |
| 629 |
"codemirror_mode": { |
| 630 |
"name": "ipython", |
| 631 |
"version": 3 |
| 632 |
}, |
| 633 |
"file_extension": ".py", |
| 634 |
"mimetype": "text/x-python", |
| 635 |
"name": "python", |
| 636 |
"nbconvert_exporter": "python", |
| 637 |
"pygments_lexer": "ipython3", |
| 638 |
"version": "3.8.0" |
| 639 |
} |
| 640 |
}, |
| 641 |
"nbformat": 4, |
| 642 |
"nbformat_minor": 2 |
| 643 |
} |