class_docstr: Replays a Population Based Training run.
  
  Population Based Training does not return a single hyperparameter
  configuration, but rather a schedule of configurations. For instance,
  PBT might discover that a larger learning rate leads to good results
  in the first training iterations, but that a smaller learning rate
  is preferable later.
  
  This scheduler enables replaying these parameter schedules from
  a finished PBT run. This requires that population based training has
  been run with ``log_config=True``, which is the default setting.
  
  The scheduler will only accept and train a single trial. It will
  start with the initial config of the existing trial and update the
  config according to the schedule.
  
  Args:
      policy_file: The PBT policy file. Usually this is
          stored in ``~/ray_results/experiment_name/pbt_policy_xxx.txt``
          where ``xxx`` is the trial ID.
  
  Example:
  
  .. code-block:: python
  
      # Replaying a result from ray.tune.examples.pbt_convnet_example
      from ray import train, tune
  
      from ray.tune.examples.pbt_convnet_example import PytorchTrainable
      from ray.tune.schedulers import PopulationBasedTrainingReplay
  
      replay = PopulationBasedTrainingReplay(
          "~/ray_results/pbt_test/pbt_policy_XXXXX_00001.txt")
  
      tuner = tune.Tuner(
          PytorchTrainable,
          run_config=train.RunConfig(
              stop={"training_iteration": 100}
          ),
          tune_config=tune.TuneConfig(
              scheduler=replay,
          ),
      )
      tuner.fit()
class_docstr_tok: ["Replays","a","Population","Based","Training","run",".","Population","Based","Training","does","not","return","a","single","hyperparameter","configuration",",","but","rather","a","schedule","of","configurations",".","For","instance",",","PBT","might","discover","that","a","larger","learning","rate","leads","to","good","results","in","the","first","training","iterations",",","but","that","a","smaller","learning","rate","is","preferable","later",".","This","scheduler","enables","replaying","these","parameter","schedules","from","a","finished","PBT","run",".","This","requires","that","population","based","training","has","been","run","with","``","log_config=True","``",",","which","is","the","default","setting",".","The","scheduler","will","only","accept","and","train","a","single","trial",".","It","will","start","with","the","initial","config","of","the","existing","trial","and","update","the","config","according","to","the","schedule",".","Args",":","policy_file",":","The","PBT","policy","file",".","Usually","this","is","stored","in","``","~\/ray_results\/experiment_name\/pbt_policy_xxx.txt","``","where","``","xxx","``","is","the","trial","ID",".","Example",":","..","code-block",":",":","python","#","Replaying","a","result","from","ray.tune.examples.pbt_convnet_example","from","ray","import","train",",","tune","from","ray.tune.examples.pbt_convnet_example","import","PytorchTrainable","from","ray.tune.schedulers","import","PopulationBasedTrainingReplay","replay","=","PopulationBasedTrainingReplay","(","``","~\/ray_results\/pbt_test\/pbt_policy_XXXXX_00001.txt","''",")","tuner","=","tune.Tuner","(","PytorchTrainable",",","run_config=train.RunConfig","(","stop=","{","``","training_iteration","''",":","100","}",")",",","tune_config=tune.TuneConfig","(","scheduler=replay",",",")",",",")","tuner.fit","(",")"]
code: def _load_policy(
      self, policy_file: str
  ) -> Tuple[Dict, List[Tuple[int, Dict]]]:
      raw_policy = []
      with open(policy_file, "rt") as fp:
          for row in fp.readlines():
              try:
                  parsed_row = json.loads(row)
              except json.JSONDecodeError:
                  raise ValueError(
                      "Could not read PBT policy file: {}.".format(
                          policy_file
                      )
                  ) from None
              raw_policy.append(tuple(parsed_row))
  
      # Loop through policy from end to start to obtain changepoints
      policy = []
      last_new_tag = None
      last_old_conf = None
      for (
          old_tag,
          new_tag,
          old_step,
          new_step,
          old_conf,
          new_conf,
      ) in reversed(raw_policy):
          if last_new_tag and old_tag != last_new_tag:
              # Tag chain ended. This means that previous changes were
              # overwritten by the last change and should be ignored.
              break
          last_new_tag = new_tag
          last_old_conf = old_conf
  
          policy.append((new_step, new_conf))
  
      return last_old_conf, list(reversed(policy))
  
imports: import copy
  import json
  import logging
  import math
  import os
  import random
  import shutil
  import warnings
  from pathlib import Path
  from typing import Callable, Dict, List, Optional, Tuple, Union, TYPE_CHECKING
  from ray.air.constants import TRAINING_ITERATION
  from ray.train import Checkpoint
  from ray.train._internal.session import _TrainingResult, _FutureTrainingResult
  from ray.tune.error import TuneError
  from ray.tune.result import DEFAULT_METRIC
  from ray.tune.search import SearchGenerator
  from ray.tune.utils.util import SafeFallbackEncoder
  from ray.tune.search.sample import Domain, Function
  from ray.tune.schedulers import FIFOScheduler, TrialScheduler
  from ray.tune.search.variant_generator import format_vars
  from ray.tune.experiment import Trial
  from ray.util import PublicAPI
  from ray.util.debug import log_once
label_desc: Use image node_id 2 for calling the PopulationBasedTrainingReplay obj's underlying member method code with example usage: obj._load_policy(policy_file) and returns: last_old_conf,  list