code: def test_r2_early_stop():
      """
      This pyunit test is written to ensure that the max_runtime_secs can restrict the model training time for all
      h2o algos.  See https://github.com/h2oai/h2o-3/issues/11581.
      """
      global model_within_max_runtime
      global err_bound
      seed = 12345
  
      # GBM run
      training1_data = h2o.import_file(
          path=pyunit_utils.locate(
              "smalldata/gridsearch/multinomial_training1_set.csv"
          )
      )
      y_index = training1_data.ncol - 1
      x_indices = list(range(y_index))
      training1_data[y_index] = (
          training1_data[y_index].round().asfactor()
      )
  
      modelNoEarlyStop = H2OGradientBoostingEstimator(
          distribution="multinomial", seed=seed
      )
      modelNoEarlyStop.train(
          x=x_indices, y=y_index, training_frame=training1_data
      )
      numTrees = pyunit_utils.extract_from_twoDimTable(
          modelNoEarlyStop._model_json["output"]["model_summary"],
          "number_of_trees",
          takeFirst=True,
      )
  
      model = H2OGradientBoostingEstimator(
          distribution="multinomial",
          seed=seed,
          stopping_metric="r2",
          stopping_tolerance=0.01,
          stopping_rounds=5,
      )
      model.train(x=x_indices, y=y_index, training_frame=training1_data)
      numTreesEarlyStop = pyunit_utils.extract_from_twoDimTable(
          model._model_json["output"]["model_summary"],
          "number_of_trees",
          takeFirst=True,
      )
      print(
          "Number of tress built with early stopping: {0}.  Number of trees built without early stopping: {1}".format(
              numTreesEarlyStop[0], numTrees[0]
          )
      )
      assert (
          numTreesEarlyStop[0] <= numTrees[0]
      ), "Early stopping criteria r2 is not working."
  
imports: import sys
  import h2o
  from tests import pyunit_utils
  from h2o.estimators.gbm import H2OGradientBoostingEstimator
label_desc: Use image node_id 1 for calling a global function with example usage: test_r2_early_stop() without return types