code: def test_result_grid_api(ray_start_2_cpus, tmp_path):
      def train_fn(config):
          peak_fn = [0, config["id"], -config["id"], 0]
          for i in range(len(peak_fn)):
              with create_dict_checkpoint({"iter": i}) as checkpoint:
                  train.report(
                      {
                          "iter": i,
                          "score": config["id"],
                          "peak": peak_fn[i],
                      },
                      checkpoint=checkpoint,
                  )
  
      tuner = tune.Tuner(
          train_fn,
          param_space={"id": tune.grid_search([1, 2])},
          run_config=train.RunConfig(
              storage_path=str(tmp_path),
              name="test_result_grid_api",
              checkpoint_config=train.CheckpointConfig(num_to_keep=2),
          ),
      )
      result_grid = tuner.fit()
  
      assert len(result_grid) == 2
      assert result_grid.experiment_path == str(
          tmp_path / "test_result_grid_api"
      )
  
      with pytest.raises(ValueError):
          result_grid.get_best_result()
      with pytest.raises(ValueError):
          result_grid.get_best_result(metric="score")
      assert (
          result_grid.get_best_result(
              metric="score", mode="max"
          ).config["id"]
          == 2
      )
  
      df = result_grid.get_dataframe()
      assert len(df) == 2
      assert df["iter"].to_list() == [3, 3]
  
      df = result_grid.get_dataframe(
          filter_metric="peak", filter_mode="max"
      )
      assert df["iter"].to_list() == [1, 1]
      df = result_grid.get_dataframe(
          filter_metric="peak", filter_mode="min"
      )
      assert df["iter"].to_list() == [2, 2]
  
      assert not result_grid.errors
      assert result_grid.num_errors == 0
      assert result_grid.num_terminated == 2
  
      for result in result_grid:
          assert result.checkpoint is not None
          assert result.error is None
          assert load_dict_checkpoint(result.checkpoint)["iter"] == 3
          assert {
              metrics["iter"] for _, metrics in result.best_checkpoints
          } == {2, 3}
          assert {
              load_dict_checkpoint(checkpoint)["iter"]
              for checkpoint, _ in result.best_checkpoints
          } == {2, 3}
  
file_path: ray/python/ray/tune/tests/test_result_grid.py
imports: import pytest
  import ray
  from ray import train, tune
  from ray.train import Checkpoint, Result
  from ray.tune.result_grid import ResultGrid
  from ray.train.tests.util import create_dict_checkpoint, load_dict_checkpoint
label_desc: Use image node_id 2 for calling a global function with example usage: test_result_grid_api(ray_start_2_cpus, tmp_path) without return types