-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest__hyperct.py
390 lines (319 loc) · 13.8 KB
/
test__hyperct.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
Unittests
Note to set the following logging level to see console output:
logging.getLogger().setLevel(logging.INFO)
"""
import unittest
from ddgclib._complex import *
import pytest
logging.getLogger().setLevel(logging.INFO) #TODO: REMOVE
def g_cons(x): # (Requires n > 2)
# return x[0] - 0.5 * x[2] + 0.5
#time.sleep(1)
return x[0] # + x[2] #+ 0.5
def func(x):
#time.sleep(1)
#if x[0] == 0:
# raise FloatingPointError
return (-(x[1] + 47.0)
* numpy.sin(numpy.sqrt(abs(x[0] / 2.0 + (x[1] + 47.0))))
- x[0] * numpy.sin(numpy.sqrt(abs(x[0] - (x[1] + 47.0))))
)#/x[0]
n = 2
gen = 2 # 7
bounds = [(-100.0, 100.0), (-100.0, 100.0)]
def test_triangulation(n=2, gen=0, bounds=None, symmetry=None):
# Generate new reference complex
HC_ref = Complex(n, domain=bounds)
# Load test data
if symmetry is None:
path = os.path.join(os.path.dirname(__file__), 'test_data',
f'test_{n + 1}_{gen + 1}_{n}D_cube_gen_{gen}.json')
else:
s = str()
for si in symmetry:
s += str(si)
path = os.path.join(os.path.dirname(__file__), 'test_data',
f'test_{n + 1}_{gen + 1}_{n}D_symm_gen_{gen}_s_{s}.json')
HC_ref.load_complex(fn=path)
# Generate data containers for test comparisons
check = [] # Check if vertex in complex list
nn_checks = {} # Check if vertex has correct neighbours dict
for v in HC_ref.V:
check.append(v.x)
nn_checks[v.x] = [vnn.x for vnn in v.nn]
# Generate new test complex
HC = Complex(n, domain=bounds, symmetry=symmetry)
HC.triangulate()
for i in range(gen):
HC.refine_all()
HC.V.print_out()
# Test that all the correct vertices are present
for i, v in enumerate(HC.V.cache):
print(f'Test if generated v.x = {v} is in reference complex')
logging.info(f'Test if generated v.x = {v} is in reference complex')
numpy.testing.assert_equal(v in check, True)
logging.info(f'Test passed')
for i, v in enumerate(check):
# Unordered check 2:
logging.info(f'Test if reference v.x = {v} is in generated complex')
numpy.testing.assert_equal(v in HC.V.cache, True)
logging.info(f'Test passed')
for v in nn_checks:
nn_test = []
for v2 in HC.V[v].nn:
nn_test.append(v2.x)
nn_t = numpy.array(nn_test)[numpy.lexsort(numpy.rot90(nn_test))]
nn_c = numpy.array(nn_checks[v])[numpy.lexsort(numpy.rot90(nn_checks[v]))]
logging.info('-' * len(f'Testing neighbours of {v}'))
logging.info(f'Testing neighbours of {v}')
logging.info('-' * len(f'Testing neighbours of {v}'))
logging.info(f'Lexicographical arrays should match:')
logging.info(f'Nearest neighbours generated by current test = {nn_t}')
logging.info(f'Reference neighbours (correct values) = {nn_c}')
try:
numpy.testing.assert_equal(nn_t, nn_c)
except AssertionError as e:
logging.info(f'Test failed, searching for defects...')
cset = set()
gsv = numpy.array([-numpy.inf,]*n)
osv = numpy.array([numpy.inf,]*n)
for v in nn_c:
cset.add(tuple(v))
for i, vi in enumerate(v):
if vi > gsv[i]:
gsv[i] = vi
if vi < osv[i]:
osv[i] = vi
logging.info(f' Approximate triangulation reference vectors:'
f' origin = {osv}'
f' supremum = {gsv}')
tset = set()
gsv = numpy.array([-numpy.inf,]*n) # Rest vectors to find out area
osv = numpy.array([numpy.inf,]*n)
for v in nn_t:
tset.add(tuple(v))
for i, vi in enumerate(v):
if vi > gsv[i]:
gsv[i] = vi
if vi < osv[i]:
osv[i] = vi
if tuple(v) not in cset:
logging.info(f'{v} should not be in v.nn')
logging.info(f' Approximate triangulation vectors computed in test:'
f' origin = {osv}'
f' supremum = {gsv}')
for v in nn_c:
if tuple(v) not in tset:
logging.info(f'{v} missing from v.nn')
raise(e)
logging.info(f'Test passed')
logging.info('-' * len(f'Test passed'))
logging.info('.')
class TestCube(object):
def test_1_1_2D_cube_init(self):
"""Test that the initial 2D cube has the correct vertices"""
test_triangulation(2, 0)
def test_1_2_2D_cube_splits(self):
"""Test that the 2D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
test_triangulation(2, 1)
def test_1_3_2D_cube_splits(self):
"""Test that the 2D cube subtriangulations has the correct vertices,
testing 2 generations of subtriangulations"""
test_triangulation(2, 2)
def test_2_1_3D_cube_init(self):
"""Test that the initial 3D cube has the correct vertices"""
test_triangulation(3, 0)
def test_2_2_3D_cube_splits(self):
"""Test that the 3D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
test_triangulation(3, 1)
def test_2_3_3D_cube_splits(self):
"""Test that the 3D cube subtriangulations has the correct vertices,
testing 2 generations of subtriangulations"""
test_triangulation(3, 2)
def test_3_1_4D_cube_init(self):
"""Test that the initial 4D cube has the correct vertices"""
test_triangulation(4, 0)
def test_3_2_4D_cube_splits(self):
"""Test that the 4D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
test_triangulation(4, 1)
#@pytest.mark.slow
@unittest.skip("Skipping slow test")
def test_3_3_4D_cube_splits(self):
"""Test that the 4D cube subtriangulations has the correct vertices,
testing 2 generations of subtriangulations"""
test_triangulation(4, 2)
def test_4_1_5D_cube_init(self):
"""Test that the initial 5D cube has the correct vertices"""
test_triangulation(5, 0)
@pytest.mark.slow
def test_4_2_5D_cube_splits(self):
"""Test that the 5D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
test_triangulation(5, 1)
@pytest.mark.slow
@unittest.skip("Skipping slow test")
def test_4_3_5D_cube_splits(self):
"""Test that the 5D cube subtriangulations has the correct vertices,
testing 2 generations of subtriangulations"""
test_triangulation(5, 2)
@unittest.skip("Skipping slow test")
def test_5_1_6D_cube_init(self):
"Test that the initial 6D cube has the correct vertices"
test_triangulation(6, 0)
@unittest.skip("Skipping slow test")
def test_5_2_6D_cube_splits(self):
"""Test that the 6D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
test_triangulation(6, 1)
@unittest.skip("Skipping slow test")
def test_6_1_7D_cube_init(self):
"Test that the initial 7D cube has the correct vertices"
test_triangulation(7, 0)
@unittest.skip("Skipping slow test")
def test_6_2_7D_cube_splits(self):
"""Test that the 7D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
test_triangulation(7, 1)
@unittest.skip("Skipping slow test")
def test_7_1_8D_cube_init(self):
"Test that the initial 8D cube has the correct vertices"
test_triangulation(8, 0)
@unittest.skip("Skipping slow test")
def test_8_1_9D_cube_init(self):
"Test that the initial 9D cube has the correct vertices"
test_triangulation(9, 0)
@unittest.skip("Skipping slow test")
def test_9_1_10D_cube_init(self):
"Test that the initial 10D cube has the correct vertices"
test_triangulation(10, 0)
@unittest.skip("Skipping slow test")
def test_99_1_11D_cube_init(self):
"Test that the initial 11D cube has the correct vertices"
test_triangulation(11, 0)
class TestSymmetry(object):
def test_1_1_2D_symm_init(self):
"""Test that the initial 2D symmetric cube has the correct vertices"""
dim = 2
symmetry = [0, ] * dim
test_triangulation(2, 0, symmetry=symmetry)
def test_1_2_2D_symm_splits(self):
"""Test that the 2D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
dim = 2
symmetry = [0, ] * dim
test_triangulation(dim , 1, symmetry=symmetry)
def test_1_3_2D_symm_splits(self):
"""Test that the 2D cube subtriangulations has the correct vertices,
testing 2 generation of subtriangulations"""
dim = 2
symmetry = [0, ] * dim
test_triangulation(dim, 2, symmetry=symmetry)
def test_2_1_3D_symm_init(self):
"""Test that the initial 3D symmetric cube has the correct vertices"""
dim = 3
symmetry = [0, ] * dim
test_triangulation(dim, 0, symmetry=symmetry)
def test_2_2_3D_symm_splits(self):
"""Test that the 3D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
dim = 3
symmetry = [0, ] * dim
test_triangulation(dim, 1, symmetry=symmetry)
def test_2_3_3D_symm_splits(self):
"""Test that the 3D cube subtriangulations has the correct vertices,
testing 2 generation of subtriangulations"""
dim = 3
symmetry = [0, ] * dim
test_triangulation(dim, 2, symmetry=symmetry)
def test_2_4_3D_psymm_init(self):
"""Test that the initial 3D partial symmetric cube [0, 0, 2] has the
correct vertices"""
dim = 3
symmetry = [0, 0, 2]
test_triangulation(dim, 0, symmetry=symmetry)
def test_2_5_3D_psymm_splits(self):
"""Test that the 3D partially symmetric [0, 0, 2] cube subtriangulations
has the correct vertices, testing 1 generation of subtriangulations"""
dim = 3
symmetry = [0, 0, 2]
test_triangulation(dim, 1, symmetry=symmetry)
def test_2_6_3D_psymm_splits(self):
"""Test that the 3D partially symmetric [0, 0, 2] cube subtriangulations
has the correct vertices, testing 2 generation of subtriangulations"""
dim = 3
symmetry = [0, 0, 2]
test_triangulation(dim, 2, symmetry=symmetry)
def test_2_7_3D_psymm_init(self):
"""Test that the initial 3D partial symmetric cube [0, 1, 1] has the
correct vertices"""
dim = 3
symmetry = [0, 1, 1]
test_triangulation(dim, 0, symmetry=symmetry)
def test_2_8_3D_psymm_splits(self):
"""Test that the 3D partially symmetric [0, 1, 1] cube subtriangulations
has the correct vertices, testing 1 generation of subtriangulations"""
dim = 3
symmetry = [0, 1, 1]
test_triangulation(dim, 1, symmetry=symmetry)
def test_2_9_3D_psymm_splits(self):
"""Test that the 3D partially symmetric [0, 1, 1] cube subtriangulations
has the correct vertices, testing 2 generation of subtriangulations"""
dim = 3
symmetry = [0, 1, 1]
test_triangulation(dim, 2, symmetry=symmetry)
def test_3_1_4D_symm_init(self):
"""Test that the initial 4D symmetric cube has the correct vertices"""
symmetry = [0,]*4
test_triangulation(4, 0, symmetry=symmetry)
def test_3_2_4D_symm_splits(self):
"""Test that the 4D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
dim = 4
symmetry = [0, ] * dim
test_triangulation(dim , 1, symmetry=symmetry)
def test_3_3_4D_symm_splits(self):
"""Test that the 4D cube subtriangulations has the correct vertices,
testing 2 generation of subtriangulations"""
dim = 4
symmetry = [0, ] * dim
test_triangulation(dim, 2, symmetry=symmetry)
def test_3_4_4D_psymm_init(self):
"""Test that the initial 3D partial symmetric cube [0, 0, 0, 3] has the
correct vertices"""
dim = 4
symmetry = [0, 0, 0, 3]
test_triangulation(dim, 0, symmetry=symmetry)
def test_3_5_4D_psymm_splits(self):
"""Test that the 4D partially symmetric [0, 0, 0, 3] cube
subtriangulations has the correct vertices, testing 2 generation of
subtriangulations"""
dim = 4
symmetry = [0, 0, 0, 3]
test_triangulation(dim, 1, symmetry=symmetry)
def test_3_6_4D_psymm_splits(self):
"""Test that the 4D partially symmetric [0, 0, 0, 3] cube
subtriangulations has the correct vertices, testing 2 generation of
subtriangulations"""
dim = 4
symmetry = [0, 0, 0, 3]
test_triangulation(dim, 2, symmetry=symmetry)
def test_4_1_5D_symm_init(self):
"""Test that the initial 5D symmetric cube has the correct vertices"""
symmetry = [0,]*5
test_triangulation(5, 0, symmetry=symmetry)
def test_4_2_5D_symm_splits(self):
"""Test that the 5D cube subtriangulations has the correct vertices,
testing 1 generation of subtriangulations"""
dim = 5
symmetry = [0, ] * dim
test_triangulation(dim, 1, symmetry=symmetry)
def test_4_3_5D_symm_splits(self):
"""Test that the 5D cube subtriangulations has the correct vertices,
testing 2 generation of subtriangulations"""
dim = 5
symmetry = [0, ] * dim
test_triangulation(dim, 2, symmetry=symmetry)