code: def check_sample_weight_invariance(name, metric, y1, y2):
      rng = np.random.RandomState(0)
      sample_weight = rng.randint(1, 10, size=len(y1))
  
      # top_k_accuracy_score always lead to a perfect score for k > 1 in the
      # binary case
      metric = (
          partial(metric, k=1)
          if name == "top_k_accuracy_score"
          else metric
      )
  
      # check that unit weights gives the same score as no weight
      unweighted_score = metric(y1, y2, sample_weight=None)
  
      assert_allclose(
          unweighted_score,
          metric(y1, y2, sample_weight=np.ones(shape=len(y1))),
          err_msg="For %s sample_weight=None is not equivalent to sample_weight=ones"
          % name,
      )
  
      # check that the weighted and unweighted scores are unequal
      weighted_score = metric(y1, y2, sample_weight=sample_weight)
  
      # use context manager to supply custom error message
      with pytest.raises(AssertionError):
          assert_allclose(unweighted_score, weighted_score)
          raise ValueError(
              "Unweighted and weighted scores are unexpectedly "
              "almost equal (%s) and (%s) "
              "for %s" % (unweighted_score, weighted_score, name)
          )
  
      # check that sample_weight can be a list
      weighted_score_list = metric(
          y1, y2, sample_weight=sample_weight.tolist()
      )
      assert_allclose(
          weighted_score,
          weighted_score_list,
          err_msg=(
              "Weighted scores for array and list "
              "sample_weight input are not equal (%s != %s) for %s"
          )
          % (weighted_score, weighted_score_list, name),
      )
  
      # check that integer weights is the same as repeated samples
      repeat_weighted_score = metric(
          np.repeat(y1, sample_weight, axis=0),
          np.repeat(y2, sample_weight, axis=0),
          sample_weight=None,
      )
      assert_allclose(
          weighted_score,
          repeat_weighted_score,
          err_msg="Weighting %s is not equal to repeating samples"
          % name,
      )
  
      # check that ignoring a fraction of the samples is equivalent to setting
      # the corresponding weights to zero
      sample_weight_subset = sample_weight[1::2]
      sample_weight_zeroed = np.copy(sample_weight)
      sample_weight_zeroed[::2] = 0
      y1_subset = y1[1::2]
      y2_subset = y2[1::2]
      weighted_score_subset = metric(
          y1_subset, y2_subset, sample_weight=sample_weight_subset
      )
      weighted_score_zeroed = metric(
          y1, y2, sample_weight=sample_weight_zeroed
      )
      assert_allclose(
          weighted_score_subset,
          weighted_score_zeroed,
          err_msg=(
              "Zeroing weights does not give the same result as "
              "removing the corresponding samples (%s != %s) for %s"
          )
          % (weighted_score_zeroed, weighted_score_subset, name),
      )
  
      if not name.startswith("unnormalized"):
          # check that the score is invariant under scaling of the weights by a
          # common factor
          for scaling in [2, 0.3]:
              assert_allclose(
                  weighted_score,
                  metric(y1, y2, sample_weight=sample_weight * scaling),
                  err_msg="%s sample_weight is not invariant under scaling"
                  % name,
              )
  
      # Check that if number of samples in y_true and sample_weight are not
      # equal, meaningful error is raised.
      error_message = (
          r"Found input variables with inconsistent numbers of "
          r"samples: \[{}, {}, {}\]".format(
              _num_samples(y1),
              _num_samples(y2),
              _num_samples(sample_weight) * 2,
          )
      )
      with pytest.raises(ValueError, match=error_message):
          metric(
              y1,
              y2,
              sample_weight=np.hstack([sample_weight, sample_weight]),
          )
  
imports: from functools import partial
  from inspect import signature
  from itertools import chain, permutations, product
  import numpy
  import pytest
  from sklearn._config import config_context
  from sklearn.datasets import make_multilabel_classification
  from sklearn.metrics import accuracy_score, average_precision_score, balanced_accuracy_score, brier_score_loss, cohen_kappa_score, confusion_matrix, coverage_error, d2_absolute_error_score, d2_pinball_score, d2_tweedie_score, dcg_score, det_curve, explained_variance_score, f1_score, fbeta_score, hamming_loss, hinge_loss, jaccard_score, label_ranking_average_precision_score, label_ranking_loss, log_loss, matthews_corrcoef, max_error, mean_absolute_error, mean_absolute_percentage_error, mean_gamma_deviance, mean_pinball_loss, mean_poisson_deviance, mean_squared_error, mean_tweedie_deviance, median_absolute_error, multilabel_confusion_matrix, ndcg_score, precision_recall_curve, precision_score, r2_score, recall_score, roc_auc_score, roc_curve, top_k_accuracy_score, zero_one_loss
  from sklearn.metrics._base import _average_binary_score
  from sklearn.preprocessing import LabelBinarizer
  from sklearn.utils import shuffle
  from sklearn.utils._array_api import _atol_for_type, yield_namespace_device_dtype_combinations
  from sklearn.utils._testing import _array_api_for_tests, assert_allclose, assert_almost_equal, assert_array_equal, assert_array_less, ignore_warnings
  from sklearn.utils.fixes import COO_CONTAINERS
  from sklearn.utils.multiclass import type_of_target
  from sklearn.utils.validation import _num_samples, check_random_state
label_desc: Use image node_id 32 for calling a global function with example usage: check_sample_weight_invariance(name, metric, y1, y2) without return types