class_docstr: Implements the median stopping rule as described in the Vizier paper:
  
  https://research.google.com/pubs/pub46180.html
  
  Args:
      time_attr: The training result attr to use for comparing time.
          Note that you can pass in something non-temporal such as
          `training_iteration` as a measure of progress, the only requirement
          is that the attribute should increase monotonically.
      metric: The training result objective value attribute. Stopping
          procedures will use this attribute. If None but a mode was passed,
          the `ray.tune.result.DEFAULT_METRIC` will be used per default.
      mode: One of {min, max}. Determines whether objective is
          minimizing or maximizing the metric attribute.
      grace_period: Only stop trials at least this old in time.
          The mean will only be computed from this time onwards. The units
          are the same as the attribute named by `time_attr`.
      min_samples_required: Minimum number of trials to compute median
          over.
      min_time_slice: Each trial runs at least this long before
          yielding (assuming it isn't stopped). Note: trials ONLY yield if
          there are not enough samples to evaluate performance for the
          current result AND there are other trials waiting to run.
          The units are the same as the attribute named by `time_attr`.
      hard_stop: If False, pauses trials instead of stopping
          them. When all other trials are complete, paused trials will be
          resumed and allowed to run FIFO.
class_docstr_tok: ["Implements","the","median","stopping","rule","as","described","in","the","Vizier","paper",":","https",":","\/\/research.google.com\/pubs\/pub46180.html","Args",":","time_attr",":","The","training","result","attr","to","use","for","comparing","time",".","Note","that","you","can","pass","in","something","non-temporal","such","as","`","training_iteration","`","as","a","measure","of","progress",",","the","only","requirement","is","that","the","attribute","should","increase","monotonically",".","metric",":","The","training","result","objective","value","attribute",".","Stopping","procedures","will","use","this","attribute",".","If","None","but","a","mode","was","passed",",","the","`","ray.tune.result.DEFAULT_METRIC","`","will","be","used","per","default",".","mode",":","One","of","{","min",",","max","}",".","Determines","whether","objective","is","minimizing","or","maximizing","the","metric","attribute",".","grace_period",":","Only","stop","trials","at","least","this","old","in","time",".","The","mean","will","only","be","computed","from","this","time","onwards",".","The","units","are","the","same","as","the","attribute","named","by","`","time_attr","`",".","min_samples_required",":","Minimum","number","of","trials","to","compute","median","over",".","min_time_slice",":","Each","trial","runs","at","least","this","long","before","yielding","(","assuming","it","is","n't","stopped",")",".","Note",":","trials","ONLY","yield","if","there","are","not","enough","samples","to","evaluate","performance","for","the","current","result","AND","there","are","other","trials","waiting","to","run",".","The","units","are","the","same","as","the","attribute","named","by","`","time_attr","`",".","hard_stop",":","If","False",",","pauses","trials","instead","of","stopping","them",".","When","all","other","trials","are","complete",",","paused","trials","will","be","resumed","and","allowed","to","run","FIFO","."]
docstr: Callback for early stopping.
  
  This stopping rule stops a running trial if the trial's best objective
  value by step `t` is strictly worse than the median of the running
  averages of all completed trials' objectives reported up to step `t`.
docstr_tok: ["Callback","for","early","stopping",".","This","stopping","rule","stops","a","running","trial","if","the","trial","'s","best","objective","value","by","step","`","t","`","is","strictly","worse","than","the","median","of","the","running","averages","of","all","completed","trials","'","objectives","reported","up","to","step","`","t","`","."]
code: def on_trial_result(
      self,
      tune_controller: "TuneController",
      trial: Trial,
      result: Dict,
  ) -> str:
      """Callback for early stopping.
  
      This stopping rule stops a running trial if the trial's best objective
      value by step `t` is strictly worse than the median of the running
      averages of all completed trials' objectives reported up to step `t`.
      """
      if self._time_attr not in result or self._metric not in result:
          return TrialScheduler.CONTINUE
  
      if trial in self._stopped_trials:
          assert not self._hard_stop
          # Fall back to FIFO
          return TrialScheduler.CONTINUE
  
      time = result[self._time_attr]
      self._results[trial].append(result)
  
      if time < self._grace_period:
          return TrialScheduler.CONTINUE
  
      trials = self._trials_beyond_time(time)
      trials.remove(trial)
  
      if len(trials) < self._min_samples_required:
          action = self._on_insufficient_samples(
              tune_controller, trial, time
          )
          if action == TrialScheduler.PAUSE:
              self._last_pause[trial] = time
              action_str = "Yielding time to other trials."
          else:
              action_str = "Continuing anyways."
          logger.debug(
              "MedianStoppingRule: insufficient samples={} to evaluate "
              "trial {} at t={}. {}".format(
                  len(trials), trial.trial_id, time, action_str
              )
          )
          return action
  
      median_result = self._median_result(trials, time)
      best_result = self._best_result(trial)
      logger.debug(
          "Trial {} best res={} vs median res={} at t={}".format(
              trial, best_result, median_result, time
          )
      )
  
      if self._compare_op(median_result, best_result) != best_result:
          logger.debug(
              "MedianStoppingRule: early stopping {}".format(trial)
          )
          self._stopped_trials.add(trial)
          if self._hard_stop:
              return TrialScheduler.STOP
          else:
              return TrialScheduler.PAUSE
      else:
          return TrialScheduler.CONTINUE
  
imports: import collections
  import logging
  from typing import Dict, List, Optional, TYPE_CHECKING
  import numpy
  from ray.tune.result import DEFAULT_METRIC
  from ray.tune.experiment import Trial
  from ray.tune.schedulers.trial_scheduler import FIFOScheduler, TrialScheduler
  from ray.util.annotations import PublicAPI
label_desc: Use image node_id 4 for calling the MedianStoppingRule obj's underlying member method code with example usage: obj.on_trial_result(tune_controller, trial, result) and returns: TrialScheduler, TrialScheduler, TrialScheduler, action, TrialScheduler, TrialScheduler, TrialScheduler