class_docstr: Tests for the new `registry` and `registry_mapping` of the transform algo wrapper.
docstr: Test the case where the underlying algo suggests a transformed trial that is equivalent
  to a previously-suggested trial in the original space.
  
  In this case, the wrapped algo's `observe` method should be called inside the wrapper's
  `suggest`, passing in the suggested transformed trial, but with the results and status
  copied over from the existing `Trial`.
docstr_tok: ["Test","the","case","where","the","underlying","algo","suggests","a","transformed","trial","that","is","equivalent","to","a","previously-suggested","trial","in","the","original","space",".","In","this","case",",","the","wrapped","algo","'s","`","observe","`","method","should","be","called","inside","the","wrapper's","`","suggest","`",",","passing","in","the","suggested","transformed","trial",",","but","with","the","results","and","status","copied","over","from","the","existing","`","Trial","`","."]
code: def test_suggest_equivalent_to_existing(
      self,
      algo_wrapper: SpaceTransform[FixedSuggestionAlgo],
      original_status: str,
  ):
      """Test the case where the underlying algo suggests a transformed trial that is equivalent
      to a previously-suggested trial in the original space.
  
      In this case, the wrapped algo's `observe` method should be called inside the wrapper's
      `suggest`, passing in the suggested transformed trial, but with the results and status
      copied over from the existing `Trial`.
      """
  
      fixed_original = format_trials.dict_to_trial(
          {"x": 10}, space=algo_wrapper.space
      )
      fixed_transformed = algo_wrapper.transformed_space.transform(
          fixed_original
      )
      assert fixed_transformed.params == {"x": numpy.log(10)}
      transformed_space = algo_wrapper.transformed_space
  
      algo_wrapper.unwrapped.fixed_suggestion = fixed_transformed
  
      # Get a suggestion from the wrapper.
      suggested_trials = algo_wrapper.suggest(1)
      assert suggested_trials is not None
      assert suggested_trials == [fixed_original]
  
      assert fixed_original in algo_wrapper.registry
      assert fixed_original in algo_wrapper.registry_mapping
      assert algo_wrapper.registry_mapping[fixed_original] == [
          fixed_transformed
      ]
      assert algo_wrapper.has_suggested(fixed_original)
      assert not algo_wrapper.has_observed(fixed_original)
  
      assert algo_wrapper.algorithm.has_suggested(fixed_transformed)
      assert not algo_wrapper.algorithm.has_observed(fixed_transformed)
  
      # Now, add some results to the first suggestion, and have the wrapper observe it.
      trial_with_results = copy.deepcopy(fixed_original)
      trial_with_results._status = original_status
      if original_status == "completed":
          trial_with_results._results = [
              Trial.Result(name="objective", type="objective", value=1)
          ]
  
      trial_with_results_transformed = transformed_space.transform(
          trial_with_results
      )
      assert (
          trial_with_results_transformed.results
          == trial_with_results.results
      )
  
      algo_wrapper.observe([trial_with_results])
  
      if original_status in ["completed", "broken"]:
          assert algo_wrapper.has_observed(trial_with_results)
          assert algo_wrapper.algorithm.has_observed(
              trial_with_results_transformed
          )
  
      # Pretty obvious, since this was true before observing. Checking again, just to be sure.
      assert algo_wrapper.has_suggested(trial_with_results)
      assert algo_wrapper.algorithm.has_suggested(
          trial_with_results_transformed
      )
  
      # Create a Trial in the transformed space that will map to the same trial as the fixed
      # suggestion in the original space.
      equivalent_transformed = format_trials.dict_to_trial(
          {"x": fixed_transformed.params["x"] + 1e-6},
          space=transformed_space,
      )
      # Make sure that both are distinct, but map to the same trial in the original space.
      assert equivalent_transformed != fixed_transformed
      assert equivalent_transformed.params != fixed_transformed.params
      equivalent_original = transformed_space.reverse(
          equivalent_transformed
      )
      assert equivalent_original == fixed_original
  
      # Check that has_observed/has_suggested is False for the equivalent trial in the algorithm.
      assert not algo_wrapper.algorithm.has_suggested(
          equivalent_transformed
      )
      assert not algo_wrapper.algorithm.has_observed(
          equivalent_transformed
      )
  
      # Update the dummy algo so that it now returns the equivalent transformed trial.
      algo_wrapper.unwrapped.fixed_suggestion = equivalent_transformed
  
      # Get another suggestion from the wrapper. (the wrapped algo will return the equivalent)
      new_suggested_trials = algo_wrapper.suggest(1)
      assert new_suggested_trials is not None
      # Here the algo wrapper should return an empty list, since the suggested trial is a
      # duplicate of one that was already suggested.
      assert new_suggested_trials == []
  
      # Check that the equivalent trial was added into the algorithm's registry, and that the
      # status and results from `trial_with_results` were also copied over.
      assert equivalent_transformed in algo_wrapper.algorithm.registry
      trial_in_registry = algo_wrapper.algorithm.registry.get_existing(
          equivalent_transformed
      )
  
      # NOTE: The algo has suggested a duplicate. We make the algo observe the duplicate, with
      # the status (and potentially also results) from the original. This is also the case for
      # broken or pending trials.
  
      assert trial_in_registry.params == equivalent_transformed.params
      assert trial_in_registry.status == trial_with_results.status
      assert trial_in_registry.results == trial_with_results.results
  
      # The algo wrapper has suggested (and observed) a trial that is equivalent to this one.
      if original_status in ["completed", "broken"]:
          assert algo_wrapper.has_observed(equivalent_original)
          assert algo_wrapper.algorithm.has_observed(
              equivalent_transformed
          )
  
      assert algo_wrapper.has_suggested(equivalent_original)
      assert algo_wrapper.algorithm.has_suggested(
          equivalent_transformed
      )
  
imports: from __future__ import annotations
  import copy
  import typing
  from typing import Any, ClassVar
  import numpy
  import pytest
  from orion.algo.base import BaseAlgorithm
  from orion.algo.space import Space
  from orion.core.io.space_builder import SpaceBuilder
  from orion.core.utils import backward, format_trials
  from orion.core.worker.algo_wrappers.space_transform import SpaceTransform
  from orion.core.worker.transformer import build_required_space
  from orion.core.worker.trial import Trial
  from orion.testing.dummy_algo import FixedSuggestionAlgo
  from .base import AlgoWrapperTests
label_desc: Use image node_id 1 for calling the TestRegistration obj's underlying member method code with example usage: obj.test_suggest_equivalent_to_existing(algo_wrapper, original_status) without return types