-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyNN_spatial.py
154 lines (115 loc) · 4.6 KB
/
pyNN_spatial.py
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
'''
@author: Daniel Hjertholm
Tests for spatially structured networks generated by PyNN.
'''
import numpy as np
import numpy.random as rnd
from numpy import exp
import pyNN.nest as sim
from testsuite.spatial_test import SpatialTester
class pyNN_SpatialTester(SpatialTester):
'''Tests for spatially structured networks generated by PyNN.'''
def __init__(self, L, N, kernel_string):
'''
Construct a test object.
Parameters
----------
L : Side length of area / volume.
N : Number of nodes.
kernel_string: Kernel, given as an evaluable string.
'''
sim.nest.set_verbosity('M_FATAL')
SpatialTester.__init__(self, L=L, N=N)
self._kernel_string = kernel_string
self._kernel = lambda d: eval(self._kernel_string)
def _reset(self, seed):
'''
Reset simulator and seed PRNGs.
Parameters
----------
seed: PRNG seed value.
'''
sim.end()
sim.setup()
if seed is None:
seed = rnd.randint(10 ** 10)
seed = 2 * seed # Reduces probability of overlapping seed values.
rnd.seed(seed)
self._rng = sim.NumpyRNG(seed=seed + 1)
def _build(self):
'''Create populations.'''
if self._dimensions == 2:
ldict = {'fill_order': 'sequential',
'dx': self._L, 'dy': self._L,
'x0': 0.0, 'y0': 0.0}
self._ls = sim.Population(1, sim.IF_cond_exp,
structure=sim.space.Grid2D(**ldict))
cube = sim.space.Cuboid(1.0, 1.0, 0.0)
else:
ldict = {'fill_order': 'sequential',
'dx': self._L, 'dy': self._L, 'dz': self._L,
'x0': 0.0, 'y0': 0.0, 'z0': 0.0}
self._ls = sim.Population(1, sim.IF_cond_exp,
structure=sim.space.Grid3D(**ldict))
cube = sim.space.Cuboid(1.0, 1.0, 1.0)
self._lt = sim.Population(self._N, sim.IF_cond_exp,
structure=sim.space.RandomStructure(cube, rng=self._rng))
def _connect(self):
'''Connect populations.'''
self._p = sim.Projection(self._ls, self._lt,
sim.DistanceDependentProbabilityConnector(self._kernel_string),
rng=self._rng)
def _positions(self):
'''Return list of position tuples for all nodes.'''
return [tuple(p) for p in np.transpose(self._lt.positions)]
def _target_positions(self):
'''Return list of position tuples of all connected target nodes.'''
return [tuple(c.target.position) for c in self._p]
def _distances(self):
'''Return list with distances to all nodes.'''
return [sim.space.distance(self._ls[0], t) for t in self._lt]
def _target_distances(self):
'''
Return list with distances from source node to all connected
target nodes.
'''
return [sim.space.distance(c.source, c.target) for c in self._p]
class Spatial2DTester(pyNN_SpatialTester):
'''Tests for 2D spatially structured networks generated by PyNN.'''
def __init__(self, L, N, kernel_string):
'''
Construct a test object.
Parameters
----------
L : Side length of area / volume.
N : Number of nodes.
kernel_string: Kernel, given as an evaluable string.
'''
self._dimensions = 2
pyNN_SpatialTester.__init__(self, L=L, N=N,
kernel_string=kernel_string)
class Spatial3DTester(pyNN_SpatialTester):
'''Tests for 3D spatially structured networks generated by PyNN.'''
def __init__(self, L, N, kernel_string):
'''
Construct a test object.
Parameters
----------
L : Side length of area / volume.
N : Number of nodes.
kernel_string: Kernel, given as an evaluable string.
'''
self._dimensions = 3
pyNN_SpatialTester.__init__(self, L=L, N=N,
kernel_string=kernel_string)
if __name__ == '__main__':
kernel_string = '%s + %s * exp(-((d - %s) ** 2 / (2.0 * %s ** 2)))' % \
(0.0, 1.0, 0.0, 1.0 / 4)
test = Spatial2DTester(L=1.0, N=10000, kernel_string=kernel_string)
ks, p = test.ks_test(control=False, seed=0)
print 'p-value of KS-test:', p
z, p = test.z_test(control=False, seed=0)
print 'p-value of Z-test:', p
test.show_network()
test.show_PDF()
test.show_CDF()