Skip to content

Commit

Permalink
FIX KMAX OMG
Browse files Browse the repository at this point in the history
  • Loading branch information
zen-juen committed Aug 24, 2021
1 parent 64156fc commit 71dff56
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions neurokit2/complexity/fractal_higuchi.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,26 @@ def _fractal_higuchi_optimal_k(signal, k_first=2, k_end=60):
"""
k_range = np.arange(k_first, k_end + 1)
slope_values = []
for i in k_range:
slope, _, _, _ = _fractal_higuchi_slope(signal, kmax=i)
for _, k in enumerate(k_range):
slope, _, _, _ = _fractal_higuchi_slope(signal, kmax=k)
slope_values.append(slope)

# Obtain saturation point of slope
optimal_k = [i for i, x in enumerate(slope_values >= 0.85 * np.max(slope_values)) if x][0] + 1
optimal_k = [i for i, x in enumerate(slope_values >= 0.85 * np.max(slope_values)) if x][0]
kmax = k_range[optimal_k]
# If no plateau
if optimal_k <= 2:
if kmax <= 2:
warn(
"The detected kmax value is 2 or less. The change in HFD values across kmax values is dependent "
" on the fractal source data and there may be no plateau in this case."
" Consider choosing a manual kmax value.",
category=NeuroKitWarning
)
grad = np.diff(slope_values) / np.diff(k_range)
optimal_k = np.where(grad == np.max(grad[2:]))[0][0]
optimal_k = np.where(grad == np.max(grad))[0][0]
kmax = k_range[optimal_k]

return optimal_k, k_range, slope_values
return kmax, k_range, slope_values


def _fractal_higuchi_optimal_k_plot(k_range, slope_values, optimal_k, ax=None):
Expand Down

0 comments on commit 71dff56

Please sign in to comment.