class_docstr: Helper handler to log engine's output and/or metrics
  
  Args:
      tag: common title for all produced plots. For example, "training"
      metric_names: list of metric names to plot or a string "all" to plot all available
          metrics.
      output_transform: output transform function to prepare `engine.state.output` as a number.
          For example, `output_transform = lambda output: output`
          This function can also return a dictionary, e.g `{"loss": loss1, "another_loss": loss2}` to label the plot
          with corresponding keys.
      global_step_transform: global step transform function to output a desired global step.
          Input of the function is `(engine, event_name)`. Output of function should be an integer.
          Default is None, global_step based on attached engine. If provided,
          uses function output as global_step. To setup global step from another engine, please use
          :meth:`~ignite.contrib.handlers.clearml_logger.global_step_from_engine`.
      state_attributes: list of attributes of the ``trainer.state`` to plot.
  
  Examples:
      .. code-block:: python
  
          from ignite.contrib.handlers.clearml_logger import *
  
          # Create a logger
  
          clearml_logger = ClearMLLogger(
              project_name="pytorch-ignite-integration",
              task_name="cnn-mnist"
          )
  
          # Attach the logger to the evaluator on the validation dataset and log NLL, Accuracy metrics after
          # each epoch. We setup `global_step_transform=global_step_from_engine(trainer)` to take the epoch
          # of the `trainer`:
          clearml_logger.attach(
              evaluator,
              log_handler=OutputHandler(
                  tag="validation",
                  metric_names=["nll", "accuracy"],
                  global_step_transform=global_step_from_engine(trainer)
              ),
              event_name=Events.EPOCH_COMPLETED
          )
          # or equivalently
          clearml_logger.attach_output_handler(
              evaluator,
              event_name=Events.EPOCH_COMPLETED,
              tag="validation",
              metric_names=["nll", "accuracy"],
              global_step_transform=global_step_from_engine(trainer)
          )
  
      Another example, where model is evaluated every 500 iterations:
  
      .. code-block:: python
  
          from ignite.contrib.handlers.clearml_logger import *
  
          @trainer.on(Events.ITERATION_COMPLETED(every=500))
          def evaluate(engine):
              evaluator.run(validation_set, max_epochs=1)
  
          # Create a logger
  
          clearml_logger = ClearMLLogger(
              project_name="pytorch-ignite-integration",
              task_name="cnn-mnist"
          )
  
          def global_step_transform(*args, **kwargs):
              return trainer.state.iteration
  
          # Attach the logger to the evaluator on the validation dataset and log NLL, Accuracy metrics after
          # every 500 iterations. Since evaluator engine does not have access to the training iteration, we
          # provide a global_step_transform to return the trainer.state.iteration for the global_step, each time
          # evaluator metrics are plotted on ClearML.
  
          clearml_logger.attach_output_handler(
              evaluator,
              event_name=Events.EPOCH_COMPLETED,
              tag="validation",
              metrics=["nll", "accuracy"],
              global_step_transform=global_step_transform
          )
  
      Another example where the State Attributes ``trainer.state.alpha`` and ``trainer.state.beta``
      are also logged along with the NLL and Accuracy after each iteration:
  
      .. code-block:: python
  
          clearml_logger.attach(
              trainer,
              log_handler=OutputHandler(
                  tag="training",
                  metric_names=["nll", "accuracy"],
                  state_attributes=["alpha", "beta"],
              ),
              event_name=Events.ITERATION_COMPLETED
          )
  
      Example of `global_step_transform`
  
      .. code-block:: python
  
          def global_step_transform(engine, event_name):
              return engine.state.get_event_attrib_value(event_name)
  
  .. versionchanged:: 0.4.7
      accepts an optional list of `state_attributes`
class_docstr_tok: ["Helper","handler","to","log","engine","'s","output","and\/or","metrics","Args",":","tag",":","common","title","for","all","produced","plots",".","For","example",",","``","training","''","metric_names",":","list","of","metric","names","to","plot","or","a","string","``","all","''","to","plot","all","available","metrics",".","output_transform",":","output","transform","function","to","prepare","`","engine.state.output","`","as","a","number",".","For","example",",","`","output_transform","=","lambda","output",":","output","`","This","function","can","also","return","a","dictionary",",","e.g","`","{","``","loss","''",":","loss1",",","``","another_loss","''",":","loss2","}","`","to","label","the","plot","with","corresponding","keys",".","global_step_transform",":","global","step","transform","function","to","output","a","desired","global","step",".","Input","of","the","function","is","`","(","engine",",","event_name",")","`",".","Output","of","function","should","be","an","integer",".","Default","is","None",",","global_step","based","on","attached","engine",".","If","provided",",","uses","function","output","as","global_step",".","To","setup","global","step","from","another","engine",",","please","use",":","meth",":","`","~ignite.contrib.handlers.clearml_logger.global_step_from_engine","`",".","state_attributes",":","list","of","attributes","of","the","``","trainer.state","``","to","plot",".","Examples",":","..","code-block",":",":","python","from","ignite.contrib.handlers.clearml_logger","import","*","#","Create","a","logger","clearml_logger","=","ClearMLLogger","(","project_name=","''","pytorch-ignite-integration","''",",","task_name=","''","cnn-mnist","''",")","#","Attach","the","logger","to","the","evaluator","on","the","validation","dataset","and","log","NLL",",","Accuracy","metrics","after","#","each","epoch",".","We","setup","`","global_step_transform=global_step_from_engine","(","trainer",")","`","to","take","the","epoch","#","of","the","`","trainer","`",":","clearml_logger.attach","(","evaluator",",","log_handler=OutputHandler","(","tag=","''","validation","''",",","metric_names=","[","``","nll","''",",","``","accuracy","''","]",",","global_step_transform=global_step_from_engine","(","trainer",")",")",",","event_name=Events.EPOCH_COMPLETED",")","#","or","equivalently","clearml_logger.attach_output_handler","(","evaluator",",","event_name=Events.EPOCH_COMPLETED",",","tag=","''","validation","''",",","metric_names=","[","``","nll","''",",","``","accuracy","''","]",",","global_step_transform=global_step_from_engine","(","trainer",")",")","Another","example",",","where","model","is","evaluated","every","500","iterations",":","..","code-block",":",":","python","from","ignite.contrib.handlers.clearml_logger","import","*","@","trainer.on","(","Events.ITERATION_COMPLETED","(","every=500",")",")","def","evaluate","(","engine",")",":","evaluator.run","(","validation_set",",","max_epochs=1",")","#","Create","a","logger","clearml_logger","=","ClearMLLogger","(","project_name=","''","pytorch-ignite-integration","''",",","task_name=","''","cnn-mnist","''",")","def","global_step_transform","(","*","args",",","*","*","kwargs",")",":","return","trainer.state.iteration","#","Attach","the","logger","to","the","evaluator","on","the","validation","dataset","and","log","NLL",",","Accuracy","metrics","after","#","every","500","iterations",".","Since","evaluator","engine","does","not","have","access","to","the","training","iteration",",","we","#","provide","a","global_step_transform","to","return","the","trainer.state.iteration","for","the","global_step",",","each","time","#","evaluator","metrics","are","plotted","on","ClearML",".","clearml_logger.attach_output_handler","(","evaluator",",","event_name=Events.EPOCH_COMPLETED",",","tag=","''","validation","''",",","metrics=","[","``","nll","''",",","``","accuracy","''","]",",","global_step_transform=global_step_transform",")","Another","example","where","the","State","Attributes","``","trainer.state.alpha","``","and","``","trainer.state.beta","``","are","also","logged","along","with","the","NLL","and","Accuracy","after","each","iteration",":","..","code-block",":",":","python","clearml_logger.attach","(","trainer",",","log_handler=OutputHandler","(","tag=","''","training","''",",","metric_names=","[","``","nll","''",",","``","accuracy","''","]",",","state_attributes=","[","``","alpha","''",",","``","beta","''","]",",",")",",","event_name=Events.ITERATION_COMPLETED",")","Example","of","`","global_step_transform","`","..","code-block",":",":","python","def","global_step_transform","(","engine",",","event_name",")",":","return","engine.state.get_event_attrib_value","(","event_name",")","..","versionchanged",":",":","0.4.7","accepts","an","optional","list","of","`","state_attributes","`"]
code: def __call__(
      self,
      engine: Engine,
      logger: ClearMLLogger,
      event_name: Union[str, Events],
  ) -> None:
      if not isinstance(logger, ClearMLLogger):
          raise RuntimeError(
              "Handler OutputHandler works only with ClearMLLogger"
          )
  
      metrics = self._setup_output_metrics_state_attrs(engine)
  
      global_step = self.global_step_transform(engine, event_name)
  
      if not isinstance(global_step, int):
          raise TypeError(
              f"global_step must be int, got {type(global_step)}."
              " Please check the output of global_step_transform."
          )
  
      for key, value in metrics.items():
          if len(key) == 2:
              logger.clearml_logger.report_scalar(
                  title=key[0],
                  series=key[1],
                  iteration=global_step,
                  value=value,
              )
          elif len(key) == 3:
              logger.clearml_logger.report_scalar(
                  title=f"{key[0]}/{key[1]}",
                  series=key[2],
                  iteration=global_step,
                  value=value,
              )
  
imports: import os
  import tempfile
  import warnings
  from collections import defaultdict
  from datetime import datetime
  from enum import Enum
  from typing import Any, Callable, DefaultDict, List, Mapping, Optional, Tuple, Type, Union
  from torch.optim import Optimizer
  import ignite.distributed
  from ignite.contrib.handlers.base_logger import BaseLogger, BaseOptimizerParamsHandler, BaseOutputHandler, BaseWeightsHandler, BaseWeightsScalarHandler
  from ignite.engine import Engine, Events
  from ignite.handlers import global_step_from_engine
  from ignite.handlers.checkpoint import DiskSaver
label_desc: Use image node_id 2 for calling the OutputHandler obj's underlying member method code with example usage: obj.__call__(engine, logger, event_name) without return types