docstr: This script calculates the SDR and SI-SNR metrics
  and stores them in a CSV file. As this evaluation
  method depends on a gold-standard reference signal,
  it is applied exclusively to the valid set and excludes
  the baseline data.
docstr_tok: ["This","script","calculates","the","SDR","and","SI-SNR","metrics","and","stores","them","in","a","CSV","file",".","As","this","evaluation","method","depends","on","a","gold-standard","reference","signal",",","it","is","applied","exclusively","to","the","valid","set","and","excludes","the","baseline","data","."]
code: def save_results(self, valid_data):
      """This script calculates the SDR and SI-SNR metrics
      and stores them in a CSV file. As this evaluation
      method depends on a gold-standard reference signal,
      it is applied exclusively to the valid set and excludes
      the baseline data.
      """
      # This package is required for SDR computation
      from mir_eval.separation import bss_eval_sources
  
      # Create folders where to store audio
      save_file = os.path.join(
          self.hparams.output_folder, "valid_results.csv"
      )
  
      # Variable init
      all_sdrs = []
      all_sdrs_i = []
      all_sisnrs = []
      all_sisnrs_i = []
      all_pesqs = []
      all_stois = []
      all_csigs = []
      all_cbaks = []
      all_covls = []
      csv_columns = [
          "snt_id",
          "sdr",
          "sdr_i",
          "si-snr",
          "si-snr_i",
          "pesq",
          "stoi",
          "csig",
          "cbak",
          "covl",
      ]
  
      valid_loader = sb.dataio.dataloader.make_dataloader(
          valid_data, **self.hparams.dataloader_opts_test
      )
  
      with open(save_file, "w") as results_csv:
          writer = csv.DictWriter(results_csv, fieldnames=csv_columns)
          writer.writeheader()
  
          # Loop over all test sentence
          with tqdm(valid_loader, dynamic_ncols=True) as t:
              for i, batch in enumerate(t):
                  # Apply Enhancement
                  noisy, noisy_len = batch.noisy_sig
                  snt_id = batch.id
                  clean = batch.clean_sig
  
                  with torch.no_grad():
                      predictions, clean = self.compute_forward(
                          batch.noisy_sig, clean, sb.Stage.TEST
                      )
  
                  # Compute PESQ
                  psq_mode = (
                      "wb"
                      if self.hparams.sample_rate == 16000
                      else "nb"
                  )
  
                  try:
                      # Compute SI-SNR
                      sisnr = self.compute_objectives(
                          predictions, clean
                      )
  
                      # Compute SI-SNR improvement
                      noisy_signal = noisy
  
                      noisy_signal = noisy_signal.to(clean.device)
                      sisnr_baseline = self.compute_objectives(
                          [noisy_signal.squeeze(-1), None], clean
                      )
                      sisnr_i = sisnr - sisnr_baseline
  
                      # Compute SDR
                      sdr, _, _, _ = bss_eval_sources(
                          clean[0].t().cpu().numpy(),
                          predictions[0][0].t().detach().cpu().numpy(),
                      )
  
                      sdr_baseline, _, _, _ = bss_eval_sources(
                          clean[0].t().cpu().numpy(),
                          noisy_signal[0].t().detach().cpu().numpy(),
                      )
  
                      sdr_i = sdr.mean() - sdr_baseline.mean()
  
                      # Compute PESQ
                      psq = pesq(
                          self.hparams.sample_rate,
                          clean.squeeze().cpu().numpy(),
                          predictions[0].squeeze().cpu().numpy(),
                          mode=psq_mode,
                      )
                      # Compute STOI
                      stoi_score = stoi(
                          clean.squeeze().cpu().numpy(),
                          predictions[0].squeeze().cpu().numpy(),
                          fs_sig=self.hparams.sample_rate,
                          extended=False,
                      )
                      # Compute CSIG, CBAK, COVL
                      composite_metrics = eval_composite(
                          clean.squeeze().cpu().numpy(),
                          predictions[0].squeeze().cpu().numpy(),
                          self.hparams.sample_rate,
                      )
                  except Exception:
                      # this handles all sorts of error that may
                      # occur when evaluating an enhanced file.
                      continue
  
                  # Saving on a csv file
                  row = {
                      "snt_id": snt_id[0],
                      "sdr": sdr.mean(),
                      "sdr_i": sdr_i,
                      "si-snr": -sisnr.item(),
                      "si-snr_i": -sisnr_i.item(),
                      "pesq": psq,
                      "stoi": stoi_score,
                      "csig": composite_metrics["csig"],
                      "cbak": composite_metrics["cbak"],
                      "covl": composite_metrics["covl"],
                  }
                  writer.writerow(row)
  
                  # Metric Accumulation
                  all_sdrs.append(sdr.mean())
                  all_sdrs_i.append(sdr_i.mean())
                  all_sisnrs.append(-sisnr.item())
                  all_sisnrs_i.append(-sisnr_i.item())
                  all_pesqs.append(psq)
                  all_stois.append(stoi_score)
                  all_csigs.append(composite_metrics["csig"])
                  all_cbaks.append(composite_metrics["cbak"])
                  all_covls.append(composite_metrics["covl"])
  
              row = {
                  "snt_id": "avg",
                  "sdr": np.array(all_sdrs).mean(),
                  "sdr_i": np.array(all_sdrs_i).mean(),
                  "si-snr": np.array(all_sisnrs).mean(),
                  "si-snr_i": np.array(all_sisnrs_i).mean(),
                  "pesq": np.array(all_pesqs).mean(),
                  "stoi": np.array(all_stois).mean(),
                  "csig": np.array(all_csigs).mean(),
                  "cbak": np.array(all_cbaks).mean(),
                  "covl": np.array(all_covls).mean(),
              }
              writer.writerow(row)
  
      logger.info(
          "Mean SISNR is {}".format(np.array(all_sisnrs).mean())
      )
      logger.info(
          "Mean SISNRi is {}".format(np.array(all_sisnrs_i).mean())
      )
      logger.info("Mean SDR is {}".format(np.array(all_sdrs).mean()))
      logger.info("Mean SDRi is {}".format(np.array(all_sdrs_i).mean()))
      logger.info("Mean PESQ {}".format(np.array(all_pesqs).mean()))
      logger.info("Mean STOI {}".format(np.array(all_stois).mean()))
      logger.info("Mean CSIG {}".format(np.array(all_csigs).mean()))
      logger.info("Mean CBAK {}".format(np.array(all_cbaks).mean()))
      logger.info("Mean COVL {}".format(np.array(all_covls).mean()))
  
imports: import os
  import glob
  import sys
  import csv
  import json
  import logging
  import numpy
  from tqdm import tqdm
  from typing import Dict
  from functools import partial
  import torch
  import torchaudio
  import braceexpand
  import webdataset
  import torch.nn.functional
  import speechbrain
  from hyperpyyaml import load_hyperpyyaml
  from composite_eval import eval_composite
  import speechbrain.nnet.schedulers
  from speechbrain.utils.distributed import run_on_main
  from speechbrain.utils.metric_stats import MetricStats
  from speechbrain.processing.features import spectral_magnitude
  from speechbrain.dataio.batch import PaddedBatch
  from speechbrain.core import AMPConfig
  from pesq import pesq
  from pystoi import stoi
label_desc: Use image node_id 11 for calling the Enhancement obj's underlying member method code with example usage: obj.save_results(valid_data) without return types