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

added a fitfun type and class #49

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pyrasa/utils/aperiodic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pandas as pd
from scipy.optimize import curve_fit

from pyrasa.utils.types import SlopeFit
from pyrasa.utils.types import SlopeFit, FitFun


def fixed_model(x: np.ndarray, b0: float, b: float) -> np.ndarray:
Expand Down Expand Up @@ -62,14 +62,16 @@ def _get_gof(psd: np.ndarray, psd_pred: np.ndarray, k: int, fit_func: str, semi_
def _compute_slope(
aperiodic_spectrum: np.ndarray,
freq: np.ndarray,
fit_func: str | Callable,
fit_func: AbstractFitFun,
fit_bounds: tuple | None = None,
scale_factor: float | int = 1,
curv_kwargs: dict = {},
semi_log: bool = True,
) -> tuple[pd.DataFrame, pd.DataFrame]:
"""get the slope of the aperiodic spectrum"""

if curv_kwargs is None:
curv_kwargs = {}

if isinstance(fit_func, str):
off_guess = [aperiodic_spectrum[0]] if fit_bounds is None else fit_bounds[0]
exp_guess = (
Expand All @@ -81,6 +83,7 @@ def _compute_slope(
assert fit_func in valid_slope_functions, f'The slope fitting function has to be in {valid_slope_functions}'

if fit_func == 'fixed':
fit_func_object = FixedFitFun(data)
fit_f = fixed_model

curv_kwargs['p0'] = np.array(off_guess + exp_guess)
Expand Down Expand Up @@ -129,7 +132,7 @@ def _compute_slope(

else:
if semi_log:
p, _ = curve_fit(fit_func, freq, np.log10(aperiodic_spectrum), **curv_kwargs)
p, _ = curve_fit(fit_func_object, freq, np.log10(aperiodic_spectrum), **fit_func_object.curve_kwargs)
else:
p, _ = curve_fit(fit_func, freq, aperiodic_spectrum, **curv_kwargs)

Expand Down
29 changes: 29 additions & 0 deletions pyrasa/utils/fitfuncs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import abc
import numpy as np


class AbstractFitFun(abc.ABC):
def __init__(self, *args: Any, **kwargs: Any):
pass

@abc.abstractmethod
def __call__(self, x: np.ndarray, *args: float, **kwargs: float) -> np.ndarray:
pass

@property
def curve_kwargs(self) -> dict[str, Any]:
return {}


class FixedFitFun(AbstractFitFun):
def __init__(self, x: np.ndarray):
self.x = x

def __call__(self, x: np.ndarray, b0: float, b: float, *args: float, **kwargs: float) -> np.ndarray:
y_hat = b0 - np.log10(x ** b)

return y_hat

@property
def curve_kwargs(self) -> dict[str, Any]:
return {"b0": 0.0, "b": 0.0}
4 changes: 4 additions & 0 deletions pyrasa/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def __call__(
) -> np.ndarray: ...


class FitFun(Protocol):
def __call__(self, x: np.ndarray, *args: float, **kwargs: float) -> np.ndarray: ...


class IrasaSprintKwargsTyped(TypedDict):
mfft: int
hop: int
Expand Down
Loading