def get_tfg(): """Get a FOOOFGroup object, with some fit power spectra, for testing.""" n_spectra = 2 xs, ys = gen_group_power_spectra(n_spectra, *default_group_params()) tfg = FOOOFGroup() tfg.fit(xs, ys) return tfg
def test_fg_report(skip_if_no_mpl): """Check that running the top level model method runs.""" n_spectra = 2 xs, ys = gen_group_power_spectra(n_spectra, *default_group_params()) tfg = FOOOFGroup() tfg.report(xs, ys) assert tfg
def test_fit_fooof_group_3d(tfg): n_spectra = 2 xs, ys, _ = gen_group_power_spectra(n_spectra, *default_group_params()) ys = np.stack([ys, ys], axis=0) tfg = FOOOFGroup() fgs = fit_fooof_group_3d(tfg, xs, ys) assert len(fgs) == 2 for fg in fgs: assert fg
def test_fg_fit_par(): """Test FOOOFGroup fit, running in parallel.""" n_spectra = 2 xs, ys = gen_group_power_spectra(n_spectra, *default_group_params()) tfg = FOOOFGroup() tfg.fit(xs, ys, n_jobs=2) out = tfg.get_results() assert out assert len(out) == n_spectra assert isinstance(out[0], FOOOFResult) assert np.all(out[1].background_params)
def test_fg_fit(): """Test FOOOFGroup fit, no knee.""" n_spectra = 2 xs, ys, _ = gen_group_power_spectra(n_spectra, *default_group_params()) tfg = FOOOFGroup() tfg.fit(xs, ys) out = tfg.get_results() assert out assert len(out) == n_spectra assert isinstance(out[0], FOOOFResults) assert np.all(out[1].aperiodic_params)
from fooof.analysis import get_band_peak, get_band_peak_group ############################################################################### # Reload some data and fit a FOOOF model to use freqs = np.load('dat/freqs_lfp.npy') spectrum = np.load('dat/spectrum_lfp.npy') fm = FOOOF(peak_width_limits=[2, 8]) fm.fit(freqs, spectrum, [3, 30]) ############################################################################### # Generate some synthetic power spectra and fit a FOOOFGroup to use freqs, spectra, _ = gen_group_power_spectra( n_spectra=10, freq_range=[3, 40], background_params=param_sampler([[20, 2], [35, 1.5]]), gauss_params=param_sampler([[], [10, 0.5, 2]])) fg = FOOOFGroup(peak_width_limits=[1, 8], min_peak_amplitude=0.05, max_n_peaks=6, verbose=False) fg.fit(freqs, spectra) ############################################################################### # FOOOF Analysis Utilities # ------------------------ # # FOOOF is packaged with minimal analysis utility functions. # # The plan is for the FOOOF module to stay this way, as supporting further analysis of FOOOF-derived results is largely outside the scope of the current module.
# - If there is a clear knee, then use knee fits. # - This is likely across larger fitting ranges such as 1-150 Hz. # - Be wary of ambiguous ranges, where there may or may not be a knee. # - Trying to fit without a knee, when there is not a single consistent aperiodic signal, can lead to very bad fits. But it is also a known issue that trying to fit with a knee can lead to suboptimal fits when no knee is present. # - We therefore currently recommend picking frequency ranges in which the expected aperiodic signal process is relatively clear. ############################################################################### # Checking Fits Across a Group # ---------------------------- # Set the parameters options for aperiodic signal and Gaussian peaks bgp_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], bgp_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'))
# Set some options for peak parameters # Generated power spectra will have either no peaks, a 10 Hz peak, or a 10 Hz & 20 Hz peak 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_psds, f_range, bgp_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. # # The main difference with the FOOOFGroup object, is that it also contains a 'power_spectra' attribute, which stores the matrix of power-spectra to be fit, and collects fit results into a 'group_results' attribute. Otherwise, FOOOFGroup supports all the same functionality, accessed in the same way as the FOOOF object. # # Internally, it runs the exact same fitting procedure, per spectrum, as the FOOOF object.