code: def test_qbin_pipeline(testing_cfg):
      """Test the uncertainty quantile binning pipeline."""
  
      # ---- setup output ----
      os.makedirs(testing_cfg["OUTPUT"]["SAVE_FOLDER"], exist_ok=True)
      logger = logging.construct_logger(
          "q_bin",
          testing_cfg["OUTPUT"]["SAVE_FOLDER"],
          log_to_terminal=True,
      )
  
      # ---- setup dataset ----
      base_dir = testing_cfg["DATASET"]["BASE_DIR"]
  
      uncertainty_pairs_val = testing_cfg["DATASET"]["UE_PAIRS_VAL"]
      uncertainty_pairs_test = testing_cfg["DATASET"]["UE_PAIRS_TEST"]
      gt_test_error_available = testing_cfg["DATASET"][
          "GROUND_TRUTH_TEST_ERRORS_AVAILABLE"
      ]
  
      ind_q_uncertainty_error_pairs = testing_cfg["PIPELINE"][
          "INDIVIDUAL_Q_UNCERTAINTY_ERROR_PAIRS"
      ]
      ind_q_models_to_compare = testing_cfg["PIPELINE"][
          "INDIVIDUAL_Q_MODELS"
      ]
  
      compare_q_uncertainty_error_pairs = testing_cfg["PIPELINE"][
          "COMPARE_Q_UNCERTAINTY_ERROR_PAIRS"
      ]
      compare_q_models_to_compare = testing_cfg["PIPELINE"][
          "COMPARE_Q_MODELS"
      ]
  
      dataset = testing_cfg["DATASET"]["DATA"]
      landmarks = testing_cfg["DATASET"]["LANDMARKS"]
      num_folds = testing_cfg["DATASET"]["NUM_FOLDS"]
  
      ind_landmarks_to_show = testing_cfg["PIPELINE"][
          "IND_LANDMARKS_TO_SHOW"
      ]
  
      pixel_to_mm_scale = testing_cfg["PIPELINE"]["PIXEL_TO_MM_SCALE"]
  
      # Define parameters for visualization
      cmaps = sns.color_palette("deep", 10).as_hex()
  
      show_individual_landmark_plots = testing_cfg["PIPELINE"][
          "SHOW_IND_LANDMARKS"
      ]
  
      for num_bins in testing_cfg["PIPELINE"]["NUM_QUANTILE_BINS"]:
          # create the folder to save to
          save_folder = os.path.join(
              testing_cfg["OUTPUT"]["SAVE_FOLDER"],
              dataset,
              str(num_bins) + "Bins",
          )
  
          # ---- This is the Fitting Phase ----
          # Fit all the options for the individual q selection and comparison q selection
  
          all_models_to_compare = np.unique(
              ind_q_models_to_compare + compare_q_models_to_compare
          )
          all_uncert_error_pairs_to_compare = np.unique(
              ind_q_uncertainty_error_pairs
              + compare_q_uncertainty_error_pairs,
              axis=0,
          )
  
          for model in all_models_to_compare:
              for landmark in landmarks:
                  # Define Paths for this loop
                  landmark_results_path_val = os.path.join(
                      testing_cfg["DATASET"]["ROOT"],
                      base_dir,
                      model,
                      dataset,
                      uncertainty_pairs_val + "_t" + str(landmark),
                  )
                  landmark_results_path_test = os.path.join(
                      testing_cfg["DATASET"]["ROOT"],
                      base_dir,
                      model,
                      dataset,
                      uncertainty_pairs_test + "_t" + str(landmark),
                  )
  
                  fitted_save_at = os.path.join(
                      save_folder,
                      "fitted_quantile_binning",
                      model,
                      dataset,
                  )
                  os.makedirs(save_folder, exist_ok=True)
  
                  helper_test_qbin_fit(
                      landmark,
                      all_uncert_error_pairs_to_compare,
                      landmark_results_path_val,
                      landmark_results_path_test,
                      num_bins,
                      testing_cfg,
                      gt_test_error_available,
                      fitted_save_at,
                      model,
                  )
  
          # Evaluation Phase ##########################
  
          comparisons_models = "_".join(ind_q_models_to_compare)
  
          comparisons_um = [
              str(x[0]) for x in ind_q_uncertainty_error_pairs
          ]
          comparisons_um = "_".join(comparisons_um)
  
          save_file_preamble = "_".join(
              [
                  testing_cfg["OUTPUT"]["SAVE_PREPEND"],
                  "ind",
                  dataset,
                  comparisons_models,
                  comparisons_um,
                  "combined"
                  + str(testing_cfg["PIPELINE"]["COMBINE_MIDDLE_BINS"]),
              ]
          )
  
          generate_fig_individual_bin_comparison(
              data=[
                  ind_q_uncertainty_error_pairs,
                  ind_q_models_to_compare,
                  dataset,
                  landmarks,
                  num_bins,
                  cmaps,
                  os.path.join(save_folder, "fitted_quantile_binning"),
                  save_file_preamble,
                  testing_cfg["PIPELINE"]["COMBINE_MIDDLE_BINS"],
                  testing_cfg["OUTPUT"]["SAVE_FIGURES"],
                  testing_cfg["DATASET"]["CONFIDENCE_INVERT"],
                  testing_cfg["BOXPLOT"]["SAMPLES_AS_DOTS"],
                  testing_cfg["BOXPLOT"]["SHOW_SAMPLE_INFO_MODE"],
                  testing_cfg["BOXPLOT"]["ERROR_LIM"],
                  show_individual_landmark_plots,
                  True,
                  num_folds,
                  ind_landmarks_to_show,
                  pixel_to_mm_scale,
              ],
              display_settings={
                  "cumulative_error": True,
                  "errors": True,
                  "jaccard": True,
                  "error_bounds": True,
                  "correlation": False,
              },
          )
  
          if num_bins == 3:
              exp = EXPECTED_FILES_IND_3
          else:
              exp = EXPECTED_FILES_IND_5
  
          search_dir = os.path.join(
              testing_cfg["OUTPUT"]["SAVE_FOLDER"], dataset, "/"
          )
  
          helper_test_expected_files_exist(exp, search_dir)
  
          # Now delete for memory
          for expected_file in exp:
              file_path = os.path.join(
                  os.path.join(
                      testing_cfg["OUTPUT"]["SAVE_FOLDER"], dataset
                  ),
                  expected_file,
              )
              os.remove(file_path)
  
      # If we are comparing bins against each other, we need to wait until all the bins have been fitted.
      for c_model in compare_q_models_to_compare:
          for c_er_pair in compare_q_uncertainty_error_pairs:
              save_file_preamble = "_".join(
                  [
                      testing_cfg["OUTPUT"]["SAVE_PREPEND"],
                      "compQ",
                      c_model,
                      c_er_pair[0],
                      dataset,
                      "combined"
                      + str(
                          testing_cfg["PIPELINE"]["COMBINE_MIDDLE_BINS"]
                      ),
                  ]
              )
  
              all_fitted_save_paths = [
                  os.path.join(
                      testing_cfg["OUTPUT"]["SAVE_FOLDER"],
                      dataset,
                      str(x_bins) + "Bins",
                      "fitted_quantile_binning",
                  )
                  for x_bins in testing_cfg["PIPELINE"][
                      "NUM_QUANTILE_BINS"
                  ]
              ]
  
              hatch_type = "o" if "PHD-NET" == c_model else ""
              color = (
                  cmaps[0]
                  if c_er_pair[0] == "S-MHA"
                  else cmaps[1]
                  if c_er_pair[0] == "E-MHA"
                  else cmaps[2]
              )
              save_folder_comparison = os.path.join(
                  testing_cfg["OUTPUT"]["SAVE_FOLDER"],
                  dataset,
                  "ComparisonBins",
              )
              os.makedirs(save_folder_comparison, exist_ok=True)
  
              logger.info(
                  "Comparison Q figures for: %s and %s ",
                  c_model,
                  c_er_pair,
              )
              generate_fig_comparing_bins(
                  data=[
                      c_er_pair,
                      c_model,
                      dataset,
                      landmarks,
                      testing_cfg["PIPELINE"]["NUM_QUANTILE_BINS"],
                      cmaps,
                      all_fitted_save_paths,
                      save_folder_comparison,
                      save_file_preamble,
                      testing_cfg["PIPELINE"]["COMBINE_MIDDLE_BINS"],
                      testing_cfg["OUTPUT"]["SAVE_FIGURES"],
                      testing_cfg["BOXPLOT"]["SAMPLES_AS_DOTS"],
                      testing_cfg["BOXPLOT"]["SHOW_SAMPLE_INFO_MODE"],
                      testing_cfg["BOXPLOT"]["ERROR_LIM"],
                      show_individual_landmark_plots,
                      True,
                      num_folds,
                      ind_landmarks_to_show,
                      pixel_to_mm_scale,
                  ],
                  display_settings={
                      "cumulative_error": True,
                      "errors": True,
                      "jaccard": True,
                      "error_bounds": True,
                      "hatch": hatch_type,
                      "color": color,
                  },
              )
  
      helper_test_expected_files_exist(
          EXPECTED_FILES_COMP,
          os.path.join(testing_cfg["OUTPUT"]["SAVE_FOLDER"], dataset),
      )
  
      # Now delete for memory
      for expected_file in EXPECTED_FILES_COMP:
          file_path = os.path.join(
              os.path.join(
                  testing_cfg["OUTPUT"]["SAVE_FOLDER"], dataset
              ),
              expected_file,
          )
          os.remove(file_path)
  
file_path: pykale/tests/pipeline/test_uncertainty_qbin_pipeline.py
imports: import os
  from typing import Any
  import numpy
  import pandas
  import pytest
  import seaborn
  import kale.utils.logger
  from kale.embed.uncertainty_fitting import fit_and_predict
  from kale.interpret.uncertainty_quantiles import generate_fig_comparing_bins, generate_fig_individual_bin_comparison
label_desc: Use image node_id 2 for calling a global function with example usage: test_qbin_pipeline(testing_cfg) without return types