~ubuntu-branches/ubuntu/wily/brian/wily

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2014-07-30 11:29:44 UTC
  • mfrom: (6.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20140730112944-ln0ogbq0kpyyuz47
Tags: 1.4.1-2
* Forgotten upload to unstable
* debian/control
  - policy boost to 3.9.5
  - updated Vcs- fields given migration to anonscm.d.o and provided -b
    debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from brian import *
2
2
from nose.tools import *
3
3
from brian.utils.approximatecomparisons import is_approx_equal
4
 
 
5
 
def test():
 
4
from brian.tests import repeat_with_global_opts
 
5
from brian.connections.construction import (random_matrix,
 
6
                                            random_matrix_fixed_column)
 
7
from scipy.sparse import issparse
 
8
 
 
9
def test_utility_functions():
 
10
    '''
 
11
    Test the (non-public) random_matrix and random_matrix_fixed_column
 
12
    functions.
 
13
    '''
 
14
    
 
15
    # Convenience functions to make the testing a bit easier
 
16
    def assert_size_and_value(matrix, shape, value):
 
17
        assert issparse(matrix)
 
18
        assert matrix.shape == shape
 
19
        rows, cols = matrix.nonzero()
 
20
        for row, col in zip(rows, cols):
 
21
            if callable(value):
 
22
                if value.func_code.co_argcount == 0:
 
23
                    assert matrix[row, col] == value()
 
24
                elif value.func_code.co_argcount == 2:
 
25
                    assert matrix[row, col] == value(row, col)
 
26
                else:
 
27
                    raise AssertionError('Illegal value argument')
 
28
            else:
 
29
                assert matrix[row, col] == value
 
30
 
 
31
                
 
32
    def assert_entries_per_column(matrix, n_entries):
 
33
        for col_idx in range(matrix.shape[1]):
 
34
            entries = matrix[:, col_idx].nonzero()[0]
 
35
            assert len(entries) == n_entries, 'number of entries is %d not %d!' % (len(entries), n_entries)
 
36
    
 
37
    # with fixed probability 
 
38
    p = 1.    
 
39
    r_m = random_matrix(2, 4, p=p)
 
40
    assert_size_and_value(r_m, (2, 4), 1.)
 
41
    
 
42
    # with fixed probability and value
 
43
    p, value = 1., 2.
 
44
    r_m = random_matrix(2, 4, p=p, value=value)
 
45
    assert_size_and_value(r_m, (2, 4), 2.)
 
46
    
 
47
    # Note: p can also be a function (like value), but as this is not
 
48
    # documented, this is also not tested
 
49
        
 
50
    # with a constant function for value
 
51
    value = lambda : 2.
 
52
    r_m = random_matrix(2, 4, p=1., value=value)
 
53
    assert_size_and_value(r_m, (2, 4), value)
 
54
    
 
55
    # with an index dependent function for value
 
56
    value = lambda i, j : (i != j) * 2.
 
57
    r_m = random_matrix(2, 4, p=1., value=value)
 
58
    assert_size_and_value(r_m, (2, 4), value)
 
59
    
 
60
 
 
61
    # Test random matrix with fixed number of entries per column
 
62
    # with fixed probability 
 
63
    p = 0.5    
 
64
    r_m = random_matrix_fixed_column(100, 4, p=p)
 
65
    assert_size_and_value(r_m, (100, 4), 1.)
 
66
    assert_entries_per_column(r_m, 50)
 
67
    
 
68
    # with fixed probability and value
 
69
    p, value = 0.5, 2.
 
70
    r_m = random_matrix_fixed_column(100, 4, p=p, value=value)
 
71
    assert_size_and_value(r_m, (100, 4), 2.)
 
72
    assert_entries_per_column(r_m, 50)
 
73
    
 
74
    # Note: p can also be a function (like value), but this is not documented
 
75
        
 
76
    # with a constant function for value
 
77
    value = lambda : 2.
 
78
    r_m = random_matrix_fixed_column(100, 4, p=0.5, value=value)
 
79
    assert_size_and_value(r_m, (100, 4), value)
 
80
    assert_entries_per_column(r_m, 50)
 
81
    
 
82
    # with an index dependent function for value
 
83
    value = lambda i, j : (i != j) * 2. + 1
 
84
    r_m = random_matrix_fixed_column(100, 4, p=0.5, value=value)
 
85
    assert_size_and_value(r_m, (100, 4), value)
 
86
    assert_entries_per_column(r_m, 50)
 
87
 
 
88
 
 
89
@repeat_with_global_opts([{'useweave': False}, {'useweave': True}])
 
90
def test_construction():
6
91
    '''
7
92
    :class:`Connection`
8
93
    ~~~~~~~~~~~~~~~~~~~
119
204
 
120
205
    reinit_default_clock()
121
206
 
 
207
@repeat_with_global_opts([{'useweave': False}, {'useweave': True}])
 
208
def test_access():
 
209
    'Test accessing the contents of a ConnectionMatrix after construction'
 
210
    G = NeuronGroup(3, model=LazyStateUpdater())
 
211
    
 
212
    # sparse matrix
 
213
    C = Connection(G, G, structure='sparse')
 
214
    C[0, 0] = 1.
 
215
    C[0, 2] = 1.
 
216
    
 
217
    assert(C[0, 0] == 1. and C[0, 2] == 1.)
 
218
    assert(C.W[0, 0] == 1. and C.W[0, 2] == 1.)    
 
219
    assert(all(C.W.todense() == 
 
220
               array([[1., 0., 1.],
 
221
                      [0., 0., 0.],
 
222
                      [0., 0., 0.]])))    
 
223
    C.compress()
 
224
    assert(C.W[0, 0] == 1. and C.W[0, 2] == 1.)
 
225
    # Note that this is a sparse matrix
 
226
    assert(all(C.W[0, :] == [1., 1.]))
 
227
    assert(size(C.W[1, :]) == 0)
 
228
    assert(all(C.W[:, 0] == [1.]))
 
229
    assert(size(C.W[:, 1]) == 0)
 
230
    assert(all(C.W.todense() == 
 
231
               array([[1., 0., 1.],
 
232
                      [0., 0., 0.],
 
233
                      [0., 0., 0.]])))
 
234
    
 
235
    # dense matrix
 
236
    C = Connection(G, G, structure='dense')
 
237
    C[0, 0] = 1.
 
238
    C[0, 2] = 1.
 
239
    
 
240
    assert(C[0, 0] == 1. and C[0, 2] == 1.)
 
241
    assert(C.W[0, 0] == 1. and C.W[0, 2] == 1.)    
 
242
    C.compress()
 
243
    assert(C.W[0, 0] == 1. and C.W[0, 2] == 1.)
 
244
    assert(all(C.W[0, :] == [1., 0., 1.]))   
 
245
    assert(all(C.W[:, 0] == [1., 0., 0.]))
 
246
    assert(all(C.W == C.W.todense()))
 
247
    
 
248
    # dynamic matrix
 
249
    C = Connection(G, G, structure='dynamic')
 
250
    C[0, 0] = 1.
 
251
    C[0, 2] = 1.
 
252
    
 
253
    assert(C[0, 0] == 1. and C[0, 2] == 1.)
 
254
    assert(C.W[0, 0] == 1. and C.W[0, 2] == 1.)    
 
255
    C.compress()
 
256
    assert(C.W[0, 0] == 1. and C.W[0, 2] == 1.)
 
257
    # Note that this is a sparse matrix
 
258
    assert(all(C.W[0, :] == [1., 1.]))
 
259
    assert(size(C.W[1, :]) == 0)
 
260
    assert(all(C.W[:, 0] == [1.]))
 
261
    assert(size(C.W[:, 1]) == 0)
 
262
    assert(all(C.W.todense() == 
 
263
               array([[1., 0., 1.],
 
264
                      [0., 0., 0.],
 
265
                      [0., 0., 0.]])))    
 
266
    
 
267
 
122
268
if __name__ == '__main__':
123
 
    test()
 
269
    test_construction()
 
270
    test_access()
 
271
    test_utility_functions()