def _reader(rindices, rcache, shape, lastread, readevent, writeevent, filename, dsname, willread): """ this function will run in separete processes to read the data into the cache """ try: import mkl mkl.set_num_threads_local(1) except ImportError: pass if willread is not None: readall = False readindices = _np.nonzero(willread)[0] inextwrite = 0 else: readall = True nextwrite = 0 with _h5py.File(filename, 'r') as file: dataset = file[dsname] n = dataset.shape[0] indices = _np.frombuffer(rindices, _np.int64) cache = _np.frombuffer(rcache, dataset.dtype).reshape(shape) while True: if lastread.value > n: return empty = _np.where(indices < (lastread.value - 1))[0] nempty = len(empty) if nempty == 0: if not readevent.wait(0.1): writeevent.set() readevent.clear() continue if readall: start = max(nextwrite, lastread.value) if start >= n: return if start + nempty > n - 1: nempty = n - start empty = empty[:nempty] nextwrite = start + nempty read = dataset[start:nextwrite, ...] cache[empty, ...] = read indices[empty] = _np.arange(start, nextwrite) writeevent.set() else: if readindices[inextwrite] < lastread.value: inextwrite = _np.argmax(readindices > lastread.value) if inextwrite + nempty > readindices.shape[0]: nempty = readindices.shape[0] - inextwrite empty = empty[:nempty] readids = readindices[inextwrite:inextwrite + nempty] read = dataset[readids, ...] cache[empty, ...] = read indices[empty] = readids inextwrite = inextwrite + nempty writeevent.set() if inextwrite == readindices.shape[0]: return
def test_set_num_threads_local(self): mkl.set_num_threads(1) status = mkl.set_num_threads_local(2) assert (status == 'global_num_threads') status = mkl.set_num_threads_local(4) assert (status == 2) status = mkl.set_num_threads_local(0) assert (status == 4) status = mkl.set_num_threads_local(8) assert (status == 'global_num_threads')
def __enter__(self): try: self.prev_num_threads = mkl.set_num_threads_local(self.n_threads) except: raise ValueError( "Class argument {} result in invalid number of threads {}". format(self.workers, self.n_threads))
from CME import CME, Gillespie, CompleteObservations, Observations_grid import matplotlib.pyplot as plt import scipy.integrate import tt.amen import timeit import sys import scipy.interpolate import scipy.stats from mpl_toolkits import mplot3d from ttInt import ttInt from mcmc import * import pickle import datetime import mkl mkl.set_num_threads_local(32) def lagrange(x, i, xm): """ Evaluates the i-th Lagrange polynomial at x based on grid data xm """ n = len(xm) - 1 y = 1 for j in range(n + 1): if i != j: y *= (x - xm[j]) / (xm[i] - xm[j]) return y
def __exit__(self, *args): # restore old value mkl.set_num_threads_local(self.prev_num_threads)
def process(args, config): n_channels, room_id, bss_algo = args # the name of the algorithm we'll use for bss bss_algo_name = config["bss_algorithms"][bss_algo]["name"] if mkl_available: mkl.set_num_threads_local(1) ref_mic = config["ref_mic"] metadata_fn = Path(config["metadata_fn"]) dataset_dir = metadata_fn.parent with open(config["metadata_fn"], "r") as f: metadata = json.load(f) rooms = metadata[f"{n_channels}_channels"] # the mixtures fn_mix = dataset_dir / rooms[room_id]["mix_filename"] fs, mix = load_audio(fn_mix) # add the noise sigma_src = np.std(mix) sigma_n = sigma_src * 10**(-config["snr"] / 20) mix += np.random.randn(*mix.shape) * sigma_n # the reference if bss_algo_name in dereverb_algos: # for dereverberation algorithms we use the anechoic reference signal fn_ref = dataset_dir / rooms[room_id]["anechoic_filenames"][ config["ref_mic"]] else: fn_ref = dataset_dir / rooms[room_id]["src_filenames"][ config["ref_mic"]] fs, ref = load_audio(fn_ref) # STFT parameters nfft = config["stft"]["nfft"] hop = config["stft"]["hop"] if config["stft"]["window"] == "hamming": win_a = pra.hamming(nfft) win_s = pra.transform.stft.compute_synthesis_window(win_a, hop) else: raise ValueError("Window not implemented") # STFT X = stft.analysis(mix, nfft, hop, win=win_a) # Separation bss_kwargs = config["bss_algorithms"][bss_algo]["kwargs"] n_iter_p_ch = config["bss_algorithms"][bss_algo]["n_iter_per_channel"] runtime_bss = time.perf_counter() if bss_algo_name == "fastmnmf": Y = bss_algorithms[bss_algo_name](X, n_iter=n_iter_p_ch * n_channels, **bss_kwargs) elif bss_algo_name in dereverb_algos: Y, Y_pb, runtime_pb = bss_algorithms[bss_algo_name]( X, n_iter=n_iter_p_ch * n_channels, proj_back_both=True, **bss_kwargs) # adjust start time to remove the projection back runtime_bss += runtime_pb else: Y = bss_algorithms[bss_algo_name](X, n_iter=n_iter_p_ch * n_channels, proj_back=False, **bss_kwargs) runtime_bss = time.perf_counter() - runtime_bss results = [{ "bss_runtime": { "bss_algo": bss_algo, "runtime": runtime_bss, } }] t = { "room_id": room_id, "n_channels": n_channels, "bss_algo": bss_algo, "proj_algo": None, "runtime": 0.0, "sdr": None, "sir": None, "p": None, "q": None, "n_iter": 1, } # Evaluation of raw signal t["proj_algo"] = "None" y, sdr, sir, _ = reconstruct_evaluate(ref, Y, nfft, hop, win=win_s, si_metric=config["si_metric"]) t["sdr"], t["sir"] = sdr.tolist(), sir.tolist() results.append(t.copy()) # projection back t["proj_algo"] = "projection_back" if bss_algo in dereverb_algos: Z = Y_pb else: runtime_pb = time.perf_counter() Z = bss_scale.projection_back(Y, X[:, :, ref_mic]) runtime_pb = time.perf_counter() - runtime_pb y, sdr, sir, _ = reconstruct_evaluate(ref, Z, nfft, hop, win=win_s, si_metric=config["si_metric"]) t["sdr"], t["sir"] = sdr.tolist(), sir.tolist() t["runtime"] = runtime_pb results.append(t.copy()) # minimum distortion lo, hi, n_steps = config["minimum_distortion"]["p_list"] p_vals = np.linspace(lo, hi, n_steps) kwargs = config["minimum_distortion"]["kwargs"] for ip, p in enumerate(p_vals): for q in p_vals[ip:]: t["p"], t["q"], t["proj_algo"] = ( f"{p:.1f}", f"{q:.1f}", "minimum_distortion", ) runtime_md = time.perf_counter() Z, t["n_iter"] = bss_scale.minimum_distortion(Y, X[:, :, ref_mic], p=p, q=q, **kwargs) runtime_md = time.perf_counter() - runtime_md y, sdr, sir, _ = reconstruct_evaluate( ref, Z, nfft, hop, win=win_s, si_metric=config["si_metric"]) t["sdr"] = sdr.tolist() t["sir"] = sir.tolist() t["runtime"] = runtime_md results.append(t.copy()) return results