def get_nonlinear(self, cols=['ENMO', 'mean_hr', 'hrv_ms']): nonlin = defaultdict(dict) for col in cols: column = defaultdict(dict) column_sleep = defaultdict(dict) column_wake = defaultdict(dict) for idx in range(len(self.sleep_windows)): params = defaultdict(dict) params['DFA'] = nolds.dfa(self.sleep_windows[idx][col], debug_data=False) params['SampEn'] = nolds.sampen(self.sleep_windows[idx][col], debug_data=False) column_sleep[idx] = params for idx in range(len(self.wake_windows)): params = defaultdict(dict) params['DFA'] = nolds.dfa(self.wake_windows[idx][col], debug_data=False) params['SampEn'] = nolds.sampen(self.wake_windows[idx][col], debug_data=False) column_wake[idx] = params column['sleep'] = column_sleep column['wake'] = column_wake nonlin[col] = column self.nonlinear = nonlin return self
def dfa(nni=None, short=(4, 16), long=(17, 64), show=False, figsize=None, legend=True): """Conducts Detrended Fluctuation Analysis for short and long-term fluctuation of an NNI series. References: [Joshua2008][Kuusela2014][Fred2017] Docs: https://pyhrv.readthedocs.io/en/latest/_pages/api/nonlinear.html#sample-entropy-sample-entropy Parameters ---------- nn : array NN intervals in [ms] or [s]. rpeaks : array R-peak times in [ms] or [s]. short : array, 2 elements Interval limits of the short term fluctuations (default: None: [4, 16]). long : array, 2 elements Interval limits of the long term fluctuations (default: None: [17, 64]). show : bool If True, shows DFA plot (default: True) legend : bool If True, adds legend with alpha1 and alpha2 values to the DFA plot (default: True) Returns (biosppy.utils.ReturnTuple Object) ------------------------------------------ [key : format] Description. dfa_short : float Alpha value of the short term fluctuations dfa_long : float Alpha value of the long term fluctuations dfa_plot : matplotlib plot figure Matplotlib plot figure of the DFA """ # Create arrays short = range(short[0], short[1] + 1) long = range(long[0], long[1] + 1) alpha1, dfa_short = nolds.dfa(nni, short, debug_data=True, overlap=False) alpha2, dfa_long = nolds.dfa(nni, long, debug_data=True, overlap=False) # Output args = (alpha1, alpha2, short, long) return biosppy.utils.ReturnTuple( args, ('dfa_alpha1', 'dfa_alpha2', 'dfa_alpha1_beats', 'dfa_alpha2_beats'))
def get_nonlin_params(df, col): params = defaultdict(dict) #params['hurst'] = nolds.hurst_rs(df[col],debug_data=False) params['DFA'] = nolds.dfa(df[col], debug_data=False) params['sampen'] = nolds.sampen(df[col], debug_data=False) #params['lyap1'] = nolds.lyap_r(df[col],debug_data=False) return params
def test_complexity_vs_Python(): signal = np.cos(np.linspace(start=0, stop=30, num=100)) # Shannon shannon = nk.entropy_shannon(signal) # assert scipy.stats.entropy(shannon, pd.Series(signal).value_counts()) assert np.allclose(shannon - pyentrp.shannon_entropy(signal), 0) # Approximate assert np.allclose(nk.entropy_approximate(signal), 0.17364897858477146) assert np.allclose( nk.entropy_approximate( signal, dimension=2, r=0.2 * np.std(signal, ddof=1)) - entropy_app_entropy(signal, 2), 0) assert nk.entropy_approximate( signal, dimension=2, r=0.2 * np.std(signal, ddof=1)) != pyeeg_ap_entropy( signal, 2, 0.2 * np.std(signal, ddof=1)) # Sample assert np.allclose( nk.entropy_sample(signal, dimension=2, r=0.2 * np.std(signal, ddof=1)) - entropy_sample_entropy(signal, 2), 0) assert np.allclose( nk.entropy_sample(signal, dimension=2, r=0.2) - nolds.sampen(signal, 2, 0.2), 0) assert np.allclose( nk.entropy_sample(signal, dimension=2, r=0.2) - entro_py_sampen(signal, 2, 0.2, scale=False), 0) assert np.allclose( nk.entropy_sample(signal, dimension=2, r=0.2) - pyeeg_samp_entropy(signal, 2, 0.2), 0) # import sampen # sampen.sampen2(signal[0:300], mm=2, r=r) assert nk.entropy_sample(signal, dimension=2, r=0.2) != pyentrp.sample_entropy( signal, 2, 0.2)[1] assert nk.entropy_sample( signal, dimension=2, r=0.2 * np.sqrt(np.var(signal))) != MultiscaleEntropy_sample_entropy( signal, 2, 0.2)[0.2][2] # MSE # assert nk.entropy_multiscale(signal, 2, 0.2*np.sqrt(np.var(signal))) != np.trapz(MultiscaleEntropy_mse(signal, [i+1 for i in range(10)], 2, 0.2, return_type="list")) # assert nk.entropy_multiscale(signal, 2, 0.2*np.std(signal, ddof=1)) != np.trapz(pyentrp.multiscale_entropy(signal, 2, 0.2, 10)) # Fuzzy assert np.allclose( nk.entropy_fuzzy(signal, dimension=2, r=0.2, delay=1) - entro_py_fuzzyen(signal, 2, 0.2, 1, scale=False), 0) # DFA assert nk.fractal_dfa(signal, windows=np.array([ 4, 8, 12, 20 ])) != nolds.dfa(signal, nvals=[4, 8, 12, 20], fit_exp="poly")
def newHurst(ts): # Create the range of lag values window_len = 70 hurst_ts = np.zeros(window_len, dtype=np.int) for tail in range(window_len, len(ts)): cur_ts = ts[max(1, tail - window_len):tail] hurst_ts = np.append(hurst_ts, nolds.dfa(cur_ts)) return hurst_ts
def DFA(self): ''' Returns the H exponent from detrended fluctuation analysis Seems like the number of points needs to be over 70 or so for good results ''' if not self.cleaned: self.removeNoise() return dfa(self.points)
def feature_extraction(data): """ Input: - data: 4 * 15360 Output: - feature array: 18*1 """ fq1, pxx1 = psd(data[0,:], fs, seglength) fq2, pxx2 = psd(data[1,:], fs, seglength) fq3, pxx3 = psd(data[2,:], fs, seglength) fq4, pxx4 = psd(data[3,:], fs, seglength) f1 = rela_alpha(fq2, pxx1) # feature 1: relative alpha power of channel 1 f2 = rela_delta(fq1, pxx1) # feature 2: relative delta power of channel 1 f3 = rela_theta(fq1, pxx1) # feature 3: relative theta power of channel 1 f4 = rela_alpha(fq2, pxx2) # feature 4: relative alpha power of channel 2 f5 = rela_delta(fq2, pxx2) # feature 5: relative delta power of channel 2 f6 = rela_theta(fq2, pxx2) # feature 6: relative theta power of channel 2 f7 = rela_alpha(fq3, pxx3) # feature 7: relative alpha power of channel 3 f8 = rela_delta(fq3, pxx3) # feature 8: relative delta power of channel 3 f9 = rela_theta(fq3, pxx3) # feature 9: relative theta power of channel 3 f10 = rela_alpha(fq4, pxx4) # feature 10: relative alpha power of channel 4 f11 = rela_delta(fq4, pxx4) # feature 11: relative delta power of channel 4 f12 = rela_theta(fq4, pxx4) # feature 12: relative theta power of channel 4 f13 = hemi_ratio(f1, f4) # feature 13: hemi-ratio of alpha band between front channels (C1, C2) f14 = hemi_ratio(f2, f5) f15 = hemi_ratio(f3, f6) f16 = hemi_ratio(f7, f10) f17 = hemi_ratio(f8, f11) f18 = hemi_ratio(f9, f12) """ multi-domain """ f19 = nolds.dfa(data[0,:]) f20 = nolds.dfa(data[1,:]) f21 = nolds.dfa(data[2,:]) f22 = nolds.dfa(data[3,:]) #f = np.array([f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18]) f = np.array([f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, f21, f22]) return f
def load_feature(s): rw = [lwalk(i) for i in s] sd = [np.std(i) for i in rw] dfa = [nolds.dfa(i) for i in rw] hurst = [nolds.hurst_rs(i) for i in rw] sampen = [nolds.sampen(i) for i in rw] ac = [autocorrelation(i, 100) for i in rw] rvntsl = [ratio_value_number_to_time_series_length(i) for i in rw] ac_200 = [autocorrelation(i, 200) for i in rw] ac_300 = [autocorrelation(i, 300) for i in rw] lyapr = [nolds.lyap_r(i) for i in rw] inpv = pd.DataFrame( [sd, dfa, hurst, sampen, ac, rvntsl, ac_200, ac_300, lyapr]) return inpv.transpose()
def feature_extract(ep): feature_mtrx = [] scale = 6 up_to = 3 for i in range(np.shape(ep)[1]): ##DWT coeff = wavedec(ep[:,i],'db4',level = scale) #Select decomposition bands up to scale # ##Features on each scale E = [] FI = [] AE = [] CoV = [] #LE = [] #Lyapunov Exponent CD = [] DFA = [] for j in range(up_to+1): length = len(coeff[j]) #Relative Energy if j == 0: tau = (2 ** (scale - 1)) / freq else: tau = j * (2**(scale-1)) / freq E.append(sum([x**2 for x in coeff[j]]) * tau / length) #Fluctuation Index FI.append( sum( np.abs(coeff[j][1:] - coeff[j][0:len(coeff[j])-1]) ) / length ) #Detrended Fluctuation Analysis DFA.append(nolds.dfa(coeff[j])) #Approximate Entropy #AE.append(shannon_entropy(coeff[j])) AE.append(ApEn(coeff[j],2,3)) #Coefficient of Variation u = np.mean(coeff[j]) v = np.std(coeff[j]) CoV.append(v**2/u**2) #Lyapunov Exponent #LE.append(nolds.lyap_r(coeff[j],emb_dim=5)) #Correlation Dimension #CD.append(nolds.corr_dim(coeff[j],2)) feature_mtrx.append([E,FI,AE,CoV,DFA]) #Normalization? ##Output return(feature_mtrx)
def test_hurst(self): st = datetime.datetime(2018, 5, 17, 1, 0, 0) et = datetime.datetime(2018, 5, 17, 2, 0, 0) _, trades, _ = DataLoader.load_split_data("/Users/jamesprince/project-data/data/consolidated-feed/LTC-USD/", st, et, "LTC-USD") prices = np.asarray(trades['price'].dropna(), dtype=np.float32) print(prices) # print(len(prices)) # res = nolds.hurst_rs(prices) res = nolds.dfa(prices) print(res)
def ft_dfa(cls, ts: np.ndarray, pol_order: int = 1, overlap_windows: bool = True) -> float: """Calculate the Hurst parameter from Detrended fluctuation analysis. Note that the ``Hurst parameter`` is not the same quantity as the ``Hurst exponent``. The Hurst parameter `H` is defined as the quantity such that the following holds: std(ts, l * n) = l ** H * std(ts, n), where `ts` is the time-series, `l` is a constant factor, `n` is some window length of `ts`, and std(ts, k) is the standard deviation of `ts` within a window of size `k`. Check `nolds.dfa` documentation for a clear explanation about the underlying function. Parameters ---------- ts : :obj:`np.ndarray` One-dimensional time-series values. pol_order : int, optional (default=1) Order of the detrending polynomial within each window of the analysis. overlap_windows : bool, optional (default=True) If True, overlap the windows used while performing the analysis. Returns ------- float Hurst parameter. References ---------- .. [1] C.-K. Peng, S. V. Buldyrev, S. Havlin, M. Simons, H. E. Stanley, and A. L. Goldberger, Mosaic organization of DNA nucleotides, Physical Review E, vol. 49, no. 2, 1994. .. [2] R. Hardstone, S.-S. Poil, G. Schiavone, R. Jansen, V. V. Nikulin, H. D. Mansvelder, and K. Linkenkaer-Hansen, Detrended fluctuation analysis: A scale-free view on neuronal oscillations, Frontiers in Physiology, vol. 30, 2012. .. [3] "nolds" Python package: https://pypi.org/project/nolds/ """ hurst_coeff = nolds.dfa(ts, order=pol_order, overlap=overlap_windows) return hurst_coeff
def dfa_fd(timeseries): """Detrended Fluctuation Analysis (DFA) DFA measures the Hurst parameter H, which is very similar to the Hurst \ exponent. The main difference is that DFA can be used for non-stationary time series\ (whose mean and/or variance change over time). :param timeseries: Your time series. :type timeseries: numpy.ndarray :return dfa: Detrended Fluctuation Analysis. .. Note:: This functions uses the dfa implementation from the Nolds package. """ ts = utils.fixseries(timeseries) return utils.truncate(nolds.dfa(ts))
def dfa_fd(series): """Detrended Fluctuation Analysis (DFA) DFA measures the Hurst parameter H, which is very similar to the Hurst exponent. The main difference is that DFA can be used for non-stationary processes (whose mean and/or variance change over time). Keyword arguments: series : numpy.array One dimensional time series. Returns ------- dfa : float Detrended Fluctuation Analysis. Notes: ------ This functions uses the dfa implementation from the Nolds package. """ dfa = nolds.dfa(series) return dfa
def dfa_fd(timeseries, nvals=None, overlap=True, order=1, nodata=-9999): """Detrended Fluctuation Analysis (DFA) measures the Hurst \ parameter H, which is very similar to the Hurst Exponent (HE). The main difference is that DFA can be used for non-stationary \ time series. :param timeseries: Time series. :type timeseries: numpy.ndarray :param nvals: Sizes of subseries to use. :type nvals: int :param overlap: if True, there will be a 50% overlap on windows \ otherwise non-overlapping windows will be used. :type overlap: Boolean :param order: Polynomial order of trend to remove. :type order: Boolean :param nodata: nodata of the time series. Default is -9999. :type nodata: int :return dfa: Detrended Fluctuation Analysis. .. Note:: This function uses the Detrended Fluctuation Analysis (DFA) \ implementation from the Nolds package. Due to time series \ characteristcs we use by default the 'RANSAC' \ fitting method as it is more robust to outliers. For more details regarding the hurst implementation, check Nolds \ documentation page. """ import nolds ts = fixseries(timeseries, nodata) return truncate(nolds.dfa(ts, nvals, overlap, order))
def complexity(signal, sampling_rate=1000, shannon=True, sampen=True, multiscale=True, spectral=True, svd=True, correlation=True, higushi=True, petrosian=True, fisher=True, hurst=True, dfa=True, lyap_r=False, lyap_e=False, emb_dim=2, tolerance="default", k_max=8, bands=None, tau=1): """ Computes several chaos/complexity indices of a signal (including entropy, fractal dimensions, Hurst and Lyapunov exponent etc.). Parameters ---------- signal : list or array List or array of values. sampling_rate : int Sampling rate (samples/second). shannon : bool Computes Shannon entropy. sampen : bool Computes approximate sample entropy (sampen) using Chebychev and Euclidean distances. multiscale : bool Computes multiscale entropy (MSE). Note that it uses the 'euclidean' distance. spectral : bool Computes Spectral Entropy. svd : bool Computes the Singular Value Decomposition (SVD) entropy. correlation : bool Computes the fractal (correlation) dimension. higushi : bool Computes the Higushi fractal dimension. petrosian : bool Computes the Petrosian fractal dimension. fisher : bool Computes the Fisher Information. hurst : bool Computes the Hurst exponent. dfa : bool Computes DFA. lyap_r : bool Computes Positive Lyapunov exponents (Rosenstein et al. (1993) method). lyap_e : bool Computes Positive Lyapunov exponents (Eckmann et al. (1986) method). emb_dim : int The embedding dimension (*m*, the length of vectors to compare). Used in sampen, fisher, svd and fractal_dim. tolerance : float Distance *r* threshold for two template vectors to be considered equal. Default is 0.2*std(signal). Used in sampen and fractal_dim. k_max : int The maximal value of k used for Higushi fractal dimension. The point at which the FD plateaus is considered a saturation point and that kmax value should be selected (Gómez, 2009). Some studies use a value of 8 or 16 for ECG signal and other 48 for MEG. bands : int Used for spectral density. A list of numbers delimiting the bins of the frequency bands. If None the entropy is computed over the whole range of the DFT (from 0 to `f_s/2`). tau : int The delay. Used for fisher, svd, lyap_e and lyap_r. Returns ---------- complexity : dict Dict containing values for each indices. Example ---------- >>> import neurokit as nk >>> import numpy as np >>> >>> signal = np.sin(np.log(np.random.sample(666))) >>> complexity = nk.complexity(signal) Notes ---------- *Details* - **Entropy**: Entropy is a measure of unpredictability of the state, or equivalently, of its average information content. - *Shannon entropy*: Shannon entropy was introduced by Claude E. Shannon in his 1948 paper "A Mathematical Theory of Communication". Shannon entropy provides an absolute limit on the best possible average length of lossless encoding or compression of an information source. - *Sample entropy (sampen)*: Measures the complexity of a time-series, based on approximate entropy. The sample entropy of a time series is defined as the negative natural logarithm of the conditional probability that two sequences similar for emb_dim points remain similar at the next point, excluding self-matches. A lower value for the sample entropy therefore corresponds to a higher probability indicating more self-similarity. - *Multiscale entropy*: Multiscale entropy (MSE) analysis is a new method of measuring the complexity of finite length time series. - *SVD Entropy*: Indicator of how many vectors are needed for an adequate explanation of the data set. Measures feature-richness in the sense that the higher the entropy of the set of SVD weights, the more orthogonal vectors are required to adequately explain it. - **fractal dimension**: The term *fractal* was first introduced by Mandelbrot in 1983. A fractal is a set of points that when looked at smaller scales, resembles the whole set. The concept of fractak dimension (FD) originates from fractal geometry. In traditional geometry, the topological or Euclidean dimension of an object is known as the number of directions each differential of the object occupies in space. This definition of dimension works well for geometrical objects whose level of detail, complexity or *space-filling* is the same. However, when considering two fractals of the same topological dimension, their level of *space-filling* is different, and that information is not given by the topological dimension. The FD emerges to provide a measure of how much space an object occupies between Euclidean dimensions. The FD of a waveform represents a powerful tool for transient detection. This feature has been used in the analysis of ECG and EEG to identify and distinguish specific states of physiologic function. Many algorithms are available to determine the FD of the waveform (Acharya, 2005). - *Correlation*: A measure of the fractal (or correlation) dimension of a time series which is also related to complexity. The correlation dimension is a characteristic measure that can be used to describe the geometry of chaotic attractors. It is defined using the correlation sum C(r) which is the fraction of pairs of points X_i in the phase space whose distance is smaller than r. - *Higushi*: Higuchi proposed in 1988 an efficient algorithm for measuring the FD of discrete time sequences. As the reconstruction of the attractor phase space is not necessary, this algorithm is simpler and faster than D2 and other classical measures derived from chaos theory. FD can be used to quantify the complexity and self-similarity of a signal. HFD has already been used to analyse the complexity of brain recordings and other biological signals. - *Petrosian Fractal Dimension*: Provide a fast computation of the FD of a signal by translating the series into a binary sequence. - **Other**: - *Fisher Information*: A way of measuring the amount of information that an observable random variable X carries about an unknown parameter θ of a distribution that models X. Formally, it is the variance of the score, or the expected value of the observed information. - *Hurst*: The Hurst exponent is a measure of the "long-term memory" of a time series. It can be used to determine whether the time series is more, less, or equally likely to increase if it has increased in previous steps. This property makes the Hurst exponent especially interesting for the analysis of stock data. - *DFA*: DFA measures the Hurst parameter H, which is very similar to the Hurst exponent. The main difference is that DFA can be used for non-stationary processes (whose mean and/or variance change over time). - *Lyap*: Positive Lyapunov exponents indicate chaos and unpredictability. Provides the algorithm of Rosenstein et al. (1993) to estimate the largest Lyapunov exponent and the algorithm of Eckmann et al. (1986) to estimate the whole spectrum of Lyapunov exponents. *Authors* - Dominique Makowski (https://github.com/DominiqueMakowski) - Christopher Schölzel (https://github.com/CSchoel) - tjugo (https://github.com/nikdon) - Quentin Geissmann (https://github.com/qgeissmann) *Dependencies* - nolds - numpy *See Also* - nolds package: https://github.com/CSchoel/nolds - pyEntropy package: https://github.com/nikdon/pyEntropy - pyrem package: https://github.com/gilestrolab/pyrem References ----------- - Accardo, A., Affinito, M., Carrozzi, M., & Bouquet, F. (1997). Use of the fractal dimension for the analysis of electroencephalographic time series. Biological cybernetics, 77(5), 339-350. - Pierzchalski, M. Application of Higuchi Fractal Dimension in Analysis of Heart Rate Variability with Artificial and Natural Noise. Recent Advances in Systems Science. - Acharya, R., Bhat, P. S., Kannathal, N., Rao, A., & Lim, C. M. (2005). Analysis of cardiac health using fractal dimension and wavelet transformation. ITBM-RBM, 26(2), 133-139. - Richman, J. S., & Moorman, J. R. (2000). Physiological time-series analysis using approximate entropy and sample entropy. American Journal of Physiology-Heart and Circulatory Physiology, 278(6), H2039-H2049. - Costa, M., Goldberger, A. L., & Peng, C. K. (2005). Multiscale entropy analysis of biological signals. Physical review E, 71(2), 021906. """ if tolerance == "default": tolerance = 0.2 * np.std(signal) # Initialize results storing complexity = {} # ------------------------------------------------------------------------------ # Shannon if shannon is True: try: complexity["Entropy_Shannon"] = entropy_shannon(signal) except: print( "NeuroKit warning: complexity(): Failed to compute Shannon entropy." ) complexity["Entropy_Shannon"] = np.nan # Sampen if sampen is True: try: complexity["Entropy_Sample"] = nolds.sampen(signal, emb_dim, tolerance, dist="chebychev", debug_plot=False, plot_file=None) except: print( "NeuroKit warning: complexity(): Failed to compute sample entropy (sampen)." ) complexity["Entropy_Sample"] = np.nan # multiscale if multiscale is True: try: complexity["Entropy_Multiscale"] = entropy_multiscale( signal, emb_dim, tolerance) except: print( "NeuroKit warning: complexity(): Failed to compute Multiscale Entropy (MSE)." ) complexity["Entropy_Multiscale"] = np.nan # spectral if spectral is True: try: complexity["Entropy_Spectral"] = entropy_spectral( signal, sampling_rate=sampling_rate, bands=bands) except: print( "NeuroKit warning: complexity(): Failed to compute Spectral Entropy." ) complexity["Entropy_Spectral"] = np.nan # SVD if svd is True: try: complexity["Entropy_SVD"] = entropy_svd(signal, tau=tau, emb_dim=emb_dim) except: print( "NeuroKit warning: complexity(): Failed to compute SVD Entropy." ) complexity["Entropy_SVD"] = np.nan # ------------------------------------------------------------------------------ # fractal_dim if correlation is True: try: complexity["Fractal_Dimension_Correlation"] = nolds.corr_dim( signal, emb_dim, rvals=None, fit="RANSAC", debug_plot=False, plot_file=None) except: print( "NeuroKit warning: complexity(): Failed to compute fractal_dim." ) complexity["Fractal_Dimension_Correlation"] = np.nan # higushi if higushi is True: try: complexity["Fractal_Dimension_Higushi"] = fd_higushi(signal, k_max) except: print("NeuroKit warning: complexity(): Failed to compute higushi.") complexity["Fractal_Dimension_Higushi"] = np.nan # petrosian if petrosian is True: try: complexity["Fractal_Dimension_Petrosian"] = fd_petrosian(signal) except: print( "NeuroKit warning: complexity(): Failed to compute petrosian.") complexity["Fractal_Dimension_Petrosian"] = np.nan # ------------------------------------------------------------------------------ # Fisher if fisher is True: try: complexity["Fisher_Information"] = fisher_info(signal, tau=tau, emb_dim=emb_dim) except: print( "NeuroKit warning: complexity(): Failed to compute Fisher Information." ) complexity["Fisher_Information"] = np.nan # Hurst if hurst is True: try: complexity["Hurst"] = nolds.hurst_rs(signal, nvals=None, fit="RANSAC", debug_plot=False, plot_file=None) except: print("NeuroKit warning: complexity(): Failed to compute hurst.") complexity["Hurst"] = np.nan # DFA if dfa is True: try: complexity["DFA"] = nolds.dfa(signal, nvals=None, overlap=True, order=1, fit_trend="poly", fit_exp="RANSAC", debug_plot=False, plot_file=None) except: print("NeuroKit warning: complexity(): Failed to compute dfa.") complexity["DFA"] = np.nan # Lyap_r if lyap_r is True: try: complexity["Lyapunov_R"] = nolds.lyap_r(signal, emb_dim=10, lag=None, min_tsep=None, tau=tau, min_vectors=20, trajectory_len=20, fit="RANSAC", debug_plot=False, plot_file=None) except: print("NeuroKit warning: complexity(): Failed to compute lyap_r.") complexity["Lyapunov_R"] = np.nan # Lyap_e if lyap_e is True: try: result = nolds.lyap_e(signal, emb_dim=10, matrix_dim=4, min_nb=None, min_tsep=0, tau=tau, debug_plot=False, plot_file=None) for i, value in enumerate(result): complexity["Lyapunov_E_" + str(i)] = value except: print("NeuroKit warning: complexity(): Failed to compute lyap_e.") complexity["Lyapunov_E"] = np.nan return (complexity)
def sig_dfa(sig): return nolds.dfa(sig)
def prepareLRMData(data): dg = pd.DataFrame(columns=[ 'uhid', 'dischargestatus', 'ecg_resprate_SE', 'ecg_resprate_DFA', 'ecg_resprate_ADF', 'ecg_resprate_Mean', 'ecg_resprate_Var', 'spo2_SE', 'spo2_DFA', 'spo2_ADF', 'spo2_Mean', 'spo2_Var', 'heartrate_SE', 'heartrate_DFA', 'heartrate_ADF', 'heartrate_Mean', 'heartrate_Var', 'peep_SE', 'peep_DFA', 'peep_ADF', 'peep_Mean', 'peep_Var', 'pip_SE', 'pip_DFA', 'pip_ADF', 'pip_Mean', 'pip_Var', 'map_SE', 'map_DFA', 'map_ADF', 'map_Mean', 'map_Var', 'tidalvol_SE', 'tidalvol_DFA', 'tidalvol_ADF', 'tidalvol_Mean', 'tidalvol_Var', 'minvol_SE', 'minvol_DFA', 'minvol_ADF', 'minvol_Mean', 'minvol_Var', 'ti_SE', 'ti_DFA', 'ti_ADF', 'ti_Mean', 'ti_Var', 'fio2_SE', 'fio2_DFA', 'fio2_ADF', 'fio2_Mean', 'fio2_Var', 'abdomen_girth', 'urine', 'totalparenteralvolume', 'new_ph', 'gender', 'birthweight', 'birthlength', 'birthheadcircumference', 'inout_patient_status', 'gestation', 'baby_type', 'central_temp', 'apgar_onemin', 'apgar_fivemin', 'apgar_tenmin', 'motherage', 'conception_type', 'mode_of_delivery', 'steroidname', 'numberofdose', 'rbs', 'temp', 'currentdateweight', 'currentdateheight', 'tpn-tfl', 'mean_bp', 'sys_bp', 'dia_bp', 'abd_difference', 'stool_day_total', 'total_intake', 'typevalue_Antibiotics', 'typevalue_Inotropes' ]) for i in data.uhid.unique(): try: print(i) t = data[data['uhid'] == i] t.fillna(t.mean(), inplace=True) t.fillna(0, inplace=True) #t = t.apply(pd.to_numeric, errors='coerce') dg = dg.append( { 'uhid': i, 'dischargestatus': t['dischargestatus'].iloc[0], 'ecg_resprate_SE': sample_entropy(t.ecg_resprate, order=2, metric='chebyshev'), 'ecg_resprate_DFA': nolds.dfa(t.ecg_resprate), 'ecg_resprate_ADF': adfuller(t.ecg_resprate)[0], 'ecg_resprate_Mean': np.mean(t.ecg_resprate), 'ecg_resprate_Var': np.var(t.ecg_resprate), 'spo2_SE': sample_entropy(t.spo2, order=2, metric='chebyshev'), 'spo2_DFA': nolds.dfa(t.spo2), 'spo2_ADF': adfuller(t.spo2)[0], 'spo2_Mean': np.mean(t.spo2), 'spo2_Var': np.var(t.spo2), 'heartrate_SE': sample_entropy(t.heartrate, order=2, metric='chebyshev'), 'heartrate_DFA': nolds.dfa(t.heartrate), 'heartrate_ADF': adfuller(t.heartrate)[0], 'heartrate_Mean': np.mean(t.heartrate), 'heartrate_Var': np.var(t.heartrate), 'peep_SE': sample_entropy(t.peep, order=2, metric='chebyshev'), 'peep_DFA': nolds.dfa(t.peep), 'peep_ADF': adfuller(t.peep)[0], 'peep_Mean': np.mean(t.peep), 'peep_Var': np.var(t.peep), 'pip_SE': sample_entropy(t.pip, order=2, metric='chebyshev'), 'pip_DFA': nolds.dfa(t.pip), 'pip_ADF': adfuller(t.pip)[0], 'pip_Mean': np.mean(t.pip), 'pip_Var': np.var(t.pip), 'map_SE': sample_entropy(t.map, order=2, metric='chebyshev'), 'map_DFA': nolds.dfa(t.map), 'map_ADF': adfuller(t.map)[0], 'map_Mean': np.mean(t.map), 'map_Var': np.var(t.map), 'tidalvol_SE': sample_entropy(t.tidalvol, order=2, metric='chebyshev'), 'tidalvol_DFA': nolds.dfa(t.tidalvol), 'tidalvol_ADF': adfuller(t.tidalvol)[0], 'tidalvol_Mean': np.mean(t.tidalvol), 'tidalvol_Var': np.var(t.tidalvol), 'minvol_SE': sample_entropy(t.minvol, order=2, metric='chebyshev'), 'minvol_DFA': nolds.dfa(t.minvol), 'minvol_ADF': adfuller(t.minvol)[0], 'minvol_Mean': np.mean(t.minvol), 'minvol_Var': np.var(t.minvol), 'ti_SE': sample_entropy(t.ti, order=2, metric='chebyshev'), 'ti_DFA': nolds.dfa(t.ti), 'ti_ADF': adfuller(t.ti)[0], 'ti_Mean': np.mean(t.ti), 'ti_Var': np.var(t.ti), 'fio2_SE': sample_entropy(t.fio2, order=2, metric='chebyshev'), 'fio2_DFA': nolds.dfa(t.fio2), 'fio2_ADF': adfuller(t.fio2)[0], 'fio2_Mean': np.mean(t.fio2), 'fio2_Var': np.var(t.fio2), 'abdomen_girth': np.mean(t.abdomen_girth), 'urine': np.nansum(t.urine), 'totalparenteralvolume': np.nansum(t.totalparenteralvolume), 'total_intake': np.nansum(t.total_intake), 'new_ph': np.mean(t.new_ph), 'gender': t['gender'].iloc[0], 'birthweight': t['birthweight'].iloc[0], 'birthlength': t['birthlength'].iloc[0], 'birthheadcircumference': t['birthheadcircumference'].iloc[0], 'inout_patient_status': t['inout_patient_status'].iloc[0], 'gestation': t['gestation'].iloc[0], 'baby_type': t['baby_type'].iloc[0], 'central_temp': np.nanmean(t.central_temp), 'apgar_onemin': t['apgar_onemin'].iloc[0], 'apgar_fivemin': t['apgar_fivemin'].iloc[0], 'apgar_tenmin': t['apgar_tenmin'].iloc[0], 'motherage': t['motherage'].iloc[0], 'conception_type': t['conception_type'].iloc[0], 'mode_of_delivery': t['mode_of_delivery'].iloc[0], 'numberofdose': np.nansum(t.numberofdose), 'rbs': np.nanmean(t.rbs), 'temp': np.nanmean(t.temp), 'currentdateweight': np.nanmean(t.currentdateweight), 'currentdateheight': np.nanmean(t.currentdateheight), 'mean_bp': np.nanmean(t.mean_bp), 'dia_bp': np.nanmean(t.dia_bp), 'sys_bp': np.nanmean(t.sys_bp), 'stool_day_total': np.nanmean(t.stool_day_total), 'tpn-tfl': np.nansum(t['tpn-tfl']), 'typevalue_Inotropes': np.nansum(t.typevalue_Inotropes), 'typevalue_Antibiotics': np.nansum(t.typevalue_Antibiotics), 'steroidname': np.nansum(t.steroidname), }, ignore_index=True) except Exception as e: print(e, "error") dg.to_csv('LRM_all_data.csv') return dg
emb_dim = 4 rolling = rolling_window(df.logR_ask, window_size, 10) rolling = rolling_window(df_std.logR_ask, window_size, window_size) rolling = rolling_window(df_QN_laplace_std.values.transpose()[0], window_size, window_size) rolling_ns = rolling_window(df.ask, window_size, 10) rolling_ts = rolling_window(df.index, window_size, 10) df_ = pd.DataFrame(rolling) sw_1 = rolling[1] sw_1_ns = rolling[1] nolds.lyap_r(sw_1, emb_dim = emb_dim) nolds.lyap_e(sw_1, emb_dim = emb_dim) nolds.sampen(sw_1, emb_dim= emb_dim) nolds.hurst_rs(sw_1) nolds.corr_dim(sw_1, emb_dim=emb_dim) nolds.dfa(sw_1) ent.shannon_entropy(sw_1) # is this even valid? we do not have any p_i states i ALSO IGNORES TEMPORAL ORDER - Practical consideration of permutation entropy ent.sample_entropy(sw_1, sample_length = 10) #what is sample length? #ent.multiscale_entropy(sw_1, sample_length = 10, tolerance = 0.1*np.std(sw_1)) # what is tolerance? "Practical considerations of permutation entropy: A Tutorial review - how to choose parameters in permutation entropy" ent.permutation_entropy(sw_1, m=8, delay = emd_dim ) #Reference paper above #ent.composite_multiscale_entropy() lempel_ziv_complexity(sw_1) gzip_compress_ratio(sw_1_ns, 9) #https://www.researchgate.net/post/How_can_we_find_out_which_value_of_embedding_dimensions_is_more_accurate #when choosing emb_dim for Takens, each dimension should have at least 10 dp ==> 10^1 == 1D, 10^2 == 2D, ..., 10^6 == 6D #FALSE NEAREST NEIGHBOR FOR DETERMINING MINIMAL EMBEDDING DIMENSION
def compute_params(df, number_of_epochs, structure): total_len = int(number_of_epochs * 20) #in sec if structure == 'spindle': profile_type = 'energy' bin_width = 20 elif structure == 'SWA': profile_type = 'energy' bin_width = 20 df_hist = _get_histogram_values_in_sec(df, total_len, bin_width, profile_type=profile_type) #in sec occ = df_hist.occurences time = df_hist.time dt = time[1] - time[0] if (structure == 'SWA') and (profile_type == 'percent'): occ[occ < 20.] = 0 #power = np.sum(df['amplitude']**2) / total_len power = np.sum(df['modulus']) / total_len profile_dfa = nolds.dfa(occ) if len(df) > 2: if structure == 'spindle': frequency_mse = np.sum( (df['frequency'] - 13)**2) / (len(df['frequency']) - 1) frequency_var = np.var(df['frequency'], ddof=1) df_params = pd.DataFrame([[power, frequency_mse, frequency_var, \ profile_dfa]], columns=['power_spindle', 'frequency_mse_spindle', 'frequency_var', \ 'profile_dfa_spindle']) elif structure == 'SWA': df_hist_deep = _get_histogram_values_in_sec(df, total_len, 20, profile_type='percent') occ_deep_sleep = df_hist_deep.occurences min_deep_sleep = len( occ_deep_sleep[occ_deep_sleep >= 20.]) * 20. / 60. #in min min_deep_sleep_50 = len( occ_deep_sleep[occ_deep_sleep >= 50.]) * 20. / 60. #in min occ_deep_sleep[occ_deep_sleep < 20.] = 0. dfa_deep_sleep = nolds.dfa(occ_deep_sleep) deep_sleep_ratio = min_deep_sleep**dfa_deep_sleep occ_deep_sleep[occ_deep_sleep < 50.] = 0. dfa_deep_sleep_50 = nolds.dfa(occ_deep_sleep) deep_sleep_ratio_50 = min_deep_sleep_50**dfa_deep_sleep_50 df_params = pd.DataFrame([[power, profile_dfa, min_deep_sleep, \ dfa_deep_sleep, deep_sleep_ratio, min_deep_sleep_50, \ dfa_deep_sleep_50, deep_sleep_ratio_50]], columns=['power_SWA', 'profile_dfa_SWA', 'min_deep_sleep', \ 'dfa_deep_sleep', 'deep_sleep_ratio', 'min_deep_sleep_50', \ 'dfa_deep_sleep_50', 'deep_sleep_ratio_50']) else: if structure == 'spindle': df_params = pd.DataFrame([[0, np.inf, np.inf, \ 0.5]], columns=['power_spindle', 'frequency_mse_spindle', 'frequency_var', \ 'profile_dfa_spindle']) elif structure == 'SWA': df_params = pd.DataFrame([[0, 0.5, 0, \ 0.5, 0, 0, \ 0.5, 0]], columns=['power_SWA', 'profile_dfa_SWA', 'min_deep_sleep', \ 'dfa_deep_sleep', 'deep_sleep_ratio', 'min_deep_sleep_50', \ 'dfa_deep_sleep_50', 'deep_sleep_ratio_50']) return df_params
import pyflux as pf from datetime import datetime import matplotlib.pyplot as plt %matplotlib inline from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score data = pd.read_csv('dataset-2007.csv', usecols=['wind direction at 100m (deg)', 'wind speed at 100m (m/s)', 'air temperature at 2m (K)', 'surface air pressure (Pa)', 'density at hub height (kg/m^3)'], skiprows=3) data.columns = ['direction', 'speed', 'temp', 'pressure', 'density'] data.head(3) D=data['speed'].values T=D[0:105120:12] F=T[0:2000] h = nolds.dfa(F) h RS=nolds.hurst_rs(F) RS # calculate standard deviation of differenced series using various lags lags = range(2, 20) tau = [sqrt(std(subtract(F[lag:], F[:-lag]))) for lag in lags] # plot on log-log scale plot(log(lags), log(tau)); show() # calculate Hurst as slope of log-log plot m = polyfit(log(lags), log(tau), 1) hurst = m[0]*2.0
#else: # return_exmo = None # return_exmos.append(return_exmo) if(row[6] != ''): return_binance = np.float(row[6]) return_binances.append(return_binance) #else: # return_binance = None # return_binances.append(return_binance) #calculating the bds test for coinbase exchange i = 0 while((i + size) < len(return_coinbases)): temp1 = return_coinbases[i:i + size] date1 = dates[i:i+size] dfa_coinbase = nolds.dfa(temp1) H_coinbases.append(dfa_coinbase) temp_coinbase_date = dates[i] Hdate_coinbases.append(temp_coinbase_date) Ljung(temp1,date1) flag_coinbase = flag_coinbase + 1 i = i + step #calculating the bds test for exmo exchange j = 0 while((j + size) < len(return_exmos)): temp2 = return_exmos[j:j + size] date2=dates[j:j+size] dfa_exmo = nolds.dfa(temp2) Ljung(temp2,date2) H_exmos.append(dfa_exmo)
def eeg_fractal_dim(epochs, entropy=True, hurst=True, dfa=False, lyap_r=False, lyap_e=False): """ """ clock = Time() df = epochs.to_data_frame(index=["epoch", "time", "condition"]) # Separate indexes index = df.index.tolist() epochs = [] times = [] events = [] for i in index: epochs.append(i[0]) times.append(i[1]) events.append(i[2]) data = {} if entropy == True: data["Entropy"] = {} if hurst == True: data["Hurst"] = {} if dfa == True: data["DFA"] = {} if lyap_r == True: data["Lyapunov_R"] = {} if lyap_e == True: data["Lyapunov_E"] = {} clock.reset() for epoch in set(epochs): subset = df.loc[epoch] if entropy == True: data["Entropy"][epoch] = [] if hurst == True: data["Hurst"][epoch] = [] if dfa == True: data["DFA"][epoch] = [] if lyap_r == True: data["Lyapunov_R"][epoch] = [] if lyap_e == True: data["Lyapunov_E"][epoch] = [] for channel in subset: if entropy == True: data["Entropy"][epoch].append(nolds.sampen(subset[channel])) if hurst == True: data["Hurst"][epoch].append(nolds.hurst_rs(subset[channel])) if dfa == True: data["DFA"][epoch].append(nolds.dfa(subset[channel])) if lyap_r == True: data["Lyapunov_R"][epoch].append(nolds.lyap_r(subset[channel])) if lyap_e == True: data["Lyapunov_E"][epoch].append(nolds.lyap_e(subset[channel])) if entropy == True: data["Entropy"][epoch] = np.mean(data["Entropy"][epoch]) if hurst == True: data["Hurst"][epoch] = np.mean(data["Hurst"][epoch]) if dfa == True: data["DFA"][epoch] = np.mean(data["DFA"][epoch]) if lyap_r == True: data["Lyapunov_R"][epoch] = np.mean(data["Lyapunov_R"][epoch]) if lyap_e == True: data["Lyapunov_E"][epoch] = np.mean(data["Lyapunov_E"][epoch]) time = clock.get(reset=False)/1000 time = time/(epoch+1) time = (time * (len(set(epochs))-epoch))/60 print(str(round((epoch+1)/len(set(epochs))*100,2)) + "% complete, remaining time: " + str(round(time, 2)) + 'min') df = pd.DataFrame.from_dict(data) list_events = [] for i in range(len(events)): list_events.append(events[i] + "_" + str(epochs[i])) list_events = list_events[np.where(find_following_duplicates(list_events))] list_events = [re.sub('_\d+', '', i) for i in list_events] df["Epoch"] = list_events return(df)
def dfa(nn=None, rpeaks=None, short=None, long=None, show=True, figsize=None, legend=True): """Conducts Detrended Fluctuation Analysis for short and long-term fluctuation of an NNI series. References: [Joshua2008][Kuusela2014][Fred2017] Docs: https://pyhrv.readthedocs.io/en/latest/_pages/api/nonlinear.html#sample-entropy-sample-entropy Parameters ---------- nn : array NN intervals in [ms] or [s]. rpeaks : array R-peak times in [ms] or [s]. short : array, 2 elements Interval limits of the short term fluctuations (default: None: [4, 16]). long : array, 2 elements Interval limits of the long term fluctuations (default: None: [17, 64]). show : bool If True, shows DFA plot (default: True) legend : bool If True, adds legend with alpha1 and alpha2 values to the DFA plot (default: True) Returns (biosppy.utils.ReturnTuple Object) ------------------------------------------ [key : format] Description. dfa_short : float Alpha value of the short term fluctuations dfa_long : float Alpha value of the long term fluctuations dfa_plot : matplotlib plot figure Matplotlib plot figure of the DFA """ # Check input values nn = pu.check_input(nn, rpeaks) # Check intervals short = pu.check_interval(short, default=(4, 16)) long = pu.check_interval(long, default=(17, 64)) # Create arrays short = range(short[0], short[1] + 1) long = range(long[0], long[1] + 1) # Prepare plot if figsize is None: figsize = (6, 6) fig = plt.figure(figsize=figsize) ax = fig.add_subplot(111) ax.set_title('Detrended Fluctuation Analysis (DFA)') ax.set_xlabel('log n [beats]') ax.set_ylabel('log F(n)') # try: # Compute alpha values try: alpha1, dfa_short = nolds.dfa(nn, short, debug_data=True, overlap=False) alpha2, dfa_long = nolds.dfa(nn, long, debug_data=True, overlap=False) except ValueError: # If DFA could not be conducted due to insufficient number of NNIs, return an empty graph and 'nan' for alpha1/2 warnings.warn( "Not enough NNI samples for Detrended Fluctuations Analysis.") ax.axis([0, 1, 0, 1]) ax.text(0.5, 0.5, '[Insufficient number of NNI samples for DFA]', horizontalalignment='center', verticalalignment='center') alpha1, alpha2 = 'nan', 'nan' else: # Plot DFA results if number of NNI were sufficent to conduct DFA # Plot short term DFA vals, flucts, poly = dfa_short[0], dfa_short[1], np.polyval( dfa_short[2], dfa_short[0]) label = r'$ \alpha_{1}: %0.2f$' % alpha1 ax.plot(vals, flucts, 'bo', markersize=1) ax.plot(vals, poly, 'b', label=label, alpha=0.7) # Plot long term DFA vals, flucts, poly = dfa_long[0], dfa_long[1], np.polyval( dfa_long[2], dfa_long[0]) label = r'$ \alpha_{2}: %0.2f$' % alpha2 ax.plot(vals, flucts, 'go', markersize=1) ax.plot(vals, poly, 'g', label=label, alpha=0.7) # Add legend if legend: ax.legend() ax.grid() # Plot axis if show: plt.show() # Output args = (fig, alpha1, alpha2, short, long) return biosppy.utils.ReturnTuple(args, ('dfa_plot', 'dfa_alpha1', 'dfa_alpha2', 'dfa_alpha1_beats', 'dfa_alpha2_beats'))
def pdfret(x, xm, t, sl, z, beta, q): val2 = np.float(1 / nolds.dfa(k1.Close)) global lim return -(1 / z) * (t**val2) * (1 + beta * (q - 1) * ((x - xm) * (t**val2))**2)**(-1 / (q - 1))
def complexity(signal, shannon=True, sampen=True, multiscale=True, fractal_dim=True, hurst=True, dfa=True, lyap_r=False, lyap_e=False, emb_dim=2, tolerance="default"): """ Returns several chaos/complexity indices of a signal (including entropy, fractal dimensions, Hurst and Lyapunov exponent etc.). Parameters ---------- signal : list or array List or array of values. shannon : bool Computes Shannon entropy. sampen : bool Computes approximate sample entropy (sampen) using Chebychev and Euclidean distances. multiscale : bool Computes multiscale entropy (MSE). Note that it uses the 'euclidean' distance. fractal_dim : bool Computes the fractal (correlation) dimension. hurst : bool Computes the Hurst exponent. dfa : bool Computes DFA. lyap_r : bool Computes Positive Lyapunov exponents (Rosenstein et al. (1993) method). lyap_e : bool Computes Positive Lyapunov exponents (Eckmann et al. (1986) method). emb_dim : int The embedding dimension (*m*, the length of vectors to compare). Used in sampen and fractal_dim. tolerance : float Distance *r* threshold for two template vectors to be considered equal. Default is 0.2*std(signal). Used in sampen and fractal_dim. Returns ---------- complexity : dict Dict containing values for each indices. Example ---------- >>> import neurokit as nk >>> import numpy as np >>> >>> signal = np.sin(np.log(np.random.sample(666))) >>> complexity = nk.complexity(signal) Notes ---------- *Details* - **shannon entropy**: Entropy is a measure of unpredictability of the state, or equivalently, of its average information content. - **sample entropy (sampen)**: Measures the complexity of a time-series, based on approximate entropy. The sample entropy of a time series is defined as the negative natural logarithm of the conditional probability that two sequences similar for emb_dim points remain similar at the next point, excluding self-matches. A lower value for the sample entropy therefore corresponds to a higher probability indicating more self-similarity. - **multiscale entropy**: Multiscale entropy (MSE) analysis is a new method of measuring the complexity of finite length time series. - **fractal dimension**: A measure of the fractal (or correlation) dimension of a time series which is also related to complexity. The correlation dimension is a characteristic measure that can be used to describe the geometry of chaotic attractors. It is defined using the correlation sum C(r) which is the fraction of pairs of points X_i in the phase space whose distance is smaller than r. - **hurst**: The Hurst exponent is a measure of the "long-term memory" of a time series. It can be used to determine whether the time series is more, less, or equally likely to increase if it has increased in previous steps. This property makes the Hurst exponent especially interesting for the analysis of stock data. - **dfa**: DFA measures the Hurst parameter H, which is very similar to the Hurst exponent. The main difference is that DFA can be used for non-stationary processes (whose mean and/or variance change over time). - **lyap**: Positive Lyapunov exponents indicate chaos and unpredictability. Provides the algorithm of Rosenstein et al. (1993) to estimate the largest Lyapunov exponent and the algorithm of Eckmann et al. (1986) to estimate the whole spectrum of Lyapunov exponents. *Authors* - Christopher Schölzel (https://github.com/CSchoel) - tjugo (https://github.com/nikdon) - Dominique Makowski (https://github.com/DominiqueMakowski) *Dependencies* - nolds - numpy *See Also* - nolds package: https://github.com/CSchoel/nolds - pyEntropy package: https://github.com/nikdon/pyEntropy References ----------- - Richman, J. S., & Moorman, J. R. (2000). Physiological time-series analysis using approximate entropy and sample entropy. American Journal of Physiology-Heart and Circulatory Physiology, 278(6), H2039-H2049. - Costa, M., Goldberger, A. L., & Peng, C. K. (2005). Multiscale entropy analysis of biological signals. Physical review E, 71(2), 021906. """ if tolerance == "default": tolerance = 0.2 * np.std(signal) # Initialize results storing complexity = {} # Shannon if shannon is True: try: complexity["Shannon_Entropy"] = entropy_shannon(signal) except: print( "NeuroKit warning: complexity(): Failed to compute Shannon entropy." ) complexity["Shannon_Entropy"] = np.nan # Sampen if sampen is True: try: complexity["Sample_Entropy_Chebychev"] = nolds.sampen( signal, emb_dim, tolerance, dist="chebychev", debug_plot=False, plot_file=None) except: print( "NeuroKit warning: complexity(): Failed to compute sample entropy (sampen) using chebychev distance." ) complexity["Sample_Entropy_Chebychev"] = np.nan try: complexity["Sample_Entropy_Euclidean"] = nolds.sampen( signal, emb_dim, tolerance, dist="euclidean", debug_plot=False, plot_file=None) except: try: complexity["Sample_Entropy_Euclidean"] = nolds.sampen( signal, emb_dim, tolerance, dist="euler", debug_plot=False, plot_file=None) except: print( "NeuroKit warning: complexity(): Failed to compute sample entropy (sampen) using euclidean distance." ) complexity["Sample_Entropy_Euclidean"] = np.nan # multiscale if multiscale is True: try: complexity["Multiscale_Entropy"] = entropy_multiscale( signal, emb_dim, tolerance) except: print( "NeuroKit warning: complexity(): Failed to compute Multiscale Entropy (MSE)." ) complexity["Multiscale_Entropy"] = np.nan # fractal_dim if fractal_dim is True: try: complexity["Fractal_Dimension"] = nolds.corr_dim(signal, emb_dim, rvals=None, fit="RANSAC", debug_plot=False, plot_file=None) except: print( "NeuroKit warning: complexity(): Failed to compute fractal_dim." ) complexity["Fractal_Dimension"] = np.nan # Hurst if hurst is True: try: complexity["Hurst"] = nolds.hurst_rs(signal, nvals=None, fit="RANSAC", debug_plot=False, plot_file=None) except: print("NeuroKit warning: complexity(): Failed to compute hurst.") complexity["Hurst"] = np.nan # DFA if dfa is True: try: complexity["DFA"] = nolds.dfa(signal, nvals=None, overlap=True, order=1, fit_trend="poly", fit_exp="RANSAC", debug_plot=False, plot_file=None) except: print("NeuroKit warning: complexity(): Failed to compute dfa.") complexity["DFA"] = np.nan # Lyap_r if lyap_r is True: try: complexity["Lyapunov_R"] = nolds.lyap_r(signal, emb_dim=10, lag=None, min_tsep=None, tau=1, min_vectors=20, trajectory_len=20, fit="RANSAC", debug_plot=False, plot_file=None) except: print("NeuroKit warning: complexity(): Failed to compute lyap_r.") complexity["Lyapunov_R"] = np.nan # Lyap_e if lyap_e is True: try: result = nolds.lyap_e(signal, emb_dim=10, matrix_dim=4, min_nb=None, min_tsep=0, tau=1, debug_plot=False, plot_file=None) for i, value in enumerate(result): complexity["Lyapunov_E_" + str(i)] = value except: print("NeuroKit warning: complexity(): Failed to compute lyap_e.") complexity["Lyapunov_E"] = np.nan return (complexity)
def scaledpdfret(x, t, xm, sl, z, beta, q): global k1 val1 = 1 / (nolds.dfa(k1.Close)) return -(1 / z) * (1 + beta * (q - 1) * (x / (t**val1) - xm / (t**val1))**2)**(-1 / (q - 1))
def dfa(y): # Detrended fluctuation analysis return nolds.dfa(y)
def processSingleFile(self, file): self.logger.log( "INFO", "EXTRACTING FEATURE {} FROM FILE: {}".format(self.algorithm, file)) res = self.readFile(file) if (self.algorithm == 'DfaMeanCorr'): feature = np.zeros([378, 1]) else: feature = np.zeros([26, 1]) if (res["error"]): return (feature) if (res["data"].shape[0] == 0): return (feature) if (res["data"].shape[1] < 10): return (feature) data = self.prepareMetrics(res["data"]) try: if (self.algorithm == 'correlation'): feature = self.correlation(data) elif (self.algorithm == 'dfa'): feature = np.zeros([26, 1]) for i in range(0, data.shape[1]): feature[i] = nolds.dfa(data[:, i]) elif (self.algorithm == 'sampen'): feature = np.zeros([26, 1]) for i in range(0, data.shape[1]): feature[i] = nolds.sampen(data[:,i],emb_dim=2,\ tolerance=0.2*np.std(data[:,i])) elif (self.algorithm == 'hurst'): feature = np.zeros([26, 1]) for i in range(0, data.shape[1]): feature[i] = nolds.hurst_rs(data[:, i]) elif (self.algorithm == 'DfaMeanCorr'): cache_filename = file + ".pkl" if (os.path.exists(cache_filename)): self.logger.log("INFO", "CACHE FOUND, USING IT") file = open(cache_filename, 'rb') cache = pickle.load(file) else: cache = {} if (not "corr" in cache): featureCorr = self.correlation(data) cache["corr"] = featureCorr else: featureCorr = cache["corr"] if (not "cons" in cache): featureConsump = self.consumption(res["data"]) cache["featureConsump"] = featureConsump else: featureConsump = cache["cons"] if (not "dfa" in cache): featureDfa = np.zeros([26, 1]) for i in range(0, int(data.shape[1])): featureDfa[i] = nolds.dfa(data[:, i]) cache["dfa"] = featureDfa else: featureDfa = cache["dfa"] if (not "entropy" in cache): featureEntropy = np.zeros([26, 1]) for i in range(0, int(data.shape[1])): featureEntropy[i] = nolds.sampen(data[:, i], 2) cache["entropy"] = featureEntropy with open(cache_filename, 'wb') as output: pickle.dump(cache, output, pickle.HIGHEST_PROTOCOL) feature = np.vstack( [featureDfa, featureConsump, featureEntropy, featureCorr]) except Exception as e: self.logger.log("ERROR","ERROR EXTRACTING FEATURE {} FROM FILE: {}, SKIPPING"\ .format(self.algorithm,file)) print(e) traceback.print_exc() for i in range(0, feature.shape[0]): feature[i] = 0 return (feature)
0.0001, 0.02, 1.3, 1.6) apq11Shimmer = call([sound, pointProcess], "Get shimmer (apq11)", 0, 0, 0.0001, 0.02, 1.3, 1.6) ddaShimmer = call([sound, pointProcess], "Get shimmer (dda)", 0, 0, 0.0001, 0.02, 1.3, 1.6) voice_report = call([sound, pitch, pointProcess], "Voice report", 0.0, 0.0, f0min, f0max, 1.3, 1.6, 0.03, 0.45) return meanF0, stdevF0, localJitter, localabsoluteJitter, rapJitter, ppq5Jitter, ddpJitter, localShimmer, localdbShimmer, apq3Shimmer, aqpq5Shimmer, apq11Shimmer, ddaShimmer, voice_report AudioFile_path = sys.argv[1] sample_rate, samples = wavfile.read(AudioFile_path) frequencies, times, spectogram = signal.spectrogram(samples, sample_rate) sound = parselmouth.Sound(AudioFile_path) DFA = nolds.dfa(times) PPE = entropy.shannon_entropy(times) (meanF0, stdevF0, localJitter, localabsoluteJitter, rapJitter, ppq5Jitter, ddpJitter, localShimmer, localdbShimmer, apq3Shimmer, aqpq5Shimmer, apq11Shimmer, ddaShimmer, voice_report) = measurePitch(sound, 75, 500, "Hertz") voice_report = voice_report.strip() hnr = voice_report[984:989] nhr = voice_report[941:953] # from sklearn.preprocessing import MinMaxScaler # sc = MinMaxScaler() # DFA = sc.fit_transform(DFA) # PPE = sc.fit_transform(PPE)
import nolds import numpy as np rwalk = np.cumsum(np.random.random(1000)) print("fractal {}".format(nolds.dfa(rwalk))) print("Lup {}".format(nolds.lyap_e(rwalk))) print("Lup {}".format(nolds.lyap_r(rwalk))) print("Hurst {}".format(nolds.hurst_rs(rwalk)))