~ubuntu-branches/ubuntu/precise/brian/precise

« back to all changes in this revision

Viewing changes to brian/tests/testinterface/test_directcontrol.py

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2012-01-02 12:49:11 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120102124911-6r1rmqgt5vr22ro3
Tags: 1.3.1-1
* Fresh upstream release
* Boosted policy compliance to 3.9.2 (no changes)
* Added up_skip_tests_with_paths patch to avoid test failures on custom 
  test scripts with hardcoded paths

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from brian import *
2
2
from brian.utils.approximatecomparisons import *
 
3
 
3
4
from nose.tools import *
4
5
 
5
6
def test():
139
140
            assert is_approx_equal(s1 * msecond, s2) # the firing times are (0,1,2,3,4)ms
140
141
    test1(spikes)
141
142
 
 
143
    # check multiple spike generator group with arrays
 
144
    # NOTE: Units are not checked, array has to be in seconds
 
145
    spiketimes = [array([0.0, 0.002, 0.004]),
 
146
                  array([0.001, 0.003])]
 
147
    spikes = mininet(MultipleSpikeGeneratorGroup, spiketimes)
 
148
    test1(spikes)
 
149
    
 
150
    # multiple spike generator group with generator and period
 
151
    def gen1():
 
152
        yield 0 * msecond
 
153
    def gen2():
 
154
        yield 1 * msecond
 
155
    spikes = mininet(MultipleSpikeGeneratorGroup, [gen1, gen2], period=2*ms)
 
156
    test1(spikes)
 
157
 
142
158
    # check that given a different clock it works as expected, wrap in a function to stop magic functions from
143
159
    # picking up the clock objects we define here
144
160
    def testwithclock():
173
189
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes)
174
190
    test1(spikes)
175
191
 
 
192
    # spike generator with list (already sorted so pass sort=False)
 
193
    spiketimes = [(0, 0 * msecond), (1, 1 * msecond), (0, 2 * msecond), (1, 3 * msecond), (0, 4 * msecond) ]
 
194
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes, sort=False)
 
195
    test1(spikes)
 
196
 
 
197
    # spike generator with unsorted (inversely sorted) list (sort=True is default)
 
198
    spiketimes = [(0, 4 * msecond), (1, 3 * msecond), (0, 2 * msecond), (1, 1 * msecond), (0, 0 * msecond) ]
 
199
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes)
 
200
    test1(spikes)
 
201
 
176
202
    # check that it works with a clock
177
203
    def testwithclock():
178
204
        spikes = mininet(SpikeGeneratorGroup, 2, spiketimes, clock=Clock(dt=0.1 * msecond))
184
210
            assert is_approx_equal(s1 * msecond, s2) # the firing times are (0,2,2,4,4)ms
185
211
    testwithclock()
186
212
 
 
213
    # spike generator with a function returning a list
 
214
    def return_spikes():
 
215
        return [(0, 0 * msecond), (1, 1 * msecond), (0, 2 * msecond),
 
216
                (1, 3 * msecond), (0, 4 * msecond) ]
 
217
        
 
218
    spikes = mininet(SpikeGeneratorGroup, 2, return_spikes)
 
219
    test1(spikes)    
 
220
 
 
221
    # spike generator with a list of spikes with simultaneous spikes across neurons
 
222
    spiketimes = [(0, 0 * msecond), (1, 0 * msecond), (0, 2 * msecond),
 
223
                (1, 2 * msecond), (0, 4 * msecond), (1, 4 * msecond) ]
 
224
    def test2(spikes):            
 
225
        assert len(spikes) == 6
 
226
        #check both neurons spiked at the correct times
 
227
        for neuron in [0, 1]:
 
228
            for s1, s2 in zip([0, 2, 4], [t for i, t in spikes if i==neuron]):
 
229
                assert is_approx_equal(s1 * msecond, s2) 
 
230
    
 
231
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes)
 
232
    test2(spikes)    
 
233
 
 
234
    # same but using the gather=True option
 
235
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes, gather=True)
 
236
    test2(spikes)    
 
237
 
 
238
    # same but with index arrays instead of single neuron indices
 
239
    spiketimes = [([0, 1], 0 * msecond), ([0, 1], 2 * msecond),
 
240
                  ([0, 1], 4 * msecond)]
 
241
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes)
 
242
    test2(spikes)    
 
243
 
 
244
    # spike generator with single indices and index arrays of varying length 
 
245
    spiketimes = [([0, 1], 0 * msecond), (0, 1 * msecond), (1, 2 * msecond), ([0], 3 * msecond) ]
 
246
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes)    
 
247
    def test3(spikes):            
 
248
        assert len(spikes) == 5
 
249
        #check both neurons spiked at the correct times
 
250
        for s1, s2 in zip([0, 1, 3], [t for i, t in spikes if i==0]):
 
251
            assert is_approx_equal(s1 * msecond, s2)
 
252
        for s1, s2 in zip([0, 2], [t for i, t in spikes if i==1]):
 
253
            assert is_approx_equal(s1 * msecond, s2)            
 
254
    test3(spikes)
 
255
 
 
256
    # spike generator with an array of (non-simultaneous) spikes
 
257
    # NOTE: For an array, the times have to be in seconds and sorted
 
258
    spiketimes = array([[0, 0.0], [1, 0.001], [0, 0.002], [1, 0.003], [0, 0.004]])
 
259
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes)
 
260
    test1(spikes)
 
261
 
 
262
    # spike generator with an array of (simultaneous) spikes
 
263
    spiketimes = array([[0, 0.0], [1, 0.0], [0, 0.002], [1, 0.002], [0, 0.004], [1, 0.004]])
 
264
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes)
 
265
    test2(spikes)
 
266
 
 
267
    # spike generator with an array of (simultaneous) spikes, using gather=True
 
268
    spiketimes = array([[0, 0.0], [1, 0.0], [0, 0.002], [1, 0.002], [0, 0.004], [1, 0.004]])
 
269
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes, gather=True)
 
270
    test2(spikes)
 
271
    
 
272
    # test the handling of an empty initialization and direct setting of spiketimes
 
273
    def test_attribute_setting():
 
274
        reinit_default_clock()        
 
275
        G = SpikeGeneratorGroup(2, [])
 
276
        M = SpikeMonitor(G, True)
 
277
        net = Network(G, M)
 
278
        net.run(5 * msecond)
 
279
        assert len(M.spikes) == 0
 
280
        reinit_default_clock()  
 
281
        net.reinit()
 
282
        G.spiketimes = [(0, 0 * msecond), (1, 1 * msecond), (0, 2 * msecond),
 
283
                        (1, 3 * msecond), (0, 4 * msecond)]
 
284
        net.run(5 * msecond)
 
285
        test1(M.spikes)
 
286
    test_attribute_setting()
 
287
    
 
288
    # tests a subtle difficulty when setting spiketimes and using a subgroup
 
289
    def test_attribute_setting_subgroup():
 
290
        reinit_default_clock()
 
291
        G = SpikeGeneratorGroup(2, [])
 
292
        subG = G.subgroup(2)
 
293
        M = SpikeMonitor(subG, True)
 
294
        G.spiketimes = [(0, 0 * msecond), (1, 1 * msecond), (0, 2 * msecond),
 
295
                        (1, 3 * msecond), (0, 4 * msecond)]
 
296
        G.spiketimes = [(0, 0 * msecond), (1, 1 * msecond), (0, 2 * msecond),
 
297
                        (1, 3 * msecond), (0, 4 * msecond)]
 
298
        net = Network(G, M)
 
299
        net.run(5 * msecond)
 
300
        test1(M.spikes)    
 
301
    test_attribute_setting_subgroup()
 
302
    
187
303
    # spike generator with generator
188
304
    def sg():
189
305
        yield (0, 0 * msecond)
199
315
    test1(spikes1)
200
316
    test1(spikes2)
201
317
 
 
318
    # spike generator group with generator and period
 
319
    def gen():
 
320
        yield (0, 0 * msecond)
 
321
        yield (1, 1 * msecond)
 
322
    spikes = mininet(SpikeGeneratorGroup, 2, gen, period=2*ms)
 
323
    test1(spikes)
 
324
 
 
325
    # spike generator group with list and period
 
326
    spiketimes = [(0, 0 * msecond), (1, 1 * msecond)]
 
327
    spikes = mininet(SpikeGeneratorGroup, 2, spiketimes, period=2*ms)
 
328
    test1(spikes)
 
329
 
202
330
    # pulse packet with 0 spread
203
331
    spikes = mininet(PulsePacket, 2.5 * msecond, 10, 0 * msecond)
204
332
    assert len(spikes) == 10
208
336
    # do not attempt to verify the behaviour of PulsePacket here, this is
209
337
    # an interface test only
210
338
 
 
339
def test_poissoninput():
 
340
    eqs = Equations("dv/dt=(1-v)/(1*second) : 1")
 
341
    group = NeuronGroup(N=1, model=eqs, reset=0, threshold=1)
 
342
    input = PoissonInput(group, N = 10, rate=50 * Hz, weight = .11, state='v')
 
343
    m = SpikeCounter(group)
 
344
    net = Network(group, input, m)
 
345
    net.run(500 * ms)
 
346
    #only checks that there some spikes
 
347
    assert (m.nspikes >= 1)
 
348
 
211
349
if __name__ == '__main__':
212
350
    test()
 
351
    test_poissoninput()