1+ import math
12from collections import Counter
23
34import numpy as np
45
56
6- def time_delay_embedding (time_series , embedding_dimension , delay = 1 ):
7- """
8- Time-delayed embedding.
7+ def time_delay_embedding (time_series , embedding_dimension , delay ):
8+ """Calculate time-delayed embedding.
99
1010 Parameters
1111 ----------
@@ -20,6 +20,7 @@ def time_delay_embedding(time_series, embedding_dimension, delay=1):
2020 -------
2121 embedded : ndarray
2222 The embedded time series with shape (n_times - (order - 1) * delay, order).
23+
2324 """
2425 series_length = len (time_series )
2526 embedded_series = np .empty ((embedding_dimension , series_length - (embedding_dimension - 1 ) * delay ))
@@ -29,8 +30,7 @@ def time_delay_embedding(time_series, embedding_dimension, delay=1):
2930
3031
3132def util_pattern_space (time_series , lag , dim ):
32- """
33- Create a set of sequences with given lag and dimension
33+ """Create a set of sequences with a given lag and dimension.
3434
3535 Parameters
3636 ----------
@@ -49,6 +49,7 @@ def util_pattern_space(time_series, lag, dim):
4949 Raises
5050 ------
5151 ValueError: If the lag is less than 1 or the result matrix exceeds the size limit.
52+
5253 """
5354 n = len (time_series )
5455
@@ -66,8 +67,7 @@ def util_pattern_space(time_series, lag, dim):
6667
6768
6869def util_granulate_time_series (time_series , scale ):
69- """
70- Extract coarse-grained time series
70+ """Extract coarse-grained time series.
7171
7272 Parameters
7373 ----------
@@ -79,7 +79,8 @@ def util_granulate_time_series(time_series, scale):
7979 Returns
8080 -------
8181 cts : np.ndarray
82- Array of coarse-grained time series with given scale factor
82+ Array of coarse-grained time series with a given scale factor
83+
8384 """
8485 if not isinstance (time_series , np .ndarray ):
8586 time_series = np .array (time_series )
@@ -91,8 +92,7 @@ def util_granulate_time_series(time_series, scale):
9192
9293
9394def shannon_entropy (time_series ):
94- """
95- Return the Shannon Entropy of the sample data.
95+ """Calculate Shannon Entropy of the sample data.
9696
9797 Parameters
9898 ----------
@@ -103,6 +103,7 @@ def shannon_entropy(time_series):
103103 -------
104104 ent: float
105105 The Shannon Entropy as float value
106+
106107 """
107108 if isinstance (time_series , str ):
108109 # Calculate frequency counts
@@ -130,11 +131,10 @@ def shannon_entropy(time_series):
130131
131132
132133def sample_entropy (time_series , sample_length , tolerance = None ):
133- """
134- Calculates the sample entropy of degree m of a time_series.
134+ """Calculate the sample entropy of degree m of a time_series.
135135
136136 This method uses Chebyshev norm.
137- It is quite fast for random data, but can be slower is there is
137+ It is quite fast for random data but can be slower is there is
138138 structure in the input time series.
139139
140140 Parameters
@@ -150,7 +150,7 @@ def sample_entropy(time_series, sample_length, tolerance=None):
150150 -------
151151 sampen: np.ndarray
152152 Array of Sample Entropies SE.
153- SE[k] is ratio `#templates of length k+1` / `#templates of length k`
153+ SE[k] is the ratio `#templates of length k+1` / `#templates of length k`
154154 where `#templates of length 0` = n*(n - 1) / 2, by definition
155155
156156 Notes
@@ -162,6 +162,7 @@ def sample_entropy(time_series, sample_length, tolerance=None):
162162 .. [1] http://en.wikipedia.org/wiki/Sample_Entropy
163163 .. [2] http://physionet.incor.usp.br/physiotools/sampen/
164164 .. [3] Madalena Costa, Ary Goldberger, CK Peng. Multiscale entropy analysis of biological signals
165+
165166 """
166167 if not isinstance (time_series , np .ndarray ):
167168 time_series = np .array (time_series )
@@ -177,7 +178,7 @@ def sample_entropy(time_series, sample_length, tolerance=None):
177178 # N_temp[k] holds matches templates of length k
178179 N_temp = np .zeros (sample_length + 2 )
179180
180- # Templates of length 0 matches by definition:
181+ # Templates of length 0 match by definition:
181182 N_temp [0 ] = n * (n - 1 ) / 2
182183
183184 for i in range (n - sample_length - 1 ):
@@ -195,9 +196,7 @@ def sample_entropy(time_series, sample_length, tolerance=None):
195196
196197
197198def multiscale_entropy (time_series , sample_length , tolerance = None , maxscale = None ):
198- """
199- Calculate the Multiscale Entropy of the given time series considering
200- different time-scales of the time series.
199+ """Calculate Multiscale Entropy considering different time-scales of the time series.
201200
202201 Parameters
203202 ----------
@@ -219,6 +218,7 @@ def multiscale_entropy(time_series, sample_length, tolerance=None, maxscale=None
219218 ----------
220219 .. [1] http://en.pudn.com/downloads149/sourcecode/math/detail646216_en.html
221220 Can be viewed at https://web.archive.org/web/20170207221539/http://en.pudn.com/downloads149/sourcecode/math/detail646216_en.html
221+
222222 """
223223 if tolerance is None :
224224 tolerance = 0.1 * np .std (time_series )
@@ -234,12 +234,11 @@ def multiscale_entropy(time_series, sample_length, tolerance=None, maxscale=None
234234
235235
236236def permutation_entropy (time_series , order = 3 , delay = 1 , normalize = False ):
237- """
238- Permutation Entropy.
237+ """Calculate Permutation Entropy.
239238
240239 Parameters
241240 ----------
242- time_series : list | np.array
241+ time_series : list | np.ndarray
243242 Time series
244243 order : int
245244 Order of permutation entropy
@@ -286,6 +285,7 @@ def permutation_entropy(time_series, order=3, delay=1, normalize=False):
286285 >>> # Return a value comprised between 0 and 1.
287286 >>> print(permutation_entropy(x, order=3, normalize=True))
288287 0.589
288+
289289 """
290290 x = np .array (time_series )
291291 hashmult = np .power (order , np .arange (order ))
@@ -299,13 +299,13 @@ def permutation_entropy(time_series, order=3, delay=1, normalize=False):
299299 p = np .true_divide (c , c .sum ())
300300 pe = - np .multiply (p , np .log2 (p )).sum ()
301301 if normalize :
302- pe /= np .log2 (np .math .factorial (order ))
302+ factorial = math .factorial (order )
303+ pe /= np .log2 (factorial )
303304 return pe
304305
305306
306307def multiscale_permutation_entropy (time_series , m , delay , scale ):
307- """
308- Calculate the Multiscale Permutation Entropy
308+ """Calculate the Multiscale Permutation Entropy.
309309
310310 Parameters
311311 ----------
@@ -328,6 +328,7 @@ def multiscale_permutation_entropy(time_series, m, delay, scale):
328328 .. [1] Francesco Carlo Morabito et al. Multivariate Multi-Scale Permutation Entropy for
329329 Complexity Analysis of Alzheimer`s Disease EEG. www.mdpi.com/1099-4300/14/7/1186
330330 .. [2] http://www.mathworks.com/matlabcentral/fileexchange/37288-multiscale-permutation-entropy-mpe/content/MPerm.m
331+
331332 """
332333 mspe = np .empty (scale )
333334 for i in range (scale ):
@@ -337,11 +338,10 @@ def multiscale_permutation_entropy(time_series, m, delay, scale):
337338
338339
339340def weighted_permutation_entropy (time_series , order = 2 , delay = 1 , normalize = False ):
340- """
341- Calculate the weighted permutation entropy. Weighted permutation
342- entropy captures the information in the amplitude of a signal where
343- standard permutation entropy only measures the information in the
344- ordinal pattern, "motif".
341+ """Calculate the weighted permutation entropy.
342+
343+ Weighted permutation entropy captures the information in the amplitude of a signal where
344+ standard permutation entropy only measures the information in the ordinal pattern, "motif".
345345
346346 Parameters
347347 ----------
@@ -385,6 +385,7 @@ def weighted_permutation_entropy(time_series, order=2, delay=1, normalize=False)
385385 >>> # Return a value comprised between 0 and 1.
386386 >>> print(permutation_entropy(x, order=3, normalize=True))
387387 0.547
388+
388389 """
389390 x = time_delay_embedding (time_series , embedding_dimension = order , delay = delay )
390391
@@ -406,14 +407,13 @@ def weighted_permutation_entropy(time_series, order=2, delay=1, normalize=False)
406407 wpe = - np .dot (pw , b )
407408
408409 if normalize :
409- wpe /= np .log2 (np . math .factorial (order ))
410+ wpe /= np .log2 (math .factorial (order ))
410411
411412 return wpe
412413
413414
414415def composite_multiscale_entropy (time_series , sample_length , scale , tolerance = None ):
415- """
416- Composite Multiscale Entropy of the given time series
416+ """Calculate Composite Multiscale Entropy.
417417
418418 Parameters
419419 ----------
@@ -435,6 +435,7 @@ def composite_multiscale_entropy(time_series, sample_length, scale, tolerance=No
435435 ----------
436436 .. [1] Wu, Shuen-De, et al. "Time series analysis using
437437 composite multiscale entropy." Entropy 15.3 (2013): 1069-1084.
438+
438439 """
439440 if tolerance is None :
440441 tolerance = 0.1 * np .std (time_series )
0 commit comments