Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SLI tests to Python #2670

Merged
merged 5 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions testsuite/pytests/sli2py_neurons/test_model_node_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
#
# test_model_node_init.py
#
# This file is part of NEST.
#
# Copyright (C) 2004 The NEST Initiative
#
# NEST is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# NEST is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.


"""
Makeshift test to see if setting model params and then creating a neuron
and creating a neuron and then setting node params lead to the same
results.

Works by connecting device to iaf_psc_alpha, measuring voltage trace over 1s
and comparing traces.
"""

import pytest
import nest


def _get_network_state(nc):
neuron = nest.Create("iaf_psc_alpha")
voltmeter = nest.Create("voltmeter")

nest.Connect(nc, neuron)
nest.Connect(voltmeter, neuron)
nest.Simulate(1000)
volts = voltmeter.get("events")["V_m"]

return (volts, nc.get())


@pytest.fixture()
def model_data():
return {"model": "mip_generator", "params": {"rate": 100.0, "p_copy": 0.5}}


@pytest.fixture()
def use_set_defaults(model_data):
nest.ResetKernel()
nest.set(overwrite_files=True)
nest.SetDefaults(model_data["model"], model_data["params"])
model_instance = nest.Create(model_data["model"])
return _get_network_state(model_instance)


@pytest.fixture()
def use_set_status(model_data):
nest.ResetKernel()
nest.set(overwrite_files=True)
model_instance = nest.Create(model_data["model"])
model_instance.set(**model_data["params"])
return _get_network_state(model_instance)


def test_set_status_vs_set_defaults(use_set_defaults, use_set_status):
assert (use_set_defaults[0] == use_set_status[0]).all()
assert use_set_defaults[1] == use_set_status[1]
90 changes: 90 additions & 0 deletions testsuite/pytests/sli2py_synapses/test_common_props_setting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
#
# test_common_props_setting.py
#
# This file is part of NEST.
#
# Copyright (C) 2004 The NEST Initiative
#
# NEST is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# NEST is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.


"""
Name: testsuite::test_common_props_setting - test that common properties can be set as defaults, not else

Synopsis: (test_common_props_setting) run -> compare response with reference data

Description:
For synapses with common properties, ensure that common and individual properties can be set by
SetDefaults and CopyModel, but that an exception is raised if they are set via an individual connection.

FirstVersion: November 2014

Author: Hans E Plesser
"""

import nest
import pytest


@pytest.fixture(autouse=True)
def prepare():
nest.ResetKernel()
volt = nest.Create("volume_transmitter")
nest.SetDefaults("stdp_dopamine_synapse", {"vt": volt.tolist()[0]})


@pytest.mark.parametrize('synapse', ["stdp_synapse_hom", "stdp_pl_synapse_hom", "stdp_facetshw_synapse_hom",
"stdp_dopamine_synapse"])
class TestSettingCommonProps:

def test_setting_common_props_on_original(self, synapse):
expected_values = {"tau_plus": 5.0, "weight": 2.0}
nest.SetDefaults(synapse, expected_values)

actual_values = nest.GetDefaults(synapse, keys=expected_values.keys())
assert actual_values == tuple(expected_values.values())

def test_setting_common_props_on_copy(self, synapse):
copied_syn = f"{synapse}_copy"
expected_values = {"tau_plus": 15.0, "weight": 20.0}
nest.CopyModel(synapse, copied_syn, expected_values)

actual_values = nest.GetDefaults(copied_syn, keys=expected_values.keys())
assert actual_values == tuple(expected_values.values())

def test_setting_non_common_props_on_instance(self, synapse):

neuron = nest.Create("iaf_psc_alpha")
nest.Connect(neuron, neuron, syn_spec={'synapse_model': synapse})

single_edge = nest.GetConnections(source=neuron)[0]
single_edge.set(weight=3.0)

assert single_edge.get("weight") == 3.0
assert nest.GetDefaults(synapse)["weight"] != 3.0

def test_setting_common_props_on_instance(self, synapse):

tau_plus_ref = nest.GetDefaults(synapse)["tau_plus"]
neuron = nest.Create("iaf_psc_alpha")
nest.Connect(neuron, neuron, syn_spec={'synapse_model': synapse})

single_edge = nest.GetConnections(source=neuron)[0]

with pytest.raises(Exception):
single_edge.set(tau_plus=(tau_plus_ref + 1) * 3)

actual_tau_plus_value = nest.GetDefaults(synapse)["tau_plus"]
assert actual_tau_plus_value == tau_plus_ref
94 changes: 0 additions & 94 deletions testsuite/unittests/model_node_init.sli

This file was deleted.