Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Katz_FD #36

Merged
merged 13 commits into from
Dec 14, 2024
6 changes: 5 additions & 1 deletion antropy/entropy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Entropy functions"""

import numpy as np
from numba import jit, types
from math import factorial, log
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 12 additions & 8 deletions antropy/fractal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Fractal functions"""

import numpy as np
from numba import jit, types
from math import log, floor
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions antropy/tests/test_entropy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test entropy functions."""

import unittest
import numpy as np
from numpy.testing import assert_equal
Expand Down
9 changes: 6 additions & 3 deletions antropy/tests/test_fractal.py
Original file line number Diff line number Diff line change
@@ -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(
[
Expand Down Expand Up @@ -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`.
Expand Down
1 change: 1 addition & 0 deletions antropy/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions antropy/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Helper functions"""

import numpy as np
from numba import jit
from math import log, floor
Expand Down
Loading