code: def _make_feature_stats_proto_topk(
      feature_path: types.FeaturePath,
      top_k_values_pairs: List[FeatureValueCount],
      is_weighted_stats: bool,
      num_top_values: int,
      frequency_threshold: Union[float, int],
      num_rank_histogram_buckets: int,
  ) -> statistics_pb2.FeatureNameStatistics:
      """Makes a FeatureNameStatistics proto containing the top-k stats."""
      # Sort (a copy of) the top_k_value_pairs in descending order by count.
      # Where multiple feature values have the same count, consider the feature with
      # the 'larger' feature value to be larger for purposes of breaking the tie.
  
      top_k_values_pairs = sorted(
          top_k_values_pairs,
          key=lambda pair: (pair.count, pair.feature_value),
          reverse=True,
      )
  
      result = statistics_pb2.FeatureNameStatistics()
      result.path.CopyFrom(feature_path.to_proto())
  
      if is_weighted_stats:
          string_stats = result.string_stats.weighted_string_stats
      else:
          string_stats = result.string_stats
  
      for i in range(len(top_k_values_pairs)):
          value, count = top_k_values_pairs[i]
          if count < frequency_threshold:
              break
          # Check if we have a valid utf-8 string. If not, assign a default invalid
          # string value.
          if isinstance(value, six.binary_type):
              decoded_value = stats_util.maybe_get_utf8(value)
              if decoded_value is None:
                  _NON_UTF8_VALUES_COUNTER.inc()
                  value = constants.NON_UTF8_PLACEHOLDER
              else:
                  value = decoded_value
          elif not isinstance(value, six.text_type):
              value = str(value)
  
          if i < num_top_values:
              freq_and_value = string_stats.top_values.add()
              freq_and_value.value = value
              freq_and_value.frequency = count
          if i < num_rank_histogram_buckets:
              bucket = string_stats.rank_histogram.buckets.add()
              bucket.low_rank = i
              bucket.high_rank = i
              bucket.sample_count = count
              bucket.label = value
      return result
  
file_path: data-validation/tensorflow_data_validation/utils/top_k_uniques_stats_util.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from typing import List, Mapping, Optional, Union
  import apache_beam
  import six
  from tensorflow_data_validation import constants
  from tensorflow_data_validation import types
  from tensorflow_data_validation.utils import stats_util
  from tensorflow_metadata.proto.v0 import schema_pb2
  from tensorflow_metadata.proto.v0 import statistics_pb2
  from tfx_bsl.types import tfx_namedtuple
label_desc: Use image node_id 6 for calling a global function with example usage: _make_feature_stats_proto_topk(feature_path, top_k_values_pairs, is_weighted_stats, num_top_values, frequency_threshold, num_rank_histogram_buckets) and returns: result