Skip to content

Commit

Permalink
Add test for specific random values
Browse files Browse the repository at this point in the history
  • Loading branch information
mstimberg committed Sep 10, 2024
1 parent 77ed84f commit aa12853
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion brian2/tests/test_neurongroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from brian2.core.network import Network
from brian2.core.preferences import prefs
from brian2.core.variables import linked_var
from brian2.devices.device import device, seed
from brian2.devices.device import device, get_device, seed
from brian2.equations.equations import Equations
from brian2.groups.group import get_dtype
from brian2.groups.neurongroup import NeuronGroup
Expand Down Expand Up @@ -1986,6 +1986,62 @@ def test_random_values_fixed_seed():
assert_allclose(G.v1[:], G.v2[:])


_random_values = {
("RuntimeDevice", "auto", None): (
[0.1636023, 0.76229608, 0.74945305, 0.82121212, 0.82669968],
[-0.7758696, 0.13295831, 0.87360834, -1.21879122, 0.62980314],
),
("RuntimeDevice", "cython", None): (
[0.1636023, 0.76229608, 0.74945305, 0.82121212, 0.82669968],
[-0.7758696, 0.13295831, 0.87360834, -1.21879122, 0.62980314],
),
("CPPStandaloneDevice", "cython", 1): (
[0.1636023, 0.76229608, 0.74945305, 0.82121212, 0.82669968],
[-0.7758696, 0.13295831, 0.87360834, -1.21879122, 0.62980314],
),
("CPPStandaloneDevice", None, 4): (
[0.1636023, 0.76229608, 0.27318909, 0.44124824, 0.69454226],
[0.36643979, -1.53883951, 0.07274151, 1.34278769, 0.63249739],
),
}


def _config_tuple():
config = [
get_device().__class__.__name__,
prefs.codegen.target,
prefs.devices.cpp_standalone.openmp_threads,
]
if config[0] == "RuntimeDevice":
config[2] = None
else:
config[1] = None
return tuple(config)


@pytest.mark.skipif(
_config_tuple() not in _random_values,
reason="Random values not known for this configuration",
)
@pytest.mark.standalone_compatible
def test_random_values_fixed_seed_numbers():
# Verify a subset of random numbers, to make sure these numbers stay the same across updates
G = NeuronGroup(
100,
"""
v1 : 1
v2 : 1
""",
)
seed(9876)
G.v1 = "rand()"
G.v2 = "randn()"
run(0 * ms) # for standalone
expected_values = _random_values[_config_tuple()]
assert_allclose(G.v1[::20], expected_values[0])
assert_allclose(G.v2[::20], expected_values[1])


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_random_values_fixed_and_random():
Expand Down

0 comments on commit aa12853

Please sign in to comment.