class_docstr: Manages issues related to non-iid data distributions.
  
  Parameters
  ----------
  datalab :
      The Datalab instance that this issue manager searches for issues in.
  
  metric :
      The distance metric used to compute the KNN graph of the examples in the dataset.
      If set to `None`, the metric will be automatically selected based on the dimensionality
      of the features used to represent the examples in the dataset.
  
  k :
      The number of nearest neighbors to consider when computing the KNN graph of the examples.
  
  num_permutations :
      The number of trials to run when performing permutation testing to determine whether
      the distribution of index-distances between neighbors in the dataset is IID or not.
  
  Note
  ----
  This class will only flag a single example as an issue if the dataset is considered non-IID. This type of issue
  is more relevant to the entire dataset as a whole, rather than to individual examples.
class_docstr_tok: ["Manages","issues","related","to","non-iid","data","distributions",".","Parameters","--","--","--","--","--","datalab",":","The","Datalab","instance","that","this","issue","manager","searches","for","issues","in",".","metric",":","The","distance","metric","used","to","compute","the","KNN","graph","of","the","examples","in","the","dataset",".","If","set","to","`","None","`",",","the","metric","will","be","automatically","selected","based","on","the","dimensionality","of","the","features","used","to","represent","the","examples","in","the","dataset",".","k",":","The","number","of","nearest","neighbors","to","consider","when","computing","the","KNN","graph","of","the","examples",".","num_permutations",":","The","number","of","trials","to","run","when","performing","permutation","testing","to","determine","whether","the","distribution","of","index-distances","between","neighbors","in","the","dataset","is","IID","or","not",".","Note","--","--","This","class","will","only","flag","a","single","example","as","an","issue","if","the","dataset","is","considered","non-IID",".","This","type","of","issue","is","more","relevant","to","the","entire","dataset","as","a","whole",",","rather","than","to","individual","examples","."]
docstr: Selects features (or pred_probs if features are None) and sets up a NearestNeighbors object if needed.
  
  Parameters
  ----------
  features :
      Original feature array or None.
  
  pred_probs :
      Predicted probabilities array or None.
  
  knn_graph :
      A precomputed KNN-graph stored in a csr_matrix or None. If None, a new NearestNeighbors object will be created.
  
  metric_changes :
      Whether the metric used to compute the KNN-graph has changed.
      This is a result of comparing the metric of a pre-existing KNN-graph and the metric specified by the user.
  
  Returns
  -------
  knn :
      A NearestNeighbors object or None.
docstr_tok: ["Selects","features","(","or","pred_probs","if","features","are","None",")","and","sets","up","a","NearestNeighbors","object","if","needed",".","Parameters","--","--","--","--","--","features",":","Original","feature","array","or","None",".","pred_probs",":","Predicted","probabilities","array","or","None",".","knn_graph",":","A","precomputed","KNN-graph","stored","in","a","csr_matrix","or","None",".","If","None",",","a","new","NearestNeighbors","object","will","be","created",".","metric_changes",":","Whether","the","metric","used","to","compute","the","KNN-graph","has","changed",".","This","is","a","result","of","comparing","the","metric","of","a","pre-existing","KNN-graph","and","the","metric","specified","by","the","user",".","Returns","--","--","--","-","knn",":","A","NearestNeighbors","object","or","None","."]
code: def _setup_knn(
      self,
      features: Optional[npt.NDArray],
      pred_probs: Optional[np.ndarray],
      knn_graph: Optional[csr_matrix],
      metric_changes: bool,
  ) -> Optional[NearestNeighbors]:
      """
      Selects features (or pred_probs if features are None) and sets up a NearestNeighbors object if needed.
  
      Parameters
      ----------
      features :
          Original feature array or None.
  
      pred_probs :
          Predicted probabilities array or None.
  
      knn_graph :
          A precomputed KNN-graph stored in a csr_matrix or None. If None, a new NearestNeighbors object will be created.
  
      metric_changes :
          Whether the metric used to compute the KNN-graph has changed.
          This is a result of comparing the metric of a pre-existing KNN-graph and the metric specified by the user.
  
      Returns
      -------
      knn :
          A NearestNeighbors object or None.
      """
      if features is None and pred_probs is not None:
          self._skip_storing_knn_graph_for_pred_probs = True
      features_to_use = self._determine_features(features, pred_probs)
  
      if self.metric is None:
          self.metric = (
              "cosine" if features_to_use.shape[1] > 3 else "euclidean"
          )
  
      if knn_graph is not None and not metric_changes:
          return None
  
      knn = NearestNeighbors(n_neighbors=self.k, metric=self.metric)
  
      if self.metric != knn.metric:
          warnings.warn(
              f"Metric {self.metric} does not match metric {knn.metric} used to fit knn. "
              "Most likely an existing NearestNeighbors object was passed in, but a different "
              "metric was specified."
          )
      self.metric = knn.metric
  
      try:
          check_is_fitted(knn)
      except NotFittedError:
          knn.fit(features_to_use)
  
      return knn
  
imports: from __future__ import annotations
  from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union, cast
  import warnings
  import itertools
  from scipy.stats import gaussian_kde
  import numpy
  import pandas
  from scipy.sparse import csr_matrix
  from sklearn.neighbors import NearestNeighbors
  from sklearn.exceptions import NotFittedError
  from sklearn.utils.validation import check_is_fitted
  from cleanlab.datalab.internal.issue_manager import IssueManager
label_desc: Use image node_id 3 for calling the NonIIDIssueManager obj's underlying member method code with example usage: obj._setup_knn(features, pred_probs, knn_graph, metric_changes) and returns: knn, None