1 2import numpy as np 3from numpy import nan 4from numpy.testing import assert_array_equal, assert_allclose 5from nose.tools import assert_equal 6from .registry import register 7 8 9@register() 10def issue241(sim): 11 # "Nest SpikeSourcePoisson populations require all parameters to be passed to constructor" 12 sim.setup() 13 spike_train1 = sim.Population(1, sim.SpikeSourcePoisson, {'rate': [5], 'start': [1000], 'duration': [1234]}) 14 spike_train2 = sim.Population(2, sim.SpikeSourcePoisson, {'rate': [5, 6], 'start': [1000, 1001], 'duration': [1234, 2345]}) 15 spike_train3 = sim.Population(1, sim.SpikeSourcePoisson, {'rate': [5], 'start': [1000], 'duration': 1234}) 16 spike_train4 = sim.Population(1, sim.SpikeSourcePoisson, {'rate': [5], 'start': [1000]}) 17 spike_train5 = sim.Population(2, sim.SpikeSourcePoisson, {'rate': [5, 6], 'start': [1000, 1001]}) 18 assert_array_equal(spike_train2.get('duration'), np.array([1234, 2345])) 19 assert_equal(spike_train3.get(['rate', 'start', 'duration']), [5, 1000, 1234]) 20 sim.end() 21 22 23@register() 24def issue302(sim): 25 # "Setting attributes fails for Projections where either the pre- or post-synaptic Population has size 1" 26 sim.setup() 27 p1 = sim.Population(1, sim.IF_cond_exp()) 28 p5 = sim.Population(5, sim.IF_cond_exp()) 29 prj15 = sim.Projection(p1, p5, sim.AllToAllConnector()) 30 prj51 = sim.Projection(p5, p1, sim.AllToAllConnector()) 31 prj55 = sim.Projection(p5, p5, sim.AllToAllConnector()) 32 prj15.set(weight=0.123) 33 prj51.set(weight=0.123) 34 prj55.set(weight=0.123) 35 sim.end() 36 37 38@register() 39def test_set_synaptic_parameters_fully_connected(sim): 40 sim.setup() 41 mpi_rank = sim.rank() 42 p1 = sim.Population(4, sim.IF_cond_exp()) 43 p2 = sim.Population(2, sim.IF_cond_exp()) 44 syn = sim.TsodyksMarkramSynapse(U=0.5, weight=0.123, delay=0.1) 45 prj = sim.Projection(p1, p2, sim.AllToAllConnector(), syn) 46 47 expected = np.array([ 48 (0.0, 0.0, 0.123, 0.1, 0.5), 49 (0.0, 1.0, 0.123, 0.1, 0.5), 50 (1.0, 0.0, 0.123, 0.1, 0.5), 51 (1.0, 1.0, 0.123, 0.1, 0.5), 52 (2.0, 0.0, 0.123, 0.1, 0.5), 53 (2.0, 1.0, 0.123, 0.1, 0.5), 54 (3.0, 0.0, 0.123, 0.1, 0.5), 55 (3.0, 1.0, 0.123, 0.1, 0.5), 56 ]) 57 actual = np.array(prj.get(['weight', 'delay', 'U'], format='list')) 58 if mpi_rank == 0: 59 ind = np.lexsort((actual[:, 1], actual[:, 0])) 60 assert_allclose(actual[ind], expected, 1e-15) 61 62 positional_weights = np.array([[0, 1], [2, 3], [4, 5], [6, 7]], dtype=float) 63 prj.set(weight=positional_weights) 64 expected = positional_weights 65 actual = prj.get('weight', format='array') 66 if mpi_rank == 0: 67 assert_array_equal(actual, expected) 68 69 u_list = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2] 70 prj.set(U=u_list) 71 expected = np.array([[0.9, 0.8], [0.7, 0.6], [0.5, 0.4], [0.3, 0.2]]) 72 actual = prj.get('U', format='array') 73 if mpi_rank == 0: 74 assert_array_equal(actual, expected) 75 76 f_delay = lambda d: 0.5 + d 77 prj.set(delay=f_delay) 78 expected = np.array([[0.5, 1.5], [1.5, 0.5], [2.5, 1.5], [3.5, 2.5]]) 79 actual = prj.get('delay', format='array') 80 if mpi_rank == 0: 81 assert_array_equal(actual, expected) 82 83 # final sanity check 84 expected = np.array([ 85 (0.0, 0.0, 0.0, 0.5, 0.9), 86 (0.0, 1.0, 1.0, 1.5, 0.8), 87 (1.0, 0.0, 2.0, 1.5, 0.7), 88 (1.0, 1.0, 3.0, 0.5, 0.6), 89 (2.0, 0.0, 4.0, 2.5, 0.5), 90 (2.0, 1.0, 5.0, 1.5, 0.4), 91 (3.0, 0.0, 6.0, 3.5, 0.3), 92 (3.0, 1.0, 7.0, 2.5, 0.2), 93 ]) 94 actual = np.array(prj.get(['weight', 'delay', 'U'], format='list')) 95 if mpi_rank == 0: 96 ind = np.lexsort((actual[:, 1], actual[:, 0])) 97 assert_array_equal(actual[ind], expected) 98test_set_synaptic_parameters_fully_connected.__test__ = False 99 100 101@register() 102def test_set_synaptic_parameters_partially_connected(sim): 103 sim.setup() 104 mpi_rank = sim.rank() 105 p1 = sim.Population(4, sim.IF_cond_exp()) 106 p2 = sim.Population(2, sim.IF_cond_exp()) 107 syn = sim.TsodyksMarkramSynapse(U=0.5, weight=0.123, delay=0.1) 108 prj = sim.Projection(p1, p2, sim.FromListConnector([(0, 0), (3, 0), (1, 1), (1, 0), (2, 1)]), syn) 109 110 expected = np.array([ 111 (0.0, 0.0, 0.123, 0.1, 0.5), 112 (1.0, 0.0, 0.123, 0.1, 0.5), 113 (1.0, 1.0, 0.123, 0.1, 0.5), 114 (2.0, 1.0, 0.123, 0.1, 0.5), 115 (3.0, 0.0, 0.123, 0.1, 0.5), 116 ]) 117 actual = np.array(prj.get(['weight', 'delay', 'U'], format='list')) 118 if mpi_rank == 0: 119 ind = np.lexsort((actual[:, 1], actual[:, 0])) 120 assert_allclose(actual[ind], expected, 1e-15) 121 122 positional_weights = np.array([[0, nan], [2, 3], [nan, 5], [6, nan]], dtype=float) 123 prj.set(weight=positional_weights) 124 expected = positional_weights 125 actual = prj.get('weight', format='array') 126 if mpi_rank == 0: 127 assert_array_equal(actual, expected) 128 129 u_list = [0.9, 0.8, 0.7, 0.6, 0.5] 130 prj.set(U=u_list) 131 expected = np.array([[0.9, nan], [0.8, 0.7], [nan, 0.6], [0.5, nan]]) 132 actual = prj.get('U', format='array') 133 if mpi_rank == 0: 134 assert_array_equal(actual, expected) 135 136 f_delay = lambda d: 0.5 + d 137 prj.set(delay=f_delay) 138 expected = np.array([[0.5, nan], [1.5, 0.5], [nan, 1.5], [3.5, nan]]) 139 actual = prj.get('delay', format='array') 140 if mpi_rank == 0: 141 assert_array_equal(actual, expected) 142 143 # final sanity check 144 expected = np.array([ 145 (0.0, 0.0, 0.0, 0.5, 0.9), 146 (1.0, 0.0, 2.0, 1.5, 0.8), 147 (1.0, 1.0, 3.0, 0.5, 0.7), 148 (2.0, 1.0, 5.0, 1.5, 0.6), 149 (3.0, 0.0, 6.0, 3.5, 0.5), 150 ]) 151 actual = np.array(prj.get(['weight', 'delay', 'U'], format='list')) 152 if mpi_rank == 0: 153 ind = np.lexsort((actual[:, 1], actual[:, 0])) 154 assert_array_equal(actual[ind], expected) 155test_set_synaptic_parameters_partially_connected.__test__ = False 156 157 158@register() 159def test_set_synaptic_parameters_multiply_connected(sim): 160 sim.setup() 161 mpi_rank = sim.rank() 162 p1 = sim.Population(4, sim.IF_cond_exp()) 163 p2 = sim.Population(2, sim.IF_cond_exp()) 164 syn = sim.TsodyksMarkramSynapse(U=0.5, weight=0.123, delay=0.1) 165 prj = sim.Projection(p1, p2, sim.FromListConnector([(0, 0), (1, 0), (3, 0), (1, 1), (1, 0), (2, 1)]), syn) 166 167 expected = np.array([ 168 (0.0, 0.0, 0.123, 0.1, 0.5), 169 (1.0, 0.0, 0.123, 0.1, 0.5), 170 (1.0, 0.0, 0.123, 0.1, 0.5), 171 (1.0, 1.0, 0.123, 0.1, 0.5), 172 (2.0, 1.0, 0.123, 0.1, 0.5), 173 (3.0, 0.0, 0.123, 0.1, 0.5), 174 ]) 175 actual = np.array(prj.get(['weight', 'delay', 'U'], format='list')) 176 if mpi_rank == 0: 177 ind = np.lexsort((actual[:, 1], actual[:, 0])) 178 assert_allclose(actual[ind], expected, 1e-15) 179 180 positional_weights = np.array([[0, nan], [2, 3], [nan, 5], [6, nan]], dtype=float) 181 prj.set(weight=positional_weights) 182 expected = np.array([ 183 (0.0, 0.0, 0.0), 184 (1.0, 0.0, 2.0), 185 (1.0, 0.0, 2.0), 186 (1.0, 1.0, 3.0), 187 (2.0, 1.0, 5.0), 188 (3.0, 0.0, 6.0), 189 ]) 190 actual = np.array(prj.get('weight', format='list')) 191 if mpi_rank == 0: 192 ind = np.lexsort((actual[:, 1], actual[:, 0])) 193 assert_allclose(actual[ind], expected, 1e-15) 194 195 # postponing implementation of this functionality until after 0.8.0 196 # u_list = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4] 197 # prj.set(U=u_list) 198 # expected = np.array([ 199 # (0.0, 0.0, 0.9), 200 # (1.0, 0.0, 0.8), 201 # (1.0, 0.0, 0.7), 202 # (1.0, 1.0, 0.6), 203 # (2.0, 1.0, 0.5), 204 # (3.0, 0.0, 0.4), 205 # ]) 206 # actual = np.array(prj.get('U', format='list')) 207 # if mpi_rank == 0: 208 # ind = np.lexsort((actual[:, 1], actual[:, 0])) 209 # assert_allclose(actual[ind], expected, 1e-16) 210 211 f_delay = lambda d: 0.5 + d 212 prj.set(delay=f_delay) 213 expected = np.array([ 214 (0.0, 0.0, 0.5), 215 (1.0, 0.0, 1.5), 216 (1.0, 0.0, 1.5), 217 (1.0, 1.0, 0.5), 218 (2.0, 1.0, 1.5), 219 (3.0, 0.0, 3.5), 220 ]) 221 actual = np.array(prj.get('delay', format='list')) 222 if mpi_rank == 0: 223 ind = np.lexsort((actual[:, 1], actual[:, 0])) 224 assert_allclose(actual[ind], expected, 1e-15) 225 226 # final sanity check 227 expected = np.array([ 228 (0.0, 0.0, 0.0, 0.5, 0.5), 229 (1.0, 0.0, 2.0, 1.5, 0.5), 230 (1.0, 0.0, 2.0, 1.5, 0.5), 231 (1.0, 1.0, 3.0, 0.5, 0.5), 232 (2.0, 1.0, 5.0, 1.5, 0.5), 233 (3.0, 0.0, 6.0, 3.5, 0.5), 234 ]) 235 actual = np.array(prj.get(['weight', 'delay', 'U'], format='list')) 236 if mpi_rank == 0: 237 ind = np.lexsort((actual[:, 1], actual[:, 0])) 238 assert_array_equal(actual[ind], expected) 239test_set_synaptic_parameters_multiply_connected.__test__ = False 240 241 242@register() 243def issue505(sim): 244 sim.setup(timestep=0.05, min_delay=0.05) 245 p = sim.Population(2, sim.IF_cond_exp()) 246 projection = sim.Projection(p, p, sim.AllToAllConnector(), sim.TsodyksMarkramSynapse(U=0.543)) 247 U = projection.get('U', format='list', with_address=False) 248 assert_equal(U, [0.543, 0.543, 0.543, 0.543]) 249 delay = projection.get('delay', format='list', with_address=False) 250 assert_equal(delay, [0.05, 0.05, 0.05, 0.05]) 251 252 253if __name__ == '__main__': 254 from pyNN.utility import get_simulator 255 sim, args = get_simulator() 256 issue241(sim) 257 issue302(sim) 258 test_set_synaptic_parameters_fully_connected(sim) 259 test_set_synaptic_parameters_partially_connected(sim) 260 issue505(sim)