code: def test_metrics_writer_mock(self, iteration_log):
      with tempfile.TemporaryDirectory() as tempdir:
          # set up engine
          def _train_func(engine, batch):
              return [batch + 1.0]
  
          engine = Engine(_train_func)
  
          # set up dummy metric
          @engine.on(Events.EPOCH_COMPLETED)
          def _update_metric(engine):
              current_metric = engine.state.metrics.get("acc", 0.1)
              engine.state.metrics["acc"] = current_metric + 0.1
              engine.state.test = current_metric
  
          # set up testing handler
          writer = SummaryWriter(log_dir=tempdir)
          stats_handler = TensorBoardStatsHandler(
              summary_writer=writer,
              iteration_log=iteration_log,
              epoch_log=False,
              output_transform=lambda x: {"loss": x[0] * 2.0},
              global_epoch_transform=lambda x: x * 3.0,
              state_attributes=["test"],
          )
          stats_handler._default_iteration_writer = MagicMock()
          stats_handler.attach(engine)
  
          num_iters = 3
          max_epochs = 2
          engine.run(range(num_iters), max_epochs=max_epochs)
          writer.close()
  
          if iteration_log is True:
              self.assertEqual(
                  stats_handler._default_iteration_writer.call_count,
                  num_iters * max_epochs,
              )
          else:
              self.assertEqual(
                  stats_handler._default_iteration_writer.call_count, 2
              )
  
file_path: MONAI/tests/test_handler_tb_stats.py
imports: from __future__ import annotations
  import glob
  import tempfile
  import unittest
  from unittest.mock import MagicMock
  from ignite.engine import Engine, Events
  from parameterized import parameterized
  from monai.handlers import TensorBoardStatsHandler
  from monai.utils import optional_import
label_desc: Use image node_id 4 for calling the TestHandlerTBStats obj's underlying member method code with example usage: obj.test_metrics_writer_mock(iteration_log) and returns: list