code: def simulate_phenotypes(
      mt,
      genotype,
      h2,
      pi=None,
      rg=None,
      annot=None,
      popstrat=None,
      popstrat_var=None,
      exact_h2=False,
  ):
      r"""Simulate phenotypes for testing LD score regression.
  
      Simulates betas (SNP effects) under the infinitesimal, spike & slab, or
      annotation-informed models, depending on parameters passed. Optionally adds
      population stratification.
  
      Parameters
      ----------
      mt : :class:`.MatrixTable`
          :class:`.MatrixTable` containing genotypes to be used. Also should contain
          variant annotations as row fields if running the annotation-informed
          model or covariates as column fields if adding population stratification.
      genotype : :class:`.Expression` or :class:`.CallExpression`
          Entry field containing genotypes of individuals to be used for the
          simulation.
      h2 : :obj:`float` or :obj:`int` or :obj:`list` or :class:`numpy.ndarray`
          SNP-based heritability of simulated trait.
      pi : :obj:`float` or :obj:`int` or :obj:`list` or :class:`numpy.ndarray`, optional
          Probability of SNP being causal when simulating under the spike & slab
          model.
      rg : :obj:`float` or :obj:`int` or :obj:`list` or :class:`numpy.ndarray`, optional
          Genetic correlation between traits.
      annot : :class:`.Expression`, optional
          Row field to use as our aggregated annotations.
      popstrat: :class:`.Expression`, optional
          Column field to use as our aggregated covariates for adding population
          stratification.
      exact_h2: :obj:`bool`, optional
          Whether to exactly simulate ratio of variance of genetic component of
          phenotype to variance of phenotype to be h2. If `False`, ratio will be
          h2 in expectation. Observed h2 in the simulation will be close to
          expected h2 for large-scale simulations.
  
      Returns
      -------
      :class:`.MatrixTable`
          :class:`.MatrixTable` with simulated betas and phenotypes, simulated according
          to specified model.
      """
      h2 = (
          h2.tolist()
          if isinstance(h2, np.ndarray)
          else ([h2] if not isinstance(h2, list) else h2)
      )
      pi = pi.tolist() if isinstance(pi, np.ndarray) else pi
      uid = Env.get_uid(base=100)
      mt = annotate_all(
          mt=mt,
          row_exprs={} if annot is None else {"annot_" + uid: annot},
          col_exprs={}
          if popstrat is None
          else {"popstrat_" + uid: popstrat},
          entry_exprs={
              "gt_" + uid: genotype.n_alt_alleles()
              if genotype.dtype is hl.dtype("call")
              else genotype
          },
      )
      mt, pi, rg = make_betas(
          mt=mt,
          h2=h2,
          pi=pi,
          annot=None if annot is None else mt["annot_" + uid],
          rg=rg,
      )
      mt = calculate_phenotypes(
          mt=mt,
          genotype=mt["gt_" + uid],
          beta=mt["beta"],
          h2=h2,
          popstrat=None if popstrat is None else mt["popstrat_" + uid],
          popstrat_var=popstrat_var,
          exact_h2=exact_h2,
      )
      mt = annotate_all(
          mt=mt,
          global_exprs={
              "ldscsim": hl.struct(
                  **{
                      "h2": h2[0] if len(h2) == 1 else h2,
                      **({} if pi == [None] else {"pi": pi}),
                      **(
                          {}
                          if rg == [None]
                          else {"rg": rg[0] if len(rg) == 1 else rg}
                      ),
                      **(
                          {}
                          if annot is None
                          else {"is_annot_inf": True}
                      ),
                      **(
                          {}
                          if popstrat is None
                          else {"is_popstrat_inf": True}
                      ),
                      **(
                          {}
                          if popstrat_var is None
                          else {"popstrat_var": popstrat_var}
                      ),
                      "exact_h2": exact_h2,
                  }
              )
          },
      )
      mt = _clean_fields(mt, uid)
      return mt
  
imports: import hail
  from hail.typecheck import typecheck, oneof, nullable
  from hail.expr.expressions import expr_float64, expr_int32, expr_array, expr_call
  from hail.matrixtable import MatrixTable
  from hail.table import Table
  from hail.utils.java import Env
  import numpy
  import pandas
  import scipy.stats
label_desc: Use image node_id 1 for calling a global function with example usage: simulate_phenotypes(mt, genotype, h2, pi, rg, annot, popstrat, popstrat_var, exact_h2) and returns: mt