|
29 | 29 | fm.fit(freqs, powers)
|
30 | 30 |
|
31 | 31 | ###################################################################################################
|
32 |
| -# Data Components |
33 |
| -# ~~~~~~~~~~~~~~~ |
| 32 | +# Data & Model Components |
| 33 | +# ----------------------- |
34 | 34 | #
|
35 | 35 | # The model fit process includes procedures for isolating aperiodic and periodic components in
|
36 | 36 | # the data, fitting each of these components separately, and then combining the model components
|
|
39 | 39 | # In doing this process, the model fit procedure computes and stores isolated data components,
|
40 | 40 | # which are available in the model.
|
41 | 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`). |
| 42 | + |
| 43 | +################################################################################################### |
| 44 | +# Full Data & Model Components |
| 45 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 46 | +# |
| 47 | +# Before diving into the isolated data components, let's check 'full' components, including |
| 48 | +# the data (`power_spectrum`) and full model fit of a model object (`fooofed_spectrum`). |
44 | 49 | #
|
45 | 50 |
|
46 | 51 | ###################################################################################################
|
|
53 | 58 | # Plot the power spectrum model from the object
|
54 | 59 | plot_spectra(fm.freqs, fm.fooofed_spectrum_, color='red')
|
55 | 60 |
|
| 61 | +################################################################################################### |
| 62 | +# Isolated Components |
| 63 | +# ------------------- |
| 64 | +# |
| 65 | +# As well as the 'full' data & model components above, the model fitting procedure includes |
| 66 | +# steps that result in isolated periodic and aperiodic components, in both the |
| 67 | +# data and model. These isolated components are stored internally in the model. |
| 68 | +# |
| 69 | +# To access these components, we can use the following `getter` methods: |
| 70 | +# |
| 71 | +# - :meth:`~fooof.FOOOF.get_data`: allows for accessing data components |
| 72 | +# - :meth:`~fooof.FOOOF.get_model`: allows for accessing model components |
| 73 | +# |
| 74 | + |
56 | 75 | ###################################################################################################
|
57 | 76 | # Aperiodic Component
|
58 | 77 | # ~~~~~~~~~~~~~~~~~~~
|
59 | 78 | #
|
60 | 79 | # To fit the aperiodic component, the model fit procedure includes a peak removal process.
|
61 | 80 | #
|
62 |
| -# The resulting 'peak-removed' data component is stored in the model object, in the |
63 |
| -# `_spectrum_peak_rm` attribute. |
| 81 | +# The resulting 'peak-removed' data component is stored in the model object, as well as the |
| 82 | +# isolated aperiodic component model fit. |
64 | 83 | #
|
65 | 84 |
|
66 | 85 | ###################################################################################################
|
67 | 86 |
|
68 | 87 | # Plot the peak removed spectrum data component
|
69 |
| -plot_spectra(fm.freqs, fm._spectrum_peak_rm, color='black') |
| 88 | +plot_spectra(fm.freqs, fm.get_data('aperiodic'), color='black') |
70 | 89 |
|
71 | 90 | ###################################################################################################
|
72 | 91 |
|
73 | 92 | # Plot the peak removed spectrum, with the model aperiodic fit
|
74 |
| -plot_spectra(fm.freqs, [fm._spectrum_peak_rm, fm._ap_fit], |
| 93 | +plot_spectra(fm.freqs, [fm.get_data('aperiodic'), fm.get_model('aperiodic')], |
75 | 94 | colors=['black', 'blue'], linestyle=['-', '--'])
|
76 | 95 |
|
77 | 96 | ###################################################################################################
|
|
81 | 100 | # To fit the periodic component, the model fit procedure removes the fit peaks from the power
|
82 | 101 | # spectrum.
|
83 | 102 | #
|
84 |
| -# The resulting 'flattened' data component is stored in the model object, in the |
85 |
| -# `_spectrum_flat` attribute. |
| 103 | +# The resulting 'flattened' data component is stored in the model object, as well as the |
| 104 | +# isolated periodic component model fit. |
86 | 105 | #
|
87 | 106 |
|
88 | 107 | ###################################################################################################
|
89 | 108 |
|
90 | 109 | # Plot the flattened spectrum data component
|
91 |
| -plot_spectra(fm.freqs, fm._spectrum_flat, color='black') |
| 110 | +plot_spectra(fm.freqs, fm.get_data('peak'), color='black') |
92 | 111 |
|
93 | 112 | ###################################################################################################
|
94 | 113 |
|
95 | 114 | # Plot the flattened spectrum data with the model peak fit
|
96 |
| -plot_spectra(fm.freqs, [fm._spectrum_flat, fm._peak_fit], colors=['black', 'green']) |
| 115 | +plot_spectra(fm.freqs, [fm.get_data('peak'), fm.get_model('peak')], |
| 116 | + colors=['black', 'green'], linestyle=['-', '--']) |
97 | 117 |
|
98 | 118 | ###################################################################################################
|
99 | 119 | # Full Model Fit
|
|
106 | 126 | ###################################################################################################
|
107 | 127 |
|
108 | 128 | # 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') |
| 129 | +plot_spectra(fm.freqs, [fm.get_model('aperiodic') + fm.get_model('peak')], color='red') |
110 | 130 |
|
111 | 131 | ###################################################################################################
|
112 |
| -# Notes on Analyzing Data Components |
113 |
| -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 132 | +# Linear vs Log Spacing |
| 133 | +# --------------------- |
114 | 134 | #
|
115 | 135 | # 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. |
| 136 | +# the fitting process - notable, in log10 spacing. |
| 137 | +# |
| 138 | +# Some analyses may aim to use these isolated components to compute certain measures of |
| 139 | +# interest on the data. However, when doing so, one may often want the linear power |
| 140 | +# representations of these components. |
| 141 | +# |
| 142 | +# Both the `get_data` and `get_model` methods accept a 'space' argument, whereby the user |
| 143 | +# can specify whether the return the components in log10 or linear spacing. |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +################################################################################################### |
| 148 | +# Aperiodic Components in Linear Space |
| 149 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 150 | +# |
| 151 | +# First we can examine the aperiodic data & model components, in linear space. |
| 152 | +# |
| 153 | + |
| 154 | +################################################################################################### |
| 155 | + |
| 156 | +# Plot the peak removed spectrum, with the model aperiodic fit |
| 157 | +plot_spectra(fm.freqs, [fm.get_data('aperiodic', 'linear'), fm.get_model('aperiodic', 'linear')], |
| 158 | + colors=['black', 'blue'], linestyle=['-', '--']) |
| 159 | + |
| 160 | +################################################################################################### |
| 161 | +# Peak Component in Linear Space |
| 162 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 163 | +# |
| 164 | +# Next, we can examine the peak data & model components, in linear space. |
| 165 | +# |
| 166 | + |
| 167 | +################################################################################################### |
| 168 | + |
| 169 | +# Plot the flattened spectrum data with the model peak fit |
| 170 | +plot_spectra(fm.freqs, [fm.get_data('peak', 'linear'), fm.get_model('peak', 'linear')], |
| 171 | + colors=['black', 'green'], linestyle=['-', '--']) |
| 172 | + |
| 173 | +################################################################################################### |
| 174 | +# Linear Space Additive Model |
| 175 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 176 | +# |
| 177 | +# Note that specifying 'linear' does not simply unlog the data components to return them |
| 178 | +# in linear space, but instead defines the space of the additive data definition such that |
| 179 | +# `power_spectrum = aperiodic_component + peak_component` (for data and/or model). |
| 180 | +# |
| 181 | +# We can see this by plotting the linear space data (or model) with the corresponding |
| 182 | +# aperiodic and periodic components summed together. Note that if you simply unlog |
| 183 | +# the components and sum them, they does not add up to reflecting the full data / model. |
| 184 | +# |
| 185 | + |
| 186 | +################################################################################################### |
| 187 | + |
| 188 | +# Plot the linear data, showing the combination of peak + aperiodic matches the full data |
| 189 | +plot_spectra(fm.freqs, |
| 190 | + [fm.get_data('full', 'linear'), |
| 191 | + fm.get_data('aperiodic', 'linear') + fm.get_data('peak', 'linear')], |
| 192 | + linestyle=['-', 'dashed'], colors=['black', 'red'], alpha=[0.3, 0.75]) |
| 193 | + |
| 194 | +################################################################################################### |
| 195 | + |
| 196 | +# Plot the linear model, showing the combination of peak + aperiodic matches the full model |
| 197 | +plot_spectra(fm.freqs, |
| 198 | + [fm.get_model('full', 'linear'), |
| 199 | + fm.get_model('aperiodic', 'linear') + fm.get_model('peak', 'linear')], |
| 200 | + linestyle=['-', 'dashed'], colors=['black', 'red'], alpha=[0.3, 0.75]) |
| 201 | + |
| 202 | +################################################################################################### |
| 203 | +# Notes on Analyzing Data & Model Components |
| 204 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 205 | +# |
| 206 | +# The functionality here allows for accessing the model components in log space (as used by |
| 207 | +# the model for fitting), as well as recomputing in linear space. |
| 208 | +# |
| 209 | +# If you are aiming to analyze these components, it is important to consider which version of |
| 210 | +# the data you should analyze for the question at hand, as there are key differences to the |
| 211 | +# different representations. Users who wish to do so post-hoc analyses of these data and model |
| 212 | +# components should consider the benefits and limitations the different representations. |
123 | 213 | #
|
0 commit comments