code: def test_init_mahalanobis(estimator, build_dataset):
      """Tests that for estimators that learn a mahalanobis matrix
      instead of a linear transformation, i.e. those that are mahalanobis metric
      learners
      where we can change the init, but not choose the n_components,
      (TODO: be more explicit on this characterization, for instance with
      safe_flags like in scikit-learn) that the init has an expected behaviour.
      """
      input_data, labels, _, X = build_dataset()
  
      matrices_to_set = []
      if hasattr(estimator, "init"):
          matrices_to_set.append("init")
      if hasattr(estimator, "prior"):
          matrices_to_set.append("prior")
  
      for param in matrices_to_set:
          model = clone(estimator)
          set_random_state(model)
          rng = np.random.RandomState(42)
  
          # Start learning from scratch
          model.set_params(**{param: "identity"})
          model.fit(input_data, labels)
  
          # Initialize with random
          model.set_params(**{param: "random"})
          model.fit(input_data, labels)
  
          # Initialize with covariance
          model.set_params(**{param: "covariance"})
          model.fit(input_data, labels)
  
          # Initialize with a random spd matrix
          init = make_spd_matrix(n_dim=X.shape[1], random_state=rng)
          model.set_params(**{param: init})
          model.fit(input_data, labels)
  
          # init.shape[1] must match X.shape[1]
          init = make_spd_matrix(n_dim=X.shape[1] + 1, random_state=rng)
          model.set_params(**{param: init})
          msg = (
              "The input dimensionality {} of the given "
              "mahalanobis matrix `{}` must match the "
              "dimensionality of the given inputs ({}).".format(
                  init.shape, param, input_data.shape[-1]
              )
          )
  
          with pytest.raises(ValueError) as raised_error:
              model.fit(input_data, labels)
          assert str(raised_error.value) == msg
  
          # The input matrix must be symmetric
          init = rng.rand(X.shape[1], X.shape[1])
          model.set_params(**{param: init})
          msg = "`{}` is not symmetric.".format(param)
          with pytest.raises(ValueError) as raised_error:
              model.fit(input_data, labels)
          assert str(raised_error.value) == msg
  
          # The input matrix must be SPD
          P = ortho_group.rvs(X.shape[1], random_state=rng)
          w = np.abs(rng.randn(X.shape[1]))
          w[0] = -10.0
          M = P.dot(np.diag(w)).dot(P.T)
          model.set_params(**{param: M})
          msg = "Matrix is not positive semidefinite (PSD)."
          with pytest.raises(NonPSDError) as raised_err:
              model.fit(input_data, labels)
          assert str(raised_err.value) == msg
  
          # init must be as specified in the docstring
          model.set_params(**{param: 1})
          msg = (
              "`{}` must be 'identity', 'covariance', "
              "'random' or a numpy array of shape "
              "(n_features, n_features).".format(param)
          )
          with pytest.raises(ValueError) as raised_error:
              model.fit(input_data, labels)
          assert str(raised_error.value) == msg
  
imports: from itertools import product
  import pytest
  import numpy
  from numpy.linalg import LinAlgError
  from numpy.testing import assert_array_almost_equal, assert_allclose, assert_array_equal
  from scipy.spatial.distance import pdist, squareform, mahalanobis
  from scipy.stats import ortho_group
  from sklearn import clone
  from sklearn.cluster import DBSCAN
  from sklearn.datasets import make_spd_matrix, make_blobs
  from sklearn.utils import check_random_state, shuffle
  from sklearn.utils.multiclass import type_of_target
  from metric_learn.sklearn_shims import set_random_state
  from metric_learn._util import make_context, _initialize_metric_mahalanobis
  from metric_learn.sdml import _BaseSDML
  from metric_learn.base_metric import _QuadrupletsClassifierMixin, _TripletsClassifierMixin, _PairsClassifierMixin
  from metric_learn.exceptions import NonPSDError
  from test.test_utils import ids_metric_learners, metric_learners, remove_y, ids_classifiers
label_desc: Use image node_id 18 for calling a global function with example usage: test_init_mahalanobis(estimator, build_dataset) without return types