.. 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 :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_testiaf.py: IAF neuron example with current generator ----------------------------------------- .. only:: html ---- Run this example as a Jupyter notebook: .. card:: :width: 25% :margin: 2 :text-align: center :link: https://lab.ebrains.eu/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2Fnest%2Fnest-simulator-examples&urlpath=lab%2Ftree%2Fnest-simulator-examples%2Fnotebooks%2Fnotebooks%2Ftestiaf.ipynb&branch=main :link-alt: JupyterHub service .. image:: https://nest-simulator.org/TryItOnEBRAINS.png .. grid:: 1 1 1 1 :padding: 0 0 2 0 .. grid-item:: :class: sd-text-muted :margin: 0 0 3 0 :padding: 0 0 3 0 :columns: 4 See :ref:`our guide ` for more information and troubleshooting. ---- 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:: Python import matplotlib.pyplot as plt import nest .. 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-77 .. code-block:: Python 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 78-80 The neuron is simulated for three different resolutions and then the voltage trace is plotted .. GENERATED FROM PYTHON SOURCE LINES 80-115 .. code-block:: Python 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) ########################################################################### # 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 potentials = vm.get("events", "V_m") times = vm.get("events", "times") ########################################################################### # Using the matplotlib library the voltage trace is plotted over time plt.plot(times, potentials, label=f"dt={dt:.2f}") print(f" Number of spikes: {sr.n_events}") ########################################################################### # Finally the axis are labelled and a legend is generated plt.legend(loc=3) plt.xlabel("time (ms)") plt.ylabel("V_m (mV)") plt.show() .. _sphx_glr_download_auto_examples_testiaf.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: testiaf.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: testiaf.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_