class_docstr: Wrapper class for computing user-defined metrics.
  
  The `Metric` class provides a wrapper for standardizing the API
  around different classes of metrics that may be useful for DeepChem
  models. The implementation provides a few non-standard conveniences
  such as built-in support for multitask and multiclass metrics.
  
  There are a variety of different metrics this class aims to support.
  Metrics for classification and regression that assume that values to
  compare are scalars are supported.
  
  At present, this class doesn't support metric computation on models
  which don't present scalar outputs. For example, if you have a
  generative model which predicts images or molecules, you will need
  to write a custom evaluation and metric setup.
docstr: Parameters
  ----------
  metric: function
      Function that takes args y_true, y_pred (in that order) and
      computes desired score. If sample weights are to be considered,
      `metric` may take in an additional keyword argument
      `sample_weight`.
  task_averager: function, default None
      If not None, should be a function that averages metrics across
      tasks.
  name: str, default None
      Name of this metric
  threshold: float, default None (DEPRECATED)
      Used for binary metrics and is the threshold for the positive
      class.
  mode: str, default None
      Should usually be "classification" or "regression."
  n_tasks: int, default None
      The number of tasks this class is expected to handle.
  classification_handling_mode: str, default None
      DeepChem models by default predict class probabilities for
      classification problems. This means that for a given singletask
      prediction, after shape normalization, the DeepChem labels and prediction will be
      numpy arrays of shape `(n_samples, n_tasks, n_classes)` with class probabilities.
      `classification_handling_mode` is a string that instructs this method
      how to handle transforming these probabilities. It can take on the
      following values:
      - "direct": Pass `y_true` and `y_pred` directy into `self.metric`.
      - "threshold": Use `threshold_predictions` to threshold `y_true` and `y_pred`.
        Use `threshold_value` as the desired threshold. This converts them into
        arrays of shape `(n_samples, n_tasks)`, where each element is a class index.
      - "threshold-one-hot": Use `threshold_predictions` to threshold `y_true` and `y_pred`
        using `threshold_values`, then apply `to_one_hot` to output.
      - None: Select a mode automatically based on the metric.
  threshold_value: float, default None
      If set, and `classification_handling_mode` is "threshold" or
      "threshold-one-hot", apply a thresholding operation to values with this
      threshold. This option is only sensible on binary classification tasks.
      For multiclass problems, or if `threshold_value` is None, argmax() is used
      to select the highest probability class for each task.
docstr_tok: ["Parameters","--","--","--","--","--","metric",":","function","Function","that","takes","args","y_true",",","y_pred","(","in","that","order",")","and","computes","desired","score",".","If","sample","weights","are","to","be","considered",",","`","metric","`","may","take","in","an","additional","keyword","argument","`","sample_weight","`",".","task_averager",":","function",",","default","None","If","not","None",",","should","be","a","function","that","averages","metrics","across","tasks",".","name",":","str",",","default","None","Name","of","this","metric","threshold",":","float",",","default","None","(","DEPRECATED",")","Used","for","binary","metrics","and","is","the","threshold","for","the","positive","class",".","mode",":","str",",","default","None","Should","usually","be","``","classification","''","or","``","regression",".","''","n_tasks",":","int",",","default","None","The","number","of","tasks","this","class","is","expected","to","handle",".","classification_handling_mode",":","str",",","default","None","DeepChem","models","by","default","predict","class","probabilities","for","classification","problems",".","This","means","that","for","a","given","singletask","prediction",",","after","shape","normalization",",","the","DeepChem","labels","and","prediction","will","be","numpy","arrays","of","shape","`","(","n_samples",",","n_tasks",",","n_classes",")","`","with","class","probabilities",".","`","classification_handling_mode","`","is","a","string","that","instructs","this","method","how","to","handle","transforming","these","probabilities",".","It","can","take","on","the","following","values",":","-","``","direct","''",":","Pass","`","y_true","`","and","`","y_pred","`","directy","into","`","self.metric","`",".","-","``","threshold","''",":","Use","`","threshold_predictions","`","to","threshold","`","y_true","`","and","`","y_pred","`",".","Use","`","threshold_value","`","as","the","desired","threshold",".","This","converts","them","into","arrays","of","shape","`","(","n_samples",",","n_tasks",")","`",",","where","each","element","is","a","class","index",".","-","``","threshold-one-hot","''",":","Use","`","threshold_predictions","`","to","threshold","`","y_true","`","and","`","y_pred","`","using","`","threshold_values","`",",","then","apply","`","to_one_hot","`","to","output",".","-","None",":","Select","a","mode","automatically","based","on","the","metric",".","threshold_value",":","float",",","default","None","If","set",",","and","`","classification_handling_mode","`","is","``","threshold","''","or","``","threshold-one-hot","''",",","apply","a","thresholding","operation","to","values","with","this","threshold",".","This","option","is","only","sensible","on","binary","classification","tasks",".","For","multiclass","problems",",","or","if","`","threshold_value","`","is","None",",","argmax","(",")","is","used","to","select","the","highest","probability","class","for","each","task","."]
code: def __init__(
      self,
      metric: Callable[..., float],
      task_averager: Optional[Callable[..., Any]] = None,
      name: Optional[str] = None,
      threshold: Optional[float] = None,
      mode: Optional[str] = None,
      n_tasks: Optional[int] = None,
      classification_handling_mode: Optional[str] = None,
      threshold_value: Optional[float] = None,
  ):
      """
      Parameters
      ----------
      metric: function
          Function that takes args y_true, y_pred (in that order) and
          computes desired score. If sample weights are to be considered,
          `metric` may take in an additional keyword argument
          `sample_weight`.
      task_averager: function, default None
          If not None, should be a function that averages metrics across
          tasks.
      name: str, default None
          Name of this metric
      threshold: float, default None (DEPRECATED)
          Used for binary metrics and is the threshold for the positive
          class.
      mode: str, default None
          Should usually be "classification" or "regression."
      n_tasks: int, default None
          The number of tasks this class is expected to handle.
      classification_handling_mode: str, default None
          DeepChem models by default predict class probabilities for
          classification problems. This means that for a given singletask
          prediction, after shape normalization, the DeepChem labels and prediction will be
          numpy arrays of shape `(n_samples, n_tasks, n_classes)` with class probabilities.
          `classification_handling_mode` is a string that instructs this method
          how to handle transforming these probabilities. It can take on the
          following values:
          - "direct": Pass `y_true` and `y_pred` directy into `self.metric`.
          - "threshold": Use `threshold_predictions` to threshold `y_true` and `y_pred`.
            Use `threshold_value` as the desired threshold. This converts them into
            arrays of shape `(n_samples, n_tasks)`, where each element is a class index.
          - "threshold-one-hot": Use `threshold_predictions` to threshold `y_true` and `y_pred`
            using `threshold_values`, then apply `to_one_hot` to output.
          - None: Select a mode automatically based on the metric.
      threshold_value: float, default None
          If set, and `classification_handling_mode` is "threshold" or
          "threshold-one-hot", apply a thresholding operation to values with this
          threshold. This option is only sensible on binary classification tasks.
          For multiclass problems, or if `threshold_value` is None, argmax() is used
          to select the highest probability class for each task.
      """
      if threshold is not None:
          logger.warn(
              "threshold is deprecated and will be removed in a future version of DeepChem."
              "Set threshold in compute_metric instead."
          )
  
      self.metric = metric
      if task_averager is None:
          self.task_averager = np.mean
      else:
          self.task_averager = task_averager
      if name is None:
          if task_averager is None:
              if hasattr(self.metric, "__name__"):
                  self.name = self.metric.__name__
              else:
                  self.name = "unknown metric"
          else:
              if hasattr(self.metric, "__name__"):
                  self.name = (
                      task_averager.__name__
                      + "-"
                      + self.metric.__name__
                  )
              else:
                  self.name = "unknown metric"
      else:
          self.name = name
  
      if mode is None:
          # These are some smart defaults
          if self.metric.__name__ in [
              "roc_auc_score",
              "matthews_corrcoef",
              "recall_score",
              "accuracy_score",
              "kappa_score",
              "cohen_kappa_score",
              "precision_score",
              "precision_recall_curve",
              "balanced_accuracy_score",
              "prc_auc_score",
              "f1_score",
              "bedroc_score",
              "jaccard_score",
              "jaccard_index",
              "pixel_error",
          ]:
              mode = "classification"
          elif self.metric.__name__ in [
              "pearson_r2_score",
              "r2_score",
              "mean_squared_error",
              "mean_absolute_error",
              "rms_score",
              "mae_score",
              "pearsonr",
              "concordance_index",
          ]:
              mode = "regression"
          else:
              raise ValueError(
                  "Please specify the mode of this metric. mode must be 'regression' or 'classification'"
              )
      if mode == "classification":
          if classification_handling_mode is None:
              # These are some smart defaults corresponding to sklearn's required
              # behavior
              if self.metric.__name__ in [
                  "matthews_corrcoef",
                  "cohen_kappa_score",
                  "kappa_score",
                  "balanced_accuracy_score",
                  "recall_score",
                  "jaccard_score",
                  "jaccard_index",
                  "pixel_error",
                  "f1_score",
              ]:
                  classification_handling_mode = "threshold"
              elif self.metric.__name__ in [
                  "accuracy_score",
                  "precision_score",
                  "bedroc_score",
              ]:
                  classification_handling_mode = "threshold-one-hot"
              elif self.metric.__name__ in [
                  "roc_auc_score",
                  "prc_auc_score",
                  "precision_recall_curve",
              ]:
                  classification_handling_mode = "direct"
          if classification_handling_mode not in [
              "direct",
              "threshold",
              "threshold-one-hot",
          ]:
              raise ValueError(
                  "classification_handling_mode must be one of 'direct', 'threshold', 'threshold_one_hot'"
              )
  
      self.mode = mode
      self.n_tasks = n_tasks
      self.classification_handling_mode = classification_handling_mode
      self.threshold_value = threshold_value
  
file_path: deepchem/deepchem/metrics/metric.py
imports: import logging
  from typing import Any, Callable, Optional
  import numpy
  from numpy.typing import ArrayLike
label_desc: Use image node_id 1 to create a new Metric object from inherited base classes: object with example: obj = Metric(metric, task_averager, name, threshold, mode, n_tasks, classification_handling_mode, threshold_value)