Skip to content

Commit

Permalink
cleaned
Browse files Browse the repository at this point in the history
  • Loading branch information
sronilsson committed Oct 11, 2023
1 parent 50192b5 commit 6147d68
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions simba/mixins/timeseries_features_mixin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from numba import njit, prange
import numpy as np
from numba import njit, prange


class TimeseriesFeatureMixin(object):

Expand All @@ -11,7 +12,7 @@ def __init__(self):
pass

@staticmethod
@njit('(float32[:],)')
@njit("(float32[:],)")
def hjort_parameters(data: np.ndarray):
"""
Jitted compute of Hjorth parameters for a given time series data. Hjorth parameters describe
Expand Down Expand Up @@ -52,7 +53,7 @@ def diff(x):
return activity, mobility, complexity

@staticmethod
@njit('(float32[:], boolean)')
@njit("(float32[:], boolean)")
def local_maxima_minima(data: np.ndarray, maxima: bool) -> np.ndarray:
"""
Jitted compute of the local maxima or minima defined as values which are higher or lower than immediately preceding and proceeding time-series neighbors, repectively.
Expand Down Expand Up @@ -98,7 +99,7 @@ def local_maxima_minima(data: np.ndarray, maxima: bool) -> np.ndarray:
return results[np.argwhere(results[:, 0].T != -1).flatten()]

@staticmethod
@njit('(float32[:], float64)')
@njit("(float32[:], float64)")
def crossings(data: np.ndarray, val: float) -> int:
"""
Jitted compute of the count in time-series where sequential values crosses a defined value.
Expand Down Expand Up @@ -131,8 +132,10 @@ def crossings(data: np.ndarray, val: float) -> int:
return cnt

@staticmethod
@njit('(float32[:], int64, int64, )', cache=True, fastmath=True)
def percentile_difference(data: np.ndarray, upper_pct: int, lower_pct: int) -> float:
@njit("(float32[:], int64, int64, )", cache=True, fastmath=True)
def percentile_difference(
data: np.ndarray, upper_pct: int, lower_pct: int
) -> float:
"""
Jitted compute of the difference between the ``upper`` and ``lower`` percentiles of the data as
a percentage of the median value.
Expand All @@ -156,11 +159,13 @@ def percentile_difference(data: np.ndarray, upper_pct: int, lower_pct: int) -> f
"""

upper_val, lower_val = np.percentile(data, upper_pct), np.percentile(data, lower_pct)
upper_val, lower_val = np.percentile(data, upper_pct), np.percentile(
data, lower_pct
)
return np.abs(upper_val - lower_val) / np.median(data)

@staticmethod
@njit('(float32[:], float64,)', cache=True, fastmath=True)
@njit("(float32[:], float64,)", cache=True, fastmath=True)
def percent_beyond_n_std(data: np.ndarray, n: float) -> float:
"""
Jitted compute of the ratio of values in time-series more than N standard deviations from the mean of the time-series.
Expand Down Expand Up @@ -188,7 +193,7 @@ def percent_beyond_n_std(data: np.ndarray, n: float) -> float:
return np.argwhere(np.abs(data) > target).shape[0] / data.shape[0]

@staticmethod
@njit('(float32[:], int64, int64, )', cache=True, fastmath=True)
@njit("(float32[:], int64, int64, )", cache=True, fastmath=True)
def percent_in_percentile_window(data: np.ndarray, upper_pct: int, lower_pct: int):
"""
Jitted compute of the ratio of values in time-series that fall between the ``upper`` and ``lower`` percentile.
Expand All @@ -212,11 +217,16 @@ def percent_in_percentile_window(data: np.ndarray, upper_pct: int, lower_pct: in
:align: center
"""

upper_val, lower_val = np.percentile(data, upper_pct), np.percentile(data, lower_pct)
return np.argwhere((data <= upper_val) & (data >= lower_val)).flatten().shape[0] / data.shape[0]
upper_val, lower_val = np.percentile(data, upper_pct), np.percentile(
data, lower_pct
)
return (
np.argwhere((data <= upper_val) & (data >= lower_val)).flatten().shape[0]
/ data.shape[0]
)

@staticmethod
@njit('(float32[:],)', fastmath=True, cache=True)
@njit("(float32[:],)", fastmath=True, cache=True)
def petrosian_fractal_dimension(data: np.ndarray) -> float:
"""
Calculate the Petrosian Fractal Dimension (PFD) of a given time series data. The PFD is a measure of the
Expand Down Expand Up @@ -250,10 +260,12 @@ def petrosian_fractal_dimension(data: np.ndarray) -> float:
return -1.0

return np.log10(data.shape[0]) / (
np.log10(data.shape[0]) + np.log10(data.shape[0] / (data.shape[0] + 0.4 * zC)))
np.log10(data.shape[0])
+ np.log10(data.shape[0] / (data.shape[0] + 0.4 * zC))
)

@staticmethod
@njit('(float32[:], int64)')
@njit("(float32[:], int64)")
def higuchi_fractal_dimension(data: np.ndarray, kmax: int = 10):
"""
Jitted compute of the Higuchi Fractal Dimension of a given time series data. The Higuchi Fractal Dimension provides a measure of the fractal
Expand Down Expand Up @@ -284,8 +296,12 @@ def higuchi_fractal_dimension(data: np.ndarray, kmax: int = 10):
"""

L, N = np.zeros(kmax - 1), len(data)
x = np.hstack((-np.log(np.arange(2, kmax + 1)).reshape(-1, 1).astype(np.float32),
np.ones(kmax - 1).reshape(-1, 1).astype(np.float32)))
x = np.hstack(
(
-np.log(np.arange(2, kmax + 1)).reshape(-1, 1).astype(np.float32),
np.ones(kmax - 1).reshape(-1, 1).astype(np.float32),
)
)
for k in prange(2, kmax + 1):
Lk = np.zeros(k)
for m in range(0, k):
Expand All @@ -297,4 +313,4 @@ def higuchi_fractal_dimension(data: np.ndarray, kmax: int = 10):
Laux = 0.01 / k if Laux == 0 else Laux
L[k - 2] = np.log(Laux)

return np.linalg.lstsq(x, L.astype(np.float32))[0][0]
return np.linalg.lstsq(x, L.astype(np.float32))[0][0]

0 comments on commit 6147d68

Please sign in to comment.