Skip to content

Commit

Permalink
Merge pull request #901 from neuropsychology/dev
Browse files Browse the repository at this point in the history
0.2.7
  • Loading branch information
DominiqueMakowski authored Nov 6, 2023
2 parents ce33299 + c1e38e1 commit 3d004b4
Show file tree
Hide file tree
Showing 22 changed files with 804 additions and 382 deletions.
26 changes: 15 additions & 11 deletions docs/functions/complexity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ Fractal Dimension
Entropy
^^^^^^^^^^^^^^^^^
*entropy_shannon()*
"""""""""""""""""""
"""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_shannon

*entropy_maximum()*
"""""""""""""""""""
"""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_maximum

*entropy_differential()*
Expand All @@ -103,6 +103,18 @@ Entropy
"""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_renyi

*entropy_approximate()*
""""""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_approximate

*entropy_sample()*
""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_sample

*entropy_quadratic()*
""""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_quadratic

*entropy_cumulativeresidual()*
"""""""""""""""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_cumulativeresidual
Expand Down Expand Up @@ -155,14 +167,6 @@ Entropy
"""""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_ofentropy

*entropy_approximate()*
""""""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_approximate

*entropy_sample()*
""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_sample

*entropy_permutation()*
""""""""""""""""""""""""
.. autofunction:: neurokit2.complexity.entropy_permutation
Expand Down Expand Up @@ -274,4 +278,4 @@ Joint/Multivariate
.. .. automodule:: neurokit2.complexity
.. :members:
.. :exclude-members: complexity, complexity_delay, complexity_dimension, complexity_tolerance, complexity_k, fractal_katz, fractal_petrosian, fractal_sevcik, fractal_nld, fractal_psdslope, fractal_higuchi, fractal_correlation, entropy_shannon, entropy_maximum, entropy_differential, entropy_tsallis, entropy_renyi, entropy_cumulativeresidual, entropy_svd, entropy_spectral, entropy_phase, entropy_grid, entropy_attention, entropy_increment, entropy_slope, entropy_symbolicdynamic, entropy_dispersion, entropy_ofentropy, entropy_approximate, entropy_sample, entropy_permutation, entropy_bubble, entropy_range, entropy_fuzzy, entropy_multiscale, entropy_hierarchical, fisher_information, complexity_hjorth, complexity_lempelziv, complexity_relativeroughness, fractal_hurst, complexity_lyapunov, complexity_rqa, fractal_mandelbrot, complexity_simulate, complexity_attractor, complexity_symbolize, complexity_coarsegraining, complexity_ordinalpatterns, recurrence_matrix, entropy_shannon_joint, entropy_rate
.. :exclude-members: complexity, complexity_delay, complexity_dimension, complexity_tolerance, complexity_k, fractal_katz, fractal_petrosian, fractal_sevcik, fractal_nld, fractal_psdslope, fractal_higuchi, fractal_correlation, entropy_shannon, entropy_maximum, entropy_differential, entropy_tsallis, entropy_renyi, entropy_cumulativeresidual, entropy_svd, entropy_spectral, entropy_phase, entropy_grid, entropy_attention, entropy_increment, entropy_slope, entropy_symbolicdynamic, entropy_dispersion, entropy_ofentropy, entropy_approximate, entropy_sample, entropy_permutation, entropy_bubble, entropy_range, entropy_fuzzy, entropy_multiscale, entropy_hierarchical, fisher_information, complexity_hjorth, complexity_lempelziv, complexity_relativeroughness, fractal_hurst, complexity_lyapunov, complexity_rqa, fractal_mandelbrot, complexity_simulate, complexity_attractor, complexity_symbolize, complexity_coarsegraining, complexity_ordinalpatterns, recurrence_matrix, entropy_shannon_joint, entropy_rate, entropy_quadratic
2 changes: 1 addition & 1 deletion neurokit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from .video import *

# Info
__version__ = "0.2.6"
__version__ = "0.2.7"


# Maintainer info
Expand Down
6 changes: 5 additions & 1 deletion neurokit2/complexity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .entropy_permutation import entropy_permutation
from .entropy_phase import entropy_phase
from .entropy_power import entropy_power
from .entropy_quadratic import entropy_quadratic
from .entropy_range import entropy_range
from .entropy_rate import entropy_rate
from .entropy_renyi import entropy_renyi
Expand Down Expand Up @@ -96,7 +97,9 @@
complexity_rcmse = functools.partial(entropy_multiscale, method="RCMSEn")
complexity_fuzzymse = functools.partial(entropy_multiscale, fuzzy=True)
complexity_fuzzycmse = functools.partial(entropy_multiscale, method="CMSEn", fuzzy=True)
complexity_fuzzyrcmse = functools.partial(entropy_multiscale, method="RCMSEn", fuzzy=True)
complexity_fuzzyrcmse = functools.partial(
entropy_multiscale, method="RCMSEn", fuzzy=True
)


complexity_dfa = fractal_dfa
Expand Down Expand Up @@ -175,6 +178,7 @@
"entropy_symbolicdynamic",
"entropy_cumulativeresidual",
"entropy_approximate",
"entropy_quadratic",
"entropy_bubble",
"entropy_coalition",
"entropy_sample",
Expand Down
33 changes: 28 additions & 5 deletions neurokit2/complexity/complexity_decorrelation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from ..signal import signal_autocor


def complexity_decorrelation(signal):
def complexity_decorrelation(signal, show=False):
"""**Decorrelation Time (DT)**
The decorrelation time (DT) is defined as the time (in samples) of the first zero crossing of
Expand All @@ -17,6 +18,8 @@ def complexity_decorrelation(signal):
----------
signal : Union[list, np.array, pd.Series]
The signal (i.e., a time series) in the form of a vector of values.
show : bool
If True, will return a plot of the autocorrelation.
Returns
-------
Expand All @@ -36,11 +39,15 @@ def complexity_decorrelation(signal):
import neurokit2 as nk
# Simulate a signal with duration os 2s
signal = nk.signal_simulate(duration=2, frequency=[5, 9, 12])
# Simulate a signal
signal = nk.signal_simulate(duration=5, sampling_rate=100, frequency=[5, 6], noise=0.5)
# Compute DT
dt, _ = nk.complexity_decorrelation(signal)
@savefig p_complexity_decorrelation1.png scale=100%
dt, _ = nk.complexity_decorrelation(signal, show=True)
@suppress
plt.close()
dt
References
Expand All @@ -60,12 +67,28 @@ def complexity_decorrelation(signal):
)

# Unbiased autocor (see https://github.com/mne-tools/mne-features/)
autocor, _ = signal_autocor(signal, method="unbiased")
autocor, _ = signal_autocor(signal, unbiased=True)

# Get zero-crossings
zc = np.diff(np.sign(autocor)) != 0
if np.any(zc):
dt = np.argmax(zc) + 1
else:
dt = -1

if show is True:
# Max length of autocorrelation to plot
max_len = int(dt * 4)
if max_len > len(autocor):
max_len = len(autocor)

plt.plot(autocor[0:max_len])
plt.xlabel("Lag")
plt.ylabel("Autocorrelation")
plt.xticks(np.arange(0, max_len, step=dt).astype(int))
plt.axvline(dt, color="red", linestyle="--", label=f"DT = {dt}")
plt.axhline(0, color="black", linestyle="--")
plt.title("Decorrelation Time (DT)")
plt.legend()

return dt, {}
Loading

0 comments on commit 3d004b4

Please sign in to comment.