code: def DER(gamma, ref, expected=True, xentropy=False):
      from itertools import permutations
  
      if not expected:
          # replace probabiities in gamma by zeros and ones
          hard_labels = gamma.argmax(1)
          gamma = np.zeros_like(gamma)
          gamma[range(len(gamma)), hard_labels] = 1
  
      err_mx = np.empty((ref.max() + 1, gamma.shape[1]))
      for s in range(err_mx.shape[0]):
          tmpgamma = gamma[ref == s, :]
          err_mx[s] = (-np.log(tmpgamma) if xentropy else tmpgamma).sum(
              0
          )
  
      if err_mx.shape[0] < err_mx.shape[1]:
          err_mx = err_mx.T
  
      # try all alignments (permutations) of reference and detected speaker
      # could be written in more efficient way using dynamic programing
      acc = [
          err_mx[perm[: err_mx.shape[1]], range(err_mx.shape[1])].sum()
          for perm in permutations(range(err_mx.shape[0]))
      ]
      if xentropy:
          return min(acc) / float(len(ref))
      else:
          return (len(ref) - max(acc)) / float(len(ref))
  
file_path: espnet/egs/libri_css/asr1/diarization/VB_diarization.py
imports: import numexpr
  import numpy
  import scipy.linalg
  from scipy.sparse import coo_matrix
label_desc: Use image node_id 4 for calling a global function with example usage: DER(gamma, ref, expected, xentropy) and returns: unknown, unknown