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: Compute a performance metric for each task.
  
  Parameters
  ----------
  y_true: ArrayLike
      An ArrayLike containing true values for each task. Must be of shape
      `(N,)` or `(N, n_tasks)` or `(N, n_tasks, n_classes)` if a
      classification metric. If of shape `(N, n_tasks)` values can either be
      class-labels or probabilities of the positive class for binary
      classification problems. If a regression problem, must be of shape
      `(N,)` or `(N, n_tasks)` or `(N, n_tasks, 1)` if a regression metric.
  y_pred: ArrayLike
      An ArrayLike containing predicted values for each task. Must be
      of shape `(N, n_tasks, n_classes)` if a classification metric,
      else must be of shape `(N, n_tasks)` if a regression metric.
  w: ArrayLike, default None
      An ArrayLike containing weights for each datapoint. If
      specified,  must be of shape `(N, n_tasks)`.
  n_tasks: int, default None
      The number of tasks this class is expected to handle.
  n_classes: int, default 2
      Number of classes in data for classification tasks.
  per_task_metrics: bool, default False
      If true, return computed metric for each task on multitask dataset.
  use_sample_weights: bool, default False
      If set, use per-sample weights `w`.
  kwargs: dict
      Will be passed on to self.metric
  
  Returns
  -------
  np.ndarray
      A numpy array containing metric values for each task.
docstr_tok: ["Compute","a","performance","metric","for","each","task",".","Parameters","--","--","--","--","--","y_true",":","ArrayLike","An","ArrayLike","containing","true","values","for","each","task",".","Must","be","of","shape","`","(","N",",",")","`","or","`","(","N",",","n_tasks",")","`","or","`","(","N",",","n_tasks",",","n_classes",")","`","if","a","classification","metric",".","If","of","shape","`","(","N",",","n_tasks",")","`","values","can","either","be","class-labels","or","probabilities","of","the","positive","class","for","binary","classification","problems",".","If","a","regression","problem",",","must","be","of","shape","`","(","N",",",")","`","or","`","(","N",",","n_tasks",")","`","or","`","(","N",",","n_tasks",",","1",")","`","if","a","regression","metric",".","y_pred",":","ArrayLike","An","ArrayLike","containing","predicted","values","for","each","task",".","Must","be","of","shape","`","(","N",",","n_tasks",",","n_classes",")","`","if","a","classification","metric",",","else","must","be","of","shape","`","(","N",",","n_tasks",")","`","if","a","regression","metric",".","w",":","ArrayLike",",","default","None","An","ArrayLike","containing","weights","for","each","datapoint",".","If","specified",",","must","be","of","shape","`","(","N",",","n_tasks",")","`",".","n_tasks",":","int",",","default","None","The","number","of","tasks","this","class","is","expected","to","handle",".","n_classes",":","int",",","default","2","Number","of","classes","in","data","for","classification","tasks",".","per_task_metrics",":","bool",",","default","False","If","true",",","return","computed","metric","for","each","task","on","multitask","dataset",".","use_sample_weights",":","bool",",","default","False","If","set",",","use","per-sample","weights","`","w","`",".","kwargs",":","dict","Will","be","passed","on","to","self.metric","Returns","--","--","--","-","np.ndarray","A","numpy","array","containing","metric","values","for","each","task","."]
code: def compute_metric(
      self,
      y_true: ArrayLike,
      y_pred: ArrayLike,
      w: Optional[ArrayLike] = None,
      n_tasks: Optional[int] = None,
      n_classes: int = 2,
      per_task_metrics: bool = False,
      use_sample_weights: bool = False,
      **kwargs,
  ) -> Any:
      """Compute a performance metric for each task.
  
      Parameters
      ----------
      y_true: ArrayLike
          An ArrayLike containing true values for each task. Must be of shape
          `(N,)` or `(N, n_tasks)` or `(N, n_tasks, n_classes)` if a
          classification metric. If of shape `(N, n_tasks)` values can either be
          class-labels or probabilities of the positive class for binary
          classification problems. If a regression problem, must be of shape
          `(N,)` or `(N, n_tasks)` or `(N, n_tasks, 1)` if a regression metric.
      y_pred: ArrayLike
          An ArrayLike containing predicted values for each task. Must be
          of shape `(N, n_tasks, n_classes)` if a classification metric,
          else must be of shape `(N, n_tasks)` if a regression metric.
      w: ArrayLike, default None
          An ArrayLike containing weights for each datapoint. If
          specified,  must be of shape `(N, n_tasks)`.
      n_tasks: int, default None
          The number of tasks this class is expected to handle.
      n_classes: int, default 2
          Number of classes in data for classification tasks.
      per_task_metrics: bool, default False
          If true, return computed metric for each task on multitask dataset.
      use_sample_weights: bool, default False
          If set, use per-sample weights `w`.
      kwargs: dict
          Will be passed on to self.metric
  
      Returns
      -------
      np.ndarray
          A numpy array containing metric values for each task.
      """
      # Attempt some limited shape imputation to find n_tasks
      y_true_arr = np.asarray(y_true)
      y_pred_arr = np.asarray(y_pred)
      if n_tasks is None:
          if self.n_tasks is None and isinstance(
              y_true_arr, np.ndarray
          ):
              if len(y_true_arr.shape) == 1:
                  n_tasks = 1
              elif len(y_true_arr.shape) >= 2:
                  n_tasks = y_true_arr.shape[1]
          else:
              n_tasks = self.n_tasks
      # check whether n_tasks is int or not
      # This is because `normalize_weight_shape` require int value.
      assert isinstance(n_tasks, int)
  
      y_true_arr = normalize_labels_shape(
          y_true_arr,
          mode=self.mode,
          n_tasks=n_tasks,
          n_classes=n_classes,
      )
      y_pred_arr = normalize_prediction_shape(
          y_pred_arr,
          mode=self.mode,
          n_tasks=n_tasks,
          n_classes=n_classes,
      )
      if self.mode == "classification":
          y_true_arr = handle_classification_mode(
              y_true_arr,
              self.classification_handling_mode,
              self.threshold_value,
          )
          y_pred_arr = handle_classification_mode(
              y_pred_arr,
              self.classification_handling_mode,
              self.threshold_value,
          )
      n_samples = y_true_arr.shape[0]
      w = normalize_weight_shape(
          None if w is None else np.asarray(w), n_samples, n_tasks
      )
      computed_metrics = []
      for task in range(n_tasks):
          y_task = y_true_arr[:, task]
          y_pred_arr_task = y_pred_arr[:, task]
          w_task = w[:, task]
  
          metric_value = self.compute_singletask_metric(
              y_task,
              y_pred_arr_task,
              w_task,
              use_sample_weights=use_sample_weights,
              **kwargs,
          )
          computed_metrics.append(metric_value)
      logger.info("computed_metrics: %s" % str(computed_metrics))
      if n_tasks == 1:
          # FIXME: Incompatible types in assignment
          computed_metrics = computed_metrics[0]  # type: ignore
  
      if not per_task_metrics:
          return self.task_averager(computed_metrics)
      else:
          return self.task_averager(computed_metrics), computed_metrics
  
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 2 for calling the Metric obj's underlying member method code with example usage: obj.compute_metric(y_true, y_pred, w, n_tasks, n_classes, per_task_metrics, use_sample_weights) and returns: self, self,  computed_metrics