def calculate(signal1: ndarray, signal2: ndarray, fs, f0, fr, opt: dict) -> Tuple[ndarray, ndarray]: """ Calculates the biphase and biamplitude from the bispectrum using the MATLAB-packaged function. """ import biphaseWavPython import matlab package = biphaseWavPython.initialize() result = package.biphaseWavPython( matlab.double(signal1), matlab.double(signal2), fs, f0, matlab.double(list(fr)), opt, nargout=2, ) biamp, biphase = result biamp = matlab_to_numpy(biamp) biphase = matlab_to_numpy(biphase) return biamp, biphase
def _time_frequency( time_series: TimeSeries, params: TFParams ) -> Tuple[str, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray]: """ Performs a wavelet transform or windowed Fourier transform using the MATLAB-packaged libraries. :param time_series: the signal to transform :param params: the input parameters for the MATLAB package :return: the name of the input signal; the times associated with the input signal; the frequencies produced by the transform; the values of the transform itself; the amplitudes of the values of the transform; the powers of the values of the transform; the average amplitudes of the transform; and the average powers of the transform. """ # Don't move the import statements. from maths.algorithms.matlabwrappers import wft from maths.algorithms.matlabwrappers import wt if params.transform == _wft: func = wft else: func = wt transform, freq = func.calculate(time_series, params) transform = matlab_to_numpy(transform) freq = matlab_to_numpy(freq) amplitude = np.abs(transform) power = np.square(amplitude) avg_ampl, avg_pow = avg_ampl_pow(amplitude) return ( time_series.name, time_series.times, freq, transform, amplitude, power, avg_ampl, avg_pow, )
def _wt_surrogate_calc(wt_signal: ndarray, surrogate: ndarray, params: PCParams) -> ndarray: """ Calculates the phase coherence between a signal and a surrogate. :param wt_signal: the wavelet transform of the signal :param surrogate: the values of the surrogate (not the wavelet transform) :param params: the params object with parameters to pass to the wavelet transform function :return: [1D array] the wavelet phase coherence between the signal and the surrogate """ from maths.algorithms.matlabwrappers import wt transform, freq = wt.calculate(surrogate, params) wt_surrogate = matlab_to_numpy(transform) surr_avg, _ = wphcoh(wt_signal, wt_surrogate) return surr_avg
def _ridge_extraction( time_series: TimeSeries, params: REParams ) -> Tuple[str, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, Tuple[float, float], ndarray, ndarray, ndarray, ]: import ridge_extraction import matlab package = ridge_extraction.initialize() d = params.get() result = package.ridge_extraction( 1, matlab.double(time_series.signal.tolist()), params.fs, d["fmin"], d["fmax"], d["CutEdges"], d["Preprocess"], d["Wavelet"], nargout=6, ) transform, freq, iamp, iphi, ifreq, filtered_signal = result transform = matlab_to_numpy(transform) freq = matlab_to_numpy(freq) iamp = matlab_to_numpy(iamp) iamp = iamp.reshape(iamp.shape[1]) iphi = matlab_to_numpy(iphi) iphi = iphi.reshape(iphi.shape[1]) ifreq = matlab_to_numpy(ifreq) ifreq = ifreq.reshape(ifreq.shape[1]) filtered_signal = matlab_to_numpy(filtered_signal) filtered_signal = filtered_signal.reshape(filtered_signal.shape[1]) amplitude = np.abs(transform) powers = np.square(amplitude) length = len(amplitude) avg_ampl = np.empty(length, dtype=np.float64) avg_pow = np.empty(length, dtype=np.float64) for i in range(length): arr = amplitude[i] row = arr[np.isfinite(arr)] avg_ampl[i] = np.mean(row) avg_pow[i] = np.mean(np.square(row)) return ( time_series.name, time_series.times, freq, transform, amplitude, powers, avg_ampl, avg_pow, (d["fmin"], d["fmax"]), filtered_signal, iphi, ifreq, )
def _bispectrum_analysis( sig1: TimeSeries, sig2: TimeSeries, params: BAParams ) -> Tuple[str, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, dict, ]: """ Performs bispectrum analysis. :param sig1: the first signal :param sig2: the second signal :param params: the params object containing parameters for the MATLAB-packaged function :return: """ from maths.algorithms.matlabwrappers import bispec_wav_new, wav_surrogate name = sig1.name sig1 = sig1.signal sig2 = sig2.signal sig1list = sig1.tolist() sig2list = sig2.tolist() sigcheck = np.sum(np.abs(sig1 - sig2)) fs = params.fs preprocess = params.preprocess ns = params.surr_count or 0 nv = params.nv fmax = params.fmax or fs / 2 fmin = params.fmin or math.nan f0 = params.f0 or 1 params = {"nv": nv, "fmin": fmin, "fmax": fmax, "f0": f0} # Note: attempted to calculate bispec_wav_new in a process each. Did not work # due to some strange problem with the Matlab runtime. Processes would # hang at the `package.initialize()` stage for unknown reasons. if sigcheck != 0: if not preprocess: # TODO: check this block. bispxxx, _, _, _, _ = bispec_wav_new.calculate( sig1list, sig1list, fs, params) bispppp, _, _, _, _ = bispec_wav_new.calculate( sig2list, sig2list, fs, params) bispxpp, freq, amp_wt1, amp_wt2, opt = bispec_wav_new.calculate( sig1list, sig2list, fs, params) bisppxx, _, _, _, _ = bispec_wav_new.calculate( sig2list, sig1list, fs, params) size = bispxxx.shape surrxxx = zeros(size) surrppp = zeros(size) surrxpp = zeros(size) surrpxx = zeros(size) if ns > 0: for j in range(ns): surr1 = wav_surrogate.calculate(sig1list, "IAAFT2", 1) surr2 = wav_surrogate.calculate(sig2list, "IAAFT2", 1) surrxxx[:, :, j] = abs( bispec_wav_new.calculate(surr1, surr1, fs, params)[0]) surrppp[:, :, j] = abs( bispec_wav_new.calculate(surr2, surr2, fs, params)[0]) surrxpp[:, :, j] = abs( bispec_wav_new.calculate(surr1, surr2, fs, params)[0]) surrpxx[:, :, j] = abs( bispec_wav_new.calculate(surr2, surr1, fs, params)[0]) else: bispxxx, _, _, _, _ = bispec_wav_new.calculate( sig1list, sig1list, fs, params) bispppp, _, _, _, _ = bispec_wav_new.calculate( sig2list, sig2list, fs, params) bispxpp, freq, amp_wt1, amp_wt2, opt = bispec_wav_new.calculate( sig1list, sig2list, fs, params) bisppxx, _, _, _, _ = bispec_wav_new.calculate( sig2list, sig1list, fs, params) size = bispxxx.shape surrxxx = zeros(size) surrppp = zeros(size) surrxpp = zeros(size) surrpxx = zeros(size) if ns > 0: for j in range( ns): # TODO: fix. Did it work in previous versions? surr1 = wav_surrogate.calculate(sig1list, "IAAFT2", 1) surr2 = wav_surrogate.calculate(sig2list, "IAAFT2", 1) surrxxx[:, :, j] = abs( bispec_wav_new.calculate(surr1, surr1, fs, params)[0]) surrppp[:, :, j] = abs( bispec_wav_new.calculate(surr2, surr2, fs, params)[0]) surrxpp[:, :, j] = abs( bispec_wav_new.calculate(surr1, surr2, fs, params)[0]) surrpxx[:, :, j] = abs( bispec_wav_new.calculate(surr2, surr1, fs, params)[0]) else: raise Exception( "sigcheck == 0. This case is not implemented yet.") # TODO freq = matlab_to_numpy(freq) avg_amp_wt1, avg_pow_wt1 = avg_ampl_pow(amp_wt1) avg_amp_wt2, avg_pow_wt2 = avg_ampl_pow(amp_wt2) pow_wt1, pow_wt2 = np.square(amp_wt1), np.square(amp_wt2) opt["PadLR1"] = matlab_to_numpy(opt["PadLR1"]) opt["PadLR2"] = matlab_to_numpy(opt["PadLR2"]) opt["twf1"] = matlab_to_numpy(opt["twf1"]) opt["twf2"] = matlab_to_numpy(opt["twf2"]) import time print(f"Adding to queue at time={time.time():.1f} seconds.") return ( name, freq, amp_wt1, pow_wt1, avg_amp_wt1, avg_pow_wt1, amp_wt2, pow_wt2, avg_amp_wt2, avg_pow_wt2, bispxxx, bispppp, bispxpp, bisppxx, surrxxx, surrppp, surrxpp, surrpxx, opt, )
def _bispectrum_analysis( sig1: TimeSeries, sig2: TimeSeries, params: BAParams ) -> Tuple[str, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, ndarray, dict, ]: """ Performs bispectrum analysis. :param sig1: the first signal :param sig2: the second signal :param params: the params object containing parameters for the MATLAB-packaged function :return: """ from maths.algorithms.matlabwrappers import bispec_wav_new, wav_surrogate name = sig1.name sig1 = sig1.signal sig2 = sig2.signal sig1list = sig1.tolist() sig2list = sig2.tolist() # Whether the signals are unique; if a single signal was loaded, it would have been duplicated and # passed to this function, making this boolean False. unique_signals = np.sum(np.abs(sig1 - sig2)) != 0 fs = params.fs preprocess = params.preprocess ns = params.surr_count or 0 nv = params.nv fmax = params.fmax or fs / 2 fmin = params.fmin or math.nan f0 = params.f0 or 1 params = {"nv": nv, "fmin": fmin, "fmax": fmax, "f0": f0} # If a unique pair of signals is being analysed, perform autobispectral and crossbispectral analysis. if unique_signals: # Note: Previously attempted to calculate bispec_wav_new in a process each. Did not work # due to some strange problem with the Matlab runtime. Processes would # hang at the `package.initialize()` stage for unknown reasons. print( "Multiple signals; performing autobispectral and crossbispectral analysis..." ) bispxxx, _, _, _, _ = bispec_wav_new.calculate(sig1list, sig1list, fs, params) bispppp, _, _, _, _ = bispec_wav_new.calculate(sig2list, sig2list, fs, params) bispxpp, freq, amp_wt1, amp_wt2, opt = bispec_wav_new.calculate( sig1list, sig2list, fs, params) bisppxx, _, _, _, _ = bispec_wav_new.calculate(sig2list, sig1list, fs, params) bisp_size = bispxxx.shape + (ns, ) surrxxx = zeros(bisp_size) surrppp = zeros(bisp_size) surrxpp = zeros(bisp_size) surrpxx = zeros(bisp_size) for j in range(ns): surr1 = wav_surrogate.calculate(sig1list, "IAAFT2", 1) surr2 = wav_surrogate.calculate(sig2list, "IAAFT2", 1) surrxxx[:, :, j] = abs( bispec_wav_new.calculate(surr1, surr1, fs, params)[0]) surrppp[:, :, j] = abs( bispec_wav_new.calculate(surr2, surr2, fs, params)[0]) surrxpp[:, :, j] = abs( bispec_wav_new.calculate(surr1, surr2, fs, params)[0]) surrpxx[:, :, j] = abs( bispec_wav_new.calculate(surr2, surr1, fs, params)[0]) # If only one signal is being analysed, instead of a pair, perform autobispectral analysis only. elif not unique_signals: print("1 signal; performing autobispectral analysis only...") bispxxx, freq, amp_wt1, amp_wt2, opt = bispec_wav_new.calculate( sig1list, sig1list, fs, params) bispxxx = abs(bispxxx) if preprocess: # Create NaN array for WT2. amp_wt2 = np.empty(amp_wt1.shape) amp_wt2.fill(NAN) # Create NaN arrays for remaining bispectra. bisp_size = bispxxx.shape surr_size = bisp_size + (ns, ) surrxxx = zeros(surr_size) # Create empty arrays and make them all NaN. bispppp = np.empty(bisp_size) bispxpp = np.empty(bisp_size) bisppxx = np.empty(bisp_size) surrppp = zeros(surr_size) surrxpp = zeros(surr_size) surrpxx = zeros(surr_size) for a in (bispppp, bispxpp, bisppxx, surrppp, surrxpp, surrpxx): a.fill(NAN) for i in range(ns): surr1 = wav_surrogate.calculate(sig1list, "IAAFT2", 1) surrxxx[:, :, i] = abs( bispec_wav_new.calculate(surr1, surr1, fs, params)[0]) freq = matlab_to_numpy(freq) avg_amp_wt1, avg_pow_wt1 = avg_ampl_pow(amp_wt1) avg_amp_wt2, avg_pow_wt2 = avg_ampl_pow(amp_wt2) pow_wt1, pow_wt2 = np.square(amp_wt1), np.square(amp_wt2) opt["PadLR1"] = matlab_to_numpy(opt["PadLR1"]) opt["PadLR2"] = matlab_to_numpy(opt["PadLR2"]) opt["twf1"] = matlab_to_numpy(opt["twf1"]) opt["twf2"] = matlab_to_numpy(opt["twf2"]) return ( name, freq, amp_wt1, pow_wt1, avg_amp_wt1, avg_pow_wt1, amp_wt2, pow_wt2, avg_amp_wt2, avg_pow_wt2, bispxxx, bispppp, bispxpp, bisppxx, surrxxx, surrppp, surrxpp, surrpxx, opt, )