code: def velocity_confidence(data, vkey='velocity', copy=False):
      
      adata = data.copy() if copy else data
      if vkey not in adata.layers.keys():
          raise ValueError(
              'You need to run `tl.velocity` first.')
  
      idx = np.array(adata.var[vkey + '_genes'].values, dtype=bool)
      X, V = adata.layers['Ms'][:, idx].copy(), adata.layers[vkey][:, idx].copy()
      indices = get_indices(dist=adata.uns['neighbors']['distances'])[0]
  
      V -= V.mean(1)[:, None]
      V_norm = norm(V)
      R = np.zeros(adata.n_obs)
  
      for i in range(adata.n_obs):
          Vi_neighs = V[indices[i]]
          Vi_neighs -= Vi_neighs.mean(1)[:, None]
          R[i] = np.mean(np.einsum('ij, j', Vi_neighs, V[i]) / (norm(Vi_neighs) * V_norm[i])[None, :])
  
      adata.obs[vkey + '_length'] = V_norm.round(2)
      adata.obs[vkey + '_confidence'] = R
  
      logg.hint('added \'' + vkey + '_confidence\' (adata.obs)')
  
      if vkey + '_confidence_transition' not in adata.obs.keys():
          velocity_confidence_transition(adata, vkey)
  
      return adata if copy else None
docs: Computes confidences of velocities.
  
      Arguments
      ---------
      data: :class:`~anndata.AnnData`
          Annotated data matrix.
      vkey: `str` (default: `'velocity'`)
          Name of velocity estimates to be used.
      copy: `bool` (default: `False`)
          Return a copy instead of writing to adata.
  
      Returns
      -------
      Returns or updates `adata` with the attributes
      velocity_length: `.obs`
          Length of the velocity vectors for each individual cell
      velocity_confidence: `.obs`
          Confidence for each cell