11from __future__ import annotations
22
33import io
4- from unittest .mock import MagicMock , patch
4+ from unittest .mock import patch
55
6- import pytest
76from colorama import Fore
87
98from bayes_opt import BayesianOptimization
@@ -52,44 +51,6 @@ def test_is_constrained_property():
5251 assert logger .is_constrained
5352
5453
55- def test_params_config_property ():
56- """Test the params_config property getter and setter."""
57- # Test the getter with default initialization (None)
58- logger = ScreenLogger ()
59- assert logger .params_config is None
60-
61- # Test initialization with a params_config
62- mock_config = {"param1" : MagicMock (), "param2" : MagicMock ()}
63- logger_with_config = ScreenLogger (params_config = mock_config )
64- assert logger_with_config .params_config is mock_config
65-
66- # Test the setter
67- new_config = {"param3" : MagicMock (), "param4" : MagicMock ()}
68- logger .params_config = new_config
69- assert logger .params_config is new_config
70-
71- # Test that the logger actually uses the params_config
72- optimizer = BayesianOptimization (target_func , PBOUNDS , random_state = 1 )
73- logger .params_config = optimizer ._space ._params_config
74- optimizer .register (params = {"p1" : 1.5 , "p2" : 2.5 }, target = 4.0 )
75-
76- # This should not raise an error now that params_config is set
77- step_str = logger ._print_step (optimizer ._space .res ()[- 1 ], optimizer ._space .keys )
78- assert "|" in step_str
79- assert "1" in step_str # iteration
80- assert "4.0" in step_str # target value
81-
82-
83- def test_print_step_without_params_config ():
84- """Test that _print_step raises an error when params_config is None."""
85- logger = ScreenLogger ()
86- optimizer = BayesianOptimization (target_func , PBOUNDS , random_state = 1 )
87- optimizer .register (params = {"p1" : 1.5 , "p2" : 2.5 }, target = 4.0 )
88-
89- with pytest .raises (ValueError , match = "Parameter configuration is not set" ):
90- logger ._print_step (optimizer ._space .res ()[- 1 ], optimizer ._space .keys )
91-
92-
9354def test_format_number ():
9455 """Test the _format_number method."""
9556 logger = ScreenLogger ()
@@ -155,22 +116,23 @@ def test_step():
155116 """Test the _print_step method."""
156117 optimizer = BayesianOptimization (target_func , PBOUNDS , random_state = 1 )
157118
158- # Create a logger with the params_config from the optimizer
159- logger = ScreenLogger (params_config = optimizer ._space ._params_config )
119+ logger = ScreenLogger ()
160120
161121 # Register a point so we have something to log
162122 optimizer .register (params = {"p1" : 1.5 , "p2" : 2.5 }, target = 4.0 )
163123
164124 # Test default color
165- step_str = logger ._print_step (optimizer ._space .res ()[- 1 ], optimizer ._space .keys )
125+ step_str = logger ._print_step (
126+ optimizer ._space .res ()[- 1 ], optimizer ._space .keys , optimizer ._space .params_config
127+ )
166128 assert "|" in step_str
167129 assert "1" in step_str # iteration
168130 assert "4.0" in step_str # target value
169131
170132 # Test with custom color
171133 custom_color = Fore .RED
172134 step_str_colored = logger ._print_step (
173- optimizer ._space .res ()[- 1 ], optimizer ._space .keys , colour = custom_color
135+ optimizer ._space .res ()[- 1 ], optimizer ._space .keys , optimizer . _space . params_config , colour = custom_color
174136 )
175137 assert custom_color in step_str_colored
176138
@@ -252,26 +214,6 @@ def test_update_tracker():
252214 assert logger ._previous_max_params == {"p1" : 2 , "p2" : 2 } # Updated
253215
254216
255- def test_time_metrics ():
256- """Test the _time_metrics method."""
257- logger = ScreenLogger ()
258-
259- # First call initializes times
260- time_str , total_elapsed , delta = logger ._time_metrics ()
261- assert isinstance (time_str , str )
262- assert isinstance (total_elapsed , float )
263- assert isinstance (delta , float )
264- assert delta <= 0.1 # First call should have very small delta
265-
266- # Subsequent call should show time difference
267- import time
268-
269- time .sleep (0.01 ) # Small delay to ensure time difference
270- time_str2 , total_elapsed2 , delta2 = logger ._time_metrics ()
271- assert total_elapsed2 > total_elapsed
272- assert delta2 > 0
273-
274-
275217@patch ("sys.stdout" , new_callable = io .StringIO )
276218def test_log_optimization_start (mock_stdout ):
277219 """Test the log_optimization_start method."""
@@ -297,8 +239,7 @@ def test_log_optimization_step(mock_stdout):
297239 """Test the log_optimization_step method."""
298240 optimizer = BayesianOptimization (target_func , PBOUNDS , random_state = 1 )
299241
300- # Create a logger with the params_config from the optimizer
301- logger = ScreenLogger (params_config = optimizer ._space ._params_config )
242+ logger = ScreenLogger ()
302243
303244 # Create logger with verbose=1 specifically, as this is the only verbose level
304245 # that doesn't print for non-max points according to the implementation:
@@ -317,14 +258,18 @@ def test_log_optimization_step(mock_stdout):
317258 # For a point that is not a new max with verbose=1, should not print
318259 mock_stdout .truncate (0 )
319260 mock_stdout .seek (0 )
320- logger .log_optimization_step (optimizer ._space .keys , optimizer ._space .res ()[- 1 ], optimizer .max )
261+ logger .log_optimization_step (
262+ optimizer ._space .keys , optimizer ._space .res ()[- 1 ], optimizer ._space .params_config , optimizer .max
263+ )
321264 assert mock_stdout .getvalue () == "" # Nothing printed for non-max point with verbose=1
322265
323266 # Register a higher value, which should trigger output with verbose=1
324267 optimizer .register (params = {"p1" : 2 , "p2" : 2 }, target = 4 )
325268 mock_stdout .truncate (0 )
326269 mock_stdout .seek (0 )
327- logger .log_optimization_step (optimizer ._space .keys , optimizer ._space .res ()[- 1 ], optimizer .max )
270+ logger .log_optimization_step (
271+ optimizer ._space .keys , optimizer ._space .res ()[- 1 ], optimizer ._space .params_config , optimizer .max
272+ )
328273 max_output = mock_stdout .getvalue ()
329274 assert max_output != "" # Something printed for new max point with verbose=1
330275 assert "4.0" in max_output # Should show target value
0 commit comments