code: def write_granular_summary_tex(
      dataset_name: str,
      granular_sections: Dict[
          str, Union[pandas.DataFrame, Dict[str, pandas.DataFrame]]
      ],
      pvals_by_section: Dict[
          str, Union[pandas.DataFrame, Dict[str, pandas.DataFrame]]
      ],
      out_dir: str,
  ):
      granular_sections = copy.deepcopy(granular_sections)
      pvals_by_section = copy.deepcopy(pvals_by_section)
  
      def _format_section(df):
          columns = ANNOTATION_COLUMN_ORDER.copy()
          if not dataset_contains_math(dataset_name):
              columns.remove("MATH")
          return df.rename(
              index=LATEX_ROW_MAPPING, columns=COLUMN_RENAMES
          )[columns]
  
      # First, concat all data into one giant table to find the top 2 values in each column to bold/underline
      all_data = []
      for section in (
          "baselines",
          "ROSCOE-SA",
          "ROSCOE-SS",
          "ROSCOE-LI",
          "ROSCOE-LC",
      ):
          if section == "baselines":
              # Take care of formatting first, so column names will be the same when we look for these top values later
              cur_section_df = _format_section(
                  granular_sections[section]
              )
              granular_sections[section] = cur_section_df
              pvals_by_section[section] = _format_section(
                  pvals_by_section[section]
              )
              all_data.append(cur_section_df)
          elif section in {"ROSCOE-SA", "ROSCOE-SS"}:
              for model in MODELS:
                  cur_section_df = _format_section(
                      granular_sections[section][model].loc[
                          ROSCOE_SECTIONS[section]
                      ]
                  )
                  granular_sections[section][model] = cur_section_df
                  pvals_by_section[section][model] = _format_section(
                      pvals_by_section[section][model]
                  )
                  all_data.append(cur_section_df)
          else:
              cur_section_df = _format_section(
                  granular_sections[section].loc[
                      ROSCOE_SECTIONS[section]
                  ]
              )
              granular_sections[section] = cur_section_df
              pvals_by_section[section] = _format_section(
                  pvals_by_section[section]
              )
              all_data.append(cur_section_df)
      format_top_2 = top_2_format_replacements(pandas.concat(all_data))
  
      def _replace_top_2(df):
          df = df.replace(format_top_2)
          df = df.replace(math.nan, "-")
          df = df.applymap(enforce_decimal_places)
          return df
  
      out_path = os.path.join(
          out_dir, f"{dataset_name}_summary_granular.tex"
      )
      with open(out_path, "w") as f:
          # baselines
          formatted_section = _replace_top_2(
              granular_sections["baselines"]
          )
          sig_mask = pvals_by_section["baselines"] < 0.05
          formatted_section[sig_mask] = formatted_section[
              sig_mask
          ].applymap(lambda x: f"{x}$\\dagger$")
          f.write(formatted_section.to_csv(**TO_CSV_TEX_KARGS))
          # separator between baselines and ROSCOE scores
          f.writelines(
              [
                  "\t\\midrule\n",
                  "\\multicolumn{10}{l}{\\textbf{\\ourmodel~Metrics (\\textbf{reference-free} on $(\\bm{s},\\bm{h})$)}} \\\\\n"
                  "\t\\hline\n",
              ]
          )
          # ROSCOE scores, by section
          for section in (
              "ROSCOE-SA",
              "ROSCOE-SS",
              "ROSCOE-LI",
              "ROSCOE-LC",
          ):
              if section in {"ROSCOE-SA", "ROSCOE-SS"}:
                  for model in MODELS:
                      f.write(
                          "\\multicolumn{10}{l}{"
                          + model_section_title_tex(section, model)
                          + "} \\\\\n"
                      )
                      formatted_section = _replace_top_2(
                          granular_sections[section][model]
                      )
                      sig_mask = pvals_by_section[section][model] < 0.05
                      formatted_section[sig_mask] = formatted_section[
                          sig_mask
                      ].applymap(lambda x: f"{x}$\\dagger$")
                      f.write(
                          formatted_section.to_csv(
                              header=False, **TO_CSV_TEX_KARGS
                          )
                      )
              else:
                  f.write(
                      "\\multicolumn{10}{l}{"
                      + LATEX_ROW_MAPPING[section]
                      + "} \\\\\n"
                  )
                  formatted_section = _replace_top_2(
                      granular_sections[section]
                  )
                  sig_mask = pvals_by_section[section] < 0.05
                  formatted_section[sig_mask] = formatted_section[
                      sig_mask
                  ].applymap(lambda x: f"{x}$\\dagger$")
                  f.write(
                      formatted_section.to_csv(
                          header=False, **TO_CSV_TEX_KARGS
                      )
                  )
              f.write("\t\\midrule\n")
  
      print(
          f"Granular summary of {dataset_name} written to: {out_path}"
      )
  
file_path: ParlAI/projects/roscoe/meta_evaluation/table_file_writing.py
imports: import copy
  import csv
  import math
  import os
  import pandas
  from typing import Dict, List, Union
  from projects.roscoe.score import FAITHFUL_SENT, FAITHFUL_WORD, INFORM_STEP, REPETITION_WORD, INFORM_CHAIN, REPETITION_SENT, COHERENCE_STEP_VS_STEP, DISCOURSE_REPRESENTATION, GRAMMAR_STEP, PPL_STEP, PPL_CHAIN
label_desc: Use image node_id 6 for calling a global function with example usage: write_granular_summary_tex(dataset_name, granular_sections, pvals_by_section, out_dir) and returns: df, df