Пример #1
0
def test_ica_core():
    """Test ICA on raw and epochs
    """
    # setup parameter
    # XXX. The None cases helped revealing bugs but are time consuming.
    noise_cov = [None, test_cov]
    # removed None cases to speed up...
    n_components = [3, 1.0]  # for future dbg add cases
    max_n_components = [4]
    picks_ = [picks]
    iter_ica_params = product(noise_cov, n_components, max_n_components,
                              picks_)

    # test init catchers
    assert_raises(ValueError, ICA, n_components=3, max_n_components=2)
    assert_raises(ValueError, ICA, n_components=1.3, max_n_components=2)

    # test essential core functionality
    for n_cov, n_comp, max_n, pcks in iter_ica_params:
        # Test ICA raw
        ica = ICA(noise_cov=n_cov,
                  n_components=n_comp,
                  max_n_components=max_n,
                  random_state=0)

        print ica  # to test repr
        # test fit checker
        assert_raises(RuntimeError, ica.get_sources_raw, raw)
        assert_raises(RuntimeError, ica.get_sources_epochs, epochs)

        # test decomposition
        ica.decompose_raw(raw, picks=pcks, start=start, stop=stop)
        # test re-init exception
        assert_raises(RuntimeError, ica.decompose_raw, raw, picks=picks)

        sources = ica.get_sources_raw(raw)
        assert_true(sources.shape[0] == ica.n_components)

        # test preload filter
        raw3 = raw.copy()
        raw3._preloaded = False
        assert_raises(ValueError,
                      ica.pick_sources_raw,
                      raw3,
                      include=[1, 2],
                      n_pca_components=ica.n_components)

        for excl, incl in (([], []), ([], [1, 2]), ([1, 2], [])):
            raw2 = ica.pick_sources_raw(raw,
                                        exclude=excl,
                                        include=incl,
                                        copy=True,
                                        n_pca_components=ica.n_components)

            assert_array_almost_equal(raw2[:, :][1], raw[:, :][1])

        #######################################################################
        # test epochs decomposition

        # test re-init exception
        assert_raises(RuntimeError, ica.decompose_epochs, epochs, picks=picks)
        ica = ICA(noise_cov=n_cov,
                  n_components=n_comp,
                  max_n_components=max_n,
                  random_state=0)

        ica.decompose_epochs(epochs, picks=picks)
        # test pick block after epochs fit
        assert_raises(ValueError,
                      ica.pick_sources_raw,
                      raw,
                      n_pca_components=ica.n_components)

        sources = ica.get_sources_epochs(epochs)
        assert_true(sources.shape[1] == ica.n_components)

        assert_raises(ValueError,
                      ica.find_sources_epochs,
                      epochs,
                      target=np.arange(1))

        # test preload filter
        epochs3 = epochs.copy()
        epochs3.preload = False
        assert_raises(ValueError,
                      ica.pick_sources_epochs,
                      epochs3,
                      include=[1, 2],
                      n_pca_components=ica.n_components)

        # test source picking
        for excl, incl in (([], []), ([], [1, 2]), ([1, 2], [])):
            epochs2 = ica.pick_sources_epochs(
                epochs,
                exclude=excl,
                include=incl,
                copy=True,
                n_pca_components=ica.n_components)

            assert_array_almost_equal(epochs2.get_data(), epochs.get_data())
Пример #2
0
def test_ica_core():
    """Test ICA on raw and epochs
    """
    # setup parameter
    # XXX. The None cases helped revealing bugs but are time consuming.
    noise_cov = [None, test_cov]
    # removed None cases to speed up...
    n_components = [3, 1.0]  # for future dbg add cases
    max_n_components = [4]
    picks_ = [picks]
    iter_ica_params = product(noise_cov, n_components, max_n_components,
                           picks_)

    # test init catchers
    assert_raises(ValueError, ICA, n_components=3, max_n_components=2)
    assert_raises(ValueError, ICA, n_components=1.3, max_n_components=2)

    # test essential core functionality
    for n_cov, n_comp, max_n, pcks in iter_ica_params:
      # Test ICA raw
        ica = ICA(noise_cov=n_cov, n_components=n_comp, max_n_components=max_n,
                  random_state=0)

        print ica  # to test repr
        # test fit checker
        assert_raises(RuntimeError, ica.get_sources_raw, raw)
        assert_raises(RuntimeError, ica.get_sources_epochs, epochs)

        # test decomposition
        ica.decompose_raw(raw, picks=pcks, start=start, stop=stop)
        # test re-init exception
        assert_raises(RuntimeError, ica.decompose_raw, raw, picks=picks)

        sources = ica.get_sources_raw(raw)
        assert_true(sources.shape[0] == ica.n_components)

        # test preload filter
        raw3 = raw.copy()
        raw3._preloaded = False
        assert_raises(ValueError, ica.pick_sources_raw, raw3,
                      include=[1, 2], n_pca_components=ica.n_components)

        for excl, incl in (([], []), ([], [1, 2]), ([1, 2], [])):
            raw2 = ica.pick_sources_raw(raw, exclude=excl, include=incl,
                                        copy=True,
                                        n_pca_components=ica.n_components)

            assert_array_almost_equal(raw2[:, :][1], raw[:, :][1])

        #######################################################################
        # test epochs decomposition

        # test re-init exception
        assert_raises(RuntimeError, ica.decompose_epochs, epochs, picks=picks)
        ica = ICA(noise_cov=n_cov, n_components=n_comp, max_n_components=max_n,
                  random_state=0)

        ica.decompose_epochs(epochs, picks=picks)
        # test pick block after epochs fit
        assert_raises(ValueError, ica.pick_sources_raw, raw,
                    n_pca_components=ica.n_components)

        sources = ica.get_sources_epochs(epochs)
        assert_true(sources.shape[1] == ica.n_components)

        assert_raises(ValueError, ica.find_sources_epochs, epochs,
                      target=np.arange(1))

        # test preload filter
        epochs3 = epochs.copy()
        epochs3.preload = False
        assert_raises(ValueError, ica.pick_sources_epochs, epochs3,
                      include=[1, 2], n_pca_components=ica.n_components)

        # test source picking
        for excl, incl in (([], []), ([], [1, 2]), ([1, 2], [])):
            epochs2 = ica.pick_sources_epochs(epochs, exclude=excl,
                                              include=incl, copy=True,
                                              n_pca_components=ica.n_components)

            assert_array_almost_equal(epochs2.get_data(),
                                      epochs.get_data())
Пример #3
0
def test_ica_additional():
    # Test additional functionality
    stop2 = 500

    ica = ICA(n_components=3, max_n_components=4)
    ica.decompose_raw(raw, picks=None, start=start, stop=stop2)

    # epochs extraction from raw fit
    assert_raises(RuntimeError, ica.get_sources_epochs, epochs)

    ica = ICA(n_components=3, max_n_components=4)
    ica.decompose_raw(raw, picks=picks, start=start, stop=stop2)
    sources = ica.get_sources_epochs(epochs)
    assert_true(sources.shape[1] == ica.n_components)

    sources = ica.get_sources_raw(raw)

    # score funcs raw
    sfunc_test = [
        ica.find_sources_raw(raw,
                             target='EOG 061',
                             score_func=n,
                             start=0,
                             stop=10) for n, f in score_funcs.items()
    ]

    # check lenght of scores
    [assert_true(ica.n_components == len(scores)) for scores in sfunc_test]

    # check univariate stats
    scores = ica.find_sources_raw(raw, score_func=stats.skew)
    # check exception handling
    assert_raises(ValueError, ica.find_sources_raw, raw, target=np.arange(1))

    ## score funcs epochs ##

    # check lenght of scores
    sfunc_test = [
        ica.find_sources_epochs(epochs_eog, target='EOG 061', score_func=n)
        for n, f in score_funcs.items()
    ]

    # check lenght of scores
    [assert_true(ica.n_components == len(scores)) for scores in sfunc_test]

    # check univariat stats
    scores = ica.find_sources_epochs(epochs, score_func=stats.skew)

    # check exception handling
    assert_raises(ValueError,
                  ica.find_sources_epochs,
                  epochs,
                  target=np.arange(1))

    # ecg functionality
    ecg_scores = ica.find_sources_raw(raw,
                                      target='MEG 1531',
                                      score_func='pearsonr')

    ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()])

    assert_true(ecg_events.ndim == 2)

    # eog functionality
    eog_scores = ica.find_sources_raw(raw,
                                      target='EOG 061',
                                      score_func='pearsonr')
    eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()])

    assert_true(eog_events.ndim == 2)

    # Test ica fiff export
    raw3 = raw.copy()
    raw3._preloaded = False
    assert_raises(ValueError, ica.export_sources, raw3, start=0, stop=100)
    ica_raw = ica.export_sources(raw, start=0, stop=100)
    assert_true(ica_raw.last_samp - ica_raw.first_samp == 100)
    ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch]
    assert_true(ica.n_components == len(ica_chans))
    test_ica_fname = op.join(op.abspath(op.curdir), 'test_ica.fif')
    ica_raw.save(test_ica_fname)
    ica_raw2 = fiff.Raw(test_ica_fname, preload=True)
    assert_array_almost_equal(ica_raw._data, ica_raw2._data)
    os.remove(test_ica_fname)

    # regression test for plot method
    assert_raises(ValueError, ica.plot_sources_raw, raw, order=np.arange(50))
    assert_raises(ValueError,
                  ica.plot_sources_epochs,
                  epochs,
                  order=np.arange(50))
Пример #4
0
def test_ica_additional():
    # Test additional functionality
    stop2 = 500

    ica = ICA(n_components=3, max_n_components=4)
    ica.decompose_raw(raw, picks=None, start=start, stop=stop2)

    # epochs extraction from raw fit
    assert_raises(RuntimeError, ica.get_sources_epochs, epochs)

    ica = ICA(n_components=3, max_n_components=4)
    ica.decompose_raw(raw, picks=picks, start=start, stop=stop2)
    sources = ica.get_sources_epochs(epochs)
    assert_true(sources.shape[1] == ica.n_components)

    sources = ica.get_sources_raw(raw)

    # score funcs raw
    sfunc_test = [ica.find_sources_raw(raw, target='EOG 061', score_func=n,
            start=0, stop=10) for  n, f in score_funcs.items()]

    # check lenght of scores
    [assert_true(ica.n_components == len(scores)) for scores in sfunc_test]

    # check univariate stats
    scores = ica.find_sources_raw(raw, score_func=stats.skew)
    # check exception handling
    assert_raises(ValueError, ica.find_sources_raw, raw,
                  target=np.arange(1))

    ## score funcs epochs ##

    # check lenght of scores
    sfunc_test = [ica.find_sources_epochs(epochs_eog, target='EOG 061',
                    score_func=n) for n, f in score_funcs.items()]

    # check lenght of scores
    [assert_true(ica.n_components == len(scores)) for scores in sfunc_test]

    # check univariat stats
    scores = ica.find_sources_epochs(epochs, score_func=stats.skew)

    # check exception handling
    assert_raises(ValueError, ica.find_sources_epochs, epochs,
                  target=np.arange(1))

    # ecg functionality
    ecg_scores = ica.find_sources_raw(raw, target='MEG 1531',
                                      score_func='pearsonr')

    ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()])

    assert_true(ecg_events.ndim == 2)

    # eog functionality
    eog_scores = ica.find_sources_raw(raw, target='EOG 061',
                                      score_func='pearsonr')
    eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()])

    assert_true(eog_events.ndim == 2)

    # Test ica fiff export
    raw3 = raw.copy()
    raw3._preloaded = False
    assert_raises(ValueError, ica.export_sources, raw3, start=0, stop=100)
    ica_raw = ica.export_sources(raw, start=0, stop=100)
    assert_true(ica_raw.last_samp - ica_raw.first_samp == 100)
    ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch]
    assert_true(ica.n_components == len(ica_chans))
    test_ica_fname = op.join(op.abspath(op.curdir), 'test_ica.fif')
    ica_raw.save(test_ica_fname)
    ica_raw2 = fiff.Raw(test_ica_fname, preload=True)
    assert_array_almost_equal(ica_raw._data, ica_raw2._data)
    os.remove(test_ica_fname)

    # regression test for plot method
    assert_raises(ValueError, ica.plot_sources_raw, raw,
                  order=np.arange(50))
    assert_raises(ValueError, ica.plot_sources_epochs, epochs,
                  order=np.arange(50))