From 32c76d195fad44bf49ea922b575f7789c9d28e51 Mon Sep 17 00:00:00 2001 From: "A.Gatti" Date: Thu, 5 Apr 2018 23:35:34 -0400 Subject: [PATCH 1/2] updated multiscale_entropy to allow changing the sample_length that is input into sample_entropy. Re-used the variable sample_length do to so. Set the default to m=2, the value proposed in the original MSE paper by Costa linked at ref[2]. Renamed the sample_length factor to be scale_factor, as this is the named used to define the coinciding variable (Tau) in the same Costa paper - ref[2]. --- pyentrp/entropy.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pyentrp/entropy.py b/pyentrp/entropy.py index b484663..0895bd4 100644 --- a/pyentrp/entropy.py +++ b/pyentrp/entropy.py @@ -141,13 +141,14 @@ def sample_entropy(time_series, sample_length, tolerance=None): return se -def multiscale_entropy(time_series, sample_length, tolerance): +def multiscale_entropy(time_series, scale_factor, tolerance, sample_length=2): """Calculate the Multiscale Entropy of the given time series considering different time-scales of the time series. Args: time_series: Time series for analysis - sample_length: Bandwidth or group of points + scale_factor: Calculate MSE upto this sized scale factor + sample_length: Number of sequential points of the time series - input to sample_entropy tolerance: Tolerance (default = 0.1...0.2 * std(time_series)) Returns: @@ -155,6 +156,7 @@ def multiscale_entropy(time_series, sample_length, tolerance): Reference: [1] http://en.pudn.com/downloads149/sourcecode/math/detail646216_en.html + [2] https://dbiom.org/files/publications/Peng_MultiscaleEntropyAnalysisComplexPhysiologicTimeSeries.pdf """ n = len(time_series) mse = np.zeros((1, sample_length)) @@ -166,7 +168,7 @@ def multiscale_entropy(time_series, sample_length, tolerance): num = sum(time_series[j * (i + 1): (j + 1) * (i + 1)]) den = i + 1 temp_ts[j] = float(num) / float(den) - se = sample_entropy(temp_ts, 1, tolerance) + se = sample_entropy(temp_ts, sample_length, tolerance) mse[0, i] = se return mse[0] From 43423f8934378d5c5fb4a0c5cee033433617d43d Mon Sep 17 00:00:00 2001 From: "A.Gatti" Date: Fri, 6 Apr 2018 00:07:05 -0400 Subject: [PATCH 2/2] Updated multiscale_entropy - forgot to change scale_length within function for last commit. It appears that changing sample_length to 2 breaks sample_entropy - it now outputs 2 variables instead of one. The Neurokit repo (https://github.com/neuropsychology/NeuroKit.py/blob/master/neurokit/signal/complexity.py) uses the nolds package to calculate sample_entropy --- pyentrp/entropy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyentrp/entropy.py b/pyentrp/entropy.py index 0895bd4..4a88bd1 100644 --- a/pyentrp/entropy.py +++ b/pyentrp/entropy.py @@ -159,9 +159,9 @@ def multiscale_entropy(time_series, scale_factor, tolerance, sample_length=2): [2] https://dbiom.org/files/publications/Peng_MultiscaleEntropyAnalysisComplexPhysiologicTimeSeries.pdf """ n = len(time_series) - mse = np.zeros((1, sample_length)) + mse = np.zeros((1, scale_factor)) - for i in range(sample_length): + for i in range(scale_factor): b = int(np.fix(n / (i + 1))) temp_ts = [0] * int(b) for j in range(b):