def monkey_patched_summaries()

in adanet/core/summary.py [0:0]


def monkey_patched_summaries(summary):
  """A context where global summary functions point to the given summary.

  Restores original summary functions upon exit.

  NOTE: This function is not thread-safe.

  Args:
    summary: An `adanet.Summary` instance.

  Yields:
    A context where summary functions are routed to the given `adanet.Summary`.
  """

  from tensorflow.python.ops import summary_ops_v2 as summary_v2_lib  # pylint: disable=g-direct-tensorflow-import,g-import-not-at-top

  old_summary_scalar = summary_lib.scalar
  old_summary_image = summary_lib.image
  old_summary_histogram = summary_lib.histogram
  old_summary_audio = summary_lib.audio
  old_summary_v2_scalar = summary_v2_lib.scalar
  old_summary_v2_image = summary_v2_lib.image
  old_summary_v2_histogram = summary_v2_lib.histogram
  old_summary_v2_audio = summary_v2_lib.audio
  old_summary_compat_v2_scalar = tf_compat.v2.summary.scalar
  old_summary_compat_v2_image = tf_compat.v2.summary.image
  old_summary_compat_v2_histogram = tf_compat.v2.summary.histogram
  old_summary_compat_v2_audio = tf_compat.v2.summary.audio

  # Monkey-patch global attributes.
  wrapped_summary = _SummaryWrapper(summary)
  setattr(tf_v1.summary, "scalar", wrapped_summary.scalar)
  setattr(tf_v1.summary, "image", wrapped_summary.image)
  setattr(tf_v1.summary, "histogram", wrapped_summary.histogram)
  setattr(tf_v1.summary, "audio", wrapped_summary.audio)
  setattr(tf_compat.v1.summary, "scalar", wrapped_summary.scalar)
  setattr(tf_compat.v1.summary, "image", wrapped_summary.image)
  setattr(tf_compat.v1.summary, "histogram", wrapped_summary.histogram)
  setattr(tf_compat.v1.summary, "audio", wrapped_summary.audio)
  setattr(summary_lib, "scalar", wrapped_summary.scalar)
  setattr(summary_lib, "image", wrapped_summary.image)
  setattr(summary_lib, "histogram", wrapped_summary.histogram)
  setattr(summary_lib, "audio", wrapped_summary.audio)
  setattr(tf_compat.v2.summary, "scalar", wrapped_summary.scalar_v3)
  setattr(tf_compat.v2.summary, "image", wrapped_summary.image_v3)
  setattr(tf_compat.v2.summary, "histogram", wrapped_summary.histogram_v3)
  setattr(tf_compat.v2.summary, "audio", wrapped_summary.audio_v3)
  setattr(summary_v2_lib, "scalar", wrapped_summary.scalar_v2)
  setattr(summary_v2_lib, "image", wrapped_summary.image_v2)
  setattr(summary_v2_lib, "histogram", wrapped_summary.histogram_v2)
  setattr(summary_v2_lib, "audio", wrapped_summary.audio_v2)
  try:
    # TF 2.0 eliminates tf.contrib.
    setattr(tf_v1.contrib.summary, "scalar", wrapped_summary.scalar_v2)
    setattr(tf_v1.contrib.summary, "image", wrapped_summary.image_v2)
    setattr(tf_v1.contrib.summary, "histogram", wrapped_summary.histogram_v2)
    setattr(tf_v1.contrib.summary, "audio", wrapped_summary.audio_v2)
  except (AttributeError, ImportError):
    # TF 2.0 eliminates tf.contrib.
    # Also set the new tf.summary to be use the new summaries in TF 2.
    if tf_compat.version_greater_or_equal("2.0.0"):
      setattr(tf.summary, "scalar", wrapped_summary.scalar_v3)
      setattr(tf.summary, "image", wrapped_summary.image_v3)
      setattr(tf.summary, "histogram", wrapped_summary.histogram_v3)
      setattr(tf.summary, "audio", wrapped_summary.audio_v3)

  try:
    yield
  finally:
    # Revert monkey-patches.
    try:
      setattr(tf_v1.contrib.summary, "audio", old_summary_v2_audio)
      setattr(tf_v1.contrib.summary, "histogram", old_summary_v2_histogram)
      setattr(tf_v1.contrib.summary, "image", old_summary_v2_image)
      setattr(tf_v1.contrib.summary, "scalar", old_summary_v2_scalar)
    except (AttributeError, ImportError):
      # TF 2.0 eliminates tf.contrib.
      pass
    setattr(summary_v2_lib, "audio", old_summary_v2_audio)
    setattr(summary_v2_lib, "histogram", old_summary_v2_histogram)
    setattr(summary_v2_lib, "image", old_summary_v2_image)
    setattr(summary_v2_lib, "scalar", old_summary_v2_scalar)
    setattr(tf.summary, "audio", old_summary_compat_v2_audio)
    setattr(tf.summary, "histogram", old_summary_compat_v2_histogram)
    setattr(tf.summary, "image", old_summary_compat_v2_image)
    setattr(tf.summary, "scalar", old_summary_compat_v2_scalar)
    setattr(tf_compat.v2.summary, "audio", old_summary_compat_v2_audio)
    setattr(tf_compat.v2.summary, "histogram", old_summary_compat_v2_histogram)
    setattr(tf_compat.v2.summary, "image", old_summary_compat_v2_image)
    setattr(tf_compat.v2.summary, "scalar", old_summary_compat_v2_scalar)
    setattr(summary_lib, "audio", old_summary_audio)
    setattr(summary_lib, "histogram", old_summary_histogram)
    setattr(summary_lib, "image", old_summary_image)
    setattr(summary_lib, "scalar", old_summary_scalar)
    setattr(tf_compat.v1.summary, "audio", old_summary_audio)
    setattr(tf_compat.v1.summary, "histogram", old_summary_histogram)
    setattr(tf_compat.v1.summary, "image", old_summary_image)
    setattr(tf_compat.v1.summary, "scalar", old_summary_scalar)
    setattr(tf_v1.summary, "audio", old_summary_audio)
    setattr(tf_v1.summary, "histogram", old_summary_histogram)
    setattr(tf_v1.summary, "image", old_summary_image)
    setattr(tf_v1.summary, "scalar", old_summary_scalar)