forrestbao / pyeeg

Python + EEG/MEG = PyEEG
GNU General Public License v3.0
241 stars 85 forks source link

Optimize ap_entropy, samp_entropy #11

Closed bnaul closed 8 years ago

bnaul commented 8 years ago

Replace Python for-loop implementations of ap_entropy, samp_entropy with faster numpy-based implementations: a 3-dimensional array is created which stores the differences between the corresponding entries of each subsequence, and the number of "in range" sequences can be computed easily by aggregating the entries of this array.

Timing example:

np.random.seed(0); x = np.cumsum(np.random.normal(size=10000))
%time pyeeg.ap_entropy_new(x, 3, np.std(x)/5.) #  optimized version
CPU times: user 9.45 s, sys: 3.33 s, total: 12.8 s
Wall time: 12.9 s
Out: 0.033903800117190248

%time pyeeg.ap_entropy_old(x, 3, np.std(x)/5.) #  pure Python version
CPU times: user 1min 28s, sys: 385 ms, total: 1min 29s
Wall time: 1min 29s
Out: 0.033903800117190248

Also fixes a couple of off-by-1 errors in computing Cm and Cmp (the numbers of "in range" subsequences); the actual values will be (negligibly) different from the old ones, but code to produce exactly the same results is included (but commented out by default).