code: def test_filter(
      named_model_space,
      engine,
      strategy,
      metric_for_invalid,
      retain_history,
  ):
      name, model_space = named_model_space
  
      if strategy == "grid":
          strategy = GridSearch(dedup=False)
      elif strategy == "random":
          strategy = Random(dedup=False)
      elif strategy == "evolution":
          strategy = RegularizedEvolution(dedup=False)
      elif strategy == "rl":
          strategy = PolicyBasedRL(policy_fn=naive_policy)
  
      def filter_fn(model):
          return model.sample["a"] <= 2
  
      strategy = Chain(
          Random(dedup=False),
          Filter(
              filter_fn,
              metric_for_invalid=metric_for_invalid,
              retain_history=retain_history,
          ),
      )
  
      if metric_for_invalid is None:
          assert (
              repr(strategy)
              == f"""Chain(
    Random(dedup=False),
    Filter(filter_fn={filter_fn})
  )"""
          )
  
      strategy(model_space, engine)
      assert strategy._status == StrategyStatus.SUCCEEDED
      assert all(not v for v in engine._callbacks.values())
  
      if metric_for_invalid is not None:
          any_invalid = False
          for model in strategy.list_models(sort=False):
              if model.sample["a"] > 2:
                  assert model.metric == -1
                  assert model.status == ModelStatus.Invalid
                  any_invalid = True
          if retain_history:
              assert any_invalid
      else:
          assert all(
              model.sample["a"] <= 2 for model in engine.list_models()
          )
  
imports: import random
  from collections import defaultdict, Counter
  import pytest
  from nni.mutable import SampleValidationError
  from nni.nas.execution import SequentialExecutionEngine
  from nni.nas.space import ModelStatus
  from nni.nas.strategy import Random, RegularizedEvolution, PolicyBasedRL, GridSearch, Strategy
  from nni.nas.strategy.base import StrategyStatus
  from nni.nas.strategy.middleware import Filter, Chain, Deduplication, FailureHandler, MultipleEvaluation
  from .test_sanity import named_model_space, engine, naive_policy
label_desc: Use image node_id 1 for calling a global function with example usage: test_filter(named_model_space, engine, strategy, metric_for_invalid, retain_history) and returns: unknown