pyentrp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyentrp-0.1.0/PKG-INFO +26 -0
- pyentrp-0.1.0/pyentrp/__init__.py +0 -0
- pyentrp-0.1.0/pyentrp/entropy.py +259 -0
- pyentrp-0.1.0/setup.cfg +8 -0
- pyentrp-0.1.0/setup.py +44 -0
pyentrp-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 1.1
|
|
2
|
+
Name: pyentrp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Functions on top of NumPy for computing different types of entropy
|
|
5
|
+
Home-page: https://github.com/nikdon/pyEntropy
|
|
6
|
+
Author: Nikolay Donets
|
|
7
|
+
Author-email: nd.startup@gmail.com
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Download-URL: https://github.com/nikdon/pyEntropy/archive/0.1.0.tar.gz
|
|
10
|
+
Description: UNKNOWN
|
|
11
|
+
Keywords: entropy,python,sample entropy,multiscale entropy,permutation entropy,composite multiscale entropy
|
|
12
|
+
Platform: UNKNOWN
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
17
|
+
Classifier: Programming Language :: Python :: 2
|
|
18
|
+
Classifier: Programming Language :: Python :: 2.7
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.4
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.5
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
26
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
File without changes
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from __future__ import unicode_literals
|
|
4
|
+
|
|
5
|
+
import itertools
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def util_pattern_space(time_series, lag, dim):
|
|
10
|
+
"""Create a set of sequences with given lag and dimension
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
time_series: Vector or string of the sample data
|
|
14
|
+
lag: Lag between beginning of sequences
|
|
15
|
+
dim: Dimension (number of patterns)
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
2D array of vectors
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
n = len(time_series)
|
|
22
|
+
|
|
23
|
+
if lag * dim > n:
|
|
24
|
+
raise Exception('Result matrix exceeded size limit, try to change lag or dim.')
|
|
25
|
+
elif lag < 1:
|
|
26
|
+
raise Exception('Lag should be greater or equal to 1.')
|
|
27
|
+
|
|
28
|
+
pattern_space = np.empty((n - lag * (dim - 1), dim))
|
|
29
|
+
for i in range(n - lag * (dim - 1)):
|
|
30
|
+
for j in range(dim):
|
|
31
|
+
pattern_space[i][j] = time_series[i + j * lag]
|
|
32
|
+
|
|
33
|
+
return pattern_space
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def util_standardize_signal(time_series):
|
|
37
|
+
return (time_series - np.mean(time_series)) / np.std(time_series)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def util_granulate_time_series(time_series, scale):
|
|
41
|
+
"""Extract coarse-grained time series
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
time_series: Time series
|
|
45
|
+
scale: Scale factor
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
Vector of coarse-grained time series with given scale factor
|
|
49
|
+
"""
|
|
50
|
+
n = len(time_series)
|
|
51
|
+
b = int(np.fix(n / scale))
|
|
52
|
+
cts = [0] * b
|
|
53
|
+
for i in range(b):
|
|
54
|
+
cts[i] = np.mean(time_series[i * scale: (i + 1) * scale])
|
|
55
|
+
return cts
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def shannon_entropy(time_series):
|
|
59
|
+
"""Return the Shannon Entropy of the sample data.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
time_series: Vector or string of the sample data
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
The Shannon Entropy as float value
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
# Check if string
|
|
69
|
+
if not isinstance(time_series, str):
|
|
70
|
+
time_series = list(time_series)
|
|
71
|
+
|
|
72
|
+
# Create a frequency data
|
|
73
|
+
data_set = list(set(time_series))
|
|
74
|
+
freq_list = []
|
|
75
|
+
for entry in data_set:
|
|
76
|
+
counter = 0.
|
|
77
|
+
for i in time_series:
|
|
78
|
+
if i == entry:
|
|
79
|
+
counter += 1
|
|
80
|
+
freq_list.append(float(counter) / len(time_series))
|
|
81
|
+
|
|
82
|
+
# Shannon entropy
|
|
83
|
+
ent = 0.0
|
|
84
|
+
for freq in freq_list:
|
|
85
|
+
ent += freq * np.log2(freq)
|
|
86
|
+
ent = -ent
|
|
87
|
+
|
|
88
|
+
return ent
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def sample_entropy(time_series, sample_length, tolerance=None):
|
|
92
|
+
"""Calculate and return Sample Entropy of the given time series.
|
|
93
|
+
Distance between two vectors defined as Euclidean distance and can
|
|
94
|
+
be changed in future releases
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
time_series: Vector or string of the sample data
|
|
98
|
+
sample_length: Number of sequential points of the time series
|
|
99
|
+
tolerance: Tolerance (default = 0.1...0.2 * std(time_series))
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
Vector containing Sample Entropy (float)
|
|
103
|
+
|
|
104
|
+
References:
|
|
105
|
+
[1] http://en.wikipedia.org/wiki/Sample_Entropy
|
|
106
|
+
[2] http://physionet.incor.usp.br/physiotools/sampen/
|
|
107
|
+
[3] Madalena Costa, Ary Goldberger, CK Peng. Multiscale entropy analysis
|
|
108
|
+
of biological signals
|
|
109
|
+
"""
|
|
110
|
+
if tolerance is None:
|
|
111
|
+
tolerance = 0.1 * np.std(time_series)
|
|
112
|
+
|
|
113
|
+
n = len(time_series)
|
|
114
|
+
prev = np.zeros(n)
|
|
115
|
+
curr = np.zeros(n)
|
|
116
|
+
A = np.zeros((sample_length, 1)) # number of matches for m = [1,...,template_length - 1]
|
|
117
|
+
B = np.zeros((sample_length, 1)) # number of matches for m = [1,...,template_length]
|
|
118
|
+
|
|
119
|
+
for i in range(n - 1):
|
|
120
|
+
nj = n - i - 1
|
|
121
|
+
ts1 = time_series[i]
|
|
122
|
+
for jj in range(nj):
|
|
123
|
+
j = jj + i + 1
|
|
124
|
+
if abs(time_series[j] - ts1) < tolerance: # distance between two vectors
|
|
125
|
+
curr[jj] = prev[jj] + 1
|
|
126
|
+
temp_ts_length = min(sample_length, curr[jj])
|
|
127
|
+
for m in range(int(temp_ts_length)):
|
|
128
|
+
A[m] += 1
|
|
129
|
+
if j < n - 1:
|
|
130
|
+
B[m] += 1
|
|
131
|
+
else:
|
|
132
|
+
curr[jj] = 0
|
|
133
|
+
for j in range(nj):
|
|
134
|
+
prev[j] = curr[j]
|
|
135
|
+
|
|
136
|
+
N = n * (n - 1) / 2
|
|
137
|
+
B = np.vstack(([N], B[:sample_length - 1]))
|
|
138
|
+
similarity_ratio = A / B
|
|
139
|
+
se = - np.log(similarity_ratio)
|
|
140
|
+
se = np.reshape(se, -1)
|
|
141
|
+
return se
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def multiscale_entropy(time_series, sample_length, tolerance):
|
|
145
|
+
"""Calculate the Multiscale Entropy of the given time series considering
|
|
146
|
+
different time-scales of the time series.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
time_series: Time series for analysis
|
|
150
|
+
sample_length: Bandwidth or group of points
|
|
151
|
+
tolerance: Tolerance (default = 0.1...0.2 * std(time_series))
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
Vector containing Multiscale Entropy
|
|
155
|
+
|
|
156
|
+
Reference:
|
|
157
|
+
[1] http://en.pudn.com/downloads149/sourcecode/math/detail646216_en.html
|
|
158
|
+
"""
|
|
159
|
+
n = len(time_series)
|
|
160
|
+
mse = np.zeros((1, sample_length))
|
|
161
|
+
|
|
162
|
+
for i in range(sample_length):
|
|
163
|
+
b = int(np.fix(n / (i + 1)))
|
|
164
|
+
temp_ts = [0] * int(b)
|
|
165
|
+
for j in range(b):
|
|
166
|
+
num = sum(time_series[j * (i + 1): (j + 1) * (i + 1)])
|
|
167
|
+
den = i + 1
|
|
168
|
+
temp_ts[j] = float(num) / float(den)
|
|
169
|
+
se = sample_entropy(temp_ts, 1, tolerance)
|
|
170
|
+
mse[0, i] = se
|
|
171
|
+
|
|
172
|
+
return mse[0]
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def permutation_entropy(time_series, m, delay):
|
|
176
|
+
"""Calculate the Permutation Entropy
|
|
177
|
+
|
|
178
|
+
Args:
|
|
179
|
+
time_series: Time series for analysis
|
|
180
|
+
m: Order of permutation entropy
|
|
181
|
+
delay: Time delay
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
Vector containing Permutation Entropy
|
|
185
|
+
|
|
186
|
+
Reference:
|
|
187
|
+
[1] Massimiliano Zanin et al. Permutation Entropy and Its Main Biomedical and Econophysics Applications:
|
|
188
|
+
A Review. http://www.mdpi.com/1099-4300/14/8/1553/pdf
|
|
189
|
+
[2] Christoph Bandt and Bernd Pompe. Permutation entropy — a natural complexity
|
|
190
|
+
measure for time series. http://stubber.math-inf.uni-greifswald.de/pub/full/prep/2001/11.pdf
|
|
191
|
+
[3] http://www.mathworks.com/matlabcentral/fileexchange/37289-permutation-entropy/content/pec.m
|
|
192
|
+
"""
|
|
193
|
+
n = len(time_series)
|
|
194
|
+
permutations = np.array(list(itertools.permutations(range(m))))
|
|
195
|
+
c = [0] * len(permutations)
|
|
196
|
+
|
|
197
|
+
for i in range(n - delay * (m - 1)):
|
|
198
|
+
# sorted_time_series = np.sort(time_series[i:i+delay*m:delay], kind='quicksort')
|
|
199
|
+
sorted_index_array = np.array(np.argsort(time_series[i:i + delay * m:delay], kind='quicksort'))
|
|
200
|
+
for j in range(len(permutations)):
|
|
201
|
+
if abs(permutations[j] - sorted_index_array).any() == 0:
|
|
202
|
+
c[j] += 1
|
|
203
|
+
|
|
204
|
+
c = [element for element in c if element != 0]
|
|
205
|
+
p = np.divide(np.array(c), float(sum(c)))
|
|
206
|
+
pe = -sum(p * np.log(p))
|
|
207
|
+
return pe
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def multiscale_permutation_entropy(time_series, m, delay, scale):
|
|
211
|
+
"""Calculate the Multiscale Permutation Entropy
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
time_series: Time series for analysis
|
|
215
|
+
m: Order of permutation entropy
|
|
216
|
+
delay: Time delay
|
|
217
|
+
scale: Scale factor
|
|
218
|
+
|
|
219
|
+
Returns:
|
|
220
|
+
Vector containing Multiscale Permutation Entropy
|
|
221
|
+
|
|
222
|
+
Reference:
|
|
223
|
+
[1] Francesco Carlo Morabito et al. Multivariate Multi-Scale Permutation Entropy for
|
|
224
|
+
Complexity Analysis of Alzheimer’s Disease EEG. www.mdpi.com/1099-4300/14/7/1186
|
|
225
|
+
[2] http://www.mathworks.com/matlabcentral/fileexchange/37288-multiscale-permutation-entropy-mpe/content/MPerm.m
|
|
226
|
+
"""
|
|
227
|
+
mspe = []
|
|
228
|
+
for i in range(scale):
|
|
229
|
+
coarse_time_series = util_granulate_time_series(time_series, i + 1)
|
|
230
|
+
pe = permutation_entropy(coarse_time_series, m, delay)
|
|
231
|
+
mspe.append(pe)
|
|
232
|
+
return mspe
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
# TODO add tests
|
|
236
|
+
def composite_multiscale_entropy(time_series, sample_length, scale):
|
|
237
|
+
"""Calculate the Composite Multiscale Entropy of the given time series.
|
|
238
|
+
|
|
239
|
+
Args:
|
|
240
|
+
time_series: Time series for analysis
|
|
241
|
+
sample_length: Number of sequential points of the time series
|
|
242
|
+
scale: Scale factor
|
|
243
|
+
|
|
244
|
+
Returns:
|
|
245
|
+
Vector containing Composite Multiscale Entropy
|
|
246
|
+
|
|
247
|
+
Reference:
|
|
248
|
+
[1] Wu, Shuen-De, et al. "Time series analysis using
|
|
249
|
+
composite multiscale entropy." Entropy 15.3 (2013): 1069-1084.
|
|
250
|
+
"""
|
|
251
|
+
cmse = np.zeros((1, scale))
|
|
252
|
+
r = np.std(time_series) * 0.15
|
|
253
|
+
|
|
254
|
+
for i in range(scale):
|
|
255
|
+
for j in range(i):
|
|
256
|
+
tmp = util_granulate_time_series(time_series[j:], i + 1)
|
|
257
|
+
cmse[i] += sample_entropy(tmp, sample_length, r) / (i + 1)
|
|
258
|
+
|
|
259
|
+
return cmse
|
pyentrp-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
[bdist_wheel]
|
|
2
|
+
# This flag says that the code is written to work on both Python 2 and Python
|
|
3
|
+
# 3. If at all possible, it is good practice to do this. If you cannot, you
|
|
4
|
+
# will need to generate wheels for each Python version that you support.
|
|
5
|
+
universal=1
|
|
6
|
+
|
|
7
|
+
[metadata]
|
|
8
|
+
description-file = README.md
|
pyentrp-0.1.0/setup.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from distutils.core import setup
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name='pyentrp',
|
|
5
|
+
version='0.1.0',
|
|
6
|
+
description='Functions on top of NumPy for computing different types of entropy',
|
|
7
|
+
url='https://github.com/nikdon/pyEntropy',
|
|
8
|
+
download_url='https://github.com/nikdon/pyEntropy/archive/0.1.0.tar.gz',
|
|
9
|
+
author='Nikolay Donets',
|
|
10
|
+
author_email='nd.startup@gmail.com',
|
|
11
|
+
maintainer='Nikolay Donets',
|
|
12
|
+
maintainer_email='nd.startup@gmail.com',
|
|
13
|
+
license='Apache-2.0',
|
|
14
|
+
packages=['pyentrp'],
|
|
15
|
+
|
|
16
|
+
install_requires=[
|
|
17
|
+
'numpy>=1.7.0 ',
|
|
18
|
+
],
|
|
19
|
+
test_suite="tests.test_entropy",
|
|
20
|
+
|
|
21
|
+
keywords=['entropy', 'python', 'sample entropy', 'multiscale entropy', 'permutation entropy',
|
|
22
|
+
'composite multiscale entropy'],
|
|
23
|
+
|
|
24
|
+
classifiers=[
|
|
25
|
+
'Development Status :: 5 - Production/Stable',
|
|
26
|
+
|
|
27
|
+
'Intended Audience :: Science/Research',
|
|
28
|
+
'Operating System :: OS Independent',
|
|
29
|
+
|
|
30
|
+
'License :: OSI Approved :: Apache Software License',
|
|
31
|
+
|
|
32
|
+
'Programming Language :: Python :: 2',
|
|
33
|
+
'Programming Language :: Python :: 2.7',
|
|
34
|
+
'Programming Language :: Python :: 3',
|
|
35
|
+
'Programming Language :: Python :: 3.3',
|
|
36
|
+
'Programming Language :: Python :: 3.4',
|
|
37
|
+
'Programming Language :: Python :: 3.5',
|
|
38
|
+
'Programming Language :: Python :: 3.6',
|
|
39
|
+
|
|
40
|
+
'Topic :: Scientific/Engineering :: Bio-Informatics',
|
|
41
|
+
'Topic :: Scientific/Engineering :: Information Analysis',
|
|
42
|
+
'Topic :: Scientific/Engineering :: Mathematics',
|
|
43
|
+
],
|
|
44
|
+
)
|