def test_correct_scope_handled(self, periodic_handler_mock): logger = TrainingLogger() logger.register_handler('handler', periodic_handler_mock) logger.register_value('value', ['handler']) with logger.scope(scope='wrong_scope'): pass periodic_handler_mock.flush.assert_not_called() with logger.scope(scope='test_scope'): pass assert periodic_handler_mock.flush.call_count == 1
def test_value_resets(self, periodic_handler_mock): logger = TrainingLogger() logger.register_handler('handler', periodic_handler_mock) logger.register_value('scope/value', ['handler'], average=True) with logger.scope(scope='test_scope'): logger.log_value('scope/value', 1) logger.log_value('scope/value', 2) logger.log_value('scope/value', 3) assert logger.get_value('scope/value') == 2 logger.reset_values('scope') with logger.scope(scope='test_scope'): logger.log_value('scope/value', 5) logger.log_value('scope/value', 10) logger.log_value('scope/value', 15) assert logger.get_value('scope/value') == 10
def test_flush_writes_to_stream(self, mocker): logger = TrainingLogger() stream = mocker.Mock() stream_handler = StreamHandler('epoch', stream=stream, fmt='* {values}') logger.register_handler('handler', stream_handler) logger.register_value('value', ['handler']) with logger.scope(): logger.log_value('value', 1) logger.log_value('value', 0.1) stream.write.assert_called_once_with('* value 0.1000\n') assert stream.flush.call_count == 1