code: def reshape_for_gwas(
      spark: SparkSession, label_df: pd.DataFrame
  ) -> DataFrame:
      """
      Reshapes a Pandas DataFrame into a Spark DataFrame with a convenient format for Glow's GWAS
      functions. This function can handle labels that are either per-sample or per-sample and
      per-contig, like those generated by GloWGR's transform_loco function.
  
      Examples:
          .. invisible-code-block:
              import pandas as pd
  
          >>> label_df = pd.DataFrame({'label1': [1, 2], 'label2': [3, 4]}, index=['sample1', 'sample2'])
          >>> reshaped = reshape_for_gwas(spark, label_df)
          >>> reshaped.head()
          Row(label='label1', values=[1, 2])
  
          >>> loco_label_df = pd.DataFrame({'label1': [1, 2], 'label2': [3, 4]},
          ...     index=pd.MultiIndex.from_tuples([('sample1', 'chr1'), ('sample1', 'chr2')]))
          >>> reshaped = reshape_for_gwas(spark, loco_label_df)
          >>> reshaped.head()
          Row(contigName='chr1', label='label1', values=[1])
  
      Requires that:
  
          - The input label DataFrame is indexed by sample id or by (sample id, contig name)
  
      Args:
          spark : A Spark session
          label_df : A pandas DataFrame containing labels. The Data Frame should either be indexed by
              sample id or multi indexed by (sample id, contig name). Each column is interpreted as a
              label.
  
      Returns:
          A Spark DataFrame with a convenient format for Glow regression functions. Each row contains
          the label name, the contig name if provided in the input DataFrame, and an array containing
          the label value for each sample.
      """
  
      assert check_argument_types()
  
      if label_df.index.nlevels == 1:  # Indexed by sample id
          transposed_df = label_df.T
          column_names = ["label", "values"]
      elif (
          label_df.index.nlevels == 2
      ):  # Indexed by sample id and contig name
          # stacking sorts the new column index, so we remember the original sample
          # ordering in case it's not sorted
          def transpose_one(contig):
              transposed = label_df.xs(contig, level=1).T
              return transposed
  
          contigs = _get_contigs_from_loco_df(label_df)
          transposed_df = pd.concat(
              [transpose_one(contig) for contig in contigs],
              keys=contigs,
              names=["contig", "label"],
          )
          column_names = ["contigName", "label", "values"]
      else:
          raise ValueError(
              "label_df must be indexed by sample id or by (sample id, contig name)"
          )
  
      # Can only create a Spark DataFrame from pandas with ndarray columns in Spark 3+
      if int(spark.version.split(".")[0]) < 3:
          values = transposed_df.to_numpy().tolist()
      else:
          values = list(transposed_df.to_numpy())
      transposed_df["values_array"] = values
      return spark.createDataFrame(
          transposed_df[["values_array"]].reset_index(), column_names
      )
  
file_path: glow/python/glow/wgr/wgr_functions.py
imports: from glow import glow
  import pandas
  import numpy
  from pyspark import SparkContext
  from pyspark.sql import DataFrame, Row, SparkSession, SQLContext
  from typeguard import check_argument_types, check_return_type
  from typing import Any, Dict, List
  from nptyping import Float, NDArray
  from .ridge_reduction import RidgeReduction
  from .ridge_regression import RidgeRegression
  from .logistic_ridge_regression import LogisticRidgeRegression
label_desc: Use image node_id 6 for calling a global function with example usage: reshape_for_gwas(spark, label_df) and returns: spark, transposed