Skip to content

Commit a44ab2e

Browse files
committed
Small fixes on tutorials
1 parent 7055e35 commit a44ab2e

8 files changed

+184
-156
lines changed

tutorials/plot_01-ModelDescription.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01: Model Description
33
=====================
44
5-
A description and introduction to the power spectrum model.
5+
A description of and introduction to the power spectrum model.
66
"""
77

88
###################################################################################################
@@ -57,15 +57,15 @@
5757

5858
###################################################################################################
5959

60-
# Initialize a power spectrum models and fit the power spectra
60+
# Initialize power spectrum model objects and fit the power spectra
6161
fm1 = FOOOF(min_peak_height=0.05, verbose=False)
6262
fm2 = FOOOF(min_peak_height=0.05, aperiodic_mode='knee', verbose=False)
6363
fm1.fit(freqs1, powers1)
6464
fm2.fit(freqs2, powers2)
6565

6666
###################################################################################################
6767
#
68-
# Now, we have some data & models to work with.
68+
# Now, we have some data and models to work with.
6969
#
7070
# To start with, we can plot one of our simulated power spectra.
7171
#
@@ -74,8 +74,8 @@
7474
#
7575
# In the plot, we see a power spectrum in which there is decreasing power across increasing
7676
# frequencies. In some frequency regions, there is a 'peak' of power, over and above the general
77-
# trend across frequencies. These properties - a pattern of power across all frequencies, with
78-
# overlying peaks - are considered to be hallmarks of neural field data.
77+
# trend across frequencies. These properties - a pattern of decreasing power across frequencies,
78+
# with overlying peaks - are considered to be hallmarks of neural field data.
7979
#
8080

8181
###################################################################################################
@@ -96,7 +96,7 @@
9696
#
9797
# By components, we mean that we are going to conceptualize neural field data as consisting
9898
# of a combination of periodic (oscillatory) and aperiodic activity. Restated, we could say
99-
# that neural data contains both periodic and aperiodic components (or activity).
99+
# that neural data contains both periodic and aperiodic components.
100100
#
101101
# The goal of the model is to measure these components, separately and explicitly,
102102
# from frequency representations of neural field data (neural power spectra).
@@ -124,21 +124,21 @@
124124
# Given the full model, in red, we can then describe this spectrum in terms of the
125125
# 'components' that make up the model fit, which are:
126126
#
127-
# - `aperiodic`: activity, with no characteristic frequency (or 'non-frequency specific' activity)
127+
# - `aperiodic`: activity with no characteristic frequency (or 'non-frequency specific' activity)
128128
#
129129
# - in power spectra, this looks like a trend across all frequencies
130-
# - in the plot above, this is what's captured by the dashed blue line
131-
# - `periodic` : activity, with a characteristic frequency
130+
# - in the plot above, this is what is captured by the dashed blue line
131+
# - `periodic` : activity with a characteristic frequency
132132
#
133133
# - in power spectra, this looks like a 'peak', or 'bump', reflecting frequency specific power
134-
# - in the plot above, this is what's captured by the green shaded peaks
134+
# - in the plot above, this is what is captured by the green shaded peaks
135135
#
136-
# Each of these components also has different 'features' or 'properties', that we can and
137-
# want to describe and measure. Since these 'features' are things that we will be fitting in
136+
# Each of these components also has different 'features' or 'properties', that we want to
137+
# describe and measure. Since these 'features' are things that we will be fitting in
138138
# the model, we will call them `parameters` (as in, the 'model parameters').
139139
#
140140
# The full model of the power spectrum is the combination of the two components:
141-
# the aperiodic component and the periodic component, which is the set of peaks.
141+
# the aperiodic component and the periodic component (the set of peaks).
142142
#
143143
# The goal of the model is to measure these two components, to create the full model
144144
# fit, in a way that accurately and quantitatively describes the data.
@@ -148,10 +148,9 @@
148148
# Mathematical Description of Overall Model
149149
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150150
#
151-
# To enact the conceptual idea and description above, we will need to formalize the
152-
# model described above. To do so, throughout the rest of this tutorial, we will lay out the
153-
# mathematical description of how neural power spectra can be modeled as a combination of
154-
# periodic and aperiodic activity.
151+
# To enact the conceptual idea described above, we will need to formalize the model. To do
152+
# so, throughout the rest of this tutorial, we will lay out the mathematical description of
153+
# how neural power spectra can be modeled as a combination of periodic and aperiodic activity.
155154
#
156155
# Overall, the goal is to describe a neural power spectrum, described as :math:`NPS`, as
157156
# a combination of periodic and aperiodic components. We will describe each of these
@@ -177,7 +176,7 @@
177176
#
178177
# To measure the periodic activity, we would like to describe these peaks, without our
179178
# measures of these peaks being influenced by co-occurring aperiodic activity.
180-
# This is important, since as we can see in the plots above, the aperiodic and periodic
179+
# This is important, since, as we can see in the plots above, the aperiodic and periodic
181180
# components of the data can 'overlap', in frequency space. This means the total power
182181
# at a given frequency may have contributions from both components. To measure periodic power,
183182
# specifically, we need to measure the power relative to the aperiodic component of the data.
@@ -202,7 +201,7 @@
202201
#
203202
# - the `center frequency` of the peak, in units of frequency
204203
# - the `power` of the peak, over the aperiodic component, in units of power
205-
# - the `bandwidth`, or the width of the peak, units of frequency
204+
# - the `bandwidth`, or the width of the peak, in units of frequency
206205
#
207206
# Wherever we detect a peak, these are the parameters that we will fit to the peak,
208207
# to describe this component of the data.
@@ -212,7 +211,7 @@
212211
# Mathematical Description of the Periodic Component
213212
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214213
#
215-
# To fit these periodic components - the regions of power over above the aperiodic component,
214+
# To fit this periodic activity - the regions of power over above the aperiodic component,
216215
# or 'peaks' - the model uses Gaussians. As we've seen, there can be multiple peaks in the model.
217216
#
218217
# Each Gaussian, :math:`n`, referred to as :math:`G(F)_n`, is of the form:
@@ -239,7 +238,7 @@
239238
# the power across frequencies decreases with a :math:`\frac{1}{F^\chi}` relationship.
240239
#
241240
# To measure the aperiodic activity, we would like to describe the pattern of activity
242-
# across all frequencies, without our measure being influenced by any co-occurring periodic
241+
# across all frequencies, without our measure being influenced by co-occurring periodic
243242
# activity (peaks).
244243
#
245244

@@ -274,7 +273,7 @@
274273
# define the aperiodic component, as:
275274
#
276275
# - :math:`b` is the broadband 'offset'
277-
# - :math:`k` relates to the 'knee'
276+
# - :math:`k` is the 'knee'
278277
# - :math:`\chi` is the 'exponent' of the aperiodic fit
279278
# - :math:`F` is the array of frequency values
280279
#
@@ -284,9 +283,9 @@
284283
#
285284
# This function form is technically described as a Lorentzian function. We use the option
286285
# of adding a knee parameter, since even though neural data is often discussed in terms
287-
# of having `1/f` activity, across broader frequency ranges, there is typically not a single
288-
# `1/f` characteristic. Using this form allows for modeling bends in the power spectrum of
289-
# the aperiodic component, if and when they occur.
286+
# of having `1/f` activity, there is often not a single `1/f` characteristic, especially
287+
# across broader frequency ranges. Therefore, using this function form allows for modeling
288+
# bends in the power spectrum of the aperiodic component, if and when they occur.
290289
#
291290
# Note that if we were to want the equivalent function in linear power, using :math:`AP`
292291
# to indicate the aperiodic component in linear spacing, it would be:
@@ -308,7 +307,7 @@
308307
# not affect how the data is stored and/or how models are fit.
309308
#
310309
# Below we can see the same spectrum again, with all the annotations on, plotted in log-log.
311-
# The most notable difference, is that the aperiodic component is a straight line in log-log
310+
# The most notable difference is that the aperiodic component is a straight line in log-log
312311
# spacing. This is a hallmark of `1/f` activity.
313312
#
314313

tutorials/plot_02-FOOOF.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
# Import the FOOOF object
1111
from fooof import FOOOF
1212

13-
# Import utility to download and load example data
13+
# Import a utility to download and load example data
1414
from fooof.utils.download import load_fooof_data
1515

1616
###################################################################################################
1717

18-
# Download examples data files needed for this example
18+
# Download example data files needed for this example
1919
freqs = load_fooof_data('freqs.npy', folder='data')
2020
spectrum = load_fooof_data('spectrum.npy', folder='data')
2121

2222
###################################################################################################
2323
# FOOOF Object
2424
# ------------
2525
#
26-
# At the core of the module, which is object oriented, is the :class:`~fooof.FOOOF` object,
27-
# which holds relevant data and settings as attributes, and contains methods to run the
28-
# algorithm to parameterize neural power spectra.
26+
# At the core of the module is the :class:`~fooof.FOOOF` object, which holds relevant data
27+
# and settings as attributes, and contains methods to run the algorithm to parameterize
28+
# neural power spectra.
2929
#
30-
# The organization is similar to sklearn:
30+
# The organization and use of the model object is similar to scikit-learn:
3131
#
3232
# - A model object is initialized, with relevant settings
3333
# - The model is used to fit the data
@@ -39,7 +39,7 @@
3939
# ~~~~~~~~~~~~~~~~~~~~~~~~~
4040
#
4141
# The :class:`~fooof.FOOOF` object fits models to power spectra. The module itself does not
42-
# compute power spectra, and so computing power spectra needs to be done prior to using
42+
# compute power spectra. Computing power spectra needs to be done prior to using
4343
# the FOOOF module.
4444
#
4545
# The model is broadly agnostic to exactly how power spectra are computed. Common
@@ -79,7 +79,7 @@
7979
#
8080
# - :meth:`~fooof.FOOOF.fit`: fits the power spectrum model
8181
# - :meth:`~fooof.FOOOF.print_results`: prints out the results
82-
# - :meth:`~fooof.FOOOF.plot`: plots to data and model fit
82+
# - :meth:`~fooof.FOOOF.plot`: plots the data and model fit
8383
#
8484
# Each of these methods can also be called individually.
8585
#
@@ -100,7 +100,7 @@
100100
# Once the power spectrum model has been calculated, the model fit parameters are stored
101101
# as object attributes that can be accessed after fitting.
102102
#
103-
# Following the sklearn convention, attributes that are fit as a result of
103+
# Following scikit-learn conventions, attributes that are fit as a result of
104104
# the model have a trailing underscore, for example:
105105
#
106106
# - ``aperiodic_params_``
@@ -144,7 +144,7 @@
144144
# Extract a model parameter with `get_params`
145145
err = fm.get_params('error')
146146

147-
# Extract parameters, indicating sub-selections of parameter
147+
# Extract parameters, indicating sub-selections of parameters
148148
exp = fm.get_params('aperiodic_params', 'exponent')
149149
cfs = fm.get_params('peak_params', 'CF')
150150

@@ -161,7 +161,7 @@
161161
#
162162
# As a reminder, you can access the documentation for a function using '?' in a
163163
# Jupyter notebook (ex: `fm.get_params?`), or more generally with the `help` function
164-
# in general Python (ex: `help(get_params)`).
164+
# in general Python (ex: `help(fm.get_params)`).
165165
#
166166

167167
###################################################################################################
@@ -187,7 +187,7 @@
187187
# [1] Since the Gaussians are fit together, if any Gaussians overlap,
188188
# than the actual height of the fit at a given point can only be assessed
189189
# when considering all Gaussians. To be better able to interpret heights
190-
# for single peak fits, we re-define the peak height as above, and label it
190+
# for individual peaks, we re-define the peak height as above, and label it
191191
# as 'power', as the units of the input data are expected to be units of power.
192192
#
193193
# [2] Gaussian standard deviation is '1 sided', where as the returned BW is '2 sided'.
@@ -237,7 +237,7 @@
237237
# From FOOOFResults, you can access the different results
238238
print('Aperiodic Parameters: \n', fres.aperiodic_params)
239239

240-
# Check the r^2 and error of the model fit
240+
# Check the R^2 and error of the model fit
241241
print('R-squared: \n {:5.4f}'.format(fm.r_squared_))
242242
print('Fit error: \n {:5.4f}'.format(fm.error_))
243243

@@ -248,6 +248,5 @@
248248
# In this tutorial, we have explored the basics of the :class:`~fooof.FOOOF` object,
249249
# fitting power spectrum models, and extracting parameters.
250250
#
251-
# Before we move on to controlling the fit procedure, and interpreting the results,
252-
# in the next tutorial, we will first explore how this model is actually fit.
251+
# In the next tutorial, we will explore how this algorithm actually works to fit the model.
253252
#

tutorials/plot_03-FOOOFAlgorithm.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
03: Fitting Algorithm
33
=====================
44
5-
A step by step overview of the algorithm for parameterizing neural power spectra.
5+
A step-by-step overview of the algorithm for parameterizing neural power spectra.
66
"""
77

88
###################################################################################################
@@ -16,10 +16,10 @@
1616
#
1717
# Broadly, the steps in the algorithm are:
1818
#
19-
# - 1) An initial fit of the aperiodic component is taken of the power spectrum
19+
# - 1) An initial fit of the aperiodic component is computed from the power spectrum
2020
# - 2) This aperiodic fit is subtracted from the power spectrum, creating a flattened spectrum
2121
# - 3) An iterative process identifies peaks in this flattened spectrum
22-
# - 4) A full peak fit is created of all peak candidates found
22+
# - 4) A full peak fit is re-fit from all of the identified peak candidates
2323
# - 5) The peak fit is subtracted from the original power spectrum,
2424
# creating a peak-removed power spectrum
2525
# - 6) A final fit of the aperiodic component is taken of the peak-removed power spectrum
@@ -38,13 +38,13 @@
3838
from fooof import FOOOF
3939

4040
# Import some internal functions
41-
# These are used here to demonstrate the algorithm.
42-
# You do not need to import these functions for standard use of the algorithm
41+
# These are used here to demonstrate the algorithm
42+
# You do not need to import these functions for standard usage of the module
4343
from fooof.sim.gen import gen_aperiodic
4444
from fooof.plts.spectra import plot_spectrum
4545
from fooof.plts.annotate import plot_annotated_peak_search
4646

47-
# Import utility to download and load example data
47+
# Import a utility to download and load example data
4848
from fooof.utils.download import load_fooof_data
4949

5050
###################################################################################################
@@ -67,8 +67,8 @@
6767
###################################################################################################
6868
#
6969
# Note that data can be added to a FOOOF object independent of fitting the model, using the
70-
# :meth:`~fooof.FOOOF.add_data` method. FOOOF objects can also be used to plot input data,
71-
# without having fit any models.
70+
# :meth:`~fooof.FOOOF.add_data` method. FOOOF objects can also be used to plot data,
71+
# prior to fitting any models.
7272
#
7373

7474
###################################################################################################
@@ -98,8 +98,8 @@
9898
# Step 1: Initial Aperiodic Fit
9999
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100100
#
101-
# We start by taking an initial aperiodic fit. The goal of this fit is to be
102-
# able to use this fit to remove our estimate of the aperiodic component from the data.
101+
# We start by taking an initial aperiodic fit. This goal of this fit is to get an initial
102+
# fit that is good enough to get started with the fitting process.
103103
#
104104

105105
###################################################################################################
@@ -119,9 +119,10 @@
119119
# Step 2: Flatten the Spectrum
120120
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121121
#
122-
# The initial fit is then used to create a flattened spectrum. The initial aperiodic
123-
# fit is subtracted out from the original data, leaving a flattened version of the data
124-
# which no longer contains the aperiodic component.
122+
# The initial fit is then used to create a flattened spectrum.
123+
#
124+
# The initial aperiodic fit is subtracted out from the original data, leaving a flattened
125+
# version of the data which no longer contains the aperiodic component.
125126
#
126127

127128
###################################################################################################
@@ -144,10 +145,10 @@
144145
#
145146
# For each iteration:
146147
#
147-
# - The maximum point of the flattened spectrum is found.
148+
# - The maximum point of the flattened spectrum is found
148149
#
149150
# - If this point fails to pass the relative or absolute height threshold,
150-
# the procedure halts.
151+
# the procedure halts
151152
# - A Gaussian is fit around this maximum point
152153
# - This 'guess' Gaussian is then subtracted from the flatted spectrum
153154
# - The procedure continues to a new iteration with the new version of the flattened spectrum,
@@ -228,13 +229,26 @@
228229
plot_spectrum(fm.freqs, fm.fooofed_spectrum_, plt_log,
229230
label='Full Model', color='red')
230231

232+
###################################################################################################
233+
#
234+
# The last stage is to calculate the goodness of fit metrics, meaning the fit error & R^2.
235+
#
236+
# At the end of the fitting process, the model object also organizes parameters, such as
237+
# updating gaussian parameters to be peak parameters,
238+
#
239+
# These results are part of what are stored, and printed, as the model results.
240+
#
241+
231242
###################################################################################################
232243

233-
# The last stage is to calculate the goodness of fit metrics (fit error & R^2)
234-
# and organize parameters, such as updating gaussian parameters -> peak parameters
235-
# These results are part of what are stored, and printed, as the model results
244+
# Print out the model results
236245
fm.print_results()
237246

247+
###################################################################################################
248+
#
249+
# Altogether, the full model fit is now available, and can be plotted.
250+
#
251+
238252
###################################################################################################
239253

240254
# Plot the full model fit of the power spectrum
@@ -283,7 +297,9 @@
283297
# Conclusion
284298
# ----------
285299
#
286-
# In this tutorial we have stepped through the parameterization algorithm fitting
287-
# power spectrum models. Next, we will continue to explore the FOOOF object,
288-
# properly introducing the settings and further exploring the parameters.
300+
# In this tutorial we have stepped through the parameterization algorithm for fitting
301+
# power spectrum models.
302+
#
303+
# Next, we will continue to explore the FOOOF object by properly introducing and more
304+
# fully describing the settings for the algorithm.
289305
#

0 commit comments

Comments
 (0)