code: def _cf_preprocess(
      extracts: types.Extracts,
      config: CounterfactualConfig,
  ) -> types.Extracts:
      """Preprocesses extracts for counterfactual prediction.
  
      This method is to be called on each Extracts object prior to applying the
      wrapped prediction PTransform.
  
      Args:
        extracts: An Extracts instance which is suitable for feeding to a non-CF
          prediction extractor.
        config: The counterfactual config which determines how the inputs should be
          counterfactually modified.
  
      Returns:
        An Extracts instance which, when fed to a predictions PTransform, will
        produce counterfactual predictions.
      """
      result = extracts.copy()
      result[_TEMP_ORIG_INPUT_KEY] = result[constants.INPUT_KEY]
      cf_inputs = []
      for serialized_input in result[constants.INPUT_KEY]:
          cf_example = tf.train.Example.FromString(serialized_input)
          for dst_key, src_key in config.items():
              cf_example.features.feature[dst_key].CopyFrom(
                  cf_example.features.feature[src_key]
              )
          cf_inputs.append(cf_example.SerializeToString())
      cf_inputs = np.array(cf_inputs, dtype=object)
      result[constants.INPUT_KEY] = cf_inputs
      return result
  
file_path: model-analysis/tensorflow_model_analysis/extractors/counterfactual_predictions_extractor.py
imports: from typing import Dict, Iterable, Mapping, Optional
  import apache_beam
  import numpy
  import tensorflow
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.api import types
  from tensorflow_model_analysis.extractors import extractor
  from tensorflow_model_analysis.extractors import predictions_extractor
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.utils import model_util
label_desc: Use image node_id 4 for calling a global function with example usage: _cf_preprocess(extracts, config) and returns: result