code: def estimate_early_stopping_savings(
      experiment: Experiment,
      map_key: Optional[str] = None,
  ) -> float:
      """Estimate resource savings due to early stopping by considering
      COMPLETED and EARLY_STOPPED trials. First, use the mean of final
      progressions of the set completed trials as a benchmark for the
      length of a single trial. The savings is then estimated as:
  
      resource_savings =
        1 - actual_resource_usage / (num_trials * length of single trial)
  
      Args:
          experiment: The experiment.
          map_key: The map_key to use when computing resource savings.
  
      Returns:
          The estimated resource savings as a fraction of total resource usage (i.e.
          0.11 estimated savings indicates we would expect the experiment to have used 11%
          more resources without early stopping present).
      """
  
      map_data = assert_is_instance(experiment.lookup_data(), MapData)
  
      # If no map_key is provided, use some arbitrary map_key in the experiment's MapData
      if map_key is not None:
          step_key = map_key
      elif len(map_data.map_key_infos) > 0:
          step_key = map_data.map_key_infos[0].key
      else:
          return 0
  
      # Get final number of steps of each trial
      trial_resources = (
          map_data.map_df[["trial_index", step_key]]
          .groupby("trial_index")
          .max()
          .reset_index()
      )
  
      early_stopped_trial_idcs = experiment.trial_indices_by_status[
          TrialStatus.EARLY_STOPPED
      ]
      completed_trial_idcs = experiment.trial_indices_by_status[
          TrialStatus.COMPLETED
      ]
  
      # Assume that any early stopped trial would have had the mean number of steps of
      # the completed trials
      mean_completed_trial_resources = trial_resources[
          trial_resources["trial_index"].isin(completed_trial_idcs)
      ][step_key].mean()
  
      # Calculate the steps saved per early stopped trial. If savings are estimated to be
      # negative assume no savings
      stopped_trial_resources = trial_resources[
          trial_resources["trial_index"].isin(early_stopped_trial_idcs)
      ][step_key]
      saved_trial_resources = (
          mean_completed_trial_resources - stopped_trial_resources
      ).clip(0)
  
      # Return the ratio of the total saved resources over the total resources used plus
      # the total saved resources
      return (
          saved_trial_resources.sum() / trial_resources[step_key].sum()
      )
  
file_path: Ax/ax/early_stopping/utils.py
imports: from collections import defaultdict
  from logging import Logger
  from typing import Dict, List, Optional, Tuple
  import pandas
  from ax.core.base_trial import TrialStatus
  from ax.core.experiment import Experiment
  from ax.core.map_data import MapData
  from ax.utils.common.logger import get_logger
  from pyre_extensions import assert_is_instance
label_desc: Use image node_id 2 for calling a global function with example usage: estimate_early_stopping_savings(experiment, map_key) and returns: unknown, int