code: def _pdf_to_log_reg_state(
      pdf: pd.DataFrame, phenotypes: pd.Series, n_covar: int
  ) -> LogRegState:
      """
      Converts a Pandas DataFrame with the contents of a LogRegState object
      into a more convenient form.
      """
      # Ensure that the null fit state is sorted identically to the input phenotype_df
      sorted_pdf = pdf.set_index("label").reindex(
          phenotypes, axis="rows"
      )
      inv_CtGammaC = np.row_stack(
          sorted_pdf["inv_CtGammaC"].array
      ).reshape(phenotypes.size, n_covar, n_covar)
      gamma = np.column_stack(sorted_pdf["gamma"].array)
      Y_res = np.column_stack(sorted_pdf["y_res"].array)
      if "firth_offset" in sorted_pdf:
          firth_offset = np.column_stack(
              sorted_pdf["firth_offset"].array
          )
      else:
          firth_offset = None
      return LogRegState(inv_CtGammaC, gamma, Y_res, firth_offset)
  
imports: from pyspark.sql.types import ArrayType, BooleanType, StringType, StructField, DataType, StructType
  from typing import Any, List, Optional, Dict, Union
  import pandas
  import numpy
  from pyspark.sql import DataFrame, SparkSession
  import statsmodels.api
  from dataclasses import dataclass
  from typeguard import typechecked
  from nptyping import Float, NDArray, Int32
  from scipy import stats
  import opt_einsum
  from .None import functions
  from .None import approx_firth
  from .functions import _VALUES_COLUMN_NAME, _get_indices_to_drop
  from ..wgr.wgr_functions import reshape_for_gwas
label_desc: Use image node_id 4 for calling a global function with example usage: _pdf_to_log_reg_state(pdf, phenotypes, n_covar) and returns: LogRegState