|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# test_iaf_psc_alpha_multisynapse.py |
| 4 | +# |
| 5 | +# This file is part of NEST. |
| 6 | +# |
| 7 | +# Copyright (C) 2004 The NEST Initiative |
| 8 | +# |
| 9 | +# NEST is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 2 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# NEST is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with NEST. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +""" |
| 23 | +Test ``iaf_psc_alpha_multisynapse`` recordables and simulated PSCs against expectation. |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +import nest |
| 28 | +import numpy as np |
| 29 | +import numpy.testing as nptest |
| 30 | +import pytest |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(autouse=True) |
| 34 | +def reset(): |
| 35 | + nest.ResetKernel() |
| 36 | + |
| 37 | + |
| 38 | +def alpha_fn(t, tau_syn): |
| 39 | + vals = np.zeros_like(t) |
| 40 | + zero_inds = t <= 0.0 |
| 41 | + nonzero_inds = ~zero_inds |
| 42 | + vals[nonzero_inds] = np.e / tau_syn * t[nonzero_inds] * np.exp(-t[nonzero_inds] / tau_syn) |
| 43 | + return vals |
| 44 | + |
| 45 | + |
| 46 | +def test_I_syn_1_in_recordables(): |
| 47 | + """Test that ``I_syn_1`` is in the list of recordables.""" |
| 48 | + |
| 49 | + nrn = nest.Create("iaf_psc_alpha_multisynapse") |
| 50 | + assert "I_syn_1" in nrn.get("recordables") |
| 51 | + |
| 52 | + |
| 53 | +def test_resize_recordables(): |
| 54 | + """ |
| 55 | + Test resizing of recordables. |
| 56 | +
|
| 57 | + This test ensures that recordables are updated correctly when the number |
| 58 | + of synaptic ports are changed. |
| 59 | + """ |
| 60 | + |
| 61 | + tau_syn1 = [5.0, 1.0, 25.0] |
| 62 | + tau_syn2 = [5.0, 1.0] |
| 63 | + tau_syn3 = [5.0, 1.0, 25.0, 50.0] |
| 64 | + |
| 65 | + nrn = nest.Create("iaf_psc_alpha_multisynapse", params={"tau_syn": tau_syn1}) |
| 66 | + assert len(nrn.recordables) == 5 |
| 67 | + |
| 68 | + nrn.set(tau_syn=tau_syn2) |
| 69 | + assert len(nrn.recordables) == 4 |
| 70 | + |
| 71 | + nrn.set(tau_syn=tau_syn3) |
| 72 | + assert len(nrn.recordables) == 6 |
| 73 | + |
| 74 | + |
| 75 | +def test_simulation_against_analytical_soln(): |
| 76 | + """ |
| 77 | + Test simulated PSCs against analytical expectation. |
| 78 | +
|
| 79 | + This test checks that the integration of the alpha-shaped currents of inputs |
| 80 | + from multiple different synaptic ports are the same as the analytical solution. |
| 81 | + """ |
| 82 | + |
| 83 | + tau_syn = [2.0, 20.0, 60.0, 100.0] |
| 84 | + delays = [100.0, 200.0, 500.0, 1200.0] |
| 85 | + weight = 1.0 |
| 86 | + spike_time = 10.0 |
| 87 | + simtime = 2500.0 |
| 88 | + |
| 89 | + nrn = nest.Create( |
| 90 | + "iaf_psc_alpha_multisynapse", |
| 91 | + params={ |
| 92 | + "C_m": 250.0, |
| 93 | + "E_L": 0.0, |
| 94 | + "V_m": 0.0, |
| 95 | + "V_th": 1500.0, |
| 96 | + "I_e": 0.0, |
| 97 | + "tau_m": 15.0, |
| 98 | + "tau_syn": tau_syn, |
| 99 | + }, |
| 100 | + ) |
| 101 | + sg = nest.Create("spike_generator", params={"spike_times": [spike_time]}) |
| 102 | + |
| 103 | + for i, syn_id in enumerate(range(1, 5)): |
| 104 | + syn_spec = {"synapse_model": "static_synapse", "delay": delays[i], "weight": weight, "receptor_type": syn_id} |
| 105 | + |
| 106 | + nest.Connect(sg, nrn, conn_spec="one_to_one", syn_spec=syn_spec) |
| 107 | + |
| 108 | + mm = nest.Create("multimeter", params={"record_from": ["I_syn_1", "I_syn_2", "I_syn_3", "I_syn_4"]}) |
| 109 | + |
| 110 | + nest.Connect(mm, nrn) |
| 111 | + nest.Simulate(simtime) |
| 112 | + times = mm.get("events", "times") |
| 113 | + I_syn = np.sum([mm.get("events", f"I_syn_{i}") for i in range(1, 5)], axis=0) |
| 114 | + |
| 115 | + I_syn_analytical = np.zeros_like(times, dtype=np.float64) |
| 116 | + for i in range(4): |
| 117 | + I_syn_analytical += alpha_fn(times - delays[i] - spike_time, tau_syn[i]) |
| 118 | + |
| 119 | + nptest.assert_array_almost_equal(I_syn, I_syn_analytical) |
0 commit comments