code: def normalize_weight_shape(
      w: Optional[np.ndarray], n_samples: int, n_tasks: int
  ) -> np.ndarray:
      """A utility function to correct the shape of the weight array.
  
      This utility function is used to normalize the shapes of a given
      weight array.
  
      Parameters
      ----------
      w: np.ndarray
          `w` can be `None` or a scalar or a `np.ndarray` of shape
          `(n_samples,)` or of shape `(n_samples, n_tasks)`. If `w` is a
          scalar, it's assumed to be the same weight for all samples/tasks.
      n_samples: int
          The number of samples in the dataset. If `w` is not None, we should
          have `n_samples = w.shape[0]` if `w` is a ndarray
      n_tasks: int
          The number of tasks. If `w` is 2d ndarray, then we should have
          `w.shape[1] == n_tasks`.
  
      Examples
      --------
      >>> import numpy as np
      >>> w_out = normalize_weight_shape(None, n_samples=10, n_tasks=1)
      >>> (w_out == np.ones((10, 1))).all()
      True
  
      Returns
      -------
      w_out: np.ndarray
          Array of shape `(n_samples, n_tasks)`
      """
      if w is None:
          w_out = np.ones((n_samples, n_tasks))
      elif isinstance(w, np.ndarray):
          if len(w.shape) == 0:
              # scalar case
              w_out = w * np.ones((n_samples, n_tasks))
          elif len(w.shape) == 1:
              if len(w) != n_samples:
                  raise ValueError("Length of w isn't n_samples")
              # per-example case
              # This is a little arcane but it repeats w across tasks.
              w_out = np.tile(w, (n_tasks, 1)).T
          elif len(w.shape) == 2:
              if w.shape == (n_samples, 1):
                  # If w.shape == (n_samples, 1) handle it as 1D
                  w = np.squeeze(w, axis=1)
                  w_out = np.tile(w, (n_tasks, 1)).T
              elif w.shape != (n_samples, n_tasks):
                  raise ValueError(
                      "Shape for w doens't match (n_samples, n_tasks)"
                  )
              else:
                  # w.shape == (n_samples, n_tasks)
                  w_out = w
          else:
              raise ValueError("w must be of dimension 1, 2, or 3")
      else:
          # scalar case
          w_out = w * np.ones((n_samples, n_tasks))
      return w_out
  
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 a global function with example usage: normalize_weight_shape(w, n_samples, n_tasks) and returns: w_out