code: def average_sphere(image, center, radius, weighted=True, ret_crop=False):
      
      sx, sy = image.shape
      x = np.arange(sx).reshape(-1, 1)
      y = np.arange(sy).reshape(1, -1)
      discsq = ((x - center[0])**2 + (y - center[1])**2)
      root = radius**2 - discsq
      # height of the cell for each x and y
      h = 2 * np.sqrt(root * (root > 0))
      # compute phase density
      rho = np.zeros(image.shape)
      hbin = h != 0
      # phase density [rad/px]
      rho[hbin] = image[hbin] / h[hbin]
      if weighted:
          # compute weighted average
          average = np.sum(rho * h) / np.sum(h)
      else:
          # compute simple average
          average = np.sum(rho) / np.sum(hbin)
  
      ret = average
      if ret_crop:
          ret = (ret, rho)
      return ret
docs: Compute the weighted average phase from a phase image of a sphere
  
      Parameters
      ----------
      image: 2d ndarray
          Quantitative phase image of a sphere
      center: tuble (x,y)
          Center of the sphere in `image` in ndarray coordinates
      radius: float
          Radius of the sphere in pixels
      weighted: bool
          If `True`, return average phase density weighted with the
          height profile obtained from the radius, otherwise return
          simple average phase density. Weighting gives data points
          at the center of the sphere more weight than those points
          at the boundary of the sphere, avoiding edge artifacts.
      ret_crop: bool
          Return the cropped image.
  
      Returns
      -------
      average: float
          The average phase value of the sphere from which the refractive
          index can be computed
      cropped_image: 2d ndarray
          Returned if `ret_crop` is True