code: def test_keep_checkpoints_by_score(order, checkpoint_paths):
      num_to_keep = 2
      score_attribute = "score"
  
      manager = _CheckpointManager(
          checkpoint_config=CheckpointConfig(
              num_to_keep=num_to_keep,
              checkpoint_score_attribute=score_attribute,
              checkpoint_score_order=order,
          )
      )
  
      scores = []
      for i in range(10):
          score = random.random()
          manager.register_checkpoint(
              _TrainingResult(
                  checkpoint=Checkpoint.from_directory(
                      checkpoint_paths[i]
                  ),
                  metrics={"iter": i, score_attribute: score},
              )
          )
          scores.append(score)
  
      sorted_scores = sorted(scores, reverse=order == "max")
      assert set(sorted_scores[:num_to_keep]) == {
          tracked_checkpoint.metrics[score_attribute]
          for tracked_checkpoint in manager.best_checkpoint_results
      }
  
      # Make sure the bottom checkpoints are deleted.
      best_checkpoint_iters = {
          tracked_checkpoint.metrics["iter"]
          for tracked_checkpoint in manager.best_checkpoint_results
      }
      for i, checkpoint_path in enumerate(checkpoint_paths):
          if i in best_checkpoint_iters or i == 9:
              # The checkpoint should only exist if it's one of the top K or the latest.
              assert Path(checkpoint_path).exists()
          else:
              assert not Path(checkpoint_path).exists()
  
file_path: ray/python/ray/train/tests/test_checkpoint_manager.py
imports: import random
  from pathlib import Path
  from typing import List
  import pytest
  from ray.train import Checkpoint, CheckpointConfig
  from ray.train._internal.checkpoint_manager import _CheckpointManager, _TrainingResult
label_desc: Use image node_id 4 for calling a global function with example usage: test_keep_checkpoints_by_score(order, checkpoint_paths) without return types