pyrasa.utils.aperiodic_utils.compute_aperiodic_model_sprint#

pyrasa.utils.aperiodic_utils.compute_aperiodic_model_sprint(aperiodic_spectrum: ndarray, freqs: ndarray, times: ndarray, fit_func: str | type[AbstractFitFun] = 'fixed', scale: bool = False, ch_names: Iterable | None = None, fit_bounds: tuple[float, float] | None = None) AperiodicFit[source]#

Extracts aperiodic parameters from the aperiodic spectrogram using scipy’s curve fitting function.

This function computes aperiodic parameters for each time point in the spectrogram by applying either one of two different curve fitting functions (fixed or knee) or a custom function specified by user to the data. See examples custom_fit_functions.ipynb. The parameters, along with the goodness of fit for each time point, are returned in a concatenated format.

Parameters:
  • aperiodic_spectrum (np.ndarray) – A 2 or 3D array of power values from the aperiodic spectrogram, with shape (Frequencies, Time) or (Channels, Frequencies, Time).

  • freqs (np.ndarray) – A 1D array of frequency values corresponding to the aperiodic spectrogram.

  • times (np.ndarray) – A 1D array of time values corresponding to the aperiodic spectrogram.

  • fit_func (str or type[AbstractFitFun], optional) – The fitting function to use. Can be “fixed” for a linear fit or “knee” for a fit that includes a knee (bend) in the spectrum or a class that is inherited from AbstractFitFun. The default is ‘fixed’..

  • ch_names (Iterable or None, optional) – Channel names corresponding to the aperiodic spectrogram. If None, channels will be named numerically in ascending order. Default is None.

  • scale (bool, optional) – Whether to scale the data to improve fitting accuracy. This is useful when fitting a knee in cases where power values are very small, leading to numerical precision issues. Default is False.

  • fit_bounds (tuple[float, float] or None, optional) – Tuple specifying the lower and upper frequency bounds for the fit function. If None, the entire frequency range is used. Otherwise, the spectrum is cropped to the specified bounds before fitting. Default is None.

Returns:

An object containing three pandas DataFrames:
  • aperiodic_paramspd.DataFrame

    A DataFrame containing the aperiodic parameters (e.g., Offset and Exponent) for each channel and each time point.

  • gofpd.DataFrame

    A DataFrame containing the goodness of fit metrics for each channel and each time point.

  • modelpd.DataFrame

    A DataFrame containing the predicted aperiodic model for each channel and each time point.

Return type:

AperiodicFit

Notes

This function iterates over each time point in the provided spectrogram to extract aperiodic parameters using the specified fit function. It leverages the compute_aperiodic_model function for individual fits at each time point, then combines the results across all time points into comprehensive DataFrames.

The fit_bounds parameter allows for frequency range restrictions during fitting, which can help in focusing the analysis on a particular frequency band of interest.

Scaling the data using the scale parameter can be particularly important when dealing with very small power values that might lead to poor fitting due to numerical precision limitations.