-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest_spatial.py
212 lines (157 loc) · 7.75 KB
/
unittest_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
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
'''
@author: Daniel Hjertholm
Unittests for 2D and 3D spatially structured networks.
'''
import unittest
import scipy.stats
from nest_spatial import Spatial2DTester, Spatial3DTester
class ConnectLayersTestCase(unittest.TestCase):
'''Statistical tests for ConnectLayers.'''
def setUp(self):
'''Set test parameters and critical values.'''
self.N = 100000 # Number of nodes
self.L = 1.0 # Layer size.
self.n_runs = 100 # Number of times to repeat test
# Critical values
self.alpha1 = 0.01
self.alpha2 = 0.01
def ks_test(self, tester_class, kernel_name, kernel_params={}):
'''
Create a single network using ConnectLayers, and perform a
Kolmogorov-Smirnov (KS) test on the distribution of source-target
distances. If the result is suspicious, the test is repeated n_runs
times, and the resulting p-values are compared with the expected uniform
distribution using the KS test.
Parameters
----------
tester_class : ConnectLayers2D_tester or ConnectLayers3D_tester.
kernel_name : Name of distance dependent probability
function (kernel) to test.
kernel_params: Parameters for kernel function. Optional.
Return values
-------------
boolean value. True if test was passed, False otherwise.
'''
test = tester_class(L=self.L, N=self.N, kernel_name=kernel_name,
kernel_params=kernel_params)
ks, p = test.ks_test()
if p > self.alpha1:
return True
else:
ps = []
print ''
for i in range(self.n_runs):
print 'Running test %d of %d.' % (i + 1, self.n_runs)
test = tester_class(L=self.L, N=self.N, kernel_name=kernel_name,
kernel_params=kernel_params,
seed=None)
ps.append(test.ks_test()[1])
ks, p = scipy.stats.kstest(ps, 'uniform', alternative='two_sided')
return True if p > self.alpha2 else False
def z_test(self, tester_class, kernel_name, kernel_params={}):
'''
Create a single network using ConnectLayers, and perform a Z-test on the
total connection count. If the result is suspicious, the test is
repeated n_runs times, and the resulting p-values are compared with the
expected uniform distribution using the KS test.
Parameters
----------
tester_class : ConnectLayers2D_tester or ConnectLayers3D_tester.
kernel_name : Name of distance dependent probability
function (kernel) to test.
kernel_params: Parameters for kernel function. Optional.
Return values
-------------
boolean value. True if test was passed, False otherwise.
'''
test = tester_class(L=self.L, N=self.N, kernel_name=kernel_name,
kernel_params=kernel_params)
z, p = test.z_test()
if p > self.alpha1:
return True
else:
ps = []
print ''
for i in range(self.n_runs):
print 'Running test %d of %d.' % (i + 1, self.n_runs)
test = tester_class(L=self.L, N=self.N,
kernel_name=kernel_name,
kernel_params=kernel_params,
seed=None)
ps.append(test.z_test()[1])
z, p = scipy.stats.kstest(ps, 'uniform', alternative='two_sided')
return True if p > self.alpha2 else False
def test_2D_constant_ks(self):
'''KS test performed on source-target node distances'''
self.assertTrue(self.ks_test(Spatial2DTester, 'constant',
kernel_params=0.5),
'ConnectLayers failed to pass the KS test.')
def test_2D_constant_z(self):
'''Z-test performed on total connection count'''
self.assertTrue(self.z_test(Spatial3DTester, 'constant',
kernel_params=0.5),
'ConnectLayers failed to pass the Z-test')
def test_2D_linear_ks(self):
'''KS test performed on source-target node distances'''
self.assertTrue(self.ks_test(Spatial2DTester, 'linear'),
'ConnectLayers failed to pass the KS test.')
def test_2D_linear_z(self):
'''Z-test performed on total connection count'''
self.assertTrue(self.z_test(Spatial2DTester, 'linear'),
'ConnectLayers failed to pass the Z-test')
def test_2D_exponential_ks(self):
'''KS test performed on source-target node distances'''
self.assertTrue(self.ks_test(Spatial2DTester, 'exponential'),
'ConnectLayers failed to pass the KS test.')
def test_2D_exponential_z(self):
'''Z-test performed on total connection count'''
self.assertTrue(self.z_test(Spatial2DTester, 'exponential'),
'ConnectLayers failed to pass the Z-test')
def test_2D_gaussian_ks(self):
'''KS test performed on source-target node distances'''
self.assertTrue(self.ks_test(Spatial2DTester, 'gaussian'),
'ConnectLayers failed to pass the KS test.')
def test_2D_gaussian_z(self):
'''Z-test performed on total connection count'''
self.assertTrue(self.z_test(Spatial2DTester, 'gaussian'),
'ConnectLayers failed to pass the Z-test')
def test_3D_constant_ks(self):
'''KS test performed on source-target node distances'''
self.assertTrue(self.ks_test(Spatial3DTester, 'constant',
kernel_params=0.5),
'ConnectLayers failed to pass the KS test.')
def test_3D_constant_z(self):
'''Z-test performed on total connection count'''
self.assertTrue(self.z_test(Spatial3DTester, 'constant',
kernel_params=0.5),
'ConnectLayers failed to pass the Z-test')
def test_3D_linear_ks(self):
'''KS test performed on source-target node distances'''
self.assertTrue(self.ks_test(Spatial3DTester, 'linear'),
'ConnectLayers failed to pass the KS test.')
def test_3D_linear_z(self):
'''Z-test performed on total connection count'''
self.assertTrue(self.z_test(Spatial3DTester, 'linear'),
'ConnectLayers failed to pass the Z-test')
def test_3D_exponential_ks(self):
'''KS test performed on source-target node distances'''
self.assertTrue(self.ks_test(Spatial3DTester, 'exponential'),
'ConnectLayers failed to pass the KS test.')
def test_3D_exponential_z(self):
'''Z-test performed on total connection count'''
self.assertTrue(self.z_test(Spatial3DTester, 'exponential'),
'ConnectLayers failed to pass the Z-test')
def test_3D_gaussian_ks(self):
'''KS test performed on source-target node distances'''
self.assertTrue(self.ks_test(Spatial3DTester, 'gaussian'),
'ConnectLayers failed to pass the KS test.')
def test_3D_gaussian_z(self):
'''Z-test performed on total connection count'''
self.assertTrue(self.z_test(Spatial3DTester, 'gaussian'),
'ConnectLayers failed to pass the Z-test')
def suite():
suite = unittest.makeSuite(ConnectLayersTestCase, 'test')
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite())