From 6147d68297db7931e71463071cc9d94b7f53d76a Mon Sep 17 00:00:00 2001 From: sronilsson Date: Wed, 11 Oct 2023 16:39:18 +0000 Subject: [PATCH] cleaned --- simba/mixins/timeseries_features_mixin.py | 50 +++++++++++++++-------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/simba/mixins/timeseries_features_mixin.py b/simba/mixins/timeseries_features_mixin.py index d98968c7b..31e60e378 100644 --- a/simba/mixins/timeseries_features_mixin.py +++ b/simba/mixins/timeseries_features_mixin.py @@ -1,5 +1,6 @@ -from numba import njit, prange import numpy as np +from numba import njit, prange + class TimeseriesFeatureMixin(object): @@ -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 @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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 @@ -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 @@ -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): @@ -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] \ No newline at end of file + return np.linalg.lstsq(x, L.astype(np.float32))[0][0]