Skip to content

Commit

Permalink
wrap pynest.mark.parametrize to give a cleaner way to define any-simu…
Browse files Browse the repository at this point in the history
…lator tests
  • Loading branch information
apdavison committed Oct 14, 2022
1 parent 151fdd4 commit a79a351
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 256 deletions.
21 changes: 18 additions & 3 deletions test/system/scenarios/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import functools
import pytest

available_modules = {}
Expand All @@ -22,8 +22,23 @@
pass


class SimulatorNotAvailable:

def __init__(self, sim_name):
self.sim_name = sim_name

def setup(self, *args, **kwargs):
pytest.skip(f"{self.sim_name} not available")


def get_simulator(sim_name):
if sim_name in available_modules:
return available_modules[sim_name]
return pytest.param(available_modules[sim_name], id=sim_name)
else:
pytest.skip(f"{sim_name} not available")
return pytest.param(SimulatorNotAvailable(sim_name), id=sim_name)


def run_with_simulators(*sim_names):
sim_modules = (get_simulator(sim_name) for sim_name in sim_names)

return pytest.mark.parametrize("sim", sim_modules)
42 changes: 17 additions & 25 deletions test/system/scenarios/test_cell_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
import quantities as pq
from pyNN.parameters import Sequence
from pyNN.errors import InvalidParameterValueError
from .fixtures import get_simulator
from .fixtures import run_with_simulators
import pytest


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_EIF_cond_alpha_isfa_ista(sim_name, plot_figure=False):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_EIF_cond_alpha_isfa_ista(sim, plot_figure=False):
sim.setup(timestep=0.01, min_delay=0.1, max_delay=4.0)
ifcell = sim.create(sim.EIF_cond_alpha_isfa_ista(
i_offset=1.0, tau_refrac=2.0, v_spike=-40))
Expand All @@ -38,9 +37,8 @@ def test_EIF_cond_alpha_isfa_ista(sim_name, plot_figure=False):
return data


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_HH_cond_exp(sim_name, plot_figure=False):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_HH_cond_exp(sim, plot_figure=False):
sim.setup(timestep=0.001, min_delay=0.1)
cellparams = {
'gbar_Na': 20.0,
Expand Down Expand Up @@ -72,9 +70,8 @@ def test_HH_cond_exp(sim_name, plot_figure=False):


# exclude brian2 - see issue 370
@pytest.mark.parametrize("sim_name", ("nest", "neuron"))
def issue367(sim_name, plot_figure=False):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron")
def issue367(sim, plot_figure=False):
# AdEx dynamics for delta_T=0
sim.setup(timestep=0.001, min_delay=0.1, max_delay=4.0)
v_thresh = -50
Expand Down Expand Up @@ -104,9 +101,8 @@ def issue367(sim_name, plot_figure=False):
return data


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_SpikeSourcePoisson(sim_name, plot_figure=False):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_SpikeSourcePoisson(sim, plot_figure=False):
try:
from scipy.stats import kstest
except ImportError:
Expand Down Expand Up @@ -154,13 +150,12 @@ def test_SpikeSourcePoisson(sim_name, plot_figure=False):
return data


@pytest.mark.parametrize("sim_name", ("nest", "neuron"))
def test_SpikeSourceGamma(sim_name, plot_figure=False):
@run_with_simulators("nest", "neuron")
def test_SpikeSourceGamma(sim, plot_figure=False):
try:
from scipy.stats import kstest
except ImportError:
pytest.skip("scipy not available")
sim = get_simulator(sim_name)
sim.setup()
params = {
"beta": [100.0, 200.0, 1000.0],
Expand Down Expand Up @@ -211,13 +206,12 @@ def test_SpikeSourceGamma(sim_name, plot_figure=False):
return data


@pytest.mark.parametrize("sim_name", ("nest", "neuron"))
def test_SpikeSourcePoissonRefractory(sim_name, plot_figure=False):
@run_with_simulators("nest", "neuron")
def test_SpikeSourcePoissonRefractory(sim, plot_figure=False):
try:
from scipy.stats import kstest
except ImportError:
pytest.skip("scipy not available")
sim = get_simulator(sim_name)
sim.setup()
params = {
"rate": [100, 100, 50.0],
Expand Down Expand Up @@ -270,19 +264,17 @@ def test_SpikeSourcePoissonRefractory(sim_name, plot_figure=False):
return data


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_issue511(sim_name):
@run_with_simulators("nest", "neuron", "brian2")
def test_issue511(sim):
"""Giving SpikeSourceArray an array of non-ordered spike times should produce an InvalidParameterValueError error"""
sim = get_simulator(sim_name)
sim.setup()
celltype = sim.SpikeSourceArray(spike_times=[[2.4, 4.8, 6.6, 9.4], [3.5, 6.8, 9.6, 8.3]])
with pytest.raises(InvalidParameterValueError):
sim.Population(2, celltype)


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_update_SpikeSourceArray(sim_name, plot_figure=False):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_update_SpikeSourceArray(sim, plot_figure=False):
sim.setup()
sources = sim.Population(2, sim.SpikeSourceArray(spike_times=[]))
sources.record('spikes')
Expand Down
29 changes: 12 additions & 17 deletions test/system/scenarios/test_connection_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from numpy.testing import assert_array_equal
import numpy as np
from pyNN import common
from .fixtures import get_simulator
from .fixtures import run_with_simulators
import pytest


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_connections_attribute(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_connections_attribute(sim):
sim.setup()
p1 = sim.Population(4, sim.SpikeSourceArray())
p2 = sim.Population(3, sim.IF_cond_exp())
Expand All @@ -23,9 +22,8 @@ def test_connections_attribute(sim_name):
assert isinstance(connections[0], common.Connection)


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_connection_access_weight_and_delay(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_connection_access_weight_and_delay(sim):
sim.setup()
p1 = sim.Population(4, sim.SpikeSourceArray())
p2 = sim.Population(3, sim.IF_cond_exp())
Expand All @@ -46,12 +44,11 @@ def test_connection_access_weight_and_delay(sim_name):
target)


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_issue672(sim_name):
@run_with_simulators("nest", "neuron", "brian2")
def test_issue672(sim):
"""
Check that creating new Projections does not mess up existing ones.
"""
sim = get_simulator(sim_name)
sim.setup(verbosity="error")

p1 = sim.Population(5, sim.IF_curr_exp())
Expand All @@ -70,10 +67,9 @@ def test_issue672(sim_name):
assert_array_equal(wA, wB)


# @pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
# def test_update_synaptic_plasticity_parameters(sim_name):
# sim = get_simulator(sim_name)
# sim.setup()
# @run_with_simulators("nest", "neuron", "brian2")
# def test_update_synaptic_plasticity_parameters(sim):
# # sim.setup()
# p1 = sim.Population(3, sim.IF_cond_exp(), label="presynaptic")
# p2 = sim.Population(2, sim.IF_cond_exp(), label="postsynaptic")

Expand All @@ -93,10 +89,9 @@ def test_issue672(sim_name):
# np.array([[0.01, 0.011], [0.012, 0.013], [0.014, 0.015]]))


@pytest.mark.parametrize("sim_name", ("nest", "neuron"))
def test_issue652(sim_name):
@run_with_simulators("nest", "neuron")
def test_issue652(sim):
"""Correctly handle A_plus = 0 in SpikePairRule."""
sim = get_simulator(sim_name)
sim.setup()

neural_population1 = sim.Population(10, sim.IF_cond_exp())
Expand Down
52 changes: 21 additions & 31 deletions test/system/scenarios/test_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from numpy.testing import assert_array_equal, assert_allclose
from pyNN.random import NumpyRNG, RandomDistribution
from pyNN.utility import connection_plot, init_logging
from .fixtures import get_simulator
from .fixtures import run_with_simulators
import pytest


Expand All @@ -13,9 +13,8 @@
# TODO: add some tests with projections between Assemblies and PopulationViews


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_all_to_all_static_no_self(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_all_to_all_static_no_self(sim):
sim.setup()
p = sim.Population(5, sim.IF_cond_exp())
synapse_type = sim.StaticSynapse(weight=RandomDistribution('gamma', k=2.0, theta=0.5), delay="0.2+0.3*d")
Expand All @@ -29,9 +28,8 @@ def test_all_to_all_static_no_self(sim_name):
sim.end()


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_all_to_all_tsodyksmarkram(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_all_to_all_tsodyksmarkram(sim):
sim.setup()
p1 = sim.Population(5, sim.IF_cond_exp())
p2 = sim.Population(7, sim.IF_cond_exp())
Expand All @@ -48,9 +46,8 @@ def test_all_to_all_tsodyksmarkram(sim_name):
sim.end()


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_fixed_number_pre_no_replacement(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_fixed_number_pre_no_replacement(sim):
sim.setup()
p1 = sim.Population(5, sim.IF_cond_exp())
p2 = sim.Population(7, sim.IF_cond_exp())
Expand Down Expand Up @@ -80,9 +77,8 @@ def test_fixed_number_pre_no_replacement(sim_name):
sim.end()


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_fixed_number_pre_with_replacement(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_fixed_number_pre_with_replacement(sim):
sim.setup()
p1 = sim.Population(5, sim.IF_cond_exp())
p2 = sim.Population(7, sim.IF_cond_exp())
Expand All @@ -98,9 +94,8 @@ def test_fixed_number_pre_with_replacement(sim_name):
assert column.sum() == 1.5


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_fixed_number_pre_with_replacement_heterogeneous_parameters(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_fixed_number_pre_with_replacement_heterogeneous_parameters(sim):
sim.setup()
p1 = sim.Population(5, sim.IF_cond_exp())
p2 = sim.Population(7, sim.IF_cond_exp())
Expand All @@ -119,9 +114,8 @@ def test_fixed_number_pre_with_replacement_heterogeneous_parameters(sim_name):
sim.end()


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_fixed_number_post_no_replacement(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_fixed_number_post_no_replacement(sim):
sim.setup()
p1 = sim.Population(5, sim.IF_cond_exp())
p2 = sim.Population(7, sim.IF_cond_exp())
Expand Down Expand Up @@ -150,9 +144,8 @@ def test_fixed_number_post_no_replacement(sim_name):
sim.end()


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_fixed_number_post_with_replacement(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_fixed_number_post_with_replacement(sim):
sim.setup()
p1 = sim.Population(5, sim.IF_cond_exp())
p2 = sim.Population(7, sim.IF_cond_exp())
Expand All @@ -174,9 +167,8 @@ def test_fixed_number_post_with_replacement(sim_name):
assert row.sum() == (row.size - n_nan)*0.5


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_fixed_number_post_with_replacement_heterogeneous_parameters(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron", "brian2")
def test_fixed_number_post_with_replacement_heterogeneous_parameters(sim):
sim.setup()
p1 = sim.Population(5, sim.IF_cond_exp())
p2 = sim.Population(7, sim.IF_cond_exp())
Expand All @@ -195,10 +187,9 @@ def test_fixed_number_post_with_replacement_heterogeneous_parameters(sim_name):
sim.end()


@pytest.mark.parametrize("sim_name", ("nest", "neuron", "brian2"))
def test_issue309(sim_name):
@run_with_simulators("nest", "neuron", "brian2")
def test_issue309(sim):
# check that FixedProbability(1) gives the same results as AllToAll
sim = get_simulator(sim_name)
sim.setup()
p = sim.Population(5, sim.IF_cond_exp())
synapse_type = sim.StaticSynapse(weight=0.1, delay="0.2+0.3*d")
Expand All @@ -212,9 +203,8 @@ def test_issue309(sim_name):
sim.end()


@pytest.mark.parametrize("sim_name", ("nest", "neuron"))
def test_issue622(sim_name):
sim = get_simulator(sim_name)
@run_with_simulators("nest", "neuron")
def test_issue622(sim):
sim.setup()
pop = sim.Population(10, sim.IF_cond_exp, {}, label="pop")

Expand Down

0 comments on commit a79a351

Please sign in to comment.