code: def find_best_confidence(
      train_confidences: List[List[Dict[ConfidenceSpec, float]]],
      train_labels: List[int],
      dev_confidences: List[List[Dict[ConfidenceSpec, float]]],
      dev_labels: List[int],
      tune_lr: bool,
      tune_lr_config: TuneConfidenceConfig,
  ) -> Tuple[ConfidenceConfig, Pipeline]:
      """Finds the best confidence configuration for model selection.
  
      Will loop over all values in the confidence dictionary and fit the LR
      model (optionally tuning its HPs). The best performing confidence (on the
      dev set) will be used for the final LR model.
  
      Args:
          train_confidences: this is an object of type
              ``List[List[Dict[ConfidenceSpec, float]]]``. The shape of this
              object is [M, N, S], where
                  M: number of models
                  N: number of utterances in all training sets
                  S: number of confidence scores to try
  
              This argument will be used to construct np.array objects for each
              of the confidence scores with the shape [M, N]
  
          train_labels: ground-truth labels of the correct model for each data
              points. This is a list of size [N]
          dev_confidences: same as training, but for the validation subset.
          dev_labels: same as training, but for the validation subset.
          tune_lr: controls whether tuning of LR hyperparameters is performed.
          tune_lr_cfg: specifies what values of LR hyperparameters to try.
  
      Returns:
          tuple: best confidence config, best model selection pipeline
      """
      max_score = 0
      best_pipe = None
      best_conf_spec = None
      LOG.info(
          "Evaluation all confidences. Total grid size: %d",
          len(train_confidences[0][0].keys()),
      )
      for conf_spec in tqdm(train_confidences[0][0].keys()):
          cur_train_confidences = []
          for model_confs in train_confidences:
              cur_train_confidences.append([])
              for model_conf in model_confs:
                  cur_train_confidences[-1].append(
                      model_conf[conf_spec]
                  )
          cur_dev_confidences = []
          for model_confs in dev_confidences:
              cur_dev_confidences.append([])
              for model_conf in model_confs:
                  cur_dev_confidences[-1].append(model_conf[conf_spec])
          # transposing with zip(*list)
          training_features = np.array(
              list(zip(*cur_train_confidences))
          )
          training_labels = np.array(train_labels)
          dev_features = np.array(list(zip(*cur_dev_confidences)))
          dev_labels = np.array(dev_labels)
          pipe, score = train_model_selection(
              training_features,
              training_labels,
              dev_features,
              dev_labels,
              tune_lr,
              tune_lr_config,
          )
          if max_score < score:
              max_score = score
              best_pipe = pipe
              best_conf_spec = conf_spec
              LOG.info(
                  "Found better parameters: %s. New score: %.4f",
                  str(conf_spec),
                  max_score,
              )
  
      return best_conf_spec.to_confidence_config(), best_pipe
  
imports: import atexit
  import logging
  import os
  import random
  import sys
  import tempfile
  from copy import deepcopy
  from dataclasses import dataclass, field
  from pathlib import Path
  from typing import Dict, List, Optional, Tuple
  import joblib
  import numpy
  import pytorch_lightning
  from omegaconf import MISSING, DictConfig, OmegaConf
  from sklearn.linear_model import LogisticRegression
  from sklearn.metrics import confusion_matrix
  from sklearn.pipeline import Pipeline, make_pipeline
  from sklearn.preprocessing import StandardScaler
  from tqdm import tqdm
  from nemo.collections.asr.models.confidence_ensemble import ConfidenceEnsembleModel, ConfidenceSpec, compute_confidence, get_filtered_logprobs
  from nemo.collections.asr.parts.utils.asr_confidence_utils import ConfidenceConfig, ConfidenceMethodConfig, get_confidence_aggregation_bank, get_confidence_measure_bank
  from nemo.collections.asr.parts.utils.rnnt_utils import Hypothesis
  from nemo.core.config import hydra_runner
label_desc: Use image node_id 6 for calling a global function with example usage: find_best_confidence(train_confidences, train_labels, dev_confidences, dev_labels, tune_lr, tune_lr_config) and returns: best_conf_spec,  best_pipe