code: def test_suite_clean_cv_models():
      kcvm = "keep_cross_validation_models"
      nfolds = 5
  
      def setup_and_train(param_enabled=None):
          h2o.remove_all()
          ds = import_dataset()
          state = (
              "enabled"
              if param_enabled is True
              else "disabled"
              if param_enabled is False
              else "default"
          )
          if param_enabled is None:
              aml = H2OAutoML(
                  project_name="keep_cross_validation_models" + state,
                  nfolds=nfolds,
                  max_models=3,
                  seed=1,
              )
          else:
              aml = H2OAutoML(
                  project_name="keep_cross_validation_models" + state,
                  nfolds=nfolds,
                  max_models=8,
                  seed=1,
                  keep_cross_validation_models=param_enabled,
              )
  
          aml.train(y=ds.target, training_frame=ds.train)
          # print(aml.leaderboard)
          return aml
  
      def test_default_behaviour():
          print("\n=== " + kcvm + " default behaviour ===")
          aml = setup_and_train()
          models = get_partitioned_model_names(aml.leaderboard)
          check_model_property(models.se, kcvm, False)
          check_model_property(models.base, kcvm, True, False, True)
          keys = list_keys_in_memory()
          tot, cv = len(keys["models_all"]), len(keys["cv_models"])
          print(
              "total models in memory = {tot}, among which {cv} CV models".format(
                  tot=tot, cv=cv
              )
          )
          assert tot > 0, "no models left in memory"
          assert (
              cv == 0
          ), "{cv} CV models were not cleaned from memory".format(cv=cv)
          for m in models.base:
              assert not h2o.get_model(m).cross_validation_models(), (
                  "unexpected cv models for model " + m
              )
          for m in models.se:
              metal = h2o.get_model(
                  h2o.get_model(m).metalearner().model_id
              )
              assert not metal.cross_validation_models(), (
                  "unexpected cv models for metalearner of model " + m
              )
  
      def test_param_enabled():
          print("\n=== enabling " + kcvm + " ===")
          aml = setup_and_train(True)
          models = get_partitioned_model_names(aml.leaderboard)
          check_model_property(models.se, kcvm, False)
          check_model_property(models.base, kcvm, True, True, True)
          keys = list_keys_in_memory()
          tot, cv = len(keys["models_all"]), len(keys["cv_models"])
          print(
              "total models in memory = {tot}, among which {cv} CV models".format(
                  tot=tot, cv=cv
              )
          )
          assert tot > 0, "no models left in memory"
          expected = len(models.all) * nfolds
          assert (
              cv == expected
          ), "missing CV models in memory, got {actual}, expected {expected}".format(
              actual=cv, expected=expected
          )
          for m in models.base:
              assert h2o.get_model(m).cross_validation_models(), (
                  "missing cv models for model " + m
              )
          for m in models.se:
              metal = h2o.get_model(
                  h2o.get_model(m).metalearner().model_id
              )
              assert metal.cross_validation_models(), (
                  "missing cv models for metalearner of model " + m
              )
  
      def test_param_disabled():
          print("\n=== disabling " + kcvm + " ===")
          aml = setup_and_train(False)
          models = get_partitioned_model_names(aml.leaderboard)
          check_model_property(models.se, kcvm, False)
          check_model_property(models.base, kcvm, True, False, True)
          keys = list_keys_in_memory()
          tot, cv = len(keys["models_all"]), len(keys["cv_models"])
          print(
              "total models in memory = {tot}, among which {cv} CV models".format(
                  tot=tot, cv=cv
              )
          )
          assert tot > 0, "no models left in memory"
          assert (
              cv == 0
          ), "{cv} CV models were not cleaned from memory".format(cv=cv)
          for m in models.base:
              assert not h2o.get_model(m).cross_validation_models(), (
                  "unexpected cv models for model " + m
              )
          for m in models.se:
              metal = h2o.get_model(
                  h2o.get_model(m).metalearner().model_id
              )
              assert not metal.cross_validation_models(), (
                  "unexpected cv models for metalearner of model " + m
              )
  
      return [
          test_default_behaviour,
          test_param_enabled,
          test_param_disabled,
      ]
  
file_path: h2o-3/h2o-py/tests/testdir_algos/automl/pyunit_automl_memory_cleanup.py
imports: import sys, os
  import re
  import h2o
  from h2o import H2OFrame
  from h2o.automl import H2OAutoML
  from tests import pyunit_utils
  from _automl_utils import import_dataset, get_partitioned_model_names
label_desc: Use image node_id 4 for calling a global function with example usage: test_suite_clean_cv_models() and returns: list, aml