code: def impute_sex_chromosome_ploidy(
      vds: VariantDataset,
      calling_intervals,
      normalization_contig: str,
      use_variant_dataset: bool = False,
  ) -> hl.Table:
      """Impute sex chromosome ploidy from depth of reference or variant data within calling intervals.
  
      Returns a :class:`.Table` with sample ID keys, with the following fields:
  
       -  ``autosomal_mean_dp`` (*float64*): Mean depth on calling intervals on normalization contig.
       -  ``x_mean_dp`` (*float64*): Mean depth on calling intervals on X chromosome.
       -  ``x_ploidy`` (*float64*): Estimated ploidy on X chromosome. Equal to ``2 * x_mean_dp / autosomal_mean_dp``.
       -  ``y_mean_dp`` (*float64*): Mean depth on calling intervals on  chromosome.
       -  ``y_ploidy`` (*float64*): Estimated ploidy on Y chromosome. Equal to ``2 * y_mean_db / autosomal_mean_dp``.
  
      Parameters
      ----------
      vds : vds: :class:`.VariantDataset`
          Dataset.
      calling_intervals : :class:`.Table` or :class:`.ArrayExpression`
          Calling intervals with consistent read coverage (for exomes, trim the capture intervals).
      normalization_contig : str
          Autosomal contig for depth comparison.
      use_variant_dataset : bool
          Whether to use depth of variant data within calling intervals instead of reference data. Default will use reference data.
  
      Returns
      -------
      :class:`.Table`
      """
  
      if not isinstance(calling_intervals, Table):
          calling_intervals = hl.Table.parallelize(
              hl.map(
                  lambda i: hl.struct(interval=i), calling_intervals
              ),
              schema=hl.tstruct(
                  interval=calling_intervals.dtype.element_type
              ),
              key="interval",
          )
      else:
          key_dtype = calling_intervals.key.dtype
          if (
              len(key_dtype) != 1
              or not isinstance(
                  calling_intervals.key[0].dtype, hl.tinterval
              )
              or calling_intervals.key[0].dtype.point_type
              != vds.reference_data.locus.dtype
          ):
              raise ValueError(
                  f"'impute_sex_chromosome_ploidy': expect calling_intervals to be list of intervals or"
                  f" table with single key of type interval<locus>, found table with key: {key_dtype}"
              )
  
      rg = vds.reference_data.locus.dtype.reference_genome
  
      par_boundaries = []
      for par_interval in rg.par:
          par_boundaries.append(par_interval.start)
          par_boundaries.append(par_interval.end)
  
      # segment on PAR interval boundaries
      calling_intervals = hl.segment_intervals(
          calling_intervals, par_boundaries
      )
  
      # remove intervals overlapping PAR
      calling_intervals = calling_intervals.filter(
          hl.all(
              lambda x: ~x.overlaps(calling_intervals.interval),
              hl.literal(rg.par),
          )
      )
  
      # checkpoint for efficient multiple downstream usages
      info(
          "'impute_sex_chromosome_ploidy': checkpointing calling intervals"
      )
      calling_intervals = calling_intervals.checkpoint(
          new_temp_file(extension="ht")
      )
  
      interval = calling_intervals.key[0]
      (
          any_bad_intervals,
          chrs_represented,
      ) = calling_intervals.aggregate(
          (
              hl.agg.any(interval.start.contig != interval.end.contig),
              hl.agg.collect_as_set(interval.start.contig),
          )
      )
      if any_bad_intervals:
          raise ValueError(
              "'impute_sex_chromosome_ploidy' does not support calling intervals that span chromosome boundaries"
          )
  
      if len(rg.x_contigs) != 1:
          raise NotImplementedError(
              f"reference genome {rg.name!r} has multiple X contigs, this is not supported in 'impute_sex_chromosome_ploidy'"
          )
      if len(rg.y_contigs) != 1:
          raise NotImplementedError(
              f"reference genome {rg.name!r} has multiple Y contigs, this is not supported in 'impute_sex_chromosome_ploidy'"
          )
  
      kept_contig_filter = hl.array(chrs_represented).map(
          lambda x: hl.parse_locus_interval(x, reference_genome=rg)
      )
      vds = VariantDataset(
          hl.filter_intervals(vds.reference_data, kept_contig_filter),
          hl.filter_intervals(vds.variant_data, kept_contig_filter),
      )
  
      if use_variant_dataset:
          mt = vds.variant_data
          calling_intervals = calling_intervals.annotate(
              interval_dup=interval
          )
          mt = mt.annotate_rows(
              interval=calling_intervals[mt.locus].interval_dup
          )
          mt = mt.filter_rows(hl.is_defined(mt.interval))
          coverage = mt.select_entries(
              sum_dp=mt.DP, interval_size=hl.is_defined(mt.DP)
          )
      else:
          coverage = interval_coverage(
              vds, calling_intervals, gq_thresholds=()
          ).drop("gq_thresholds")
  
      return impute_sex_chr_ploidy_from_interval_coverage(
          coverage, normalization_contig
      )
  
imports: from typing import Sequence
  import hail
  from hail import ir
  from hail.expr import expr_any, expr_array, expr_interval, expr_locus, expr_str, expr_bool
  from hail.matrixtable import MatrixTable
  from hail.methods.misc import require_first_key_field_locus
  from hail.table import Table
  from hail.typecheck import sequenceof, typecheck, nullable, oneof, enumeration, func_spec, dictof
  from hail.utils.java import Env, info, warning
  from hail.utils.misc import divide_null, new_temp_file, wrap_to_list
  from hail.vds.variant_dataset import VariantDataset
label_desc: Use image node_id 7 for calling a global function with example usage: impute_sex_chromosome_ploidy(vds, calling_intervals, normalization_contig, use_variant_dataset) and returns: impute_sex_chr_ploidy_from_interval_coverage