code: def generate_equi_width_histogram(
      quantiles: np.ndarray,
      cumulative_counts: np.ndarray,
      finite_min: float,
      finite_max: float,
      num_buckets: int,
      num_pos_inf: float,
  ) -> statistics_pb2.Histogram:
      """Generates an equal bucket width hist by combining a quantiles histogram.
  
      Args:
        quantiles: A numpy array containing the quantile boundaries.
        cumulative_counts: A numpy array of the same length as quantiles containing
          the cumulative quantile counts (sum of weights).
        finite_min: The mimimum finite value.
        finite_max: The maximum finite value.
        num_buckets: The required number of buckets in the equi-width histogram.
        num_pos_inf: The number of positive infinite values. May be non- integral if
          weighted.
  
      Returns:
        A standard histogram. Bucket counts are determined via linear interpolation.
      """
      result = statistics_pb2.Histogram()
      result.type = statistics_pb2.Histogram.STANDARD
      # If there were no finite values at all, return a single bucket.
      if not np.isfinite(finite_min) and not np.isfinite(finite_max):
          result.buckets.add(
              low_value=finite_min,
              high_value=finite_max,
              sample_count=cumulative_counts[-1],
          )
          return result
  
      assert np.isfinite(finite_min)
      assert np.isfinite(finite_max)
      # Verify that quantiles are sorted.
      assert np.all(quantiles[:-1] <= quantiles[1:])
      # First, strip off positive and negative infinities.
      quantiles, cumulative_counts, num_neg_inf = _strip_infinities(
          quantiles, cumulative_counts, finite_max, num_pos_inf
      )
  
      # TODO(zwestrick): Skip this and operate directly on the arrays?
      quantiles_hist = generate_quantiles_histogram(
          quantiles, cumulative_counts
      )
      if finite_min == finite_max:
          new_boundaries = np.array([finite_min, finite_max])
      else:
          new_boundaries = np.linspace(
              finite_min, finite_max, num_buckets + 1
          )
          if not np.isfinite(new_boundaries).all():
              # Something has gone wrong, probably overflow. Bail out and return an
              # empty histogram. We can't meaningfully proceed, but this may not be an
              # error.
              return result
      start_index = 0
      # If we stripped off negative infinities, add them back as a single bucket.
      if num_neg_inf:
          result.buckets.add(
              low_value=float("-inf"),
              high_value=float("-inf"),
              sample_count=num_neg_inf,
          )
      # Now build the standard histogram by merging quantiles histogram buckets.
      for i in range(new_boundaries.size - 1):
          low_bound = new_boundaries[i]
          high_bound = new_boundaries[i + 1]
          sample_count = 0
          # Find the first bucket with nonzero overlap with the first hist.
          for current_index in range(
              start_index, len(quantiles_hist.buckets)
          ):
              overlap, bucket_precedes, bucket_follows = _overlap(
                  quantiles_hist.buckets[current_index],
                  low_bound=low_bound,
                  high_bound=high_bound,
                  first_bucket=i == 0,
              )
              if bucket_follows:
                  # We're entirely after the current interval.
                  # Time to bail.
                  break
              if bucket_precedes:
                  # The bucket we considered is totally before the current interval, so
                  # we can start subsequent searches from here.
                  start_index = current_index
              sample_count += (
                  overlap
                  * quantiles_hist.buckets[current_index].sample_count
              )
              current_index += 1
          result.buckets.add(
              low_value=low_bound,
              high_value=high_bound,
              sample_count=sample_count,
          )
      # If we stripped off positive infinites, add them back as a single bucket.
      if num_pos_inf:
          result.buckets.add(
              low_value=float("inf"),
              high_value=float("inf"),
              sample_count=num_pos_inf,
          )
      return result
  
file_path: data-validation/tensorflow_data_validation/utils/quantiles_util.py
imports: from typing import Tuple
  import numpy
  from tensorflow_metadata.proto.v0 import statistics_pb2
label_desc: Use image node_id 7 for calling a global function with example usage: generate_equi_width_histogram(quantiles, cumulative_counts, finite_min, finite_max, num_buckets, num_pos_inf) and returns: result, result, result