Пример #1
0
#

###################################################################################################
# get_band_peak_group
# ~~~~~~~~~~~~~~~~~~~
#
# The :func:`get_band_peak_group` function can be used to select peaks within specific
# frequency ranges, from across a group of FOOOF fits.
#

###################################################################################################

# Generate some synthetic power spectra and fit a FOOOFGroup to use
freqs, spectra, _ = gen_group_power_spectra(
    n_spectra=10,
    freq_range=[3, 40],
    aperiodic_params=param_sampler([[20, 2], [35, 1.5]]),
    gauss_params=param_sampler([[], [10, 0.5, 2]]))

###################################################################################################

# Fit FOOOF models across the group of synthesized power spectra
fg = FOOOFGroup(peak_width_limits=[1, 8],
                min_peak_height=0.05,
                max_n_peaks=6,
                verbose=False)
fg.fit(freqs, spectra)

###################################################################################################

# Get all alpha oscillations from a FOOOFGroup object
Пример #2
0
Use the plots available with FOOOF.
"""

###################################################################################################

# Import FOOOF plotting functions
from fooof.plts.spectra import plot_spectrum, plot_spectra
from fooof.plts.spectra import plot_spectrum_shading, plot_spectra_shading

###################################################################################################

# Create a couple synthetic power spectra to explore plotting with
#   Here we create two spectra, with different aperiodic components
#   but each with the same oscillations (a theta, alpha & beta)
from fooof.synth.gen import gen_group_power_spectra
fs, ps, _ = gen_group_power_spectra(2, [3, 40], [[0.75, 1.5], [0.25, 1]],
                                    [6, 0.2, 1, 10, 0.3, 1, 25, 0.15, 3])
ps1, ps2 = ps

###################################################################################################
# The FOOOF plotting module has plots for plotting single or multiple
# power spectra, options for plotting in linear or log space, and
# plots for shading frequency regions of interest.
#
# Plotting in FOOOF uses matplotlib. Plotting functions can also take in any
# matplotlib keyword arguments, that will be passed into the plot call.

###################################################################################################

# Create a spectrum plot with a single power spectrum
plot_spectrum(fs, ps2, log_powers=True)
Пример #3
0
gauss_opts = param_sampler([[], [10, 0.5, 2], [10, 0.5, 2, 20, 0.3, 4]])

###################################################################################################
#
# We can now feed these settings into :func:`gen_group_power_spectra`,
# that will generate a group of power spectra for us.
#
# Note that this function also returns a list of the parameters
# used to generate each power spectrum.
#

###################################################################################################

# Generate the group of synthetic spectra
#  Note that this function also returns a list of the parameters for each func
freqs, spectra, syn_params = gen_group_power_spectra(n_spectra, f_range,
                                                     ap_opts, gauss_opts)

###################################################################################################
# FOOOFGroup
# ----------
#
# The FOOOFGroup object is very similar to the FOOOF object (programmatically, it inherits
# from the FOOOF object), and can be used in the same way.
#
# The main difference is that instead of running across a single power spectrum, it
# operates across 2D matrices containing multiple power spectra.
#
# Note that by 'group' we mean merely to refer to a group of power-spectra,
# not necessarily to a group in terms of multiple subjects or similar.
# Most likely, a FOOOFGroup will be run across a collection of spectra from across
# channels, and/or across trials, within or across subjects.
Пример #4
0
# - `nlv`
#

###################################################################################################

# Set up settings for synthesizing a group of power spectra
n_spectra = 2
freq_range = [3, 40]
ap_params = [[0.5, 1], [1, 1.5]]
gauss_params = [[10, 0.4, 1], [10, 0.2, 1, 22, 0.1, 3]]
nlv = 0.02

###################################################################################################

# Synthesize a group of power spectra
fs, ps, syn_params = gen_group_power_spectra(n_spectra, freq_range, ap_params,
                                             gauss_params, nlv)

###################################################################################################

# Print out the SynParams objects that track the parameters used to create power spectra
for syn_param in syn_params:
    print(syn_param)

###################################################################################################

# You can also use a SynParams object to regenerate a particular power spectrum
cur_params = syn_params[0]
fs, ps = gen_power_spectrum(freq_range, *cur_params)

###################################################################################################
# Managing Parameters
Пример #5
0
# Check reconstructed parameters from synthetic definition
print('Ground Truth \t\t FOOOF Reconstructions')
for sy, fi in zip(np.array(group_three(gauss_params)), fm._gaussian_params):
    print('{:5.2f} {:5.2f} {:5.2f} \t {:5.2f} {:5.2f} {:5.2f}'.format(
        *sy, *fi))

###################################################################################################
# Checking Fits Across a Group
# ----------------------------

# Set the parameters options for aperiodic signal and Gaussian peaks
ap_opts = param_sampler([[20, 2], [50, 2.5], [35, 1.5]])
gauss_opts = param_sampler([[], [10, 0.5, 2], [10, 0.5, 2, 20, 0.3, 4]])

# Generate a group of power spectra
freqs, power_spectra, syn_params = gen_group_power_spectra(
    10, [3, 50], ap_opts, gauss_opts)

###################################################################################################

# Initialize a FOOOFGroup
fg = FOOOFGroup(peak_width_limits=[1, 6])

###################################################################################################

# Fit FOOOF and report on the group
fg.report(freqs, power_spectra)

###################################################################################################

# Find the index of the worst FOOOF fit from the group
worst_fit_ind = np.argmax(fg.get_all_data('error'))
# a function or generator that can be called to return parameters for each spectrum.
#

###################################################################################################

# Create some new settings for synthesizing a group of power spectra
n_spectra = 2
freq_range = [3, 40]
ap_params = [[0.5, 1], [1, 1.5]]
gauss_params = [10, 0.4, 1]
nlv = 0.02

###################################################################################################

# Synthesize a group of power spectra
fs, ps, syn_params = gen_group_power_spectra(n_spectra, freq_range, ap_params,
                                             gauss_params, nlv)

###################################################################################################

# Plot the power spectra that were just generated
plot_spectra(fs, ps, log_freqs=True, log_powers=True)

###################################################################################################
#
# Note that when you simulate a group of power spectra, FOOOF returns SynParam objects that
# keep track of the simulations. This, and other utilties to manage parameters and provide
# parameter definitions for synthesizing groups of power spectra are covered in the
# `Synthetic Parameters` example.
#
Пример #7
0
from fooof.synth.params import param_sampler

###################################################################################################

# Settings for creating synthetic data
n_spectra = 10
freq_range = [3, 40]
ap_opts = param_sampler([[0, 1.0], [0, 1.5], [0, 2]])
gauss_opts = param_sampler([[], [10, 1, 1], [10, 1, 1, 20, 2, 1]])

###################################################################################################

# Generate some synthetic power spectra, and organize into a 3D matrix
spectra = []
for ind in range(3):
    fs, ps, _ = gen_group_power_spectra(n_spectra, freq_range, ap_opts, gauss_opts)
    spectra.append(ps)
spectra = np.array(spectra)

###################################################################################################

# Check the shape of the spectra
#   This kind of 3D organization can be thought to represent [n_conditions, n_channels, n_freqs]
print(spectra.shape)

###################################################################################################
# fit_fooof_group_3d
# ------------------
#
# The :func:`fit_fooof_group_3d` is a function that takes in a FOOOFGroup object,
# which has been initialized with the desired settings, as well as a frequency