def _load_best_model(dpath): """Return best model amongst models in <dpath>.""" mdlpath = _path_to_best_model(dpath) if not mdlpath: log.critical("No best model fount at \"{0}\"".format(dpath)) return load_model(mdlpath)
def _read(fpath): """Read content of numpy archive file at <fpath>.""" if not pth.__is_file(fpath): log.critical( "Numpy file \"{0}\" not found, cannot read data".format(fpath)) log.debug("Reading data from \"{0}\"".format(fpath)) return np.load(fpath)
def extract(fpath, dpath): """Extract archive at <fpath> into <dpath>.""" log.debug("Extracting \"{0}\" into \"{1}\"".format(fpath, dpath)) try: unpack_archive(fpath, dpath) except ValueError: log.critical("\"{0}\" not a valid archive".format(fpath)) pth.__remove_file(fpath)
def _load(fpath): """Get content of json file at <fpath>.""" if not pth.__is_file(fpath): log.critical( "\"{0}\" doesn\'t exist, can\'t load data from it".format(fpath)) log.debug("Loading data from \"{0}\"".format(fpath)) fjson = pth.__open_file(fpath) ret = json.load(fjson) pth.__close_file(fjson) return ret
def _export(npy_arrays, outdpath=None, override=True): """Save a list of numpy arrays as wave files at <outdpath>. If <outdpath> is None, create a random directory. If <oudtpath> doesn't exist, it creates it. """ if outdpath is None: outdpath = mkrdir() elif pth.__is_file(outdpath): log.critical( "Can't export songs in {0} since it is a file".format(outdpath)) elif not pth.__exists(outdpath): pth.__make_dir(outdpath) for idx, npy_array in enumerate(npy_arrays): _save(npy_array, rfname(path=outdpath, prefix='{0}_'.format(idx)), override)
def download(furl, dpath='.'): """Download file at <furl> and write it at <dpath>. Return path to extracted file. """ log.debug("Downloading file from \"{0}\" into \"{1}\"".format(furl, dpath)) response = req.get(furl) if response.status_code != req.codes.ok: if response.status_code == HTTP.NOT_FOUND: log.critical(''.join(['File not found at: ', furl])) else: log.critical(''.join( ['Error code: ', response.status_code, ', getting: ', furl])) fpath = pth.__join_path(dpath, pth.__file_name(furl)) pth.__write_file(fpath, response.content) return fpath
def apply_fx(dry, fx, func=convolve): """Apply an fx to a dry signal. Return the resulting signal. """ if func is None: func = convolve n_params = n_parameters(func) if n_params == -1: log.critical("A function is needed to apply fx") elif n_params != 2: log.critical( "\'{0}\' function doesn't take exactly two arguments, can't apply fx" .format(func.__name__)) if dry.frame_count() == 0: log.warning("Applying fx to an empty signal") return func(dry, fx)
def filter_elements(dpath, cond_lst_lst): """Remove songs at <dpath> that don't fit condition functions in <cond_lst_lst>. <cond_lst_lst> is a list of condition lists, so that global condition is an intersection of unions. """ if not isinstance(cond_lst_lst, list) or not all( map(isinstance, cond_lst_lst, repeat(list))): log.critical( "Please use a list of list of conditions to filter samples") jsnpaths = list(filter(__is_json_file, pth.__list_files(dpath))) if len(jsnpaths) != 1: log.critical( "There should be exactly one json file at \"{0}\"".format(dpath)) data = jsn._load(jsnpaths[0]) audio_fnames = __list_audio_files(dpath) for afile in audio_fnames: if not all( map( lambda cond_lst: any( map(lambda cond: cond(data[pth.__no_extension(afile)]), cond_lst)), cond_lst_lst)): pth.__remove_file(afile)