code: def test_threshold_optimization_demographic_parity_e2e(data_X_y_sf):
      adjusted_predictor = ThresholdOptimizer(
          estimator=ExamplePredictor(scores_ex),
          constraints="demographic_parity",
          flip=True,
          predict_method="predict",
      )
      adjusted_predictor.fit(
          data_X_y_sf.X,
          data_X_y_sf.y,
          sensitive_features=data_X_y_sf.sensitive_features,
      )
      predictions = adjusted_predictor._pmf_predict(
          data_X_y_sf.X,
          sensitive_features=data_X_y_sf.sensitive_features,
      )
  
      expected_ps = _expected_ps_demographic_parity[
          data_X_y_sf.example_name
      ]
  
      # assert demographic parity
      for sensitive_feature_name in data_X_y_sf.feature_names:
          average_probs = np.average(
              predictions[
                  _map_into_single_column(
                      data_X_y_sf.sensitive_features
                  )
                  == sensitive_feature_name
              ],
              axis=0,
          )
          assert np.isclose(average_probs[0], expected_ps[_P0])
          assert np.isclose(average_probs[1], expected_ps[_P1])
  
imports: from copy import deepcopy
  from test.unit.input_convertors import _map_into_single_column
  import numpy
  import pandas
  import pytest
  from sklearn.base import BaseEstimator, ClassifierMixin
  from sklearn.datasets import make_classification
  from fairlearn.postprocessing import ThresholdOptimizer
  from fairlearn.postprocessing._threshold_optimizer import BASE_ESTIMATOR_NONE_ERROR_MESSAGE, NOT_SUPPORTED_CONSTRAINTS_ERROR_MESSAGE
  from fairlearn.postprocessing._tradeoff_curve_utilities import DEGENERATE_LABELS_ERROR_MESSAGE
  from fairlearn.utils._input_validation import _LABELS_NOT_0_1_ERROR_MESSAGE, _MESSAGE_SENSITIVE_FEATURES_NONE, _MESSAGE_Y_NONE
  from .conftest import ExamplePredictor, X_ex, _get_predictions_by_sensitive_feature, candidate_A_transforms, candidate_X_transforms, candidate_Y_transforms, degenerate_labels_ex, is_invalid_transformation, labels_ex, scores_ex, sensitive_feature_names_ex1, sensitive_features_ex1
label_desc: Use image node_id 10 for calling a global function with example usage: test_threshold_optimization_demographic_parity_e2e(data_X_y_sf) without return types