docstr: Test threshold at which is needed based on truncation_quantile
docstr_tok: ["Test","threshold","at","which","is","needed","based","on","truncation_quantile"]
code: def test_truncate(self, truncation_quantile, space, monkeypatch):
      """Test threshold at which is needed based on truncation_quantile"""
      # Test that trial within threshold is not replaced
      lineages = build_lineages_for_exploit(space, monkeypatch)
      trials = self.get_trials(lineages, TrialStub(objective=50))
      trials = sorted(trials, key=lambda trial: trial.objective.value)
  
      threshold_index = int(truncation_quantile * len(trials))
  
      good_trial = trials[threshold_index - 1]
      selected_trial = trials[-1]
  
      # Add non completed trials and shuffle the list to test it is filtered and sorted properly
      lots_of_trials = trials + space.sample(20, seed=2)
      numpy.random.shuffle(lots_of_trials)
  
      exploit = self.constructor(
          truncation_quantile=truncation_quantile,
          candidate_pool_ratio=0.2,
      )
  
      if truncation_quantile > 0.0:
  
          def mocked_choice(choices, *args, **kwargs):
              raise RuntimeError("Should not be called")
  
          rng = RNGStub()
          rng.choice = mocked_choice
  
          trial = exploit._truncate(
              rng,
              good_trial,
              lots_of_trials,
          )
  
          assert trial is good_trial
  
      if truncation_quantile < 1.0:
          bad_trial = trials[threshold_index]
  
          def mocked_choice(choices, *args, **kwargs):
              return -1
  
          rng = RNGStub()
          rng.choice = mocked_choice
  
          trial = exploit._truncate(
              rng,
              bad_trial,
              lots_of_trials,
          )
  
          assert trial is selected_trial
  
imports: import numpy
  import pytest
  from base import RNGStub, TrialStub
  from orion.algo.pbt.exploit import BacktrackExploit, PipelineExploit, TruncateExploit
  from orion.algo.pbt.pbt import Lineages
label_desc: Use image node_id 9 for calling the TruncateGenericTests obj's underlying member method code with example usage: obj.test_truncate(truncation_quantile, space, monkeypatch) and returns: unknown