diff --git a/antropy/entropy.py b/antropy/entropy.py index 2e7c7a9..197da5c 100644 --- a/antropy/entropy.py +++ b/antropy/entropy.py @@ -1,4 +1,5 @@ """Entropy functions""" + import numpy as np from numba import jit, types from math import factorial, log @@ -408,7 +409,10 @@ def _app_samp_entropy(x, order, r, metric="chebyshev", approximate=True): return phi -@jit((types.Array(types.float64, 1, "C", readonly=True), types.int32, types.float64), nopython=True) +@jit( + (types.Array(types.float64, 1, "C", readonly=True), types.int32, types.float64), + nopython=True, +) def _numba_sampen(sequence, order, r): """ Fast evaluation of the sample entropy using Numba. diff --git a/antropy/fractal.py b/antropy/fractal.py index 74a857a..744dba9 100644 --- a/antropy/fractal.py +++ b/antropy/fractal.py @@ -1,4 +1,5 @@ """Fractal functions""" + import numpy as np from numba import jit, types from math import log, floor @@ -183,15 +184,18 @@ def katz_fd(x, axis=-1): 1.0000 """ x = np.asarray(x) + + # Define total length of curve dists = np.abs(np.diff(x, axis=axis)) - ll = dists.sum(axis=axis) - ln = np.log10(ll / dists.mean(axis=axis)) - aux_d = x - np.take(x, indices=[0], axis=axis) - d = np.max(np.abs(aux_d), axis=axis) - kfd = np.squeeze(ln / (ln + np.log10(d / ll))) - if not kfd.ndim: - kfd = kfd.item() - return kfd + L = np.sum(dists, axis=axis) + + # Average distance between successive points + a = np.mean(dists, axis=axis) + + # Compute the farthest distance between starting point and any other point + d = np.max(np.abs(x - x.take([0], axis=axis)), axis=axis) + kfd_val = np.log10(L / a) / (np.log10(d / a)) + return kfd_val @jit((types.Array(types.float64, 1, "C", readonly=True), types.int32), nopython=True) diff --git a/antropy/tests/test_entropy.py b/antropy/tests/test_entropy.py index 3d900a8..5678d17 100644 --- a/antropy/tests/test_entropy.py +++ b/antropy/tests/test_entropy.py @@ -1,4 +1,5 @@ """Test entropy functions.""" + import unittest import numpy as np from numpy.testing import assert_equal diff --git a/antropy/tests/test_fractal.py b/antropy/tests/test_fractal.py index ca1a69c..4ce48d0 100644 --- a/antropy/tests/test_fractal.py +++ b/antropy/tests/test_fractal.py @@ -1,12 +1,14 @@ """Test fractal dimension functions.""" + import unittest import numpy as np from numpy.testing import assert_equal from numpy import apply_along_axis as aal from antropy import petrosian_fd, katz_fd, higuchi_fd, detrended_fluctuation +import stochastic.processes.noise as sn -from utils import RANDOM_TS, NORMAL_TS, PURE_SINE, ARANGE, TEST_DTYPES +from utils import RANDOM_TS, NORMAL_TS, PURE_SINE, PURE_COSINE, ARANGE, TEST_DTYPES PPG_SIGNAL = np.array( [ @@ -55,8 +57,9 @@ def test_katz_fd(self): x_k = [0.0, 0.0, 2.0, -2.0, 0.0, -1.0, -1.0, 0.0] self.assertEqual(np.round(katz_fd(x_k), 3), 5.783) # 2D data - assert_equal(aal(katz_fd, axis=1, arr=data), katz_fd(data)) - assert_equal(aal(katz_fd, axis=0, arr=data), katz_fd(data, axis=0)) + data_kfd = np.vstack((RANDOM_TS, NORMAL_TS, PURE_SINE, PURE_COSINE, ARANGE)) + assert_equal(aal(katz_fd, axis=1, arr=data_kfd), katz_fd(data_kfd)) + assert_equal(aal(katz_fd, axis=0, arr=data_kfd), katz_fd(data_kfd, axis=0)) def test_higuchi_fd(self): """Test for function `higuchi_fd`. diff --git a/antropy/tests/utils.py b/antropy/tests/utils.py index a8c4a04..3221a58 100644 --- a/antropy/tests/utils.py +++ b/antropy/tests/utils.py @@ -5,6 +5,7 @@ NORMAL_TS = np.random.normal(size=3000) RANDOM_TS_LONG = np.random.rand(6000) PURE_SINE = np.sin(2 * np.pi * 1 * np.arange(3000) / 100) +PURE_COSINE = np.cos(2 * np.pi * 1 * np.arange(3000) / 100) ARANGE = np.arange(3000) # Data types for which to test input array compatibility diff --git a/antropy/utils.py b/antropy/utils.py index ba06205..10ac2e8 100644 --- a/antropy/utils.py +++ b/antropy/utils.py @@ -1,4 +1,5 @@ """Helper functions""" + import numpy as np from numba import jit from math import log, floor