code: def diarize_dataset(
      full_meta, split_type, n_lambdas, pval, n_neighbors=10
  ):
      """This function diarizes all the recordings in a given dataset. It performs
      computation of embedding and clusters them using spectral clustering (or other backends).
      The output speaker boundary file is stored in the RTTM format.
      """
  
      # Prepare `spkr_info` only once when Oracle num of speakers is selected.
      # spkr_info is essential to obtain number of speakers from groundtruth.
      if params["oracle_n_spkrs"] is True:
          full_ref_rttm_file = (
              params["ref_rttm_dir"]
              + "/fullref_ami_"
              + split_type
              + ".rttm"
          )
  
          rttm = diar.read_rttm(full_ref_rttm_file)
  
          spkr_info = list(  # noqa F841
              filter(lambda x: x.startswith("SPKR-INFO"), rttm)
          )
  
      # Get all the recording IDs in this dataset.
      all_keys = full_meta.keys()
      A = [word.rstrip().split("_")[0] for word in all_keys]
      all_rec_ids = list(set(A[1:]))
      all_rec_ids.sort()
      split = "AMI_" + split_type
      i = 1
  
      # Setting eval modality.
      params["embedding_model"].eval()
      msg = "Diarizing " + split_type + " set"
      logger.info(msg)
  
      if len(all_rec_ids) <= 0:
          msg = "No recording IDs found! Please check if meta_data json file is properly generated."
          raise ValueError(msg)
  
      # Diarizing different recordings in a dataset.
      for rec_id in tqdm(all_rec_ids):
          # This tag will be displayed in the log.
          tag = (
              "["
              + str(split_type)
              + ": "
              + str(i)
              + "/"
              + str(len(all_rec_ids))
              + "]"
          )
          i = i + 1
  
          # Log message.
          msg = "Diarizing %s : %s " % (tag, rec_id)
          logger.debug(msg)
  
          # Embedding directory.
          if not os.path.exists(
              os.path.join(params["embedding_dir"], split)
          ):
              os.makedirs(os.path.join(params["embedding_dir"], split))
  
          # File to store embeddings.
          emb_file_name = (
              rec_id + "." + params["mic_type"] + ".emb_stat.pkl"
          )
          diary_stat_emb_file = os.path.join(
              params["embedding_dir"], split, emb_file_name
          )
  
          # Prepare a metadata (json) for one recording. This is basically a subset of full_meta.
          # Lets keep this meta-info in embedding directory itself.
          json_file_name = rec_id + "." + params["mic_type"] + ".json"
          meta_per_rec_file = os.path.join(
              params["embedding_dir"], split, json_file_name
          )
  
          # Write subset (meta for one recording) json metadata.
          prepare_subset_json(full_meta, rec_id, meta_per_rec_file)
  
          # Prepare data loader.
          diary_set_loader = dataio_prep(params, meta_per_rec_file)
  
          # Putting modules on the device.
          params["compute_features"].to(run_opts["device"])
          params["mean_var_norm"].to(run_opts["device"])
          params["embedding_model"].to(run_opts["device"])
          params["mean_var_norm_emb"].to(run_opts["device"])
  
          # Compute Embeddings.
          diary_obj = embedding_computation_loop(
              "diary", diary_set_loader, diary_stat_emb_file
          )
  
          # Adding tag for directory path.
          type_of_num_spkr = (
              "oracle" if params["oracle_n_spkrs"] else "est"
          )
          tag = (
              type_of_num_spkr
              + "_"
              + str(params["affinity"])
              + "_"
              + params["backend"]
          )
          out_rttm_dir = os.path.join(
              params["sys_rttm_dir"], params["mic_type"], split, tag
          )
          if not os.path.exists(out_rttm_dir):
              os.makedirs(out_rttm_dir)
          out_rttm_file = out_rttm_dir + "/" + rec_id + ".rttm"
  
          # Processing starts from here.
          if params["oracle_n_spkrs"] is True:
              # Oracle num of speakers.
              num_spkrs = diar.get_oracle_num_spkrs(rec_id, spkr_info)
          else:
              if params["affinity"] == "nn":
                  # Num of speakers tunned on dev set (only for nn affinity).
                  num_spkrs = n_lambdas
              else:
                  # Num of speakers will be estimated using max eigen gap for cos based affinity.
                  # So adding None here. Will use this None later-on.
                  num_spkrs = None
  
          if params["backend"] == "kmeans":
              diar.do_kmeans_clustering(
                  diary_obj,
                  out_rttm_file,
                  rec_id,
                  num_spkrs,
                  pval,
              )
  
          if params["backend"] == "SC":
              # Go for Spectral Clustering (SC).
              diar.do_spec_clustering(
                  diary_obj,
                  out_rttm_file,
                  rec_id,
                  num_spkrs,
                  pval,
                  params["affinity"],
                  n_neighbors,
              )
  
          # Can used for AHC later. Likewise one can add different backends here.
          if params["backend"] == "AHC":
              # call AHC
              threshold = pval  # pval for AHC is nothing but threshold.
              diar.do_AHC(
                  diary_obj, out_rttm_file, rec_id, num_spkrs, threshold
              )
  
      # Once all RTTM outputs are generated, concatenate individual RTTM files to obtain single RTTM file.
      # This is not needed but just staying with the standards.
      concate_rttm_file = out_rttm_dir + "/sys_output.rttm"
      logger.debug("Concatenating individual RTTM files...")
      with open(concate_rttm_file, "w") as cat_file:
          for f in glob.glob(out_rttm_dir + "/*.rttm"):
              if f == concate_rttm_file:
                  continue
              with open(f, "r") as indi_rttm_file:
                  shutil.copyfileobj(indi_rttm_file, cat_file)
  
      msg = "The system generated RTTM file for %s set : %s" % (
          split_type,
          concate_rttm_file,
      )
      logger.debug(msg)
  
      return concate_rttm_file
  
imports: import os
  import sys
  import torch
  import logging
  import pickle
  import json
  import glob
  import shutil
  import numpy
  import speechbrain
  from tqdm.contrib import tqdm
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.utils.distributed import run_on_main
  from speechbrain.processing.PLDA_LDA import StatObject_SB
  from speechbrain.processing import diarization
  from speechbrain.utils.DER import DER
  from speechbrain.dataio.dataio import read_audio
  from speechbrain.dataio.dataio import read_audio_multichannel
label_desc: Use image node_id 4 for calling a global function with example usage: diarize_dataset(full_meta, split_type, n_lambdas, pval, n_neighbors) and returns: concate_rttm_file