.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/testiaf.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_testiaf.py: IAF neuron example with current generator ----------------------------------------- A DC current is injected into the neuron using a current generator device. The membrane potential as well as the spiking activity are recorded by corresponding devices. It can be observed how the current charges the membrane, a spike is emitted, the neuron becomes absolute refractory, and finally starts to recover. .. GENERATED FROM PYTHON SOURCE LINES 37-38 First, we import all necessary modules for simulation and plotting .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: default import nest import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 43-58 Second the function ``build_network`` is defined to build the network and return the handles of the ``spike_recorder`` and the ``voltmeter``. The function takes the simulation resolution as argument The function first resets the simulation kernel and sets the number of threads and the simulation resolution. The ``iaf_psc_alpha`` neuron is created and the handle is stored in the variable `neuron`. The status of the neuron is changed so it receives an external current. Next a ``voltmeter`` and a ``spike_recorder`` are created and their handles stored in the variables `vm` and `sr` respectively. The voltmeter and spike recorder are then connected to the neuron. ``Connect`` takes the device and neuron handles as input. The voltmeter is connected to the neuron and the neuron to the spike recorder because the neuron sends spikes to the recorder and the voltmeter 'observes' the neuron. .. GENERATED FROM PYTHON SOURCE LINES 58-78 .. code-block:: default def build_network(dt): nest.ResetKernel() nest.local_num_threads = 1 nest.resolution = dt neuron = nest.Create('iaf_psc_alpha') neuron.I_e = 376.0 vm = nest.Create('voltmeter') sr = nest.Create('spike_recorder') nest.Connect(vm, neuron) nest.Connect(neuron, sr) return vm, sr .. GENERATED FROM PYTHON SOURCE LINES 79-81 The neuron is simulated for three different resolutions and then the voltage trace is plotted .. GENERATED FROM PYTHON SOURCE LINES 81-88 .. code-block:: default for dt in [0.1, 0.5, 1.0]: print(f"Running simulation with dt={dt:.2f}") vm, sr = build_network(dt) nest.Simulate(1000.0) .. GENERATED FROM PYTHON SOURCE LINES 89-98 The network is simulated using ``Simulate``, which takes the desired simulation time in milliseconds and advances the network state by this amount of time. During simulation, the ``spike_recorder`` counts the spikes of the target neuron and the total number is read out at the end of the simulation period. The values of the voltage recorded by the voltmeter are read out and the values for the membrane potential are stored in potential and the corresponding times in the times array .. GENERATED FROM PYTHON SOURCE LINES 98-102 .. code-block:: default potentials = vm.get('events', 'V_m') times = vm.get('events', 'times') .. GENERATED FROM PYTHON SOURCE LINES 103-104 Using the matplotlib library the voltage trace is plotted over time .. GENERATED FROM PYTHON SOURCE LINES 104-108 .. code-block:: default plt.plot(times, potentials, label=f"dt={dt:.2f}") print(f" Number of spikes: {sr.n_events}") .. GENERATED FROM PYTHON SOURCE LINES 109-110 Finally the axis are labelled and a legend is generated .. GENERATED FROM PYTHON SOURCE LINES 110-116 .. code-block:: default plt.legend(loc=3) plt.xlabel("time (ms)") plt.ylabel("V_m (mV)") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_testiaf.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: testiaf.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: testiaf.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_