コード例 #1
0
ファイル: test_ica.py プロジェクト: starzynski/mne-python
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
ファイル: test_ica.py プロジェクト: starzynski/mne-python
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())