|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# test_issue_2629.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 | +Regression test for Issue #2629 (GitHub). |
| 24 | +
|
| 25 | +The issue was that ``DumpLayerConnections`` failed when a source layer was |
| 26 | +connected to more than one target layer. The test ensures that this is no |
| 27 | +longer the case. |
| 28 | +
|
| 29 | +For each connection between the specified source and target layer, |
| 30 | +``DumpLayerConnections`` writes the following to file: |
| 31 | +
|
| 32 | + source_node_id target_node_id weight delay dx dy [dz] |
| 33 | +
|
| 34 | +where (dx, dy [, dz]) is the displacement from source to target node. |
| 35 | +
|
| 36 | +This test uses the ``tmp_path`` Pytest fixture, which will provide a |
| 37 | +temporary directory unique to the test invocation. ``tmp_path`` is a |
| 38 | +``pathlib.Path`` object. Hence, the test also implicitly verifies that it |
| 39 | +is possible to pass a ``pathlib.Path`` object as filename. |
| 40 | +""" |
| 41 | + |
| 42 | +import pytest |
| 43 | + |
| 44 | +import nest |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture(scope="module") |
| 48 | +def network(): |
| 49 | + """Fixture for building network.""" |
| 50 | + |
| 51 | + grid = nest.spatial.grid(shape=[2, 1]) |
| 52 | + src_layer = nest.Create("iaf_psc_alpha", positions=grid) |
| 53 | + tgt_layer_1 = nest.Create("iaf_psc_alpha", positions=grid) |
| 54 | + tgt_layer_2 = nest.Create("iaf_psc_alpha", positions=grid) |
| 55 | + |
| 56 | + nest.Connect(src_layer, tgt_layer_1, "all_to_all") |
| 57 | + nest.Connect(src_layer, tgt_layer_2, "one_to_one") |
| 58 | + |
| 59 | + return src_layer, tgt_layer_1, tgt_layer_2 |
| 60 | + |
| 61 | + |
| 62 | +def test_dump_layer_connections_target_1(tmp_path, network): |
| 63 | + """Test that dumping connections with target layer 1 works.""" |
| 64 | + |
| 65 | + src_layer, tgt_layer_1, _ = network |
| 66 | + |
| 67 | + fname_1 = tmp_path / "conns_1.txt" |
| 68 | + nest.DumpLayerConnections(src_layer, tgt_layer_1, "static_synapse", fname_1) |
| 69 | + expected_dump_1 = [ |
| 70 | + "1 3 1 1 0 0", |
| 71 | + "1 4 1 1 0.5 0", |
| 72 | + "2 3 1 1 -0.5 0", |
| 73 | + "2 4 1 1 0 0", |
| 74 | + ] |
| 75 | + actual_dump_1 = fname_1.read_text().splitlines() |
| 76 | + assert actual_dump_1 == expected_dump_1 |
| 77 | + |
| 78 | + |
| 79 | +def test_dump_layer_connections_target_2(tmp_path, network): |
| 80 | + """Test that dumping connections with target layer 2 works.""" |
| 81 | + |
| 82 | + src_layer, _, tgt_layer_2 = network |
| 83 | + |
| 84 | + fname_2 = tmp_path / "conns_2.txt" |
| 85 | + nest.DumpLayerConnections(src_layer, tgt_layer_2, "static_synapse", fname_2) |
| 86 | + expected_dump_2 = [ |
| 87 | + "1 5 1 1 0 0", |
| 88 | + "2 6 1 1 0 0", |
| 89 | + ] |
| 90 | + actual_dump_2 = fname_2.read_text().splitlines() |
| 91 | + assert actual_dump_2 == expected_dump_2 |
0 commit comments