code: def _replace_nan_with_none(
      plot_data: Union[Dict[str, Any], str],
      plot_keys: Dict[str, Dict[str, str]],
  ) -> Union[Dict[str, Any], str]:
      """Replaces all instances of nan with None in plot data.
  
      This is necessary for Colab integration where we serializes the data into json
      string as NaN is not supported by json standard. Turning nan into None will
      make the value null once parsed. The visualization already handles falsy
      values by setting them to zero.
  
      Args:
        plot_data: The original plot data
        plot_keys: A dictionary containing field names of plot data.
  
      Returns:
        Transformed plot data where all nan has been replaced with None.
      """
      output_metrics = {}
      for plot_type in plot_keys:
          metric_name = plot_keys[plot_type]["metricName"]
          if metric_name in plot_data:
              data_series_name = plot_keys[plot_type]["dataSeries"]
              if data_series_name in plot_data[metric_name]:
                  data_series = plot_data[metric_name][data_series_name]
                  outputs = []
                  for entry in data_series:
                      output = {}
                      for key in entry:
                          value = entry[key]
                          # When converting protocol buffer into dict, float value nan is
                          # automatically converted into the string 'NaN'.
                          output[key] = (
                              None if value == "NaN" else value
                          )
                      outputs.append(output)
                  output_metrics[metric_name] = {
                      data_series_name: outputs
                  }
  
      return output_metrics
  
imports: import json
  import os
  from typing import Any, Dict, List, Optional, Tuple, Union
  from absl import logging
  from tensorflow_model_analysis.metrics import example_count
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import weighted_example_count
  from tensorflow_model_analysis.post_export_metrics import metric_keys
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.proto import metrics_for_slice_pb2
  from tensorflow_model_analysis.slicer import slicer_lib
  from tensorflow_model_analysis.view import view_types
  from google.protobuf import json_format
label_desc: Use image node_id 5 for calling a global function with example usage: _replace_nan_with_none(plot_data, plot_keys) and returns: output_metrics