-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_Population3D.py
More file actions
313 lines (281 loc) · 10.5 KB
/
Copy pathtest_Population3D.py
File metadata and controls
313 lines (281 loc) · 10.5 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""
This file is part of ANNarchy.
:copyright: Copyright 2013 - now, see AUTHORS.
:license: GPLv2, see LICENSE for details.
"""
import unittest
import numpy
from conftest import TARGET_FOLDER
from ANNarchy import Network, Neuron, Uniform
# neuron defintions common used for test cases
neuron = Neuron(parameters="""tau = 10""", equations="""r += t/tau""")
neuron2 = Neuron(parameters="tau = 10: population", equations="r += t/tau : init = 1.0")
class test_Population3D(unittest.TestCase):
"""
Test several functions of the *Population* object in this particular test,
we focus on three-dimensional case:
* access methods for variables and parameters
* coordinate transformations
"""
@classmethod
def setUpClass(cls):
"""
Compile the network for this test
"""
cls._network = Network()
cls._population_1 = cls._network.create(geometry=(3, 3, 3), neuron=neuron)
cls._population_2 = cls._network.create(geometry=(3, 3, 3), neuron=neuron2)
cls._network.compile(silent=True, directory=TARGET_FOLDER)
@classmethod
def tearDownClass(cls):
"""
All tests of this class are done. We can destroy the network.
"""
del cls._network
def setUp(self):
"""
Automatically called before each test method, basically to reset the
network after every test.
"""
self._network.reset()
#
# Coordinate transformations
#
def test_coordinates_from_rank(self):
"""
ANNarchy allows two types of indexing, coordinates and ranks. In this
test we prove coordinate to rank transformation.
"""
self.assertSequenceEqual(self._population_1.coordinates_from_rank(2), (0, 0, 2))
self.assertSequenceEqual(self._population_1.coordinates_from_rank(6), (0, 2, 0))
self.assertSequenceEqual(
self._population_1.coordinates_from_rank(18), (2, 0, 0)
)
def test_rank_from_coordinates(self):
"""
ANNarchy allows two types of indexing, coordinates and ranks. In this
test we prove rank to coordinate transformation.
"""
self.assertEqual(self._population_1.rank_from_coordinates((0, 0, 2)), 2)
self.assertEqual(self._population_1.rank_from_coordinates((0, 2, 0)), 6)
self.assertEqual(self._population_1.rank_from_coordinates((2, 0, 0)), 18)
#
# Parameters
#
def test_get_tau(self):
"""
Test retrieval of parameter *tau* from population *tc3_pop1* by
directly access. As population has the size 27 there should be 27
entries with value 10.
"""
numpy.testing.assert_allclose(
self._population_1.tau,
[
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
],
)
def test_get_tau2(self):
"""
Test retrieval of parameter *tau* from population *tc3_pop1* by *get()*
method. As population has the size 27 there should be 27 entries with
value 10.
"""
numpy.testing.assert_allclose(
self._population_1.get("tau"),
[
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
],
)
def test_get_neuron_tau(self):
"""
Tests retrieval of parameter *tau* from a specific neuron from
population *tc3_pop1* by direct access.
"""
numpy.testing.assert_allclose(self._population_1.neuron(1).tau, 10.0)
def test_set_tau(self):
"""
Assigned a new value, all instances will change.
"""
self._population_1.tau = 5.0
numpy.testing.assert_allclose(
self._population_1.tau,
[
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
],
)
def test_set_tau_2(self):
"""
Assigned a new value, all instances will change.
"""
self._population_1.set({"tau": 5.0})
numpy.testing.assert_allclose(
self._population_1.tau,
[
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
],
)
def test_set_tau_popview(self):
"""
Assigned a new value, all instances will change normally.
One can use *PopulationView* to update more specific.
"""
self._population_1[0:3, 1, 1:3].tau = 5.0
numpy.testing.assert_allclose(
self._population_1.tau,
[
[[10.0, 10.0, 10.0], [10.0, 5.0, 5.0], [10.0, 10.0, 10.0]],
[[10.0, 10.0, 10.0], [10.0, 5.0, 5.0], [10.0, 10.0, 10.0]],
[[10.0, 10.0, 10.0], [10.0, 5.0, 5.0], [10.0, 10.0, 10.0]],
],
)
def test_get_tau_population(self):
"""
Test access to parameter, modified with *Population* keyword, as
consequence there should be only one instance of tau.
"""
self.assertEqual(self._population_2.tau, 10.0)
def test_popattributes(self):
"""
Tests the listing of *Population* attributes.
"""
self.assertEqual(
self._population_1.attributes, ["tau", "r"], "failed listing attributes"
)
self.assertEqual(
self._population_1.parameters, ["tau"], "failed listing parameters"
)
self.assertEqual(
self._population_1.variables, ["r"], "failed listing variables"
)
#
# Variables
#
def test_get_r(self):
"""
By default all variables are initialized with zero, which is tested
here by retrieving *r* directly.
"""
numpy.testing.assert_allclose(
self._population_1.r,
[
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
],
)
def test_get_r2(self):
"""
Tests the retrieval of the variable *r* through the *get()* method.
"""
numpy.testing.assert_allclose(
self._population_1.get("r"),
[
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
],
)
def test_get_neuron_r(self):
"""
Tests the retrieval of the variable *r* from a specific neuron by
direct access.
"""
numpy.testing.assert_allclose(self._population_1.neuron(18).r, 0.0)
def test_get_r_with_init(self):
"""
By default all variables are initialized with zero, we now modified
this with init = 1.0 and test it.
"""
numpy.testing.assert_allclose(
self._population_2.r,
[
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
],
)
def test_set_r(self):
"""
Test the setting of the variable *r* by direct access.
"""
self._population_1.r = 1.0
numpy.testing.assert_allclose(
self._population_1.r,
[
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
],
)
def test_set_r_2(self):
"""
Here we set only a change the variable of a selected field of neurons.
The rest should stay the same.
"""
self._population_1[0:3, 1, 1:3].r = 2.0
numpy.testing.assert_allclose(
self._population_1.r,
[
[[0.0, 0.0, 0.0], [0.0, 2.0, 2.0], [0.0, 0.0, 0.0]],
[[0.0, 0.0, 0.0], [0.0, 2.0, 2.0], [0.0, 0.0, 0.0]],
[[0.0, 0.0, 0.0], [0.0, 2.0, 2.0], [0.0, 0.0, 0.0]],
],
)
def test_set_r_uniform(self):
"""
Test the setting of the variable *r* by the *Uniform()* method. This
method assigns a random value (within a chosen interval) to the
variable of each neuron.
"""
self._population_1.r = Uniform(0.0, 1.0).get_values(27)
self.assertTrue(
any(self._population_1[0:3, 0:3, 0:3].r >= 0.0)
and all(self._population_1[0:3, 0:3, 0:3].r <= 1.0)
)
def test_set_r3(self):
"""
Test the setting of the variable *r* by the *set()* method.
"""
self._population_1.set({"r": 1.0})
numpy.testing.assert_allclose(
self._population_1.r,
[
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
],
)
#
# Reset-Test
#
def test_reset(self):
"""
Tests the functionality of the *reset()* method, which we use in our
*setUp()* function.
"""
self._population_1.tau = 5.0
numpy.testing.assert_allclose(
self._population_1.tau,
[
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
[[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]],
],
)
self._network.reset()
numpy.testing.assert_allclose(
self._population_1.tau,
[
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
[[10.0, 10.0, 10.0], [10.0, 10.0, 10.0], [10.0, 10.0, 10.0]],
],
)