Passed
Push — master ( 80db5f...5e948f )
by Simon
01:24
created

tests.test_converter   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 621
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 376
dl 0
loc 621
rs 10
c 0
b 0
f 0
wmc 24

21 Functions

Rating   Name   Duplication   Size   Complexity  
A equal_arraysInList() 0 2 1
A test_value2position_1() 0 13 1
A test_positions_scores2memory_dict_0() 0 15 1
A reorder() 0 2 1
A test_para2value_0() 0 10 1
A test_position2value_0() 0 10 1
A equal_dictKeysValues() 0 8 4
A test_value2position_0() 0 10 1
A test_positions2values_0() 0 10 1
A test_para2value_1() 0 13 1
A test_values2positions_1() 0 13 1
A test_positions2values_1() 0 13 1
A test_value2para_1() 0 13 1
A test_position2value_1() 0 15 1
A test_memory_dict2positions_scores_0() 0 20 1
A test_value2para_0() 0 10 1
A test_dataframe2memory_dict_0() 0 15 1
A get_idx_order() 0 2 1
A unordered_dict_workaround() 0 13 1
A test_memory_dict2dataframe_0() 0 23 1
A test_values2positions_0() 0 10 1
1
import pytest
2
import numpy as np
3
import pandas as pd
4
from gradient_free_optimizers.converter import Converter
5
6
7
def equal_arraysInList(list1, list2):
8
    return all((e1 == e2).all() for e1, e2 in zip(list1, list2))
9
10
11
def equal_dictKeysValues(dict1, dict2):
12
    if len(dict1.keys()) != len(dict2.keys()):
13
        return False
14
    for key1 in dict1.keys:
15
        if dict1[key1] != dict2[key1]:
16
            return False
17
18
    return True
19
20
21
def get_idx_order(list1, list2):
22
    return [idx for o in list1 for idx, name in enumerate(list2) if o == name]
23
24
25
def reorder(list1, idx_list):
26
    return [list1[i] for i in idx_list]
27
28
29
def unordered_dict_workaround(conv, order):
30
    # workaround for doing this test with unordered dicts
31
32
    idx_order = get_idx_order(order, conv.para_names)
33
    search_space_values_reordered = reorder(
34
        conv.search_space_values, idx_order
35
    )
36
    para_names_reordered = reorder(conv.para_names, idx_order)
37
38
    conv.search_space_values = search_space_values_reordered
39
    conv.para_names = para_names_reordered
40
41
    return conv
42
43
44
######### test position2value #########
45
46
47
position2value_test_para_0 = [
48
    (np.array([0]), np.array([-10])),
49
    (np.array([20]), np.array([10])),
50
    (np.array([10]), np.array([0])),
51
]
52
53
54
@pytest.mark.parametrize("test_input,expected", position2value_test_para_0)
55
def test_position2value_0(test_input, expected):
56
    search_space = {
57
        "x1": np.arange(-10, 11, 1),
58
    }
59
60
    conv = Converter(search_space)
61
    value = conv.position2value(test_input)
62
63
    assert value == expected
64
65
66
position2value_test_para_1 = [
67
    (np.array([0, 0]), np.array([-10, 0])),
68
    (np.array([20, 0]), np.array([10, 0])),
69
    (np.array([10, 10]), np.array([0, 10])),
70
]
71
72
73
@pytest.mark.parametrize("test_input,expected", position2value_test_para_1)
74
def test_position2value_1(test_input, expected):
75
    search_space = {
76
        "x1": np.arange(-10, 11, 1),
77
        "x2": np.arange(0, 11, 1),
78
    }
79
80
    conv = Converter(search_space)
81
82
    order = ["x1", "x2"]
83
    conv = unordered_dict_workaround(conv, order)
84
85
    value = conv.position2value(test_input)
86
87
    assert (value == expected).all()
88
89
90
######### test value2position #########
91
92
93
value2position_test_para_0 = [
94
    (np.array([-10]), np.array([0])),
95
    (np.array([10]), np.array([20])),
96
    (np.array([0]), np.array([10])),
97
]
98
99
100
@pytest.mark.parametrize("test_input,expected", value2position_test_para_0)
101
def test_value2position_0(test_input, expected):
102
    search_space = {
103
        "x1": np.arange(-10, 11, 1),
104
    }
105
106
    conv = Converter(search_space)
107
    position = conv.value2position(test_input)
108
109
    assert position == expected
110
111
112
value2position_test_para_1 = [
113
    (np.array([-10, 11]), np.array([0, 10])),
114
    (np.array([10, 11]), np.array([20, 10])),
115
    (np.array([0, 0]), np.array([10, 0])),
116
]
117
118
119
@pytest.mark.parametrize("test_input,expected", value2position_test_para_1)
120
def test_value2position_1(test_input, expected):
121
    search_space = {
122
        "x1": np.arange(-10, 11, 1),
123
        "x2": np.arange(0, 11, 1),
124
    }
125
126
    conv = Converter(search_space)
127
    order = ["x1", "x2"]
128
    conv = unordered_dict_workaround(conv, order)
129
    position = conv.value2position(test_input)
130
131
    assert (position == expected).all()
132
133
134
######### test value2para #########
135
136
137
value2para_test_para_0 = [
138
    (np.array([-10]), {"x1": np.array([-10])}),
139
    (np.array([10]), {"x1": np.array([10])}),
140
    (np.array([0]), {"x1": np.array([0])}),
141
]
142
143
144
@pytest.mark.parametrize("test_input,expected", value2para_test_para_0)
145
def test_value2para_0(test_input, expected):
146
    search_space = {
147
        "x1": np.arange(-10, 11, 1),
148
    }
149
150
    conv = Converter(search_space)
151
    para = conv.value2para(test_input)
152
153
    assert para == expected
154
155
156
value2para_test_para_1 = [
157
    (np.array([-10, 11]), {"x1": np.array([-10]), "x2": np.array([11])}),
158
    (np.array([10, 11]), {"x1": np.array([10]), "x2": np.array([11])}),
159
    (np.array([0, 0]), {"x1": np.array([0]), "x2": np.array([0])}),
160
]
161
162
163
@pytest.mark.parametrize("test_input,expected", value2para_test_para_1)
164
def test_value2para_1(test_input, expected):
165
    search_space = {
166
        "x1": np.arange(-10, 11, 1),
167
        "x2": np.arange(0, 11, 1),
168
    }
169
170
    conv = Converter(search_space)
171
    order = ["x1", "x2"]
172
    conv = unordered_dict_workaround(conv, order)
173
    para = conv.value2para(test_input)
174
175
    assert para == expected
176
177
178
######### test para2value #########
179
180
181
para2value_test_para_0 = [
182
    ({"x1": np.array([-10])}, np.array([-10])),
183
    ({"x1": np.array([10])}, np.array([10])),
184
    ({"x1": np.array([0])}, np.array([0])),
185
]
186
187
188
@pytest.mark.parametrize("test_input,expected", para2value_test_para_0)
189
def test_para2value_0(test_input, expected):
190
    search_space = {
191
        "x1": np.arange(-10, 11, 1),
192
    }
193
194
    conv = Converter(search_space)
195
    value = conv.para2value(test_input)
196
197
    assert value == expected
198
199
200
para2value_test_para_1 = [
201
    ({"x1": np.array([-10]), "x2": np.array([11])}, np.array([-10, 11])),
202
    ({"x1": np.array([10]), "x2": np.array([11])}, np.array([10, 11])),
203
    ({"x1": np.array([0]), "x2": np.array([0])}, np.array([0, 0])),
204
]
205
206
207
@pytest.mark.parametrize("test_input,expected", para2value_test_para_1)
208
def test_para2value_1(test_input, expected):
209
    search_space = {
210
        "x1": np.arange(-10, 11, 1),
211
        "x2": np.arange(0, 11, 1),
212
    }
213
214
    conv = Converter(search_space)
215
    order = ["x1", "x2"]
216
    conv = unordered_dict_workaround(conv, order)
217
    value = conv.para2value(test_input)
218
219
    assert (value == expected).all()
220
221
222
######### test values2positions #########
223
224
225
values_0 = [
226
    np.array([-10]),
227
    np.array([10]),
228
    np.array([0]),
229
]
230
231
positions_0 = [
232
    np.array([0]),
233
    np.array([20]),
234
    np.array([10]),
235
]
236
237
238
values_1 = [
239
    np.array([-10]),
240
    np.array([10]),
241
    np.array([0]),
242
    np.array([-10]),
243
    np.array([10]),
244
    np.array([0]),
245
    np.array([-10]),
246
    np.array([10]),
247
    np.array([0]),
248
]
249
250
positions_1 = [
251
    np.array([0]),
252
    np.array([20]),
253
    np.array([10]),
254
    np.array([0]),
255
    np.array([20]),
256
    np.array([10]),
257
    np.array([0]),
258
    np.array([20]),
259
    np.array([10]),
260
]
261
262
263
values2positions_test_para_0 = [
264
    (values_0, positions_0),
265
    (values_1, positions_1),
266
]
267
268
269
@pytest.mark.parametrize("test_input,expected", values2positions_test_para_0)
270
def test_values2positions_0(test_input, expected):
271
    search_space = {
272
        "x1": np.arange(-10, 11, 1),
273
    }
274
275
    conv = Converter(search_space)
276
    positions = conv.values2positions(test_input)
277
278
    assert positions == expected
279
280
281
values_0 = [
282
    np.array([-10, 10]),
283
    np.array([10, 10]),
284
    np.array([0, 0]),
285
]
286
287
positions_0 = [
288
    np.array([0, 10]),
289
    np.array([20, 10]),
290
    np.array([10, 0]),
291
]
292
293
294
values_1 = [
295
    np.array([-10, 10]),
296
    np.array([10, 10]),
297
    np.array([0, 0]),
298
    np.array([-10, 10]),
299
    np.array([10, 10]),
300
    np.array([0, 0]),
301
    np.array([-10, 10]),
302
    np.array([10, 10]),
303
    np.array([0, 0]),
304
]
305
306
positions_1 = [
307
    np.array([0, 10]),
308
    np.array([20, 10]),
309
    np.array([10, 0]),
310
    np.array([0, 10]),
311
    np.array([20, 10]),
312
    np.array([10, 0]),
313
    np.array([0, 10]),
314
    np.array([20, 10]),
315
    np.array([10, 0]),
316
]
317
318
319
values2positions_test_para_1 = [
320
    (values_0, positions_0),
321
    (values_1, positions_1),
322
]
323
324
325
@pytest.mark.parametrize("test_input,expected", values2positions_test_para_1)
326
def test_values2positions_1(test_input, expected):
327
    search_space = {
328
        "x1": np.arange(-10, 11, 1),
329
        "x2": np.arange(0, 11, 1),
330
    }
331
332
    conv = Converter(search_space)
333
    order = ["x1", "x2"]
334
    conv = unordered_dict_workaround(conv, order)
335
    positions = conv.values2positions(test_input)
336
337
    assert equal_arraysInList(positions, expected)
338
339
340
""" --- test positions2values --- """
341
342
values_0 = [
343
    np.array([-10]),
344
    np.array([10]),
345
    np.array([0]),
346
]
347
348
positions_0 = [
349
    np.array([0]),
350
    np.array([20]),
351
    np.array([10]),
352
]
353
354
355
values_1 = [
356
    np.array([-10]),
357
    np.array([10]),
358
    np.array([0]),
359
    np.array([-10]),
360
    np.array([10]),
361
    np.array([0]),
362
    np.array([-10]),
363
    np.array([10]),
364
    np.array([0]),
365
]
366
367
positions_1 = [
368
    np.array([0]),
369
    np.array([20]),
370
    np.array([10]),
371
    np.array([0]),
372
    np.array([20]),
373
    np.array([10]),
374
    np.array([0]),
375
    np.array([20]),
376
    np.array([10]),
377
]
378
379
380
positions2values_test_para_0 = [
381
    (positions_0, values_0),
382
    (positions_1, values_1),
383
]
384
385
386
@pytest.mark.parametrize("test_input,expected", positions2values_test_para_0)
387
def test_positions2values_0(test_input, expected):
388
    search_space = {
389
        "x1": np.arange(-10, 11, 1),
390
    }
391
392
    conv = Converter(search_space)
393
    values = conv.positions2values(test_input)
394
395
    assert values == expected
396
397
398
values_0 = [
399
    np.array([-10, 10]),
400
    np.array([10, 10]),
401
    np.array([0, 0]),
402
]
403
404
positions_0 = [
405
    np.array([0, 10]),
406
    np.array([20, 10]),
407
    np.array([10, 0]),
408
]
409
410
411
values_1 = [
412
    np.array([-10, 10]),
413
    np.array([10, 10]),
414
    np.array([0, 0]),
415
    np.array([-10, 10]),
416
    np.array([10, 10]),
417
    np.array([0, 0]),
418
    np.array([-10, 10]),
419
    np.array([10, 10]),
420
    np.array([0, 0]),
421
]
422
423
positions_1 = [
424
    np.array([0, 10]),
425
    np.array([20, 10]),
426
    np.array([10, 0]),
427
    np.array([0, 10]),
428
    np.array([20, 10]),
429
    np.array([10, 0]),
430
    np.array([0, 10]),
431
    np.array([20, 10]),
432
    np.array([10, 0]),
433
]
434
435
436
positions2values_test_para_1 = [
437
    (positions_0, values_0),
438
    (positions_1, values_1),
439
]
440
441
442
@pytest.mark.parametrize("test_input,expected", positions2values_test_para_1)
443
def test_positions2values_1(test_input, expected):
444
    search_space = {
445
        "x1": np.arange(-10, 11, 1),
446
        "x2": np.arange(0, 11, 1),
447
    }
448
449
    conv = Converter(search_space)
450
    order = ["x1", "x2"]
451
    conv = unordered_dict_workaround(conv, order)
452
    values = conv.positions2values(test_input)
453
454
    assert equal_arraysInList(values, expected)
455
456
457
""" --- test positions_scores2memory_dict --- """
458
459
460
positions_0 = [
461
    np.array([0, 10]),
462
    np.array([20, 10]),
463
    np.array([10, 0]),
464
]
465
466
scores_0 = [0.1, 0.2, 0.3]
467
468
469
memory_dict_0 = {
470
    (0, 10): 0.1,
471
    (20, 10): 0.2,
472
    (10, 0): 0.3,
473
}
474
475
positions_scores2memory_dict_test_para_0 = [
476
    ((positions_0, scores_0), memory_dict_0),
477
    # ((positions_1, scores_1), values_1),
478
]
479
480
481
@pytest.mark.parametrize(
482
    "test_input,expected", positions_scores2memory_dict_test_para_0
483
)
484
def test_positions_scores2memory_dict_0(test_input, expected):
485
    search_space = {
486
        "x1": np.arange(-10, 11, 1),
487
        "x2": np.arange(0, 11, 1),
488
    }
489
490
    conv = Converter(search_space)
491
    order = ["x1", "x2"]
492
    conv = unordered_dict_workaround(conv, order)
493
    memory_dict = conv.positions_scores2memory_dict(*test_input)
494
495
    assert memory_dict == expected
496
497
498
""" --- test memory_dict2positions_scores --- """
499
500
501
positions_0 = [
502
    np.array([0, 10]),
503
    np.array([20, 10]),
504
    np.array([10, 0]),
505
]
506
507
scores_0 = [0.1, 0.2, 0.3]
508
509
510
memory_dict_0 = {
511
    (0, 10): 0.1,
512
    (20, 10): 0.2,
513
    (10, 0): 0.3,
514
}
515
516
memory_dict2positions_scores_test_para_0 = [
517
    (memory_dict_0, (positions_0, scores_0)),
518
]
519
520
521
@pytest.mark.parametrize(
522
    "test_input,expected", memory_dict2positions_scores_test_para_0
523
)
524
def test_memory_dict2positions_scores_0(test_input, expected):
525
    search_space = {
526
        "x1": np.arange(-10, 11, 1),
527
        "x2": np.arange(0, 11, 1),
528
    }
529
530
    conv = Converter(search_space)
531
    order = ["x1", "x2"]
532
    conv = unordered_dict_workaround(conv, order)
533
    positions, scores = conv.memory_dict2positions_scores(test_input)
534
535
    idx_order = get_idx_order(scores, expected[1])
536
    scores_reordered = reorder(scores, idx_order)
537
    positions_reordered = reorder(positions, idx_order)
538
539
    assert equal_arraysInList(positions_reordered, expected[0])
540
    assert scores_reordered == expected[1]
541
542
543
""" --- test dataframe2memory_dict --- """
544
545
546
dataframe = pd.DataFrame(
547
    [[-10, 10, 0.1], [10, 10, 0.2], [0, 0, 0.3]], columns=["x1", "x2", "score"]
548
)
549
550
551
memory_dict_0 = {
552
    (0, 10): 0.1,
553
    (20, 10): 0.2,
554
    (10, 0): 0.3,
555
}
556
557
dataframe2memory_dict_test_para_0 = [
558
    (dataframe, memory_dict_0),
559
]
560
561
562
@pytest.mark.parametrize(
563
    "test_input,expected", dataframe2memory_dict_test_para_0
564
)
565
def test_dataframe2memory_dict_0(test_input, expected):
566
    search_space = {
567
        "x1": np.arange(-10, 11, 1),
568
        "x2": np.arange(0, 11, 1),
569
    }
570
571
    conv = Converter(search_space)
572
    order = ["x1", "x2"]
573
    conv = unordered_dict_workaround(conv, order)
574
    memory_dict = conv.dataframe2memory_dict(test_input)
575
576
    assert memory_dict == expected
577
578
579
""" --- test memory_dict2dataframe --- """
580
581
582
dataframe = pd.DataFrame(
583
    [[-10, 10, 0.1], [10, 10, 0.2], [0, 0, 0.3]], columns=["x1", "x2", "score"]
584
)
585
586
587
memory_dict_0 = {
588
    (0, 10): 0.1,
589
    (20, 10): 0.2,
590
    (10, 0): 0.3,
591
}
592
593
memory_dict2dataframe_test_para_0 = [
594
    (memory_dict_0, dataframe),
595
]
596
597
598
@pytest.mark.parametrize(
599
    "test_input,expected", memory_dict2dataframe_test_para_0
600
)
601
def test_memory_dict2dataframe_0(test_input, expected):
602
    search_space = {
603
        "x1": np.arange(-10, 11, 1),
604
        "x2": np.arange(0, 11, 1),
605
    }
606
607
    conv = Converter(search_space)
608
    order = ["x1", "x2"]
609
    conv = unordered_dict_workaround(conv, order)
610
    dataframe = conv.memory_dict2dataframe(test_input)
611
612
    dataframe.sort_values("score", inplace=True)
613
    expected.sort_values("score", inplace=True)
614
615
    dataframe.reset_index(drop=True, inplace=True)
616
    expected.reset_index(drop=True, inplace=True)
617
618
    dataframe = dataframe[expected.columns]
619
620
    assert dataframe.equals(expected)
621