def calc(images, adversarial_images, measure='sampen'): """Calculate and returns the nonlinear measure of both original and adversarial images. Set measure to what you want to calculate. 'sampen' : Sample entropy 'frac' : Correlation/Fractal dimension 'hurst' : Hurst exponent 'lyapr' : Largest Lyapunov exponent using Rosenstein et al. methods Docs : https://cschoel.github.io/nolds/ If the adversarial image is found to be NaN, we output 0. The reason some adversarial iamges are NaN is because adversarial generation were unsuccessful for them. There is a maximum iteration one can set for adversarial generation, the program outputs NaN when the max iteration is reached before an adversarial perturbation is found. For more info look at "adversarial_gen.ipynb" """ imageCalc_data = [] advimageCalc_data = [] for i in tqdm(range(len(images))): image = images[i] image = image.flatten() advimage = adversarial_images[i] advimage = advimage.flatten() if measure == 'sampen': imageCalc_data.append(nolds.sampen(image)) if np.isnan(np.sum(advimage)): advimageCalc_data.append(0) else: advimageCalc_data.append(nolds.samepn(advimage)) elif measure == 'frac': imageCalc_data.append(nolds.corr_dim(image, 1)) if np.isnan(np.sum(advimage)): advimageCalc_data.append(0) else: advimageCalc_data.append(nolds.corr_dim(advimage, 1)) elif measure == 'hurst': imageCalc_data.append(nolds.hurst_rs(image)) if np.isnan(np.sum(advimage)): advimageCalc_data.append(0) else: advimageCalc_data.append(nolds.hurst_rs(advimage)) elif measure == 'lyapr': imageCalc_data.append(nolds.lyap_r(image)) if np.isnan(np.sum(advimage)): advimageCalc_data.append(0) else: advimageCalc_data.append(nolds.lyap_r(advimage)) return imageCalc_data, advimageCalc_data
def calc_nonli_hrv(RR_list,label): diff_RR = np.diff(RR_list) sd_heart_period = np.std(diff_RR, ddof=1) ** 2 SD1 = np.sqrt(sd_heart_period * 0.5) SD2 = 2 * sd_heart_period - 0.5 * sd_heart_period pA = SD1*SD2 if SD2 != 0: pQ = SD1 / SD2 else: print("SD2 is zero") pQ = 0 ApEn = approximate_entropy(RR_list,2,3) shanEn = shannon_entropy(RR_list) #sampEn = nolds.sampen(RR_list,emb_dim=2) D2 = nolds.corr_dim(RR_list, emb_dim=2) #dfa1 = nolds.dfa(RR_list, range(4,17)) # dfa2 = nolds.dfa(RR_list, range(16,min(len(RR_list)-1, 66))) #dimension, delay, threshold, norm, minimum_diagonal_line_length = 3, 2, 0.7, "manhattan", 2 #rec_mat = recurrence_matrix(RR_list, dimension, delay, threshold, norm) #REC, RPImean, RPImax, RPadet = recurrence_quantification_analysis(rec_mat, minimum_diagonal_line_length) # recurrence_rate, average_diagonal_line_length, longest_diagonal_line_length, determinism features = {'SD1': SD1, 'SD2': SD2, 'pA': pA, 'pQ': pQ, 'ApEn' : ApEn, 'shanEn': shanEn, 'D2': D2, 'label': label} # 'dfa1': dfa1, 'dfa2': dfa2, 'REC': REC, 'RPImean': RPImean, 'RPImax': RPImax, 'RPadet': RPadet, return features
def test_complexity_sanity(): signal = np.cos(np.linspace(start=0, stop=30, num=1000)) # Entropy assert np.allclose(nk.entropy_fuzzy(signal), nk.entropy_sample(signal, fuzzy=True), atol=0.000001) # Fractal assert np.allclose(nk.fractal_dfa(signal, windows=np.array([4, 8, 12, 20])), 2.1009048365682133, atol=0.000001) assert np.allclose(nk.fractal_dfa(signal), 1.957966586191164, atol=0.000001) assert np.allclose(nk.fractal_dfa(signal, multifractal=True), 1.957966586191164, atol=0.000001) assert np.allclose(nk.fractal_correlation(signal), 0.7884473170763334, atol=0.000001) assert np.allclose(nk.fractal_correlation(signal, r="nolds"), nolds.corr_dim(signal, 2), atol=0.0001)
def _calculate_embedding_dimension(self, dataset): embedding_dims = [] for i in xrange(1, 101): embedding_dims.append(nolds.corr_dim(dataset[:self._training_length], i)) max_dim = max(embedding_dims) # round for 2 digits accuracy if(100*max_dim%100 < 50): return int(math.floor(max_dim)) else: return int(math.ceil(max_dim))
def corrDim(self, emb_dim=5): ''' Returns the correlation dimension of trajectory ''' if not self.cleaned: self.removeNoise() self.corr_dim = corr_dim(self.points, emb_dim) return self.corr_dim
def correlation_dimension_n(x: np.ndarray, n): """ Correlation Dimension with Embedding Dimension n :param x: a 1-d numeric vector :param n: int denoting the embedding dimension :return: scalar feature """ try: out = nolds.corr_dim(x, emb_dim=n) except AssertionError: out = -1 return out
def plot_cd( df1, df2 ): # calculating and plotting the correlation integal for embeding dimensions in range (1-10) cd1 = [] cd2 = [] n = [] for i in range(1, 11): cd1.append(nolds.corr_dim(df1, i, fit='RANSAC')) cd2.append(nolds.corr_dim(df2, i, fit='RANSAC')) n.append(i) print(i) plt.grid() plt.plot(n, cd1, color='red', label='Model 1') plt.scatter(n, cd1, color='red') plt.plot(n, cd2, color='green', label='Model 2') plt.scatter(n, cd2, color='green') plt.xlabel('Embedding dimension') plt.ylabel('Correlation dimension') plt.legend() plt.show() print('Model 1 max: ', max(cd1)) print('Model 2 max: ', max(cd2))
def extractNonLinearDomain(self, x): try: nni = self.extractRR(x) sampEntro = nn.sample_entropy(nni=nni, dim=2) #change dim from 1 to 2 lyapEx = self.lyapunov_exponent(nni=nni, emb_dim=3, matrix_dim=2) pointCare = nn.poincare(nni=nni, show=False) # hrust = nolds.hurst_rs(nni) corrDim = nolds.corr_dim(nni, emb_dim=3) # csi = get_csi_cvi_features(nni) #caused worse results # geo = get_geometrical_features(nni) #caused worse results # dfa = nolds.dfa(nni) # return np.array([sampEntro["sampen"], lyapEx["lyapex"]]) return np.array([sampEntro["sampen"], lyapEx["lyapex"], pointCare["sd1"], pointCare["sd2"], pointCare["sd_ratio"], pointCare["ellipse_area"], corrDim]) except: return np.array([])
def ft_corr_dim(cls, ts: np.ndarray, emb_dim: int = 1) -> float: """Correlation dimension of the time-series. It is used the Grassberger-Procaccia algorithm for the correlation dimension estimation. Parameters ---------- ts : :obj:`np.ndarray` One-dimensional time-series values. emb_dim : int, optional (default=1) Embedding dimension to estimate the correlation dimension. Returns ------- float Estimated correlation dimension. References ---------- .. [1] P. Grassberger and I. Procaccia, Characterization of strange attractors, Physical review letters, vol. 50, no. 5, p. 346, 1983. .. [2] P. Grassberger and I. Procaccia, Measuring the strangeness of strange attractors, Physica D: Nonlinear Phenomena, vol. 9, no. 1, pp. 189–208, 1983. .. [3] P. Grassberger, Grassberger-Procaccia algorithm, Scholarpedia, vol. 2, no. 5, p. 3043. .. [4] "nolds" Python package. URL: https://pypi.org/project/nolds/ """ try: corr_dim = nolds.corr_dim(ts, emb_dim=emb_dim) except AssertionError: corr_dim = np.nan return corr_dim
def StatisticalFeatures(self, data): mean = np.mean(data) # Mean of data std = np.std(data) # std of data pfd = pyeeg.pfd(data) # Petrosian Fractal Dimension hurst = pyeeg.hurst(data) # Hurst Exponent Feature dfa = pyeeg.dfa(data) # Detrended Fluctuation Analysis corr = nolds.corr_dim(data, 1) # Correlation Dimension Feature power = np.sum(np.abs(data)**2) / len(data) # Power feature FD = hfda(data, 5) # fractal dimension statistics = { "mean": mean, "std": std, "pfd": pfd, "hurst": hurst, "hjorth": hjorth, "dfa": dfa, "corr": corr, "power": power } return (statistics)
def corrDim(self): ''' Returns the correlation dimension of the start/end locations ''' return corr_dim(self.startEndLocs(), emb_dim=5)
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)
import numpy as np import nolds states = np.loadtxt("Rossler_reservoir_states_node200.txt", dtype='float32') dimension = 5 correlation_dimension = np.zeros((dimension, 2)) for i in range(1, dimension, 1): data = states.T[1, :1000 * (10**i) * 5] correlation_dimension[i - 1] = np.array( [[nolds.corr_dim(data=data, emb_dim=i), i]]) print("correlation dimension at dimension" + str(i) + "is" + str(correlation_dimension)) print("#############################################") np.savetxt("Rossler_correlation_dimension.txt", correlation_dimension)
window_size = 2000 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
def CorrelationDimension(self): resp = nolds.corr_dim(self.channel_data, 1) return [np.array([resp]), ['CorrelationDimension']]
def CorrelationDimension(x): resp = nolds.corr_dim(x, 1) return resp
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 process_data(data, channelNames, srate): global f_labels, processed_channel_names # Default RQA parameters embedding = 10 # Embedding dimension tdelay = 2 # Time delay tau = 30 # threshold # Multiscaling is accomplished with a wavelet transform # Options for basis functions: ['haar', 'db', 'sym', 'coif', 'bior', 'rbio', 'dmey'] #wavelet = 'haar' wavelet = 'db4' mode = 'cpd' #mode = pywt.Modes.smooth # Simple array for entropy value ent = np.zeros(1) # Determine the number of levels required so that # the lowest level approximation is roughly the # delta band (freq range 0-4 Hz) if srate <= 128: levels = 4 elif srate <= 256: levels = 5 elif srate <= 512: # subsample srate = srate / 2.0 n = len(data[0]) data = data[0:, 0:n:2] levels = 5 elif srate <= 1024: srate = srate / 4.0 n = len(data[0]) data = data[0:, 0:n:4] levels = 5 nbands = levels wavelet_scale = {} f_limit = {} # The following function returns the highest level (ns) approximation # in dec[0], then details for level ns in dec[1]. Each successive # level of detail coefficients is in dec[2] through dec[ns]. # # level approximation details # 0 original signal -- # 1 - dec[ns] # 2 - dec[ns-1] # 3 - dec[ns-2] # i - dec[ns-i+1] # ns dec[0] dec[1] WRITE_RP_IMAGE_FILE = False # Print screen headers sys.stdout.write("%10s %6s " % ("Sensor", "Freq")) for f in all_features: sys.stdout.write(" %8s " % (f)) sys.stdout.write("\n") D = {} for c, ch in enumerate(channelNames): if ch in master_channel_list: processed_channel_names.append(ch) # Create a raw recurrence plot image for the original signal from this channel if WRITE_RP_IMAGE_FILE: rp_plot_name = filename + "_" + ch + "_" + "rp" + ".png" print(" write rp image file ", rp_plot_name) settings = Settings(data[c], embedding_dimension=embedding, time_delay=tdelay, neighbourhood=FixedRadius(0)) #computation = RQAComputation.create(settings, verbose=False) rp_computation = RecurrencePlotComputation.create( settings, verbose=False) result = rp_computation.run() ImageGenerator.save_recurrence_plot( result.recurrence_matrix_reverse, rp_plot_name) D[ch] = {} #-------------------------------------------------------------------- # Get the wavelet decomposition. See pywavelet (or pywt) documents. # Deconstruct the waveforms # S = An + Dn + Dn-1 + ... + D1 #-------------------------------------------------------------------- w = pywt.Wavelet(wavelet) m = np.mean(data[c]) a_orig = data[c] - m # the original signal, initially a = a_orig ca = [] # all the approximations cd = [] # all the details sqrt2 = np.sqrt(2.0) for i in range(nbands): (a, d) = pywt.dwt(a, w, mode) f = pow(sqrt2, i + 1) ca.append(a / f) cd.append(d / f) if 1 == 0: # this will build full reconstructed signals at every level rec_a = [] # reconstructed approximations rec_d = [] # reconstructed details for i, coeff in enumerate(ca): coeff_list = [coeff, None] + [None] * i rec_a.append(pywt.waverec(coeff_list, w)) for i, coeff in enumerate(cd): coeff_list = [None, coeff] + [None] * i rec_d.append(pywt.waverec(coeff_list, w)) else: rec_a = ca rec_d = cd # Use the details and last approximation to create all the power-of-2 freq bands f_labels = ['A0'] wavelet_scale = {} wavelet_scale['A0'] = 0 f_limit = {} f_limit['A0'] = srate / 2.0 fs = [srate] freqband = [a_orig] # A0 is the original signal N = len(a_orig) f = srate / 4.0 for j, r in enumerate(rec_a): freq_name = 'A' + str(j + 1) wavelet_scale[freq_name] = j + 1 f_limit[freq_name] = f f = f / 2.0 f_labels.append(freq_name) freqband.append(r[0:N]) # wavelet approximation for this band f = srate / 2.0 for j, r in enumerate(rec_d): freq_name = 'D' + str(j + 1) wavelet_scale[freq_name] = j + 1 f_limit[freq_name] = f f = f / 2.0 f_labels.append(freq_name) freqband.append(r[0:N]) # wavelet details for this band #-------------------------------------------------------------------- # Compute features on each of the frequency bands #-------------------------------------------------------------------- for f in all_features: D[ch][f] = {} #---------------------- # Feature set 1: Power for i, y in enumerate(freqband): v = bandpower(y) D[ch]["Power"][f_labels[i]] = v #---------------------- # Feature set 2: Sample Entropy, Hurst parameter, DFA, Lyapunov exponents D[ch]["SampE"][f_labels[i]] = nolds.sampen(y) try: D[ch]["hurst_rs"][f_labels[i]] = nolds.hurst_rs(y) except: D[ch]["hurst_rs"][f_labels[i]] = 0.0 try: D[ch]["dfa"][f_labels[i]] = nolds.dfa(y) except: D[ch]["dfa"][f_labels[i]] = 0.0 try: D[ch]["cd"][f_labels[i]] = nolds.corr_dim(y, embedding) except: D[ch]["cd"][f_labels[i]] = 0.0 try: #lyap = nolds.lyap_e(y, emb_dim= embedding) lyap0 = nolds.lyap_r(y, emb_dim=embedding) except: #lyap = [0.0, 0.0, 0.0] lyap0 = 0.0 D[ch]["lyap0"][f_labels[i]] = lyap0 #---------------------- # Feature set 3: Recurrence Quantitative Analysis (RQA) # This routine seems to be incredibly slow and may need improvement rqa_features = [ "RR", "DET", "LAM", "L_entr", "L_max", "L_mean", "TT" ] pyRQA_names = ['recurrence_rate', 'determinism', 'laminarity', 'entropy_diagonal_lines', \ 'longest_diagonal_line','average_diagonal_line', 'trapping_time' ] # First check to see if RQA values are needed at all compute_RQA = False for r in rqa_features: if r in all_features: compute_RQA = True break if compute_RQA: #for i, y in enumerate(freqband): settings = Settings( y, embedding_dimension=embedding, time_delay=tdelay, neighbourhood=FixedRadius(tau) #similarity_measure=EuclideanMetric, #theiler_corrector=1, #min_diagonal_line_length=2, #min_vertical_line_length=2, #min_white_vertical_line_length=2) ) computation = RQAComputation.create(settings, verbose=False) result = computation.run() # We have to pull out each value w = f_labels[i] D[ch]["RR"][w] = result.recurrence_rate D[ch]["DET"][w] = result.determinism D[ch]["LAM"][w] = result.laminarity D[ch]["L_entr"][w] = result.entropy_diagonal_lines D[ch]["L_max"][w] = result.longest_diagonal_line D[ch]["L_mean"][w] = result.average_diagonal_line D[ch]["TT"][w] = result.trapping_time # Write results from first channel to the screen, to give # visual feedback that the code is running w = f_labels[i] sys.stdout.write("%10s %6s " % (ch, w)) for dyn_inv in all_features: # D[ch].keys(): v = D[ch][dyn_inv][w] sys.stdout.write(" %8.3f " % (v)) sys.stdout.write("\n") return D, srate, wavelet_scale, f_limit
# 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 hurst #farctal dimension (correlation dimension)= slope of the line fitted to log(r) vs log(C(r)) # If the correlation dimension is constant for all ‘m’ the time series will be deterministic #if the correlation exponentincreases with increase in ‘m’ the time series will be stochastic. h01 = nolds.corr_dim(F,2,debug_plot=True) h01 #lyap_r = estimate largest lyapunov exponent h1=nolds.lyap_r(F,emb_dim=2,debug_plot=True) h1 #lyap_e = estimate whole spectrum of lyapunov exponents h2=nolds.lyap_e(F) h2 from pyentrp import entropy as ent T1=np.std(F) T1 k= 0.2*T1 k
cv2.imshow("Extracted liver segment", merged) # get chain-code of surface using chain () and draw chain-code by draw_chain () image_chain = np.zeros((120, 180)) chain_code = chain(contours) surface_chain = draw_chain(120, 180, 32, 2, chain_code[3:], 1) cv2.namedWindow("chain_code") cv2.moveWindow("chain_code", 1100, 0) cv2.imshow("chain_code", surface_chain) cv2.waitKey(0) cv2.destroyAllWindows() print("chain code of surface: ", chain_code) print("chain code of tissue: ", direction) print() print("chain code of surface (length): ", len(chain_code)) print("chain code of tissue (length): ", len(direction)) print() box_surf = boxcount(surface_chain) print("Hausdorff-Dimension (Surface): ", box_surf) box_tissue = boxcount(labelled) print("Hausdorff-Dimension (Tissue): ", box_tissue) print() corr_surface = nolds.corr_dim(chain_code, 2) print("Correllation dimension (Surface): ", corr_surface) #corr_tissue=nolds.corr_dim(direction,2) #print ("Correllation dimension (Tissue): ",corr_tissue) print() print("Roughness (Surface): ", rough / len(chain_code)) print("Roughness (Tissue) : ", rt / len(direction))
def CorrelationDimension(self): resp = nolds.corr_dim(self.channel_data,1) return [np.array([resp]),['CorrelationDimension']]
def correlationDimension(self, data): temp = nolds.corr_dim(data, emb_dim=3) return temp