code: def calculate_new_intervals(
      mt, desired_average_partition_size: int, tmp_path: str
  ):
      """takes a table, keyed by ['locus', ...] and produces a list of intervals suitable
      for repartitioning a combiner matrix table.
  
      Parameters
      ----------
      mt : :class:`.MatrixTable`
          Sparse MT intermediate.
      desired_average_partition_size : :obj:`int`
          Average target number of rows for each partition.
      tmp_path : :obj:`str`
          Temporary path for scan checkpointing.
  
      Returns
      -------
      (:obj:`List[Interval]`, :obj:`.Type`)
      """
      assert list(mt.row_key) == ["locus"]
      assert isinstance(mt.locus.dtype, hl.tlocus)
      reference_genome = mt.locus.dtype.reference_genome
      end = hl.Locus(
          reference_genome.contigs[-1],
          reference_genome.lengths[reference_genome.contigs[-1]],
          reference_genome=reference_genome,
      )
  
      (n_rows, n_cols) = mt.count()
  
      if n_rows == 0:
          raise ValueError("empty table!")
  
      # split by a weight function that takes into account the number of
      # dense entries per row. However, give each row some base weight
      # to prevent densify computations from becoming unbalanced (these
      # scale roughly linearly with N_ROW * N_COL)
      ht = (
          mt.select_rows(weight=hl.agg.count() + (n_cols // 25) + 1)
          .rows()
          .checkpoint(tmp_path)
      )
  
      total_weight = ht.aggregate(hl.agg.sum(ht.weight))
      partition_weight = int(
          total_weight / (n_rows / desired_average_partition_size)
      )
  
      ht = ht.annotate(
          cumulative_weight=hl.scan.sum(ht.weight),
          last_weight=hl.scan._prev_nonnull(ht.weight),
          row_idx=hl.scan.count(),
      )
  
      def partition_bound(x):
          return x - (x % hl.int64(partition_weight))
  
      at_partition_bound = partition_bound(
          ht.cumulative_weight
      ) != partition_bound(ht.cumulative_weight - ht.last_weight)
  
      ht = ht.filter(at_partition_bound | (ht.row_idx == n_rows - 1))
      ht = ht.annotate(
          start=hl.or_else(
              hl.scan._prev_nonnull(
                  hl.locus_from_global_position(
                      ht.locus.global_position() + 1,
                      reference_genome=reference_genome,
                  )
              ),
              hl.locus_from_global_position(
                  0, reference_genome=reference_genome
              ),
          )
      )
      ht = ht.select(
          interval=hl.interval(
              start=hl.struct(locus=ht.start),
              end=hl.struct(locus=ht.locus),
              includes_end=True,
          )
      )
  
      intervals_dtype = hl.tarray(ht.interval.dtype)
      intervals = ht.aggregate(hl.agg.collect(ht.interval))
      last_st = hl.eval(
          hl.locus_from_global_position(
              hl.literal(intervals[-1].end.locus).global_position() + 1,
              reference_genome=reference_genome,
          )
      )
      interval = hl.Interval(
          start=hl.Struct(locus=last_st),
          end=hl.Struct(locus=end),
          includes_end=True,
      )
      intervals.append(interval)
      return intervals, intervals_dtype
  
imports: import math
  from typing import Collection, Optional, Set, Union, List, Tuple, Dict
  import hail
  from hail import MatrixTable, Table
  from hail.experimental.function import Function
  from hail.expr import StructExpression, unify_all, construct_expr
  from hail.expr.expressions import expr_bool, expr_str
  from hail.genetics.reference_genome import reference_genome_type
  from hail.ir import Apply, TableMapRows
  from hail.typecheck import oneof, sequenceof, typecheck
  from ..variant_dataset import VariantDataset
label_desc: Use image node_id 22 for calling a global function with example usage: calculate_new_intervals(mt, desired_average_partition_size, tmp_path) and returns: intervals,  intervals_dtype, unknown