tests.test_converter.test_position2value_0()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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