10
10
from optimagic .optimization .history import (
11
11
History ,
12
12
HistoryEntry ,
13
+ _batch_apply ,
13
14
_calculate_monotone_sequence ,
15
+ _get_batch_start ,
14
16
_get_flat_param_names ,
15
17
_get_flat_params ,
16
18
_is_1d_array ,
@@ -143,8 +145,8 @@ def params():
143
145
144
146
145
147
@pytest .fixture
146
- def history (params ):
147
- data = {
148
+ def history_data (params ):
149
+ return {
148
150
"fun" : [10 , None , 9 , None , 2 , 5 ],
149
151
"task" : [
150
152
EvalTask .FUN ,
@@ -157,9 +159,19 @@ def history(params):
157
159
"start_time" : [0 , 2 , 5 , 7 , 10 , 12 ],
158
160
"stop_time" : [1 , 4 , 6 , 9 , 11 , 14 ],
159
161
"params" : params ,
160
- "batches" : [0 , 0 , 1 , 1 , 2 , 2 ],
162
+ "batches" : [0 , 1 , 2 , 3 , 4 , 5 ],
161
163
}
162
164
165
+
166
+ @pytest .fixture
167
+ def history (history_data ):
168
+ return History (direction = Direction .MINIMIZE , ** history_data )
169
+
170
+
171
+ @pytest .fixture
172
+ def history_with_batch_data (history_data ):
173
+ data = history_data .copy ()
174
+ data ["batches" ] = [0 , 0 , 1 , 1 , 2 , 2 ]
163
175
return History (direction = Direction .MINIMIZE , ** data )
164
176
165
177
@@ -211,9 +223,8 @@ def test_history_fun_data_with_fun_evaluations_cost_model_and_monotone(history):
211
223
assert_frame_equal (got , exp , check_dtype = False , check_categorical = False )
212
224
213
225
214
- @pytest .mark .xfail (reason = "Must be fixed!" )
215
- def test_history_fun_data_with_fun_batches_cost_model (history ):
216
- got = history .fun_data (
226
+ def test_history_fun_data_with_fun_batches_cost_model (history_with_batch_data ):
227
+ got = history_with_batch_data .fun_data (
217
228
cost_model = om .timing .fun_batches ,
218
229
monotone = False ,
219
230
)
@@ -328,23 +339,23 @@ def test_flat_param_names(history):
328
339
329
340
def test_get_time_per_task_fun (history ):
330
341
got = history ._get_time_per_task (EvalTask .FUN , cost_factor = 1 )
331
- exp = np .array ([1 , 1 , 2 , 2 , 3 , 3 ])
342
+ exp = np .array ([1 , 0 , 1 , 0 , 1 , 0 ])
332
343
assert_array_equal (got , exp )
333
344
334
345
335
- def test_get_time_per_task_jac (history ):
336
- got = history ._get_time_per_task (EvalTask .JAC , cost_factor = 1 )
337
- exp = np .array ([0 , 1 , 1 , 2 , 2 , 2 ])
346
+ def test_get_time_per_task_jac_cost_factor_none (history ):
347
+ got = history ._get_time_per_task (EvalTask .JAC , cost_factor = None )
348
+ exp = np .array ([0 , 2 , 0 , 2 , 0 , 0 ])
338
349
assert_array_equal (got , exp )
339
350
340
351
341
352
def test_get_time_per_task_fun_and_jac (history ):
342
- got = history ._get_time_per_task (EvalTask .FUN_AND_JAC , cost_factor = 1 )
343
- exp = np .array ([0 , 0 , 0 , 0 , 0 , 1 ])
353
+ got = history ._get_time_per_task (EvalTask .FUN_AND_JAC , cost_factor = - 0.5 )
354
+ exp = np .array ([0 , 0 , 0 , 0 , 0 , - 0.5 ])
344
355
assert_array_equal (got , exp )
345
356
346
357
347
- def test_get_time_cost_model (history ):
358
+ def test_get_time_custom_cost_model (history ):
348
359
cost_model = om .timing .CostModel (
349
360
fun = 0.5 , jac = 1 , fun_and_jac = 2 , label = "test" , aggregate_batch_time = sum
350
361
)
@@ -362,6 +373,30 @@ def test_get_time_cost_model(history):
362
373
assert_array_equal (got , exp )
363
374
364
375
376
+ def test_get_time_fun_evaluations (history ):
377
+ got = history ._get_time (cost_model = om .timing .fun_evaluations )
378
+ exp = np .array ([1 , 1 , 2 , 2 , 3 , 4 ])
379
+ assert_array_equal (got , exp )
380
+
381
+
382
+ def test_get_time_fun_batches (history ):
383
+ got = history ._get_time (cost_model = om .timing .fun_batches )
384
+ exp = np .array ([1 , 1 , 2 , 2 , 3 , 4 ])
385
+ assert_array_equal (got , exp )
386
+
387
+
388
+ def test_get_time_fun_batches_with_batch_data (history_with_batch_data ):
389
+ got = history_with_batch_data ._get_time (cost_model = om .timing .fun_batches )
390
+ exp = np .array ([1 , 1 , 2 , 2 , 3 , 3 ])
391
+ assert_array_equal (got , exp )
392
+
393
+
394
+ def test_get_time_evaluation_time (history ):
395
+ got = history ._get_time (cost_model = om .timing .evaluation_time )
396
+ exp = np .array ([1 , 3 , 4 , 6 , 7 , 9 ])
397
+ assert_array_equal (got , exp )
398
+
399
+
365
400
def test_get_time_wall_time (history ):
366
401
got = history ._get_time (cost_model = "wall_time" )
367
402
exp = np .array ([1 , 4 , 6 , 9 , 11 , 14 ])
@@ -381,7 +416,7 @@ def test_stop_time_property(history):
381
416
382
417
383
418
def test_batches_property (history ):
384
- assert history .batches == [0 , 0 , 1 , 1 , 2 , 2 ]
419
+ assert history .batches == [0 , 1 , 2 , 3 , 4 , 5 ]
385
420
386
421
387
422
# Tasks
@@ -466,3 +501,25 @@ def test_task_as_categorical():
466
501
got = _task_as_categorical (task )
467
502
assert got .tolist () == ["fun" , "jac" , "fun_and_jac" ]
468
503
assert isinstance (got .dtype , pd .CategoricalDtype )
504
+
505
+
506
+ def test_get_batch_start ():
507
+ batches = [0 , 0 , 1 , 1 , 1 , 2 , 2 , 3 ]
508
+ got = _get_batch_start (batches )
509
+ assert got == [0 , 2 , 5 , 7 ]
510
+
511
+
512
+ def test_batch_apply_sum ():
513
+ data = np .array ([0 , 1 , 2 , 3 , 4 ])
514
+ batch_ids = [0 , 0 , 1 , 1 , 2 ]
515
+ exp = np .array ([1 , 0 , 5 , 0 , 4 ])
516
+ got = _batch_apply (data , batch_ids , sum )
517
+ assert_array_equal (exp , got )
518
+
519
+
520
+ def test_batch_apply_max ():
521
+ data = np .array ([0 , 1 , 2 , 3 , 4 ])
522
+ batch_ids = [0 , 0 , 1 , 1 , 2 ]
523
+ exp = np .array ([1 , 0 , 3 , 0 , 4 ])
524
+ got = _batch_apply (data , batch_ids , max )
525
+ assert_array_equal (exp , got )
0 commit comments