Ejemplo n.º 1
0
def test_component_sign():
    # We should have a heuristic that flips the sign of components in
    # CanICA to have more positive values than negative values, for
    # instance by making sure that the largest value is positive.

    # make data (SVD)
    rng = np.random.RandomState(0)
    shape = (20, 10, 1)
    affine = np.eye(4)
    components = _make_canica_components(shape)

    # make +ve
    for mp in components:
        mp[rng.randn(*mp.shape) > .8] *= -5.
        assert_less_equal(mp.max(), -mp.min())  # goal met ?

    # synthesize data with given components
    data = _make_data_from_components(components, affine, shape, rng=rng,
                                      n_subjects=2)
    mask_img = nibabel.Nifti1Image(np.ones(shape, dtype=np.int8), affine)

    # run CanICA many times (this is known to produce different results)
    canica = CanICA(n_components=4, random_state=rng, mask=mask_img)
    for _ in range(3):
        canica.fit(data)
        for mp in iter_img(canica.masker_.inverse_transform(
                canica.components_)):
            mp = mp.get_data()
            assert_less_equal(-mp.min(), mp.max())
Ejemplo n.º 2
0
def _make_canica_test_data(rng=None, n_subjects=8, noisy=False):
    if rng is None:
        rng = np.random.RandomState(0)
    shape = (30, 30, 5)
    affine = np.eye(4)
    components = _make_canica_components(shape)
    if noisy:  # Creating noisy non positive data
        components[rng.randn(*components.shape) > .8] *= -5.

    for mp in components:
        assert_less_equal(mp.max(), -mp.min())  # Goal met ?

    # Create a "multi-subject" dataset
    data = _make_data_from_components(components, affine, shape, rng=rng,
                                      n_subjects=n_subjects)
    mask = np.ones(shape)
    mask[:5] = 0
    mask[-5:] = 0
    mask[:, :5] = 0
    mask[:, -5:] = 0
    mask[..., -2:] = 0
    mask[..., :2] = 0

    mask_img = nibabel.Nifti1Image(mask, affine)
    return data, mask_img, components, rng
Ejemplo n.º 3
0
def _make_canica_test_data(rng=None, n_subjects=8, noisy=False):
    if rng is None:
        rng = np.random.RandomState(0)
    shape = (30, 30, 5)
    affine = np.eye(4)
    components = _make_canica_components(shape)
    if noisy:  # Creating noisy non positive data
        components[rng.randn(*components.shape) > .8] *= -5.

    for mp in components:
        assert_less_equal(mp.max(), -mp.min())  # Goal met ?

    # Create a "multi-subject" dataset
    data = _make_data_from_components(components,
                                      affine,
                                      shape,
                                      rng=rng,
                                      n_subjects=n_subjects)
    mask = np.ones(shape)
    mask[:5] = 0
    mask[-5:] = 0
    mask[:, :5] = 0
    mask[:, -5:] = 0
    mask[..., -2:] = 0
    mask[..., :2] = 0

    mask_img = nibabel.Nifti1Image(mask, affine)
    return data, mask_img, components, rng
Ejemplo n.º 4
0
def test_component_sign():
    # We should have a heuristic that flips the sign of components in
    # CanICA to have more positive values than negative values, for
    # instance by making sure that the largest value is positive.

    data, mask_img, components, rng = _make_canica_test_data(n_subjects=2,
                                                             noisy=True)

    # run CanICA many times (this is known to produce different results)
    canica = CanICA(n_components=4, random_state=rng, mask=mask_img)
    for _ in range(3):
        canica.fit(data)
        for mp in iter_img(canica.components_img_):
            mp = get_data(mp)
            assert_less_equal(-mp.min(), mp.max())
Ejemplo n.º 5
0
def test_component_sign():
    # We should have a heuristic that flips the sign of components in
    # CanICA to have more positive values than negative values, for
    # instance by making sure that the largest value is positive.

    data, mask_img, components, rng = _make_canica_test_data(n_subjects=2,
                                                             noisy=True)

    # run CanICA many times (this is known to produce different results)
    canica = CanICA(n_components=4, random_state=rng, mask=mask_img)
    for _ in range(3):
        canica.fit(data)
        for mp in iter_img(canica.components_img_):
            mp = mp.get_data()
            assert_less_equal(-mp.min(), mp.max())
Ejemplo n.º 6
0
def test_component_sign():
    # Regression test
    # We should have a heuristic that flips the sign of components in
    # DictLearning to have more positive values than negative values, for
    # instance by making sure that the largest value is positive.

    data, mask_img, components, rng = _make_canica_test_data(n_subjects=2, noisy=True)
    for mp in components:
        assert_less_equal(-mp.min(), mp.max())

    dict_learning = DictLearning(n_components=4, random_state=rng, mask=mask_img, smoothing_fwhm=0.0, alpha=1)
    dict_learning.fit(data)
    for mp in iter_img(dict_learning.masker_.inverse_transform(dict_learning.components_)):
        mp = mp.get_data()
        assert_less_equal(np.sum(mp[mp <= 0]), np.sum(mp[mp > 0]))
Ejemplo n.º 7
0
def _make_test_data(rng=None, n_subjects=8, noisy=False):
    if rng is None:
        rng = np.random.RandomState(0)
    shape = (20, 20, 1)
    affine = np.eye(4)
    components = _make_components(shape)
    if noisy:  # Creating noisy non positive data
        components[rng.randn(*components.shape) > 0.8] *= -5.0

    for mp in components:
        assert_less_equal(mp.max(), -mp.min())  # Goal met ?

    # Create a "multi-subject" dataset
    data = _make_data_from_components(components, affine, shape, rng=rng, n_subjects=n_subjects)
    mask_img = nibabel.Nifti1Image(np.ones(shape, dtype=np.int8), affine)
    return data, mask_img, components, rng
Ejemplo n.º 8
0
def test_component_sign():
    # Regression test
    # We should have a heuristic that flips the sign of components in
    # DictLearning to have more positive values than negative values, for
    # instance by making sure that the largest value is positive.

    data, mask_img, components, init = _make_test_data(n_subjects=2,
                                                       noisy=True)

    dict_fact = fMRIDictFact(n_components=4,
                             random_state=0,
                             mask=mask_img,
                             smoothing_fwhm=0.)
    dict_fact.fit(data)
    for mp in iter_img(dict_fact.components_):
        mp = mp.get_data()
        assert_less_equal(np.sum(mp[mp <= 0]), np.sum(mp[mp > 0]))
Ejemplo n.º 9
0
def test_component_sign():
    # Regression test
    # We should have a heuristic that flips the sign of components in
    # DictLearning to have more positive values than negative values, for
    # instance by making sure that the largest value is positive.

    data, mask_img, components, rng = _make_canica_test_data(n_subjects=2,
                                                             noisy=True)
    for mp in components:
        assert_less_equal(-mp.min(), mp.max())

    dict_learning = DictLearning(n_components=4, random_state=rng,
                                 mask=mask_img,
                                 smoothing_fwhm=0., alpha=1)
    dict_learning.fit(data)
    for mp in iter_img(dict_learning.components_img_):
        mp = get_data(mp)
        assert_less_equal(np.sum(mp[mp <= 0]), np.sum(mp[mp > 0]))
Ejemplo n.º 10
0
def test_component_sign():
    # Regression test
    # We should have a heuristic that flips the sign of components in
    # DictLearning to have more positive values than negative values, for
    # instance by making sure that the largest value is positive.

    data, mask_img, components, rng = _make_test_data(n_subjects=2, noisy=True)
    for mp in components:
        assert_less_equal(-mp.min(), mp.max())

    sparse_pca = SpcaFmri(n_components=4,
                          random_state=rng,
                          mask=mask_img,
                          smoothing_fwhm=0.)
    sparse_pca.fit(data)
    for mp in iter_img(
            sparse_pca.masker_.inverse_transform(sparse_pca.components_)):
        mp = mp.get_data()
        assert_less_equal(np.sum(mp[mp <= 0]), np.sum(mp[mp > 0]))
Ejemplo n.º 11
0
def _make_test_data(n_subjects=8, noisy=False):
    rng = np.random.RandomState(0)
    shape = (20, 20, 1)
    components = _make_components(shape)
    if noisy:  # Creating noisy non positive data
        components[rng.randn(*components.shape) > .8] *= -5.
        for component in components:
            assert_less_equal(component.max(), -component.min())  # Goal met ?

    # Create a "multi-subject" dataset
    data = _make_data_from_components(components, n_subjects=n_subjects)
    affine = np.eye(4)
    mask_img = nibabel.Nifti1Image(np.ones(shape, dtype=np.int8), affine)
    masker = MultiNiftiMasker(mask_img).fit()
    init = components + 1 * rng.randn(*components.shape)
    components = masker.inverse_transform(components)
    init = masker.inverse_transform(init)
    data = masker.inverse_transform(data)

    return data, mask_img, components, init