|
| 1 | +""" |
| 2 | +Exploring Data Components |
| 3 | +========================= |
| 4 | +
|
| 5 | +This example explores the different data components, exploring the isolated aperiodic |
| 6 | +and periodic components as they are extracted from the data. |
| 7 | +""" |
| 8 | + |
| 9 | +################################################################################################### |
| 10 | + |
| 11 | +# sphinx_gallery_thumbnail_number = 3 |
| 12 | + |
| 13 | +# Import FOOOF model objects |
| 14 | +from fooof import FOOOF, FOOOFGroup |
| 15 | + |
| 16 | +# Import function to plot power spectra |
| 17 | +from fooof.plts.spectra import plot_spectra |
| 18 | + |
| 19 | +# Import simulation functions to create some example data |
| 20 | +from fooof.sim import gen_power_spectrum, gen_group_power_spectra |
| 21 | + |
| 22 | +################################################################################################### |
| 23 | + |
| 24 | +# Simulate example power spectrum |
| 25 | +freqs, powers = gen_power_spectrum([1, 50], [0, 10, 1], [10, 0.25, 2], freq_res=0.25) |
| 26 | + |
| 27 | +# Initialize model object and fit power spectrum |
| 28 | +fm = FOOOF() |
| 29 | +fm.fit(freqs, powers) |
| 30 | + |
| 31 | +################################################################################################### |
| 32 | +# Data Components |
| 33 | +# ~~~~~~~~~~~~~~~ |
| 34 | +# |
| 35 | +# The model fit process includes procedures for isolating aperiodic and periodic components in |
| 36 | +# the data, fitting each of these components separately, and then combining the model components |
| 37 | +# as the final fit (see the Tutorials for further details on these procedures). |
| 38 | +# |
| 39 | +# In doing this process, the model fit procedure computes and stores isolated data components, |
| 40 | +# which are available in the model. |
| 41 | +# |
| 42 | +# Before diving into the isolated data components, let's check the data (`power_spectrum`) |
| 43 | +# and full model fit of a model object (`fooofed_spectrum`). |
| 44 | +# |
| 45 | + |
| 46 | +################################################################################################### |
| 47 | + |
| 48 | +# Plot the original power spectrum data from the object |
| 49 | +plot_spectra(fm.freqs, fm.power_spectrum, color='black') |
| 50 | + |
| 51 | +################################################################################################### |
| 52 | + |
| 53 | +# Plot the power spectrum model from the object |
| 54 | +plot_spectra(fm.freqs, fm.fooofed_spectrum_, color='red') |
| 55 | + |
| 56 | +################################################################################################### |
| 57 | +# Aperiodic Component |
| 58 | +# ~~~~~~~~~~~~~~~~~~~ |
| 59 | +# |
| 60 | +# To fit the aperiodic component, the model fit procedure includes a peak removal process. |
| 61 | +# |
| 62 | +# The resulting 'peak-removed' data component is stored in the model object, in the |
| 63 | +# `_spectrum_peak_rm` attribute. |
| 64 | +# |
| 65 | + |
| 66 | +################################################################################################### |
| 67 | + |
| 68 | +# Plot the peak removed spectrum data component |
| 69 | +plot_spectra(fm.freqs, fm._spectrum_peak_rm, color='black') |
| 70 | + |
| 71 | +################################################################################################### |
| 72 | + |
| 73 | +# Plot the peak removed spectrum, with the model aperiodic fit |
| 74 | +plot_spectra(fm.freqs, [fm._spectrum_peak_rm, fm._ap_fit], |
| 75 | + colors=['black', 'blue'], linestyle=['-', '--']) |
| 76 | + |
| 77 | +################################################################################################### |
| 78 | +# Periodic Component |
| 79 | +# ~~~~~~~~~~~~~~~~~~ |
| 80 | +# |
| 81 | +# To fit the periodic component, the model fit procedure removes the fit peaks from the power |
| 82 | +# spectrum. |
| 83 | +# |
| 84 | +# The resulting 'flattened' data component is stored in the model object, in the |
| 85 | +# `_spectrum_flat` attribute. |
| 86 | +# |
| 87 | + |
| 88 | +################################################################################################### |
| 89 | + |
| 90 | +# Plot the flattened spectrum data component |
| 91 | +plot_spectra(fm.freqs, fm._spectrum_flat, color='black') |
| 92 | + |
| 93 | +################################################################################################### |
| 94 | + |
| 95 | +# Plot the flattened spectrum data with the model peak fit |
| 96 | +plot_spectra(fm.freqs, [fm._spectrum_flat, fm._peak_fit], colors=['black', 'green']) |
| 97 | + |
| 98 | +################################################################################################### |
| 99 | +# Full Model Fit |
| 100 | +# ~~~~~~~~~~~~~~ |
| 101 | +# |
| 102 | +# The full model fit, which we explored earlier, is calculated as the combination of the |
| 103 | +# aperiodic and peak fit, which we can check by plotting these combined components. |
| 104 | +# |
| 105 | + |
| 106 | +################################################################################################### |
| 107 | + |
| 108 | +# Plot the full model fit, as the combination of the aperiodic and peak model components |
| 109 | +plot_spectra(fm.freqs, [fm._ap_fit + fm._peak_fit], color='red') |
| 110 | + |
| 111 | +################################################################################################### |
| 112 | +# Notes on Analyzing Data Components |
| 113 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 114 | +# |
| 115 | +# The above shows data components as they are available on the model object, and used in |
| 116 | +# the fitting process. Some analyses may aim to use these isolated components to compute |
| 117 | +# certain measures of interest on the data. Note that these data components are stored in |
| 118 | +# 'private' attributes (indicated by a leading underscore), meaning in normal function they |
| 119 | +# are not expected to be accessed by the user, but as we've seen above they can still be accessed. |
| 120 | +# However, analyses derived from these isolated data components is not currently officially |
| 121 | +# supported by the module, and so users who wish to do so should consider the benefits and |
| 122 | +# limitations of any such analyses. |
| 123 | +# |
0 commit comments