2
2
from nose.tools import *
3
3
from brian.utils.approximatecomparisons import is_approx_equal
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
9
def test_utility_functions():
11
Test the (non-public) random_matrix and random_matrix_fixed_column
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):
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)
27
raise AssertionError('Illegal value argument')
29
assert matrix[row, col] == value
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)
37
# with fixed probability
39
r_m = random_matrix(2, 4, p=p)
40
assert_size_and_value(r_m, (2, 4), 1.)
42
# with fixed probability and value
44
r_m = random_matrix(2, 4, p=p, value=value)
45
assert_size_and_value(r_m, (2, 4), 2.)
47
# Note: p can also be a function (like value), but as this is not
48
# documented, this is also not tested
50
# with a constant function for value
52
r_m = random_matrix(2, 4, p=1., value=value)
53
assert_size_and_value(r_m, (2, 4), value)
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)
61
# Test random matrix with fixed number of entries per column
62
# with fixed probability
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)
68
# with fixed probability and value
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)
74
# Note: p can also be a function (like value), but this is not documented
76
# with a constant function for value
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)
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)
89
@repeat_with_global_opts([{'useweave': False}, {'useweave': True}])
90
def test_construction():
120
205
reinit_default_clock()
207
@repeat_with_global_opts([{'useweave': False}, {'useweave': True}])
209
'Test accessing the contents of a ConnectionMatrix after construction'
210
G = NeuronGroup(3, model=LazyStateUpdater())
213
C = Connection(G, G, structure='sparse')
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() ==
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() ==
236
C = Connection(G, G, structure='dense')
240
assert(C[0, 0] == 1. and C[0, 2] == 1.)
241
assert(C.W[0, 0] == 1. and C.W[0, 2] == 1.)
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()))
249
C = Connection(G, G, structure='dynamic')
253
assert(C[0, 0] == 1. and C[0, 2] == 1.)
254
assert(C.W[0, 0] == 1. and C.W[0, 2] == 1.)
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() ==
122
268
if __name__ == '__main__':
271
test_utility_functions()