code: def _linearity_measure(
      predict_fn: Callable,
      x: np.ndarray,
      X_train: Optional[np.ndarray] = None,
      feature_range: Optional[Union[List, np.ndarray]] = None,
      method: Optional[str] = None,
      epsilon: float = 0.04,
      nb_samples: int = 10,
      res: int = 100,
      alphas: Optional[np.ndarray] = None,
      model_type: str = "classifier",
      agg: str = "global",
  ) -> np.ndarray:
      """Calculate the linearity measure of the model around an instance of interest `x`.
  
      Parameters
      ----------
      predict_fn
          Model prediction function.
      x
          Instance of interest.
      X_train
          Training set.
      feature_range
          Array with min and max values for each feature.
      method
          Method for sampling. Supported values: ``'knn'`` | ``'grid'``.
      epsilon
          Size of the sampling region around the central instance as a percentage of feature range.
      nb_samples
          Number of samples to generate.
      res
          Resolution of the grid. Number of intervals in which the feature range is discretized.
      alphas
          Array of coefficients in the superposition.
      model_type
          Type of task. Supported values: ``'regressor'`` | ``'classifier'``.
      agg
          Aggregation method. Supported values: ``'global'`` | ``'pairwise'``.
  
      Returns
      -------
      Linearity score.
  
      """
      input_shape = x.shape[1:]
  
      if method == "knn":
          assert (
              X_train is not None
          ), "The 'knn' method requires X_train != None"
          X_sampled = _sample_knn(x, X_train, nb_samples=nb_samples)
      elif method == "grid":
          assert (
              feature_range is not None
          ), "The 'grid' method requires feature_range != None."
          if isinstance(feature_range, list):
              feature_range = np.asarray(feature_range)
          X_sampled = _sample_grid(
              x,
              feature_range=feature_range,
              epsilon=epsilon,
              nb_samples=nb_samples,
              res=res,
          )
      else:
          raise ValueError(
              'Method not understood. Supported methods: "knn", "grid"'
          )
      logger.debug(x.shape)
      logger.debug(X_sampled.shape)
  
      if agg == "pairwise":
          if alphas is None:
              alphas = np.array([0.5, 0.5])
          score = _calculate_pairwise_linearity(
              predict_fn, x, input_shape, X_sampled, model_type, alphas
          )
      elif agg == "global":
          if alphas is None:
              alphas = np.array(
                  [1 / float(nb_samples) for _ in range(nb_samples)]
              )
          score = _calculate_global_linearity(
              predict_fn, input_shape, X_sampled, model_type, alphas
          )
      else:
          raise ValueError(
              'Aggregation argument supported values: "global" or "pairwise "'
          )
  
      return score
  
file_path: alibi/alibi/confidence/model_linearity.py
imports: import logging
  import string
  from time import time
  from typing import Callable, List, Optional, Tuple, Union
  import numpy
  from numpy.linalg import norm
  from sklearn.neighbors import NearestNeighbors
label_desc: Use image node_id 6 for calling a global function with example usage: _linearity_measure(predict_fn, x, X_train, feature_range, method, epsilon, nb_samples, res, alphas, model_type, agg) and returns: score