File size: 3,822 Bytes
e4cdd5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | """Tests for network builder."""
import pytest
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import neurocore as nc
from neurocore.exceptions import (
NetworkTooLargeError, WeightOutOfRangeError, NeurocoreError,
)
from neurocore.constants import MAX_CORES, NEURONS_PER_CORE
class TestPopulation:
def test_create_population(self):
net = nc.Network()
pop = net.population(64, label="test")
assert pop.size == 64
assert pop.label == "test"
assert pop.id == 0
def test_population_params_dict(self):
net = nc.Network()
pop = net.population(16, params={"threshold": 800, "leak": 5})
assert pop.params.threshold == 800
assert pop.params.leak == 5
assert pop.params.resting == 0 # default
def test_population_invalid_param(self):
net = nc.Network()
with pytest.raises(ValueError, match="Unknown neuron parameter"):
net.population(16, params={"bogus": 42})
def test_population_zero_size(self):
net = nc.Network()
with pytest.raises(ValueError, match="positive"):
net.population(0)
def test_population_slicing(self):
net = nc.Network()
pop = net.population(32)
s = pop[:8]
assert len(s) == 8
assert s.indices == list(range(8))
def test_population_single_index(self):
net = nc.Network()
pop = net.population(10)
s = pop[5]
assert len(s) == 1
assert s.indices == [5]
def test_population_negative_index(self):
net = nc.Network()
pop = net.population(10)
s = pop[-1]
assert s.indices == [9]
def test_population_index_out_of_range(self):
net = nc.Network()
pop = net.population(10)
with pytest.raises(IndexError):
pop[10]
class TestConnection:
def test_create_connection(self):
net = nc.Network()
a = net.population(8)
b = net.population(8)
conn = net.connect(a, b, topology="all_to_all", weight=200)
assert conn.source is a
assert conn.target is b
assert conn.weight == 200
def test_weight_out_of_range(self):
net = nc.Network()
a = net.population(8)
b = net.population(8)
with pytest.raises(WeightOutOfRangeError):
net.connect(a, b, weight=40000)
def test_invalid_compartment(self):
net = nc.Network()
a = net.population(8)
b = net.population(8)
with pytest.raises(ValueError, match="Compartment"):
net.connect(a, b, compartment=5)
def test_negative_weight(self):
net = nc.Network()
a = net.population(8)
b = net.population(8)
conn = net.connect(a, b, weight=-300)
assert conn.weight == -300
class TestNetwork:
def test_total_neurons(self):
net = nc.Network()
net.population(64)
net.population(16)
assert net.total_neurons() == 80
def test_validate_ok(self, small_network):
net, _, _ = small_network
warnings = net.validate()
assert warnings == []
def test_validate_too_large(self):
net = nc.Network()
# P13: 128 cores * 1024 neurons = 131072 max
net.population(MAX_CORES * NEURONS_PER_CORE + 1)
with pytest.raises(NetworkTooLargeError):
net.validate()
def test_validate_empty(self):
net = nc.Network()
warnings = net.validate()
assert "no neurons" in warnings[0].lower()
def test_repr(self):
net = nc.Network()
net.population(10)
assert "neurons=10" in repr(net)
|