code: def test_detect_nan():
      # Test the code snippet example that detects NaN values.
  
      nan_detected = [False]
  
      def detect_nan(fgraph, i, node, fn):
          for output in fn.outputs:
              if np.isnan(output[0]).any():
                  print("*** NaN detected ***")
                  debugprint(node)
                  print(
                      "Inputs : %s" % [input[0] for input in fn.inputs]
                  )
                  print(
                      "Outputs: %s"
                      % [output[0] for output in fn.outputs]
                  )
                  nan_detected[0] = True
                  break
  
      x = dscalar("x")
      f = function(
          [x],
          [log(x) * x],
          mode=MonitorMode(post_func=detect_nan),
      )
      try:
          old_stdout = sys.stdout
          sys.stdout = StringIO()
          f(0)  # log(0) * 0 = -inf * 0 = NaN
      finally:
          sys.stdout = old_stdout
      assert nan_detected[0]
  
file_path: pytensor/tests/compile/test_monitormode.py
imports: import sys
  from io import StringIO
  import numpy
  from pytensor.compile import MonitorMode
  from pytensor.compile.function import function
  from pytensor.printing import debugprint
  from pytensor.tensor import log, outer
  from pytensor.tensor.type import dscalar, vector
label_desc: Use image node_id 1 for calling a global function with example usage: test_detect_nan() without return types