code: def _MaterializeFeatures(
      extracts: types.Extracts,
      additional_extracts: Optional[List[str]] = None,
      excludes: Optional[List[bytes]] = None,
      source: str = constants.FEATURES_PREDICTIONS_LABELS_KEY,
      dest: str = constants.MATERIALIZE_COLUMNS,
  ) -> types.Extracts:
      """Converts FeaturesPredictionsLabels into MaterializedColumn in the extract.
  
      It must be the case that the PredictExtractor was called before calling this
      function.
  
      Args:
        extracts: The Extracts to be augmented.
        additional_extracts: Optional list of additional extracts to include along
          with the features, predictions, and labels.
        excludes: Optional list of strings containing features, predictions, or
          labels to exclude from materialization.
        source: Source for extracting features. Currently it supports extracting
          features from FPLs and input tf.Example protos.
        dest: Destination for extracted features. Currently supported are adding
          materialized columns, or the features dict of the FPLs.
  
      Returns:
        Returns Extracts (which is a deep copy of the original Extracts, so the
          original isn't mutated) with features populated.
  
      Raises:
        RuntimeError: When tfma.FEATURES_PREDICTIONS_LABELS_KEY key is not populated
          by PredictExtractor for FPL source or incorrect extraction source given.
      """
      # Make a deep copy, so we don't mutate the original.
      result = copy.deepcopy(extracts)
  
      if additional_extracts:
          for key in additional_extracts:
              if key in result:
                  _AugmentExtracts(result[key], key, excludes, result)
  
      if source == constants.FEATURES_PREDICTIONS_LABELS_KEY:
          fpl = result.get(constants.FEATURES_PREDICTIONS_LABELS_KEY)
          if not fpl:
              raise RuntimeError(
                  "FPL missing. Ensure PredictExtractor was called."
              )
  
          if not isinstance(fpl, types.FeaturesPredictionsLabels):
              raise TypeError(
                  "Expected FPL to be instance of FeaturesPredictionsLabel. FPL was: %s"
                  "of type %s" % (str(fpl), type(fpl))
              )
  
          # We disable pytyping here because we know that 'fpl' key corresponds to a
          # non-materialized column.
          # pytype: disable=attribute-error
          _AugmentExtracts(
              fpl.features, constants.FEATURES_KEY, excludes, result
          )
          _AugmentExtracts(
              fpl.predictions,
              constants.PREDICTIONS_KEY,
              excludes,
              result,
          )
          _AugmentExtracts(
              fpl.labels, constants.LABELS_KEY, excludes, result
          )
          # pytype: enable=attribute-error
          return result
      elif source == constants.INPUT_KEY:
          serialized_example = result.get(constants.INPUT_KEY)
          if not serialized_example:
              raise RuntimeError(
                  "tf.Example missing. Ensure extracts contain "
                  "serialized tf.Example."
              )
          materialize_columns = dest == constants.MATERIALIZE_COLUMNS
          _ParseExample(result, materialize_columns)
          return result
      else:
          raise RuntimeError("Unsupported feature extraction source.")
  
file_path: model-analysis/tensorflow_model_analysis/extractors/legacy_feature_extractor.py
imports: import copy
  from typing import Any, Dict, List, Optional
  from absl import logging
  import apache_beam
  import numpy
  import tensorflow
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.api import types
  from tensorflow_model_analysis.eval_saved_model import encoding
  from tensorflow_model_analysis.extractors import extractor
  from tensorflow_model_analysis.utils import util
label_desc: Use image node_id 4 for calling a global function with example usage: _MaterializeFeatures(extracts, additional_extracts, excludes, source, dest) and returns: result, result