code: def main():
      parser = get_parser()
      args = parser.parse_args()
  
      logfmt = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s"
      if args.verbose > 0:
          logging.basicConfig(level=logging.INFO, format=logfmt)
      else:
          logging.basicConfig(level=logging.WARN, format=logfmt)
      logging.info(get_commandline_args())
      if len(args.reffiles) != len(args.enhfiles):
          raise RuntimeError(
              "The number of ref files are different "
              "from the enh files: {} != {}".format(
                  len(args.reffiles), len(args.enhfiles)
              )
          )
      if len(args.enhfiles) == 1:
          args.permutation = False
  
      # Read text files and created a mapping of key2filepath
      reffiles_dict = OrderedDict()  # Dict[str, Dict[str, str]]
      for ref in args.reffiles:
          d = OrderedDict()
          with open(ref, "r") as f:
              for line in f:
                  key, path = line.split(None, 1)
                  d[key] = path.rstrip()
          reffiles_dict[ref] = d
  
      enhfiles_dict = OrderedDict()  # Dict[str, Dict[str, str]]
      for enh in args.enhfiles:
          d = OrderedDict()
          with open(enh, "r") as f:
              for line in f:
                  key, path = line.split(None, 1)
                  d[key] = path.rstrip()
          enhfiles_dict[enh] = d
  
      if args.keylist is not None:
          with open(args.keylist, "r") as f:
              keylist = [line.rstrip().split()[0] for line in f]
      else:
          keylist = list(reffiles_dict.values())[0]
  
      if len(keylist) == 0:
          raise RuntimeError("No keys are found")
  
      if not os.path.exists(args.outdir):
          os.makedirs(args.outdir)
  
      evaltypes = []
      for evaltype in args.evaltypes:
          if evaltype == "SDR":
              evaltypes += ["SDR", "ISR", "SIR", "SAR"]
          else:
              evaltypes.append(evaltype)
  
      # Open files in write mode
      writers = {
          k: open(os.path.join(args.outdir, k), "w") for k in evaltypes
      }
  
      for key in keylist:
          # 1. Load ref files
          rate_prev = None
  
          ref_signals = []
          for listname, d in reffiles_dict.items():
              if key not in d:
                  raise RuntimeError(
                      "{} doesn't exist in {}".format(key, listname)
                  )
              filepath = d[key]
              signal, rate = soundfile.read(filepath, dtype=np.int16)
              if signal.ndim == 1:
                  # (Nframe) -> (Nframe, 1)
                  signal = signal[:, None]
              ref_signals.append(signal)
              if rate_prev is not None and rate != rate_prev:
                  raise RuntimeError("Sampling rates mismatch")
              rate_prev = rate
  
          # 2. Load enh files
          enh_signals = []
          for listname, d in enhfiles_dict.items():
              if key not in d:
                  raise RuntimeError(
                      "{} doesn't exist in {}".format(key, listname)
                  )
              filepath = d[key]
              signal, rate = soundfile.read(filepath, dtype=np.int16)
              if signal.ndim == 1:
                  # (Nframe) -> (Nframe, 1)
                  signal = signal[:, None]
              enh_signals.append(signal)
              if rate_prev is not None and rate != rate_prev:
                  raise RuntimeError("Sampling rates mismatch")
              rate_prev = rate
  
          for signal in ref_signals + enh_signals:
              if signal.shape[1] != ref_signals[0].shape[1]:
                  raise RuntimeError("The number of channels mismatch")
  
          # 3. Zero padding to adjust the length to the maximum length in inputs
          ml = max(len(s) for s in ref_signals + enh_signals)
          ref_signals = [
              np.pad(s, [(0, ml - len(s)), (0, 0)], mode="constant")
              if len(s) < ml
              else s
              for s in ref_signals
          ]
  
          enh_signals = [
              np.pad(s, [(0, ml - len(s)), (0, 0)], mode="constant")
              if len(s) < ml
              else s
              for s in enh_signals
          ]
  
          # ref_signals, enh_signals: (Nsrc, Nframe, Nmic)
          ref_signals = np.stack(ref_signals, axis=0)
          enh_signals = np.stack(enh_signals, axis=0)
  
          # 4. Evaluates
          for evaltype in args.evaltypes:
              if evaltype == "SDR":
                  (sdr, isr, sir, sar, perm) = museval.metrics.bss_eval(
                      ref_signals,
                      enh_signals,
                      window=np.inf,
                      hop=np.inf,
                      compute_permutation=args.permutation,
                      filters_len=512,
                      framewise_filters=args.bss_eval_version == "v3",
                      bsseval_sources_version=not args.bss_eval_images,
                  )
  
                  # sdr: (Nsrc, Nframe)
                  writers["SDR"].write(
                      "{} {}\n".format(
                          key, " ".join(map(str, sdr[:, 0]))
                      )
                  )
                  writers["ISR"].write(
                      "{} {}\n".format(
                          key, " ".join(map(str, isr[:, 0]))
                      )
                  )
                  writers["SIR"].write(
                      "{} {}\n".format(
                          key, " ".join(map(str, sir[:, 0]))
                      )
                  )
                  writers["SAR"].write(
                      "{} {}\n".format(
                          key, " ".join(map(str, sar[:, 0]))
                      )
                  )
  
              elif evaltype == "STOI":
                  stoi, perm = eval_STOI(
                      ref_signals,
                      enh_signals,
                      rate,
                      extended=False,
                      compute_permutation=args.permutation,
                  )
                  writers["STOI"].write(
                      "{} {}\n".format(key, " ".join(map(str, stoi)))
                  )
  
              elif evaltype == "ESTOI":
                  estoi, perm = eval_STOI(
                      ref_signals,
                      enh_signals,
                      rate,
                      extended=True,
                      compute_permutation=args.permutation,
                  )
                  writers["ESTOI"].write(
                      "{} {}\n".format(key, " ".join(map(str, estoi)))
                  )
  
              elif evaltype == "PESQ":
                  pesq, perm = eval_PESQ(
                      ref_signals,
                      enh_signals,
                      rate,
                      compute_permutation=args.permutation,
                  )
                  writers["PESQ"].write(
                      "{} {}\n".format(key, " ".join(map(str, pesq)))
                  )
              else:
                  # Cannot reach
                  raise RuntimeError
  
imports: import argparse
  import itertools
  import logging
  import os
  import shutil
  import subprocess
  import sys
  import warnings
  from collections import OrderedDict
  from distutils.util import strtobool
  from pathlib import Path
  from tempfile import TemporaryDirectory
  import museval
  import numpy
  import soundfile
  from pystoi.stoi import stoi
  from espnet.utils.cli_utils import get_commandline_args
label_desc: Use image node_id 4 for calling a global function with example usage: main() without return types