code: def write_run_display_json(
      run_path: str, run_spec: RunSpec, schema: Schema
  ):
      """Write run JSON files that are used by the web frontend.
  
      The derived JSON files that are used by the web frontend are much more compact than
      the source JSON files. This speeds up web frontend loading significantly.
  
      Reads:
  
      - ScenarioState from `scenario_state.json`
      - List[PerInstanceStats] from `per_instance_stats.json`
  
      Writes:
  
      - List[Instance] to `instances.json`
      - List[DisplayPrediction] to `display_predictions.json`
      - List[DisplayRequest] to `display_requests.json`
      """
      scenario_state = _read_scenario_state(run_path)
      per_instance_stats = _read_per_instance_stats(run_path)
  
      metric_names = _get_metric_names_for_groups(
          run_spec.groups, schema
      )
  
      if (
          run_spec.adapter_spec.method
          in ADAPT_MULTIPLE_CHOICE_SEPARATE_METHODS
      ):
          metric_names.add("predicted_index")
  
      stats_by_trial: Dict[
          Tuple[str, Optional[PerturbationDescription], int],
          Dict[str, float],
      ] = defaultdict(dict)
      for original_stats in per_instance_stats:
          stats_dict: Dict[str, float] = {
              original_stat.name.name: original_stat.mean
              for original_stat in original_stats.stats
              if original_stat.name.name in metric_names
              and original_stat.mean is not None
          }
  
          key = (
              original_stats.instance_id,
              original_stats.perturbation,
              original_stats.train_trial_index,
          )
          stats_by_trial[key].update(stats_dict)
  
      instance_id_to_instance: Dict[
          Tuple[str, Optional[PerturbationDescription]], Instance
      ] = OrderedDict()
      predictions: List[DisplayPrediction] = []
      requests: List[DisplayRequest] = []
  
      for request_state in scenario_state.request_states:
          assert request_state.instance.id is not None
          if request_state.result is None:
              continue
  
          # For the multiple_choice_separate_calibrated adapter method,
          # only keep the original prediction and discard the calibration prediction.
          if (
              run_spec.adapter_spec.method
              == ADAPT_MULTIPLE_CHOICE_SEPARATE_CALIBRATED
              and request_state.request_mode == "calibration"
          ):
              continue
  
          stats_key = (
              request_state.instance.id,
              request_state.instance.perturbation,
              request_state.train_trial_index,
          )
          trial_stats: Dict[str, float] = stats_by_trial[stats_key]
          # For the multiple_choice_separate_* adapter methods,
          # only keep the prediction for the chosen reference and discard the rest.
          if (
              run_spec.adapter_spec.method
              in ADAPT_MULTIPLE_CHOICE_SEPARATE_METHODS
              and "predicted_index" in trial_stats
              and trial_stats["predicted_index"]
              != request_state.reference_index
          ):
              continue
  
          predicted_text = (
              request_state.result.completions[0].text
              if request_state.result is not None
              and request_state.result.completions
              else ""
          )
          mapped_output = (
              request_state.output_mapping.get(predicted_text.strip())
              if request_state.output_mapping
              else None
          )
          instance_id_to_instance[
              (
                  request_state.instance.id,
                  request_state.instance.perturbation,
              )
          ] = request_state.instance
          predictions.append(
              DisplayPrediction(
                  instance_id=request_state.instance.id,
                  perturbation=request_state.instance.perturbation,
                  train_trial_index=request_state.train_trial_index,
                  predicted_text=predicted_text,
                  truncated_predicted_text=_truncate_predicted_text(
                      predicted_text,
                      request_state,
                      run_spec.adapter_spec,
                  ),
                  mapped_output=mapped_output,
                  reference_index=request_state.reference_index,
                  stats=trial_stats,
              )
          )
          requests.append(
              DisplayRequest(
                  instance_id=request_state.instance.id,
                  perturbation=request_state.instance.perturbation,
                  train_trial_index=request_state.train_trial_index,
                  request=request_state.request,
              )
          )
  
      write(
          os.path.join(run_path, "instances.json"),
          to_json(list(instance_id_to_instance.values())),
      )
      write(
          os.path.join(run_path, "display_predictions.json"),
          to_json(predictions),
      )
      write(
          os.path.join(run_path, "display_requests.json"),
          to_json(requests),
      )
  
imports: from collections import OrderedDict, defaultdict
  from dataclasses import dataclass
  import os
  from typing import Dict, Iterable, List, Optional, Set, Tuple
  from helm.benchmark.adaptation.adapters.adapter_factory import ADAPT_MULTIPLE_CHOICE_SEPARATE_METHODS, ADAPT_MULTIPLE_CHOICE_SEPARATE_CALIBRATED
  from helm.benchmark.adaptation.adapter_spec import AdapterSpec
  from helm.benchmark.adaptation.request_state import RequestState
  from helm.benchmark.adaptation.scenario_state import ScenarioState
  from helm.benchmark.augmentations.perturbation_description import PerturbationDescription
  from helm.benchmark.metrics.metric import PerInstanceStats
  from helm.benchmark.presentation.schema import Schema
  from helm.benchmark.runner import RunSpec
  from helm.benchmark.scenarios.scenario import Instance
  from helm.common.general import write
  from helm.common.hierarchical_logger import htrack
  from helm.common.request import Request
  from helm.common.codec import from_json, to_json
label_desc: Use image node_id 6 for calling a global function with example usage: write_run_display_json(run_path, run_spec, schema) without return types