You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: R/03-R_GroupPSDs.Rmd
+66-50Lines changed: 66 additions & 50 deletions
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,9 @@ from fooof import FOOOF,FOOOFGroup
37
37
# Import useful parameterization related utilities and plot functions
38
38
from fooof.bands import Bands
39
39
from fooof.analysis import get_band_peak_fg
40
+
from fooof.utils import trim_spectrum
41
+
from fooof.data import FOOOFSettings
42
+
from fooof.plts import plot_spectrum
40
43
from fooof.plts.periodic import plot_peak_fits, plot_peak_params
41
44
from fooof.plts.aperiodic import plot_aperiodic_params, plot_aperiodic_fits
42
45
@@ -66,6 +69,12 @@ print(freqs.shape)
66
69
print(spectra.shape)
67
70
```
68
71
72
+
```{python, message = FALSE}
73
+
# Get the number of participants
74
+
n_subjs = spectra.shape[0]
75
+
print('There are {:d} participants'.format(n_subjs))
76
+
```
77
+
69
78
### Fit power spectra
70
79
```{python, message = FALSE}
71
80
# Define `peak_width_limit` setting
@@ -166,24 +175,29 @@ plt.show()
166
175
There are no strict guidelines about optimal parameters that will be appropriate across data sets and recording modalities. We suggest applying a data-driven approach to tune model fitting for optimal performance, while taking into account your expectations about periodic and aperiodic activity given the data, the question of interest, and prior findings.
167
176
168
177
One option is to parameterize a subset of data to evaluate the appropriateness of model fit settings prior to fitting each power spectrum in the data set. Here, we test parameters on a randomly selected 10% of the data.
First, lets randomly sub-sample 10% of the power spectra that we will use to test model settings.
181
+
```{python, message = FALSE}
182
+
# Set random seed
183
+
np.random.seed(1)
184
+
```
175
185
176
-
# Save object as array for specparam fitting
177
-
spectra_sub_array = spectra_sub.to_numpy()
186
+
```{python, message = FALSE}
187
+
# Define settings for subsampling a selection of power spectra
188
+
subsample_frac = 0.10
189
+
n_sample = int(n_subjs * subsample_frac)
178
190
```
179
191
180
-
Here, we define settings for two models to be fit and compared. A user could also use the `trim_spectrum` to define the frequency ranges for each model; see the [specparam website](https://fooof-tools.github.io/fooof/generated/fooof.utils.trim_spectrum.html#fooof.utils.trim_spectrum) for details on how to use this utility.
181
192
```{python, message = FALSE}
182
-
# Create frequency vector for model #1 ("m1", 2-20 Hz) and model #2 ("m2", 3-40 Hz)
Alternatively, you can fit the subset of data with `FOOOFGroup`, with different settings for each model object.
251
+
#### Other ways to manage settings
252
+
Another way to manage model settings is with the `FOOOFSettings` object. Here we will redefine group model objects (`FOOOFGroup`), again using different settings for each one.
245
253
```{python, message = FALSE}
246
-
# Initialize model objects for spectral parameterization, with some settings
Note that when fitting power spectra, you can specify a fit range to fit the model to, so you don't have to explicitly trim the spectra. Here we will refit the example data, specifying the fit range, and then save the group reports.
251
267
```{python, message = FALSE}
252
268
# Fit group PSD over the 2-20 Hz and 3-40 Hz ranges, respectively
Note that when fitting power spectra, you can specify a fit range to fit the model to, so you don't have to explicitly trim the spectra. Here we will refit the example data, specifying the fit range, and then save the group reports.
120
134
```{python, message = FALSE}
121
135
# Fit group PSD over the 2-20 Hz and 3-40 Hz ranges, respectively
122
-
fg1.fit(freqs, spectra_sub_array, m1_PSD_range)
123
-
fg2.fit(freqs, spectra_sub_array, m2_PSD_range)
136
+
fg1.fit(freqs, spectra_subsample, m1_PSD_range)
137
+
fg2.fit(freqs, spectra_subsample, m2_PSD_range)
124
138
```
125
139
126
140
```{python, message = FALSE}
127
-
# Print and save subset results and plots of fit parameters, for further examination
141
+
# Save subset results and plots of fit parameters, for further examination
Based on model fit results from the subset of participants, published findings, and our expectations of data in this sample, we will move forward with fitting the group power spectra over the 3-40 Hz frequency range with settings from the *fg2* model object.
134
148
```{python, message = FALSE}
135
-
# Initialize a model object for spectral parameterization, with some settings
149
+
# Initialize a model object for spectral parameterization
0 commit comments