code: def flatten_prediction_df(
      blockdf: DataFrame,
      sample_blocks: Dict[str, Iterable[str]],
      labeldf: pd.DataFrame,
  ) -> pd.DataFrame:
      """
      Transforms a Spark DataFrame containing a block matrix of model predictions to a flat Pandas DataFrame with the same
      shape as labeldf.
  
      Args:
          blockdf : Spark DataFrame containing a block matrix of predictions for the labels in labeldf
          sample_blocks : Dict containing a mapping of sample_block ID to a list of corresponding sample IDs
          labeldf : Pandas DataFrame containing target labels
  
      Returns:
          Pandas DataFrame containing prediction values. The shape and order match labeldf such that the
              rows are indexed by sample ID and the columns by label
      """
      sample_block_df = blockdf.sql_ctx.createDataFrame(
          sample_blocks.items(), ["sample_block", "sample_ids"]
      ).selectExpr(
          "sample_block", "posexplode(sample_ids) as (idx, sample_id)"
      )
  
      flattened_prediction_df = (
          blockdf.selectExpr(
              "sample_block",
              "label",
              "posexplode(values) as (idx, value)",
          )
          .join(sample_block_df, ["sample_block", "idx"], "inner")
          .select("sample_id", "label", "value")
      )
  
      pivoted_df = (
          flattened_prediction_df.toPandas()
          .pivot(index="sample_id", columns="label", values="value")
          .reindex(index=labeldf.index, columns=labeldf.columns)
      )
  
      return pivoted_df
  
imports: import itertools
  import math
  import re
  import warnings
  from typing import Any, Dict, Iterable, List, Tuple
  import numpy
  import pandas
  import pyspark.sql.functions
  import scipy.optimize
  from nptyping import Float, Int, NDArray
  from pyspark.sql import DataFrame, Row
  from pyspark.sql.window import Window
  from typeguard import typechecked
label_desc: Use image node_id 28 for calling a global function with example usage: flatten_prediction_df(blockdf, sample_blocks, labeldf) and returns: pivoted_df