code: def main():
      parser = get_parser()
      args = parser.parse_args()
  
      source_path = osp.join(args.source, args.split)
      cluster_path = osp.join(args.cluster_dir, args.split + ".cluster")
  
      print(f"data path: {source_path}")
  
      features = np.load(source_path + ".npy", mmap_mode="r")
      sizes = []
      offsets = []
      offset = 0
      with open(source_path + ".lengths", "r") as len_f:
          for line in len_f.readlines():
              utt_id, length = line.split()
              length = int(length)
              sizes.append(length)
              offsets.append(offset)
              offset += length
  
      clusters = []
      with open(cluster_path, "r") as cf:
          for line in cf.readlines():
              line_list = line.split()
              items = line_list[1:]
              items = list(map(int, items))
              clusters.append(items)
  
      os.makedirs(args.save_dir, exist_ok=True)
      save_path = osp.join(args.save_dir, args.split)
  
      if osp.exists(save_path + ".npy"):
          os.remove(save_path + ".npy")
      npaa = NpyAppendArray(save_path + ".npy")
  
      def merge(feats, clust):
          feats = torch.from_numpy(feats.copy())
          clust = torch.LongTensor(clust)
          _, counts = clust.unique_consecutive(return_counts=True)
          curr = 0
  
          merged = []
          for c in counts:
              c = c.item()
              start = curr
              end = curr + c
              curr += c
              if args.pooling == "mean":
                  new_x = feats[start:end].mean(dim=0)
              elif args.pooling == "sample":
                  new_x = feats[start + int(random.random() * c)]
              else:
                  raise NotImplementedError()
              merged.append(new_x)
  
          return torch.stack(merged, dim=0).numpy()
  
      with open(save_path + ".lengths_pure", "w") as l_f:
          for size, offset, clust in tqdm.tqdm(
              zip(sizes, offsets, clusters), total=len(sizes)
          ):
              end = size + offset
              feats = features[offset:end]
              feats = merge(feats, clust)
              print(len(feats), file=l_f)
              npaa.append(feats)
  
file_path: espnet/egs2/acesinger/svs1/pyscripts/feats/merge_clusters.py
imports: import argparse
  import os
  import os.path
  import random
  from shutil import copyfile
  import numpy
  import torch
  import tqdm
  from npy_append_array import NpyAppendArray
label_desc: Use image node_id 2 for calling a global function with example usage: main() and returns: torch