class_docstr: Tests for TopkUniquesStatsGenerator.
class_docstr_tok: ["Tests","for","TopkUniquesStatsGenerator","."]
code: def test_topk_uniques_with_weights(self):
      # non-weighted ordering
      # fa: 3 'a', 2 'e', 2 'd', 2 'c', 1 'b'
      # fb: 1 'v', 1 'w', 1 'x', 1 'y', 1 'z'
      # weighted ordering
      # fa: 20 'e', 20 'd', 15 'a', 10 'c', 5 'b'
      # fb: 6 'z', 4 'x', 4 'y', 4 'w', 2 'v'
      examples = [
          pa.RecordBatch.from_arrays(
              [
                  pa.array(
                      [
                          ["a", "b", "c", "e"],
                          ["a", "c", "d", "a"],
                          ["d", "e"],
                      ]
                  ),
                  pa.array([[5.0], [5.0], [15.0]]),
                  pa.array([["v"], ["w", "x", "y"], ["z"]]),
                  pa.array([[2], [4], [6]]),
              ],
              ["fa", "w", "fb", "w_b"],
          )
      ]
  
      expected_result = [
          text_format.Parse(
              """
              features {
                path {
                  step: 'fa'
                }
                string_stats {
                  top_values {
                    value: 'a'
                    frequency: 3.0
                  }
                  top_values {
                    value: 'e'
                    frequency: 2.0
                  }
                  top_values {
                    value: 'd'
                    frequency: 2.0
                  }
                  top_values {
                    value: 'c'
                    frequency: 2.0
                  }
                  rank_histogram {
                    buckets {
                      low_rank: 0
                      high_rank: 0
                      label: "a"
                      sample_count: 3.0
                    }
                    buckets {
                      low_rank: 1
                      high_rank: 1
                      label: "e"
                      sample_count: 2.0
                    }
                    buckets {
                      low_rank: 2
                      high_rank: 2
                      label: "d"
                      sample_count: 2.0
                    }
                  }
                }
              }""",
              statistics_pb2.DatasetFeatureStatistics(),
          ),
          text_format.Parse(
              """
              features {
                string_stats {
                  top_values {
                    value: "z"
                    frequency: 1.0
                  }
                  top_values {
                    value: "y"
                    frequency: 1.0
                  }
                  top_values {
                    value: "x"
                    frequency: 1.0
                  }
                  top_values {
                    value: "w"
                    frequency: 1.0
                  }
                  rank_histogram {
                    buckets {
                      label: "z"
                      sample_count: 1.0
                    }
                    buckets {
                      low_rank: 1
                      high_rank: 1
                      label: "y"
                      sample_count: 1.0
                    }
                    buckets {
                      low_rank: 2
                      high_rank: 2
                      label: "x"
                      sample_count: 1.0
                    }
                  }
                }
                path {
                  step: "fb"
                }
              }""",
              statistics_pb2.DatasetFeatureStatistics(),
          ),
          text_format.Parse(
              """
              features {
                path {
                  step: 'fa'
                }
                string_stats {
                  weighted_string_stats {
                    top_values {
                      value: 'e'
                      frequency: 20.0
                    }
                    top_values {
                      value: 'd'
                      frequency: 20.0
                    }
                    top_values {
                      value: 'a'
                      frequency: 15.0
                    }
                    top_values {
                      value: 'c'
                      frequency: 10.0
                    }
                    rank_histogram {
                      buckets {
                        low_rank: 0
                        high_rank: 0
                        label: "e"
                        sample_count: 20.0
                      }
                      buckets {
                        low_rank: 1
                        high_rank: 1
                        label: "d"
                        sample_count: 20.0
                      }
                      buckets {
                        low_rank: 2
                        high_rank: 2
                        label: "a"
                        sample_count: 15.0
                      }
                    }
                  }
                }
              }""",
              statistics_pb2.DatasetFeatureStatistics(),
          ),
          text_format.Parse(
              """
              features {
                string_stats {
                  weighted_string_stats {
                    top_values {
                      value: "z"
                      frequency: 6.0
                    }
                    top_values {
                      value: "y"
                      frequency: 4.0
                    }
                    top_values {
                      value: "x"
                      frequency: 4.0
                    }
                    top_values {
                      value: "w"
                      frequency: 4.0
                    }
                    rank_histogram {
                      buckets {
                        label: "z"
                        sample_count: 6.0
                      }
                      buckets {
                        low_rank: 1
                        high_rank: 1
                        label: "y"
                        sample_count: 4.0
                      }
                      buckets {
                        low_rank: 2
                        high_rank: 2
                        label: "x"
                        sample_count: 4.0
                      }
                    }
                  }
                }
                path {
                  step: "fb"
                }
              }""",
              statistics_pb2.DatasetFeatureStatistics(),
          ),
          text_format.Parse(
              """
              features {
                path {
                  step: 'fa'
                }
                string_stats {
                  unique: 5
                }
              }""",
              statistics_pb2.DatasetFeatureStatistics(),
          ),
          text_format.Parse(
              """
              features {
                string_stats {
                  unique: 5
                }
                path {
                  step: "fb"
                }
              }""",
              statistics_pb2.DatasetFeatureStatistics(),
          ),
      ]
  
      generator = (
          top_k_uniques_stats_generator.TopKUniquesStatsGenerator(
              example_weight_map=ExampleWeightMap(
                  weight_feature="w",
                  per_feature_override={
                      types.FeaturePath(["fb"]): "w_b"
                  },
              ),
              num_top_values=4,
              num_rank_histogram_buckets=3,
          )
      )
      self.assertSlicingAwareTransformOutputEqual(
          examples,
          generator,
          expected_result,
          add_default_slice_key_to_input=True,
          add_default_slice_key_to_output=True,
      )
  
imports: from absl.testing import absltest
  import pyarrow
  from tensorflow_data_validation import types
  from tensorflow_data_validation.statistics.generators import top_k_uniques_stats_generator
  from tensorflow_data_validation.utils import test_util
  from tensorflow_data_validation.utils.example_weight_map import ExampleWeightMap
  from google.protobuf import text_format
  from tensorflow_metadata.proto.v0 import schema_pb2
  from tensorflow_metadata.proto.v0 import statistics_pb2
label_desc: Use image node_id 2 for calling the TopkUniquesStatsGeneratorTest obj's underlying member method code with example usage: obj.test_topk_uniques_with_weights() without return types