Path: blob/main/modules/parallel_wavegan/utils/utils.py
694 views
# -*- coding: utf-8 -*-12# Copyright 2019 Tomoki Hayashi3# MIT License (https://opensource.org/licenses/MIT)45"""Utility functions."""67import fnmatch8import logging9import os10import sys1112import h5py13import numpy as np141516def find_files(root_dir, query="*.wav", include_root_dir=True):17"""Find files recursively.1819Args:20root_dir (str): Root root_dir to find.21query (str): Query to find.22include_root_dir (bool): If False, root_dir name is not included.2324Returns:25list: List of found filenames.2627"""28files = []29for root, dirnames, filenames in os.walk(root_dir, followlinks=True):30for filename in fnmatch.filter(filenames, query):31files.append(os.path.join(root, filename))32if not include_root_dir:33files = [file_.replace(root_dir + "/", "") for file_ in files]3435return files363738def read_hdf5(hdf5_name, hdf5_path):39"""Read hdf5 dataset.4041Args:42hdf5_name (str): Filename of hdf5 file.43hdf5_path (str): Dataset name in hdf5 file.4445Return:46any: Dataset values.4748"""49if not os.path.exists(hdf5_name):50logging.error(f"There is no such a hdf5 file ({hdf5_name}).")51sys.exit(1)5253hdf5_file = h5py.File(hdf5_name, "r")5455if hdf5_path not in hdf5_file:56logging.error(f"There is no such a data in hdf5 file. ({hdf5_path})")57sys.exit(1)5859hdf5_data = hdf5_file[hdf5_path][()]60hdf5_file.close()6162return hdf5_data636465def write_hdf5(hdf5_name, hdf5_path, write_data, is_overwrite=True):66"""Write dataset to hdf5.6768Args:69hdf5_name (str): Hdf5 dataset filename.70hdf5_path (str): Dataset path in hdf5.71write_data (ndarray): Data to write.72is_overwrite (bool): Whether to overwrite dataset.7374"""75# convert to numpy array76write_data = np.array(write_data)7778# check folder existence79folder_name, _ = os.path.split(hdf5_name)80if not os.path.exists(folder_name) and len(folder_name) != 0:81os.makedirs(folder_name)8283# check hdf5 existence84if os.path.exists(hdf5_name):85# if already exists, open with r+ mode86hdf5_file = h5py.File(hdf5_name, "r+")87# check dataset existence88if hdf5_path in hdf5_file:89if is_overwrite:90logging.warning("Dataset in hdf5 file already exists. "91"recreate dataset in hdf5.")92hdf5_file.__delitem__(hdf5_path)93else:94logging.error("Dataset in hdf5 file already exists. "95"if you want to overwrite, please set is_overwrite = True.")96hdf5_file.close()97sys.exit(1)98else:99# if not exists, open with w mode100hdf5_file = h5py.File(hdf5_name, "w")101102# write data to hdf5103hdf5_file.create_dataset(hdf5_path, data=write_data)104hdf5_file.flush()105hdf5_file.close()106107108class HDF5ScpLoader(object):109"""Loader class for a fests.scp file of hdf5 file.110111Examples:112key1 /some/path/a.h5:feats113key2 /some/path/b.h5:feats114key3 /some/path/c.h5:feats115key4 /some/path/d.h5:feats116...117>>> loader = HDF5ScpLoader("hdf5.scp")118>>> array = loader["key1"]119120key1 /some/path/a.h5121key2 /some/path/b.h5122key3 /some/path/c.h5123key4 /some/path/d.h5124...125>>> loader = HDF5ScpLoader("hdf5.scp", "feats")126>>> array = loader["key1"]127128"""129130def __init__(self, feats_scp, default_hdf5_path="feats"):131"""Initialize HDF5 scp loader.132133Args:134feats_scp (str): Kaldi-style feats.scp file with hdf5 format.135default_hdf5_path (str): Path in hdf5 file. If the scp contain the info, not used.136137"""138self.default_hdf5_path = default_hdf5_path139with open(feats_scp, encoding='utf-8') as f:140lines = [line.replace("\n", "") for line in f.readlines()]141self.data = {}142for line in lines:143key, value = line.split()144self.data[key] = value145146def get_path(self, key):147"""Get hdf5 file path for a given key."""148return self.data[key]149150def __getitem__(self, key):151"""Get ndarray for a given key."""152p = self.data[key]153if ":" in p:154return read_hdf5(*p.split(":"))155else:156return read_hdf5(p, self.default_hdf5_path)157158def __len__(self):159"""Return the length of the scp file."""160return len(self.data)161162def __iter__(self):163"""Return the iterator of the scp file."""164return iter(self.data)165166def keys(self):167"""Return the keys of the scp file."""168return self.data.keys()169170171