code: def write_every_dataset_summary_table_tex(
      summaries_by_dataset: Dict[
          str, Union[pandas.DataFrame, Dict[str, pandas.DataFrame]]
      ],
      out_dir: str,
  ):
      def _format_section(df):
          return df[DATASET_COLUMN_ORDER].rename(
              index=LATEX_ROW_MAPPING
          )
  
      table_sections = (
          "baselines",
          "ROSCOE-SA",
          "ROSCOE-SS",
          "ROSCOE-LI",
          "ROSCOE-LC",
      )
      summaries_by_section = {}
      # 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 table_sections:
          if section == "baselines":
              cur_section_df = pandas.concat(
                  [
                      sections[section].rename(columns={"MAX": name})
                      for name, sections in summaries_by_dataset.items()
                      if sections is not None
                  ],
                  axis=1,
              )
              # 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(cur_section_df)
              summaries_by_section[section] = cur_section_df
              all_data.append(cur_section_df)
          elif section in {"ROSCOE-SA", "ROSCOE-SS"}:
              summaries_by_section[section] = {}
              for model in MODELS:
                  cur_section_df = pandas.concat(
                      [
                          sections[section][model]
                          .loc[ROSCOE_SECTIONS[section]]
                          .rename(columns={"MAX": name})
                          for name, sections in summaries_by_dataset.items()
                          if sections is not None
                      ],
                      axis=1,
                  )
                  cur_section_df = _format_section(cur_section_df)
                  summaries_by_section[section][model] = cur_section_df
                  all_data.append(cur_section_df)
          else:
              cur_section_df = pandas.concat(
                  [
                      sections[section]
                      .loc[ROSCOE_SECTIONS[section]]
                      .rename(columns={"MAX": name})
                      for name, sections in summaries_by_dataset.items()
                      if sections is not None
                  ],
                  axis=1,
              )
              cur_section_df = _format_section(cur_section_df)
              summaries_by_section[section] = cur_section_df
              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, "summary_granular.tex")
      with open(out_path, "w") as f:
          # baselines section
          f.write(
              _replace_top_2(summaries_by_section["baselines"]).to_csv(
                  **TO_CSV_TEX_KARGS
              )
          )
  
          # separator between baselines and ROSCOE scores
          f.writelines(
              [
                  "\\midrule\n",
                  "\\multicolumn{6}{l}{\\textbf{\\ourmodel~Metrics (\\textbf{reference-free} on $(\\bm{s},\\bm{h})$)}} \\\\\n"
                  "\\hline\n",
              ]
          )
  
          for section in table_sections[1:]:
              if section in {"ROSCOE-SA", "ROSCOE-SS"}:
                  for model in MODELS:
                      f.write(
                          "\\multicolumn{6}{l}{"
                          + model_section_title_tex(section, model)
                          + "} \\\\\n"
                      )
                      f.write(
                          _replace_top_2(
                              summaries_by_section[section][model]
                          ).to_csv(header=False, **TO_CSV_TEX_KARGS)
                      )
              else:
                  f.write(
                      "\\multicolumn{6}{l}{"
                      + LATEX_ROW_MAPPING[section]
                      + "} \\\\\n"
                  )
                  f.write(
                      _replace_top_2(
                          summaries_by_section[section]
                      ).to_csv(header=False, **TO_CSV_TEX_KARGS)
                  )
              f.write("\\midrule\n")
  
      print(f"Summary 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 7 for calling a global function with example usage: write_every_dataset_summary_table_tex(summaries_by_dataset, out_dir) and returns: df, df