-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from schmidtfa/minor_fixes
- Loading branch information
Showing
11 changed files
with
5,629 additions
and
506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#%% | ||
import scipy.signal as dsp | ||
from pyrasa.utils.aperiodic_utils import compute_slope | ||
from pyrasa.utils.fit_funcs import AbstractFitFun | ||
import numpy as np | ||
from neurodsp.sim import sim_powerlaw | ||
from typing import Any | ||
|
||
n_secs = 60 | ||
fs=500 | ||
f_range = [1.5, 300] | ||
exponent = -1.5 | ||
|
||
sig = sim_powerlaw(n_seconds=n_secs, fs=fs, exponent=exponent) | ||
|
||
# test whether recombining periodic and aperiodic spectrum is equivalent to the original spectrum | ||
freqs, psd = dsp.welch(sig, fs, nperseg=int(4 * fs)) | ||
freq_logical = np.logical_and(freqs >= f_range[0], freqs <= f_range[1]) | ||
psd, freqs = psd[freq_logical], freqs[freq_logical] | ||
|
||
|
||
class CustomFitFun(AbstractFitFun): | ||
def func(self, x: np.ndarray, a: float, b: float) -> np.ndarray: | ||
""" | ||
Specparams fixed fitting function. | ||
Use this to model aperiodic activity without a spectral knee | ||
""" | ||
y_hat = a + b * x | ||
|
||
return y_hat | ||
|
||
@property | ||
def curve_kwargs(self) -> dict[str, Any]: | ||
aperiodic_nolog = 10**self.aperiodic_spectrum | ||
off_guess = [aperiodic_nolog[0]] | ||
exp_guess = [ | ||
np.abs(np.log10(aperiodic_nolog[0] / aperiodic_nolog[-1]) / np.log10(self.freq[-1] / self.freq[0])) | ||
] | ||
return { | ||
'maxfev': 10_000, | ||
'ftol': 1e-5, | ||
'xtol': 1e-5, | ||
'gtol': 1e-5, | ||
'p0': np.array(off_guess + exp_guess), | ||
'bounds': ((-np.inf, -np.inf), (np.inf, np.inf)), | ||
} | ||
|
||
#%% | ||
slope_fit = compute_slope(np.log10(psd), np.log10(freqs), fit_func=CustomFitFun) | ||
# %% |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.