|
31 | 31 | import matplotlib.pyplot as plt |
32 | 32 |
|
33 | 33 | # Import the parameterization model objects |
34 | | -from fooof import FOOOF, FOOOFGroup |
| 34 | +from specparam import SpectralModel, SpectralGroupModel |
35 | 35 |
|
36 | 36 | # Import useful parameterization related utilities and plot functions |
37 | | -from fooof.bands import Bands |
38 | | -from fooof.analysis import get_band_peak_fg |
39 | | -from fooof.utils import trim_spectrum |
40 | | -from fooof.utils.data import subsample_spectra |
41 | | -from fooof.sim.gen import gen_aperiodic |
42 | | -from fooof.data import FOOOFSettings |
43 | | -from fooof.plts.templates import plot_hist |
44 | | -from fooof.plts.spectra import plot_spectra |
45 | | -from fooof.plts.periodic import plot_peak_fits, plot_peak_params |
46 | | -from fooof.plts.aperiodic import plot_aperiodic_params, plot_aperiodic_fits |
| 37 | +from specparam.bands import Bands |
| 38 | +from specparam.analysis import get_band_peak_group |
| 39 | +from specparam.utils import trim_spectrum |
| 40 | +from specparam.utils.data import subsample_spectra |
| 41 | +from specparam.sim.gen import gen_aperiodic |
| 42 | +from specparam.data import ModelSettings |
| 43 | +from specparam.plts.templates import plot_hist |
| 44 | +from specparam.plts.spectra import plot_spectra |
| 45 | +from specparam.plts.periodic import plot_peak_fits, plot_peak_params |
| 46 | +from specparam.plts.aperiodic import plot_aperiodic_params, plot_aperiodic_fits |
47 | 47 |
|
48 | 48 | # Import functions to examine frequency-by-frequency error of model fits |
49 | | -from fooof.analysis.error import compute_pointwise_error_fm, compute_pointwise_error_fg |
| 49 | +from specparam.analysis.error import compute_pointwise_error_model, compute_pointwise_error_group |
50 | 50 |
|
51 | 51 | # Import helper utility to access data |
52 | | -from fooof.utils.download import fetch_fooof_data |
| 52 | +from specparam.utils.download import fetch_example_data |
53 | 53 |
|
54 | 54 | ################################################################################################### |
55 | 55 | # Access Example Data |
|
67 | 67 | data_path = Path('data') |
68 | 68 |
|
69 | 69 | # Collect the example data |
70 | | -fetch_fooof_data('freqs.csv', data_path, data_url) |
71 | | -fetch_fooof_data('indv.csv', data_path, data_url) |
| 70 | +fetch_example_data('freqs.csv', data_path, data_url) |
| 71 | +fetch_example_data('indv.csv', data_path, data_url) |
72 | 72 |
|
73 | 73 | ################################################################################################### |
74 | 74 | # Fitting an Individual Power Spectrum |
|
106 | 106 | ################################################################################################### |
107 | 107 |
|
108 | 108 | # Initialize a model object for spectral parameterization, with some settings |
109 | | -fm = FOOOF(peak_width_limits=peak_width, max_n_peaks=n_peaks, |
110 | | - min_peak_height=peak_height, verbose=False) |
| 109 | +fm = SpectralModel(peak_width_limits=peak_width, max_n_peaks=n_peaks, |
| 110 | + min_peak_height=peak_height, verbose=False) |
111 | 111 |
|
112 | 112 | # Fit individual PSD over 3-40 Hz range |
113 | 113 | fm.report(freqs, spectrum, PSD_range) |
|
226 | 226 | # It can be useful to plot frequency-by-frequency error of the model fit, |
227 | 227 | # to identify where in frequency space the spectrum is (or is not) being fit well. |
228 | 228 | # When fitting individual spectrum, this can be accomplished using the |
229 | | -# `compute_pointwise_error_fm` function. |
| 229 | +# `compute_pointwise_error_model` function. |
230 | 230 | # |
231 | 231 | # In this case, we can see that error fluctuates around 0.05, which is the same as |
232 | 232 | # the mean absolute error for the model (MAE). There are points in the spectrum where |
|
237 | 237 | ################################################################################################### |
238 | 238 |
|
239 | 239 | # Plot frequency-by-frequency error |
240 | | -compute_pointwise_error_fm(fm, plot_errors=True) |
| 240 | +compute_pointwise_error_model(fm, plot_errors=True) |
241 | 241 |
|
242 | 242 | ################################################################################################### |
243 | 243 |
|
244 | 244 | # Compute the frequency-by-frequency errors |
245 | | -errs_fm = compute_pointwise_error_fm(fm, plot_errors=False, return_errors=True) |
| 245 | +errs_fm = compute_pointwise_error_model(fm, plot_errors=False, return_errors=True) |
246 | 246 |
|
247 | 247 | ################################################################################################### |
248 | 248 |
|
249 | 249 | # Note that the average of this error is the same as the global error stored |
250 | 250 | print('Average freq-by-freq error:\t {:1.3f}'.format(np.mean(errs_fm))) |
251 | | -print('FOOOF model fit error: \t\t {:1.3f}'.format(fm.error_)) |
| 251 | +print('Total model fit error: \t\t {:1.3f}'.format(fm.error_)) |
252 | 252 |
|
253 | 253 | ################################################################################################### |
254 | 254 | # Fitting a Group of Power Spectra |
|
265 | 265 | ################################################################################################### |
266 | 266 |
|
267 | 267 | # Collect the example data |
268 | | -fetch_fooof_data('freqs.csv', data_path, data_url) |
269 | | -fetch_fooof_data('eop.csv', data_path, data_url) |
| 268 | +fetch_example_data('freqs.csv', data_path, data_url) |
| 269 | +fetch_example_data('eop.csv', data_path, data_url) |
270 | 270 |
|
271 | 271 | ################################################################################################### |
272 | 272 |
|
|
288 | 288 | ################################################################################################### |
289 | 289 |
|
290 | 290 | # Initialize a model object for spectral parameterization, with some settings |
291 | | -fg = FOOOFGroup(peak_width_limits=peak_width, max_n_peaks=n_peaks, |
292 | | - min_peak_height=peak_height, verbose=False) |
| 291 | +fg = SpectralGroupModel(peak_width_limits=peak_width, max_n_peaks=n_peaks, |
| 292 | + min_peak_height=peak_height, verbose=False) |
293 | 293 |
|
294 | 294 | # Fit group PSDs over the 3-40 Hz range |
295 | 295 | fg.fit(freqs, spectra, PSD_range) |
|
311 | 311 | # As with the individual model object, the `get_params` method can be |
312 | 312 | # used to access model fit attributes. |
313 | 313 | # |
314 | | -# In addition, here we will use a `Bands` object and the `get_band_peak_fg` |
| 314 | +# In addition, here we will use a `Bands` object and the `get_band_peak_group` |
315 | 315 | # function to organize fit peaks into canonical band ranges. |
316 | 316 | # |
317 | 317 |
|
|
342 | 342 | ################################################################################################### |
343 | 343 |
|
344 | 344 | # Extract band-limited peaks information |
345 | | -thetas = get_band_peak_fg(fg, bands.theta) |
346 | | -alphas = get_band_peak_fg(fg, bands.alpha) |
347 | | -betas = get_band_peak_fg(fg, bands.beta) |
| 345 | +thetas = get_band_peak_group(fg, bands.theta) |
| 346 | +alphas = get_band_peak_group(fg, bands.alpha) |
| 347 | +betas = get_band_peak_group(fg, bands.beta) |
348 | 348 |
|
349 | 349 | ################################################################################################### |
350 | 350 | # |
|
451 | 451 | ################################################################################################### |
452 | 452 |
|
453 | 453 | # Fit model object with model 1 settings |
454 | | -fg1 = FOOOFGroup(peak_width_limits=m1_peak_width, max_n_peaks=m1_n_peaks, |
455 | | - min_peak_height=m1_peak_height) |
| 454 | +fg1 = SpectralGroupModel(peak_width_limits=m1_peak_width, max_n_peaks=m1_n_peaks, |
| 455 | + min_peak_height=m1_peak_height) |
456 | 456 | fg1.fit(m1_freq, m1_spectra) |
457 | 457 |
|
458 | 458 | # Create individual reports for model 1 settings (these could be saved and checked) |
459 | 459 | for ind in range(len(fg1)): |
460 | | - temp_model = fg1.get_fooof(ind, regenerate=True) |
| 460 | + temp_model = fg1.get_model(ind, regenerate=True) |
461 | 461 |
|
462 | 462 | ################################################################################################### |
463 | 463 | # |
|
467 | 467 | ################################################################################################### |
468 | 468 |
|
469 | 469 | # Fit model object with model 2 settings |
470 | | -fg2 = FOOOFGroup(peak_width_limits=m2_peak_width, max_n_peaks=m2_n_peaks, |
471 | | - min_peak_height=m2_peak_height) |
| 470 | +fg2 = SpectralGroupModel(peak_width_limits=m2_peak_width, max_n_peaks=m2_n_peaks, |
| 471 | + min_peak_height=m2_peak_height) |
472 | 472 | fg2.fit(m2_freq, m2_spectra) |
473 | 473 |
|
474 | 474 | # Create individual reports for model 2 settings (these could be saved and checked) |
475 | 475 | for ind in range(len(fg2)): |
476 | | - temp_model = fg2.get_fooof(ind, regenerate=True) |
| 476 | + temp_model = fg2.get_model(ind, regenerate=True) |
477 | 477 |
|
478 | 478 | ################################################################################################### |
479 | 479 | # |
480 | | -# There are also other ways to manage settings, for example, using the `FOOOFSettings` object. |
| 480 | +# There are also other ways to manage settings, for example, using the `ModelSettings` object. |
481 | 481 | # |
482 | | -# Here we will redefine group model objects (`FOOOFGroup`), |
| 482 | +# Here we will redefine group model objects (`SpectralGroupModel`), |
483 | 483 | # again using different settings for each one. |
484 | 484 | # |
485 | 485 |
|
486 | 486 | ################################################################################################### |
487 | 487 |
|
488 | 488 | # Define settings for model 1 |
489 | | -settings1 = FOOOFSettings(peak_width_limits=m1_peak_width, max_n_peaks=m1_n_peaks, |
| 489 | +settings1 = ModelSettings(peak_width_limits=m1_peak_width, max_n_peaks=m1_n_peaks, |
490 | 490 | min_peak_height=m1_peak_height, peak_threshold=2., |
491 | 491 | aperiodic_mode='fixed') |
492 | 492 |
|
493 | 493 | # Define settings for model 2 |
494 | | -settings2 = FOOOFSettings(peak_width_limits=m2_peak_width, max_n_peaks=m2_n_peaks, |
| 494 | +settings2 = ModelSettings(peak_width_limits=m2_peak_width, max_n_peaks=m2_n_peaks, |
495 | 495 | min_peak_height=m2_peak_height, peak_threshold=2., |
496 | 496 | aperiodic_mode='fixed') |
497 | 497 |
|
498 | 498 | ################################################################################################### |
499 | 499 |
|
500 | 500 | # Initialize model objects for spectral parameterization, with some settings |
501 | | -fg1 = FOOOFGroup(*settings1) |
502 | | -fg2 = FOOOFGroup(*settings2) |
| 501 | +fg1 = SpectralGroupModel(*settings1) |
| 502 | +fg2 = SpectralGroupModel(*settings2) |
503 | 503 |
|
504 | 504 | ################################################################################################### |
505 | 505 | # |
|
545 | 545 | worst_fit_ind = np.argmax(fg.get_params('error')) |
546 | 546 |
|
547 | 547 | # Extract this model fit from the group |
548 | | -fm = fg.get_fooof(worst_fit_ind, regenerate=True) |
| 548 | +fm = fg.get_model(worst_fit_ind, regenerate=True) |
549 | 549 |
|
550 | 550 | ################################################################################################### |
551 | 551 |
|
|
565 | 565 | underfit_check = [] |
566 | 566 | for ind, res in enumerate(fg): |
567 | 567 | if res.error > underfit_error_threshold: |
568 | | - underfit_check.append(fg.get_fooof(ind, regenerate=True)) |
| 568 | + underfit_check.append(fg.get_model(ind, regenerate=True)) |
569 | 569 |
|
570 | 570 | ################################################################################################### |
571 | 571 |
|
|
585 | 585 | overfit_check = [] |
586 | 586 | for ind, res in enumerate(fg): |
587 | 587 | if res.error < overfit_error_threshold: |
588 | | - overfit_check.append(fg.get_fooof(ind, regenerate=True)) |
| 588 | + overfit_check.append(fg.get_model(ind, regenerate=True)) |
589 | 589 |
|
590 | 590 | ################################################################################################### |
591 | 591 |
|
|
619 | 619 | # It can be useful to plot frequency-by-frequency error of the model fit, |
620 | 620 | # to identify where in frequency space the spectrum is (or is not) being fit well. |
621 | 621 | # When fitting individual spectrum, this can be accomplished using the |
622 | | -# `compute_pointwise_error_fg` function. When plotting the error, the plot line is |
| 622 | +# `compute_pointwise_error_group` function. When plotting the error, the plot line is |
623 | 623 | # the mean error per frequency, across fits, and the shading indicates the standard deviation |
624 | 624 | # of the error, also per frequency. |
625 | 625 | # |
|
634 | 634 | ################################################################################################### |
635 | 635 |
|
636 | 636 | # Plot frequency-by-frequency error |
637 | | -compute_pointwise_error_fg(fg, plot_errors=True) |
| 637 | +compute_pointwise_error_group(fg, plot_errors=True) |
638 | 638 |
|
639 | 639 | ################################################################################################### |
640 | 640 |
|
641 | 641 | # Return the errors - this returns a 2D matrix of errors for all fits |
642 | | -errs_fg = compute_pointwise_error_fg(fg, plot_errors=False, return_errors=True) |
| 642 | +errs_fg = compute_pointwise_error_group(fg, plot_errors=False, return_errors=True) |
643 | 643 |
|
644 | 644 | # Check which frequency has the highest error |
645 | 645 | f_max_err = fg.freqs[np.argmax(np.mean(errs_fg, 0))] |
|
0 commit comments