Пример #1
0
def test_fg_fail():
    """Test FOOOFGroup fit, in a way that some fits will fail.
    Also checks that model failures don't cause errors.
    """

    # Create some noisy spectra that will be hard to fit
    fs, ps = gen_group_power_spectra(10, [3, 6], [1, 1], [10, 1, 1], nlvs=10)

    # Use a fg with the max iterations set so low that it will fail to converge
    ntfg = FOOOFGroup()
    ntfg._maxfev = 5

    # Fit models, where some will fail, to see if it completes cleanly
    ntfg.fit(fs, ps)

    # Check that results are all
    for res in ntfg.get_results():
        assert res

    # Test that get_params works with failed model fits
    outs1 = ntfg.get_params('aperiodic_params')
    outs2 = ntfg.get_params('aperiodic_params', 'exponent')
    outs3 = ntfg.get_params('peak_params')
    outs4 = ntfg.get_params('peak_params', 0)
    outs5 = ntfg.get_params('gaussian_params', 2)

    # Test shortcut labels
    outs6 = ntfg.get_params('aperiodic')
    outs6 = ntfg.get_params('peak', 'CF')

    # Test the property attributes related to null model fits
    #   This checks that they do the right thing when there are null fits (failed fits)
    assert ntfg.n_null_ > 0
    assert ntfg.null_inds_
Пример #2
0
def test_fg_drop():
    """Test function to drop results from FOOOFGroup."""

    n_spectra = 3
    xs, ys = gen_group_power_spectra(n_spectra, *default_group_params())

    tfg = FOOOFGroup(verbose=False)

    # Test dropping one ind
    tfg.fit(xs, ys)
    tfg.drop(0)

    dropped_fres = tfg.group_results[0]
    for field in dropped_fres._fields:
        assert np.all(np.isnan(getattr(dropped_fres, field)))

    # Test dropping multiple inds
    tfg.fit(xs, ys)
    drop_inds = [0, 2]
    tfg.drop(drop_inds)

    for drop_ind in drop_inds:
        dropped_fres = tfg.group_results[drop_ind]
        for field in dropped_fres._fields:
            assert np.all(np.isnan(getattr(dropped_fres, field)))

    # Test that a FOOOFGroup that has had inds dropped still works with `get_params`
    cfs = tfg.get_params('peak_params', 1)
    exps = tfg.get_params('aperiodic_params', 'exponent')
    assert np.all(np.isnan(exps[drop_inds]))
    assert np.all(np.invert(np.isnan(np.delete(exps, drop_inds))))
Пример #3
0
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(verbose=False)
    tfg.report(xs, ys)

    assert tfg
Пример #4
0
def test_fg_fit_nk_noise():
    """Test FOOOFGroup fit, no knee, on noisy data, to make sure nothing breaks."""

    n_spectra = 5
    xs, ys = gen_group_power_spectra(n_spectra, *default_group_params(), nlvs=1.0)

    tfg = FOOOFGroup(max_n_peaks=8, verbose=False)
    tfg.fit(xs, ys)

    # No accuracy checking here - just checking that it ran
    assert tfg.has_model
Пример #5
0
def test_fit_fooof_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_3d(tfg, xs, ys)

    assert len(fgs) == 2
    for fg in fgs:
        assert fg
Пример #6
0
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(verbose=False)
    tfg.fit(xs, ys, n_jobs=2)
    out = tfg.get_results()

    assert out
    assert len(out) == n_spectra
    assert isinstance(out[0], FOOOFResults)
    assert np.all(out[1].aperiodic_params)
Пример #7
0
def test_fg_fit_nk():
    """Test FOOOFGroup fit, no knee."""

    n_spectra = 2
    xs, ys = gen_group_power_spectra(n_spectra, *default_group_params(), nlvs=0)

    tfg = FOOOFGroup(verbose=False)
    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)
Пример #8
0
def test_fg_fit_knee():
    """Test FOOOFGroup fit, with a knee."""

    n_spectra = 2
    ap_params = [50, 2, 1]
    gaussian_params = [10, 0.5, 2, 20, 0.3, 4]

    xs, ys = gen_group_power_spectra(n_spectra, [1, 150], ap_params, gaussian_params, nlvs=0)

    tfg = FOOOFGroup(aperiodic_mode='knee', verbose=False)
    tfg.fit(xs, ys)

    # No accuracy checking here - just checking that it ran
    assert tfg.has_model
Пример #9
0
def test_fit_fooof_3d(tfg):

    n_groups = 2
    n_spectra = 3
    xs, ys = gen_group_power_spectra(n_spectra, *default_group_params())
    ys = np.stack([ys] * n_groups, axis=0)
    spectra_shape = np.shape(ys)

    tfg = FOOOFGroup()
    fgs = fit_fooof_3d(tfg, xs, ys)

    assert len(fgs) == n_groups == spectra_shape[0]
    for fg in fgs:
        assert fg
        assert len(fg) == n_spectra
        assert fg.power_spectra.shape == spectra_shape[1:]