code: def test_calculate_losses():
      # In a 0-1 ranged scorer, make sure that the loss
      # has an expected positive value
      y_pred = np.array([0, 1, 0, 1, 1, 1, 0, 0, 0, 0])
      y_true = np.array([0, 1, 0, 1, 1, 0, 0, 0, 0, 0])
      score = sklearn.metrics.accuracy_score(y_true, y_pred)
      assert {"accuracy": pytest.approx(score)} == calculate_scores(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[autosklearn.metrics.accuracy],
      )
      assert {
          "accuracy": pytest.approx(1.0 - score)
      } == calculate_losses(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[autosklearn.metrics.accuracy],
      )
  
      # Test two metrics
      score_dict = calculate_scores(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[
              autosklearn.metrics.accuracy,
              autosklearn.metrics.balanced_accuracy,
          ],
      )
      expected_score_dict = {
          "accuracy": 0.9,
          "balanced_accuracy": 0.9285714285714286,
      }
      loss_dict = calculate_losses(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[
              autosklearn.metrics.accuracy,
              autosklearn.metrics.balanced_accuracy,
          ],
      )
      for (
          expected_metric,
          expected_score,
      ) in expected_score_dict.items():
          assert (
              pytest.approx(expected_score)
              == score_dict[expected_metric]
          )
          assert (
              pytest.approx(1 - expected_score)
              == loss_dict[expected_metric]
          )
  
      # Test no metric
      with pytest.raises(
          ValueError,
          match="Number of metrics to compute must be greater than zero.",
      ):
          calculate_scores(
              solution=y_true,
              prediction=y_pred,
              task_type=BINARY_CLASSIFICATION,
              metrics=[],
          )
  
      with pytest.raises(
          ValueError,
          match="Number of metrics to compute must be greater than zero.",
      ):
          calculate_scores(
              solution=y_true,
              prediction=y_pred,
              task_type=BINARY_CLASSIFICATION,
              metrics=[],
              scoring_functions=[
                  autosklearn.metrics.accuracy,
                  autosklearn.metrics.balanced_accuracy,
              ],
          )
  
      # Test the same metric twice
      accuracy_fixture = {"accuracy": pytest.approx(0.9)}
      assert accuracy_fixture == calculate_scores(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[
              autosklearn.metrics.accuracy,
              autosklearn.metrics.accuracy,
          ],
      )
      assert accuracy_fixture == calculate_scores(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[autosklearn.metrics.accuracy],
          scoring_functions=[autosklearn.metrics.accuracy],
      )
      assert accuracy_fixture == calculate_scores(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[autosklearn.metrics.accuracy],
          scoring_functions=[
              autosklearn.metrics.accuracy,
              autosklearn.metrics.accuracy,
          ],
      )
  
      # Test the same name for multiple metrics!
      bogus_accuracy = autosklearn.metrics.make_scorer(
          "accuracy",
          score_func=sklearn.metrics.roc_auc_score,
      )
      with pytest.raises(ValueError, match="used multiple times"):
          calculate_scores(
              solution=y_true,
              prediction=y_pred,
              task_type=BINARY_CLASSIFICATION,
              metrics=[autosklearn.metrics.accuracy, bogus_accuracy],
          )
  
      # Test additional scoring functions
      score_dict = calculate_scores(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[autosklearn.metrics.accuracy],
          scoring_functions=[
              autosklearn.metrics.accuracy,
              autosklearn.metrics.balanced_accuracy,
          ],
      )
      expected_score_dict = {
          "accuracy": 0.9,
          "balanced_accuracy": 0.9285714285714286,
      }
      loss_dict = calculate_losses(
          solution=y_true,
          prediction=y_pred,
          task_type=BINARY_CLASSIFICATION,
          metrics=[autosklearn.metrics.accuracy],
          scoring_functions=[
              autosklearn.metrics.accuracy,
              autosklearn.metrics.balanced_accuracy,
          ],
      )
      for (
          expected_metric,
          expected_score,
      ) in expected_score_dict.items():
          assert (
              pytest.approx(expected_score)
              == score_dict[expected_metric]
          )
          assert (
              pytest.approx(1 - expected_score)
              == loss_dict[expected_metric]
          )
  
      # Lastly make sure that metrics whose optimum is zero
      # are also properly working
      y_true = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])
      y_pred = np.array([0.11, 0.22, 0.33, 0.44, 0.55, 0.66])
      score = sklearn.metrics.mean_squared_error(y_true, y_pred)
      assert {
          "mean_squared_error": pytest.approx(0 - score)
      } == calculate_scores(
          solution=y_true,
          prediction=y_pred,
          task_type=REGRESSION,
          metrics=[autosklearn.metrics.mean_squared_error],
      )
      assert {
          "mean_squared_error": pytest.approx(score)
      } == calculate_losses(
          solution=y_true,
          prediction=y_pred,
          task_type=REGRESSION,
          metrics=[autosklearn.metrics.mean_squared_error],
      )
  
imports: import warnings
  import numpy
  import sklearn.metrics
  import autosklearn.metrics
  from autosklearn.constants import BINARY_CLASSIFICATION, REGRESSION
  from autosklearn.metrics import calculate_losses, calculate_scores, compute_single_metric
  import pytest
  import unittest
label_desc: Use image node_id 9 for calling a global function with example usage: test_calculate_losses() without return types