specparam (formerly fooof)

repository·main·Indexed 19 days ago

https://github.com/fooof-tools/fooof

A tool for parameterizing neural power spectra into periodic (oscillations) and aperiodic (1/f-like) components, designed for electrophysiological data such as EEG, MEG, ECoG, and LFP. It provides interfaces like SpectralModel for fitting individual spectra, as well as SpectralGroupModel, SpectralTimeModel, and SpectralTimeEventModel for complex data structures. The library includes modules for simulating spectra, visualizing results, and evaluating model performance using various goodness-of-fit and error metrics.

Tokens
4.8K
Snippets
12
Records
32
Agent score
65%

What's inside specparam

  1. What is spectral parameterization and how does it work?

    main

    Spectral parameterization is the process of fitting a model to a power spectrum to describe its components. The specparam module provides a framework for this, operating on frequency representations (power spectra) of digital signals.

    Broadly, the process involves:

    1. Model Definitions: Characterizing periodic (rhythmic) and aperiodic (non-periodic) components.
    2. Fitting Algorithms: Algorithms that fit these definitions to the power spectra.

    The resulting model quantifies each component separately and provides a combined model fit of the entire power spectrum.

  2. How the spectral parameterization model works

    main

    The power spectrum model decomposes a neural power spectrum into two distinct functional processes:

    1. Aperiodic component: A component with power across all frequencies (e.g., 1/f-like characteristics).
    2. Periodic components: A variable number of putative oscillations, represented as peaks rising above the aperiodic component.

    This model-driven approach allows for measuring periodic properties (center frequency, power, and bandwidth) without predefining specific frequency bands, while simultaneously characterizing the aperiodic component. It is applicable to electrophysiological data such as EEG, MEG, ECoG, and LFP.

  3. Understanding periodic vs aperiodic activity

    main

    Spectral parameterization assumes that a power spectrum is composed of two separable components:

    • Aperiodic activity: Non-periodic or arrhythmic activity with no characteristic frequency (e.g., white noise). In neural data, this often follows a $1/f$-like distribution where power decreases as frequency increases. It is a continuous component present at all frequencies.
    • Periodic activity: Rhythmic features with activity at a characteristic frequency (often called 'oscillatory' activity). In the spectral domain, these appear as 'peaks' of power that rise above the underlying aperiodic component.

    Note on Peaks: A peak is operationally defined as power over and above the aperiodic component. If no peak is detected in a frequency band, it is likely that the activity in that band is purely aperiodic.

  4. Understand the components of the Power Spectrum Model

    main

    The fooof power spectrum model conceptualizes neural power spectra as a combination of two distinct types of components: Periodic and Aperiodic components.

    • Periodic (or rhythmic) components: These represent putative oscillations. In the model, these are identified as Peaks that have power above and beyond the aperiodic component.
    • Aperiodic (or arrhythmic) components: This represents the background activity, typically characterized as 1/f-like activity (where power follows a $1/f^\chi$ distribution).

    When you fit a model to field data (such as EEG, MEG, ECoG, or LFP), the algorithm produces Parameters for each of these components.

  5. Define spectral fit modes and functions

    main

    Spectral models are defined by Mode objects. You can specify different mathematical functions to fit various components of the spectrum.

    Aperiodic Fit Functions

    Used for the continuous part of the spectrum:

    • powerlaw_function
    • lorentzian_function
    • double_expo_function

    Peak Fit Functions

    Used for periodic components:

    • gaussian_function
    • skewed_gaussian_function
    • cauchy_function
    • gamma_function
    • triangle_function
  6. Applying spectral parameterization to task-based data

    main

    Spectral parameterization can be applied to task-based analyses using two main approaches:

    1. Condition-level averaging: Calculate power spectra (e.g., FFTs) per trial, average them across all trials in a specific condition, and fit one model per condition. This produces smoother spectra and better fits, which is useful for short trials.
    2. Trial-level analysis: Calculate power spectra and fit models for every individual trial. This allows you to analyze the distribution of model parameters per condition. This is suitable for longer trials (a rule of thumb is segments of at least ~500 ms) and can be used if you want to account for trial-to-trial variability.
  7. Recommended information for methods reporting

    main

    When reporting the use of specparam in scientific work, it is recommended to include the following in your methods section:

    Required Information:

    • The version number of the specparam module used.
    • All algorithm settings (report all public settings, even if using default values).
    • The frequency range of the data that was fit.

    Recommended Information:

    • Details of the data (modality and preprocessing steps).
    • How power spectra were generated (e.g., length of segments used).
    • How model goodness-of-fit measures were assessed.
  8. Choosing the frequency range for model fitting

    main

    The frequency range used for fitting depends on your data and research questions.

    Best Practices:

    • Use broad ranges: It is generally better to fit a broad frequency range rather than a narrow one, even if you are only interested in a specific oscillation band. Fitting a narrow range makes it difficult to accurately estimate the aperiodic component, which can lead to poor separation of periodic activity.
    • Example (M/EEG): For investigating theta, alpha, or beta bands, a range of [3, 35] Hz is a common starting point.
    • Example (ECoG): For high-frequency activity, use ranges like [1, 150] Hz or [50, 150] Hz.
    • Post-hoc extraction: If you are interested in a specific band (e.g., alpha, 7-14 Hz), fit a broad range (e.g., 3-40 Hz) and then use utilities in the analysis module to extract peaks from that specific band.
  9. Check the installed specparam version

    main

    To verify which version of the specparam module is currently installed in your environment, import __version__ from the package.

    # Check the version of the tool
    from specparam import __version__ as specparam_version
    print('Current specparam version:', specparam_version)
  10. Explore spectral parameterization with animated visualizers

    main

    The fooof project provides animated visualizers to help explore concepts related to spectral parameterization, such as the correlation between offset and exponent during spectral rotation. These visualizers are hosted in a separate repository.

    You can find the source code for these visualizations in the Visualizers repository.

  11. Tuning algorithm settings for model fitting

    main

    While the fitting algorithm initializes with default values, individual datasets often require tuning to optimize the fit.

    Tuning Strategy:

    1. Select a 'training set': Choose a subset of power spectra from your dataset.
    2. Fit and Inspect: Fit models to this subset, check model fit properties, and visually inspect the results.
    3. Apply to full dataset: Once the best settings are identified for the subset, apply those same settings to the entire dataset to ensure systematic comparability across conditions, tasks, or subjects.
  12. Install the specparam test version (v2.0 release candidate)

    main

    To install the current release candidate for the upcoming 2.0 version (renamed from fooof to specparam), use pip. Note that this version is not yet finalized and may contain breaking changes.

    $ pip install specparam