class_docstr: TensorBoardImageHandler is an Ignite Event handler that can visualize images, labels and outputs as 2D/3D images.
  2D output (shape in Batch, channel, H, W) will be shown as simple image using the first element in the batch,
  for 3D to ND output (shape in Batch, channel, H, W, D) input, each of ``self.max_channels`` number of images'
  last three dimensions will be shown as animated GIF along the last axis (typically Depth).
  
  It can be used for any Ignite Engine (trainer, validator and evaluator).
  User can easily add it to engine for any expected Event, for example: ``EPOCH_COMPLETED``,
  ``ITERATION_COMPLETED``. The expected data source is ignite's ``engine.state.batch`` and ``engine.state.output``.
  
  Default behavior:
      - Show y_pred as images (GIF for 3D) on TensorBoard when Event triggered,
      - Need to use ``batch_transform`` and ``output_transform`` to specify
        how many images to show and show which channel.
      - Expects ``batch_transform(engine.state.batch)`` to return data
        format: (image[N, channel, ...], label[N, channel, ...]).
      - Expects ``output_transform(engine.state.output)`` to return a torch
        tensor in format (y_pred[N, channel, ...], loss).
class_docstr_tok: ["TensorBoardImageHandler","is","an","Ignite","Event","handler","that","can","visualize","images",",","labels","and","outputs","as","2D\/3D","images",".","2D","output","(","shape","in","Batch",",","channel",",","H",",","W",")","will","be","shown","as","simple","image","using","the","first","element","in","the","batch",",","for","3D","to","ND","output","(","shape","in","Batch",",","channel",",","H",",","W",",","D",")","input",",","each","of","``","self.max_channels","``","number","of","images'","last","three","dimensions","will","be","shown","as","animated","GIF","along","the","last","axis","(","typically","Depth",")",".","It","can","be","used","for","any","Ignite","Engine","(","trainer",",","validator","and","evaluator",")",".","User","can","easily","add","it","to","engine","for","any","expected","Event",",","for","example",":","``","EPOCH_COMPLETED","``",",","``","ITERATION_COMPLETED","``",".","The","expected","data","source","is","ignite","'s","``","engine.state.batch","``","and","``","engine.state.output","``",".","Default","behavior",":","-","Show","y_pred","as","images","(","GIF","for","3D",")","on","TensorBoard","when","Event","triggered",",","-","Need","to","use","``","batch_transform","``","and","``","output_transform","``","to","specify","how","many","images","to","show","and","show","which","channel",".","-","Expects","``","batch_transform","(","engine.state.batch",")","``","to","return","data","format",":","(","image","[","N",",","channel",",","...","]",",","label","[","N",",","channel",",","...","]",")",".","-","Expects","``","output_transform","(","engine.state.output",")","``","to","return","a","torch","tensor","in","format","(","y_pred","[","N",",","channel",",","...","]",",","loss",")","."]
docstr: Args:
      summary_writer: user can specify TensorBoard SummaryWriter,
          default to create a new writer.
      log_dir: if using default SummaryWriter, write logs to this directory, default is `./runs`.
      interval: plot content from engine.state every N epochs or every N iterations, default is 1.
      batch_transform: a callable that is used to extract `image` and `label` from `ignite.engine.state.batch`,
          then construct `(image, label)` pair. for example: if `ignite.engine.state.batch` is `{"image": xxx,
          "label": xxx, "other": xxx}`, `batch_transform` can be `lambda x: (x["image"], x["label"])`.
          will use the result to plot image from `result[0][index]` and plot label from `result[1][index]`.
      output_transform: a callable that is used to extract the `predictions` data from
          `ignite.engine.state.output`, will use the result to plot output from `result[index]`.
      global_iter_transform: a callable that is used to customize global step number for TensorBoard.
          For example, in evaluation, the evaluator engine needs to know current epoch from trainer.
      index: plot which element in a data batch, default is the first element.
      max_channels: number of channels to plot.
      max_frames: number of frames for 2D-t plot.
docstr_tok: ["Args",":","summary_writer",":","user","can","specify","TensorBoard","SummaryWriter",",","default","to","create","a","new","writer",".","log_dir",":","if","using","default","SummaryWriter",",","write","logs","to","this","directory",",","default","is","`",".\/runs","`",".","interval",":","plot","content","from","engine.state","every","N","epochs","or","every","N","iterations",",","default","is","1.","batch_transform",":","a","callable","that","is","used","to","extract","`","image","`","and","`","label","`","from","`","ignite.engine.state.batch","`",",","then","construct","`","(","image",",","label",")","`","pair",".","for","example",":","if","`","ignite.engine.state.batch","`","is","`","{","``","image","''",":","xxx",",","``","label","''",":","xxx",",","``","other","''",":","xxx","}","`",",","`","batch_transform","`","can","be","`","lambda","x",":","(","x","[","``","image","''","]",",","x","[","``","label","''","]",")","`",".","will","use","the","result","to","plot","image","from","`","result","[","0","]","[","index","]","`","and","plot","label","from","`","result","[","1","]","[","index","]","`",".","output_transform",":","a","callable","that","is","used","to","extract","the","`","predictions","`","data","from","`","ignite.engine.state.output","`",",","will","use","the","result","to","plot","output","from","`","result","[","index","]","`",".","global_iter_transform",":","a","callable","that","is","used","to","customize","global","step","number","for","TensorBoard",".","For","example",",","in","evaluation",",","the","evaluator","engine","needs","to","know","current","epoch","from","trainer",".","index",":","plot","which","element","in","a","data","batch",",","default","is","the","first","element",".","max_channels",":","number","of","channels","to","plot",".","max_frames",":","number","of","frames","for","2D-t","plot","."]
code: def __init__(
      self,
      summary_writer: Optional[SummaryWriter] = None,
      log_dir: str = "./runs",
      interval: int = 1,
      batch_transform: Callable = lambda x: x,
      output_transform: Callable = lambda x: x,
      global_iter_transform: Callable = lambda x: x,
      index: int = 0,
      max_channels: int = 1,
      max_frames: int = 64,
  ) -> None:
      """
      Args:
          summary_writer: user can specify TensorBoard SummaryWriter,
              default to create a new writer.
          log_dir: if using default SummaryWriter, write logs to this directory, default is `./runs`.
          interval: plot content from engine.state every N epochs or every N iterations, default is 1.
          batch_transform: a callable that is used to extract `image` and `label` from `ignite.engine.state.batch`,
              then construct `(image, label)` pair. for example: if `ignite.engine.state.batch` is `{"image": xxx,
              "label": xxx, "other": xxx}`, `batch_transform` can be `lambda x: (x["image"], x["label"])`.
              will use the result to plot image from `result[0][index]` and plot label from `result[1][index]`.
          output_transform: a callable that is used to extract the `predictions` data from
              `ignite.engine.state.output`, will use the result to plot output from `result[index]`.
          global_iter_transform: a callable that is used to customize global step number for TensorBoard.
              For example, in evaluation, the evaluator engine needs to know current epoch from trainer.
          index: plot which element in a data batch, default is the first element.
          max_channels: number of channels to plot.
          max_frames: number of frames for 2D-t plot.
      """
  
      if summary_writer is None:
          self._writer = SummaryWriter(log_dir=log_dir)
          self.internal_writer = True
      else:
          self._writer = summary_writer
          self.internal_writer = False
  
      self.interval = interval
      self.batch_transform = batch_transform
      self.output_transform = output_transform
      self.global_iter_transform = global_iter_transform
      self.index = index
      self.max_frames = max_frames
      self.max_channels = max_channels
  
file_path: MONAILabel/monailabel/deepedit/handlers.py
imports: import logging
  from typing import TYPE_CHECKING, Any, Callable, Dict, Optional
  import numpy
  import torch
  from monai.config import IgniteInfo
  from monai.utils import min_version, optional_import
  from monai.visualize import plot_2d_or_3d_image
  from torch import distributed
  from torchvision.utils import make_grid
label_desc: Use image node_id 1 to create a new TensorBoardImageHandler object  with example: obj = TensorBoardImageHandler(summary_writer, log_dir, interval, batch_transform, output_transform, global_iter_transform, index, max_channels, max_frames)