def test_on_step_end(self, mocker): """ should update episode_metrics for the current agent and steps_cycle_metrics for all agents on_step_end. """ mocker.patch(self.logging_callback_path + '._update_metrics_all_agents') mocker.patch(self.logging_callback_path + '._update_metrics') logging_callback = LoggingCallback() logging_callback.n_agents = 1 logging_callback.on_step_end(step=7) args, kwargs = logging_callback._update_metrics.call_args check.equal(args[1], 'episode') check.is_false(kwargs.get('reset')) check.is_none(kwargs.get('agent_id')) args, kwargs = logging_callback._update_metrics_all_agents.call_args check.equal(args[1], 'steps_cycle') check.is_false(kwargs.get('reset')) # Multi agents logging_callback = LoggingCallback() logging_callback.n_agents = 5 logging_callback.on_step_end(step=7, logs={'agent_id': 3}) args, kwargs = logging_callback._update_metrics.call_args check.equal(args[1], 'episode') check.is_false(kwargs.get('reset')) check.equal(kwargs.get('agent_id'), 3) args, kwargs = logging_callback._update_metrics_all_agents.call_args check.equal(args[1], 'steps_cycle') check.is_false(kwargs.get('reset'))
def test_logging_episodes_operators_(eps_operator, cycle_operator, metric_name): print(eps_operator, cycle_operator, '\n') logging_callback = LoggingCallback( detailed_step_metrics=[], metrics=[('reward', { 'episode': eps_operator, 'episodes': cycle_operator }), ('loss', { 'episode': eps_operator, 'episodes': cycle_operator })], ) def check_function(callbacks, logs): callback_dict = callbacks.callbacks[0].__dict__ for position in ('episode', 'episodes_cycle'): if position == 'episode': expected = logs.get(f'{metric_name}_episode_{eps_operator}') elif position == 'episodes_cycle': expected = logs.get( f'{metric_name}_{eps_operator}_cycle_{cycle_operator}') logged = callback_dict[f'{position}_{metric_name}'] if expected is not None: print(position.capitalize(), metric_name, logged, expected) assert logged != 'N/A' and np.isclose(logged, expected), \ f'Logged {logged} instead of {expected}' playground = DummyPlayground() playground.run([logging_callback], eps_end_func=check_function, verbose=1) print()
def setup(self): self.metric_list = MetricList( [Metric("metric_1"), Metric("metric_2"), Metric("metric_2")]) self.logging_callback = LoggingCallback()
def test_get_value_no_prefix_no_logs(self, mocker): """ should return N/A when nothing is found. """ expected_value = 'N/A' logging_callback = LoggingCallback() metric = Metric('attribute') value = logging_callback._get_value(metric) check.equal(value, expected_value)
def test_update_attr_avg(self): """ should update arguments correctly with avg operator. """ logging_callback = LoggingCallback() logging_callback.reward = 0 logging_callback.reward_seen = 2 logging_callback._update_attr('reward', 1, 'avg') check.equal(logging_callback.reward, 1 / 3) check.equal(logging_callback.reward_seen, 3) # With N/A logging_callback = LoggingCallback() logging_callback.reward = 'N/A' logging_callback.reward_seen = 0 logging_callback._update_attr('reward', 1, 'avg') check.equal(logging_callback.reward, 1) check.equal(logging_callback.reward_seen, 1)
def test_reset_attr_avg(self): """ should reset arguments correctly. """ logging_callback = LoggingCallback() logging_callback.reward = 0 logging_callback._reset_attr('reward', 'avg') check.equal(logging_callback.reward, 'N/A') check.equal(logging_callback.reward_seen, 0)
def test_on_step_cycle_begin(self, mocker): """ should reset steps_cycle_metrics on step_cycle_begin. """ mocker.patch(self.logging_callback_path + '._update_metrics_all_agents') logging_callback = LoggingCallback() logging_callback.on_steps_cycle_begin(step=7) args, kwargs = logging_callback._update_metrics_all_agents.call_args check.equal(args[1], 'steps_cycle') check.is_true(kwargs.get('reset'))
def test_on_episode_end(self, mocker): """ should update episodes_cycle_metrics on_episode_end from episode metrics. """ mocker.patch(self.logging_callback_path + '._update_metrics_all_agents') logging_callback = LoggingCallback() logging_callback.on_episode_end(episode=7) args, kwargs = logging_callback._update_metrics_all_agents.call_args check.equal(args[1], 'episodes_cycle') check.is_false(kwargs.get('reset')) check.equal(kwargs.get('source_prefix'), 'episode')
def test_on_run_begin(self, mocker): """ should set n_agents on_run_begin. """ class DummyPlayground(): """DummyPlaygrounf""" def __init__(self, n_agents): self.agents = [None] * n_agents n_agents = 5 mocker.patch(self.logging_callback_path + '', return_value=n_agents) logging_callback = LoggingCallback() logging_callback.playground = DummyPlayground(n_agents) check.is_none(logging_callback.n_agents) logging_callback.on_run_begin() check.equal(logging_callback.n_agents, n_agents)
def test_get_value_no_prefix_logs(self, mocker): """ should return value in logs when no prefix is given. """ expected_value = 123 def _extract_metric_from_logs(self, metric_name, logs, agent_id): return logs[metric_name] mocker.patch( 'learnrl.callbacks.logging_callback.LoggingCallback._extract_metric_from_logs', _extract_metric_from_logs) logging_callback = LoggingCallback() logs = {'attribute': 123} metric = Metric('attribute') value = logging_callback._get_value(metric, logs=logs) check.equal(value, expected_value)
def test_get_value_with_prefix(self, mocker): """ should return attr value when prefix is given. """ expected_value = 123 def _get_attr_name(*args): return 'attribute' mocker.patch( 'learnrl.callbacks.logging_callback.LoggingCallback._get_attr_name', _get_attr_name) logging_callback = LoggingCallback() logging_callback.attribute = expected_value metric = Metric('attribute') value = logging_callback._get_value(metric, prefix='prefix') check.equal(value, expected_value)
def test_logging_steps_operators_(cycle_operator, metric_name): print(cycle_operator, metric_name) logging_callback = LoggingCallback(metrics=[('reward', { 'steps': cycle_operator }), ('loss', { 'steps': cycle_operator })], ) def check_function(callbacks, logs): callback_dict = callbacks.callbacks[0].__dict__ expected = logs.get(f'{metric_name}_steps_{cycle_operator}') logged = callback_dict[f'steps_cycle_{metric_name}'] if expected is not None: print(metric_name, logged, expected) assert logged != 'N/A' and np.isclose(logged, expected), \ f'Logged {logged} instead of {expected}' playground = DummyPlayground() playground.run([logging_callback], eps_end_func=check_function, verbose=3) print()
def test_update_attr_raise(self): """ should raise ValueError if operator is unknowed. """ with pytest.raises(ValueError, match=r"Unknowed operator.*"): logging_callback = LoggingCallback() logging_callback.reward = 'N/A' logging_callback._update_attr('reward', 1, 'x')
def test_update_attr_last(self): """ should update arguments correctly with last operator. """ logging_callback = LoggingCallback() logging_callback.reward = 2 logging_callback._update_attr('reward', 1, 'last') check.equal(logging_callback.reward, 1)
def test_init(self): """ should instanciate correctly. """ LoggingCallback()