class_docstr: Testing the best point utilities functionality that is not tested in
  main `AxClient` testing suite (`TestServiceAPI`).
class_docstr_tok: ["Testing","the","best","point","utilities","functionality","that","is","not","tested","in","main","`","AxClient","`","testing","suite","(","`","TestServiceAPI","`",")","."]
code: def test_extract_Y_from_data(self) -> None:
      experiment = get_branin_experiment()
      sobol_generator = get_sobol(search_space=experiment.search_space)
      for i in range(20):
          sobol_run = sobol_generator.gen(n=1)
          trial = experiment.new_trial(
              generator_run=sobol_run
          ).mark_running(no_runner_required=True)
          if i in [3, 8, 10]:
              trial.mark_early_stopped()
          else:
              trial.mark_completed()
  
      df_dicts = []
      for trial_idx in range(20):
          for metric_name in ["foo", "bar"]:
              df_dicts.append(
                  {
                      "trial_index": trial_idx,
                      "metric_name": metric_name,
                      "arm_name": f"{trial_idx}_0",
                      "mean": float(trial_idx)
                      if metric_name == "foo"
                      else trial_idx + 5.0,
                      "sem": 0.0,
                  }
              )
      experiment.attach_data(
          Data(df=pd.DataFrame.from_records(df_dicts))
      )
  
      expected_Y = torch.stack(
          [
              torch.arange(20, dtype=torch.double),
              torch.arange(5, 25, dtype=torch.double),
          ],
          dim=-1,
      )
      Y, trial_indices = extract_Y_from_data(
          experiment=experiment,
          metric_names=["foo", "bar"],
      )
      expected_trial_indices = torch.arange(20)
      self.assertTrue(torch.allclose(Y, expected_Y))
      self.assertTrue(
          torch.equal(trial_indices, expected_trial_indices)
      )
      # Check that it respects ordering of metric names.
      Y, trial_indices = extract_Y_from_data(
          experiment=experiment,
          metric_names=["bar", "foo"],
      )
      self.assertTrue(torch.allclose(Y, expected_Y[:, [1, 0]]))
      self.assertTrue(
          torch.equal(trial_indices, expected_trial_indices)
      )
      # Extract partial metrics.
      Y, trial_indices = extract_Y_from_data(
          experiment=experiment, metric_names=["bar"]
      )
      self.assertTrue(torch.allclose(Y, expected_Y[:, [1]]))
      self.assertTrue(
          torch.equal(trial_indices, expected_trial_indices)
      )
      # Works with messed up ordering of data.
      clone_dicts = df_dicts.copy()
      random.shuffle(clone_dicts)
      experiment._data_by_trial = {}
      experiment.attach_data(
          Data(df=pd.DataFrame.from_records(clone_dicts))
      )
      Y, trial_indices = extract_Y_from_data(
          experiment=experiment,
          metric_names=["foo", "bar"],
      )
      self.assertTrue(torch.allclose(Y, expected_Y))
      self.assertTrue(
          torch.equal(trial_indices, expected_trial_indices)
      )
  
      # Check that it skips trials that are not completed.
      experiment.trials[0].mark_running(
          no_runner_required=True, unsafe=True
      )
      experiment.trials[1].mark_abandoned(unsafe=True)
      Y, trial_indices = extract_Y_from_data(
          experiment=experiment,
          metric_names=["foo", "bar"],
      )
      self.assertTrue(torch.allclose(Y, expected_Y[2:]))
      self.assertTrue(
          torch.equal(trial_indices, expected_trial_indices[2:])
      )
  
      # Error with missing data.
      with self.assertRaisesRegex(
          UserInputError, "Trial 2 is missing data on metrics {'foo'}."
      ):
          # Skipping first 5 data points since first two trials are not completed.
  
          extract_Y_from_data(
              experiment=experiment,
              metric_names=["foo", "bar"],
              data=Data(df=pd.DataFrame.from_records(df_dicts[5:])),
          )
  
      # Error with extra data.
      with self.assertRaisesRegex(
          UserInputError,
          "Trial data has more than one row per arm, metric pair. ",
      ):
          # Skipping first 5 data points since first two trials are not completed.
          base_df = pd.DataFrame.from_records(df_dicts[5:])
          extract_Y_from_data(
              experiment=experiment,
              metric_names=["foo", "bar"],
              data=Data(df=pd.concat((base_df, base_df))),
          )
  
      # Check that it works with BatchTrial.
      experiment = get_branin_experiment()
      batch_trial = BatchTrial(experiment=experiment, index=0)
      batch_trial.add_arm(
          Arm(name="0_0", parameters={"x1": 0.0, "x2": 0.0})
      )
      batch_trial.add_arm(
          Arm(name="0_1", parameters={"x1": 1.0, "x2": 0.0})
      )
      batch_trial.mark_running(no_runner_required=True).mark_completed()
      df_dicts_batch = []
      for i in (0, 1):
          for metric_name in ["foo", "bar"]:
              df_dicts_batch.append(
                  {
                      "trial_index": 0,
                      "metric_name": metric_name,
                      "arm_name": f"0_{i}",
                      "mean": float(i)
                      if metric_name == "foo"
                      else i + 5.0,
                      "sem": 0.0,
                  }
              )
      batch_df = pd.DataFrame.from_records(df_dicts_batch)
      Y, trial_indices = extract_Y_from_data(
          experiment=experiment,
          metric_names=["foo", "bar"],
          data=Data(df=batch_df),
      )
      self.assertTrue(torch.allclose(Y, expected_Y[:2]))
      self.assertTrue(
          torch.equal(trial_indices, torch.zeros(2, dtype=torch.long))
      )
  
imports: import random
  from typing import List
  from unittest.mock import MagicMock, patch
  import pandas
  import torch
  from ax.core.arm import Arm
  from ax.core.batch_trial import BatchTrial
  from ax.core.data import Data
  from ax.core.generator_run import GeneratorRun
  from ax.core.objective import ScalarizedObjective
  from ax.core.optimization_config import OptimizationConfig
  from ax.core.outcome_constraint import OutcomeConstraint
  from ax.core.types import ComparisonOp
  from ax.exceptions.core import UserInputError
  from ax.modelbridge.cross_validation import AssessModelFitResult
  from ax.modelbridge.registry import Models
  from ax.modelbridge.torch import TorchModelBridge
  from ax.plot.pareto_utils import get_tensor_converter_model
  from ax.service.utils.best_point import _derel_opt_config_wrapper, _is_row_feasible, extract_Y_from_data, get_best_parameters, get_best_raw_objective_point, logger
  from ax.utils.common.testutils import TestCase
  from ax.utils.common.typeutils import not_none
  from ax.utils.testing.core_stubs import get_branin_experiment, get_branin_metric, get_experiment_with_observations, get_sobol
  from ax.utils.testing.mock import fast_botorch_optimize
label_desc: Use image node_id 8 for calling the TestBestPointUtils obj's underlying member method code with example usage: obj.test_extract_Y_from_data() without return types