code: def test_lagrangian_eval(
      eps, Constraints, use_Q_callable, opt_lambda
  ):
      X, y, A = _get_data(A_two_dim=False)
      estimator = LeastSquaresBinaryClassifierLearner()
  
      if issubclass(Constraints, LossMoment):
          task_type = "regression"
          constraints = Constraints(ZeroOneLoss(), upper_bound=eps)
      else:
          task_type = "classification"
          constraints = Constraints(difference_bound=eps)
  
      # epsilon (and thereby also B) only affects L_high and L
      B = 1 / eps
  
      lagrangian = _Lagrangian(
          X=X,
          y=y,
          estimator=estimator,
          constraints=deepcopy(constraints),
          B=B,
          opt_lambda=opt_lambda,
          sensitive_features=A,
      )
  
      lambda_vec = get_lambda_vec(constraints, X, y, A)
  
      # call oracle to determine error and gamma and calculate exp
      fitted_estimator = lagrangian._call_oracle(lambda_vec)
  
      def h(X):
          return fitted_estimator.predict(X)
  
      best_h_error = lagrangian.obj.gamma(h).iloc[0]
      best_h_gamma = lagrangian.constraints.gamma(h)
  
      # opt_lambda affects only the calculation of L
      if opt_lambda:
          projected_lambda = constraints.project_lambda(lambda_vec)
          L_expected = (
              best_h_error
              + np.sum(projected_lambda * best_h_gamma)
              - eps * np.sum(projected_lambda)
          )
      else:
          L_expected = (
              best_h_error
              + np.sum(lambda_vec * best_h_gamma)
              - eps * np.sum(lambda_vec)
          )
  
      L_high_expected = best_h_error + B * (best_h_gamma.max() - eps)
  
      # manually set errors and gammas which would otherwise be done in the best_h step
      lagrangian.errors = pd.Series([best_h_error])
      lagrangian.gammas = pd.Series([best_h_gamma])
  
      # call _eval to get the desired results L, L_high, gamma, error;
      # _eval is compatible with a callable h or a vector Q
      Q_vec = pd.Series([1.0])
      L, L_high, gamma, error = lagrangian._eval(
          h if use_Q_callable else Q_vec, lambda_vec
      )
  
      # in this particular example the estimator is always the same
      expected_estimator_weights = {
          "regression": pd.Series(
              {"X1": 0.541252, "X2": 0.454293, "X3": 0.019203}
          ),
          "classification": pd.Series(
              {"X1": 0.538136, "X2": 0.457627, "X3": 0.021186}
          ),
      }
      assert (
          np.isclose(
              fitted_estimator.weights,
              expected_estimator_weights[task_type],
              atol=1.0e-6,
          )
      ).all()
  
      assert L == pytest.approx(L_expected, abs=_PRECISION)
      assert L_high == pytest.approx(L_high_expected, abs=_PRECISION)
      assert error == 0.25
      assert (gamma == best_h_gamma).all()
  
file_path: fairlearn/test/unit/reductions/exponentiated_gradient/test_lagrangian.py
imports: from copy import deepcopy
  import numpy
  import pandas
  import pytest
  from sklearn.base import BaseEstimator
  from sklearn.dummy import DummyClassifier
  from fairlearn.reductions import BoundedGroupLoss, DemographicParity, EqualizedOdds, ErrorRate, ErrorRateParity, FalsePositiveRateParity, LossMoment, TruePositiveRateParity, ZeroOneLoss
  from fairlearn.reductions._exponentiated_gradient._lagrangian import _MESSAGE_BAD_OBJECTIVE, _Lagrangian
  from fairlearn.reductions._moments.bounded_group_loss import MeanLoss
  from .simple_learners import LeastSquaresBinaryClassifierLearner, MockEstimator
  from .test_utilities import _get_data
label_desc: Use image node_id 1 for calling a global function with example usage: test_lagrangian_eval(eps, Constraints, use_Q_callable, opt_lambda) and returns: fitted_estimator