Exemplo n.º 1
0
def test_fit():
    from brainiak.reprsimil.brsa import BRSA
    import brainiak.utils.utils as utils
    import scipy.stats
    import numpy as np
    import os.path
    np.random.seed(10)
    file_path = os.path.join(os.path.dirname(__file__), "example_design.1D")
    # Load an example design matrix
    design = utils.ReadDesign(fname=file_path)


    # concatenate it by 4 times, mimicking 4 runs of itenditcal timing
    design.design_task = np.tile(design.design_task[:,:-1],[4,1])
    design.n_TR = design.n_TR * 4

    # start simulating some data
    n_V = 200
    n_C = np.size(design.design_task,axis=1)
    n_T = design.n_TR

    noise_bot = 0.5
    noise_top = 1.5
    noise_level = np.random.rand(n_V)*(noise_top-noise_bot)+noise_bot
    # noise level is random.

    # AR(1) coefficient
    rho1_top = 0.8
    rho1_bot = -0.2
    rho1 = np.random.rand(n_V)*(rho1_top-rho1_bot)+rho1_bot

    # generating noise
    noise = np.zeros([n_T,n_V])
    noise[0,:] = np.random.randn(n_V) * noise_level / np.sqrt(1-rho1**2)
    for i_t in range(1,n_T):
        noise[i_t,:] = noise[i_t-1,:] * rho1 +  np.random.randn(n_V) * noise_level

    noise = noise + np.random.rand(n_V)
    # Random baseline

    # ideal covariance matrix
    ideal_cov = np.zeros([n_C,n_C])
    ideal_cov = np.eye(n_C)*0.6
    ideal_cov[0:4,0:4] = 0.2
    for cond in range(0,4):
        ideal_cov[cond,cond] = 2
    ideal_cov[5:9,5:9] = 0.9
    for cond in range(5,9):
        ideal_cov[cond,cond] = 1
    idx = np.where(np.sum(np.abs(ideal_cov),axis=0)>0)[0]
    L_full = np.linalg.cholesky(ideal_cov)        

    # generating signal
    snr_level = 5.0 # test with high SNR    
    # snr = np.random.rand(n_V)*(snr_top-snr_bot)+snr_bot
    # Notice that accurately speaking this is not snr. the magnitude of signal depends
    # not only on beta but also on x.
    inten = np.random.randn(n_V) * 20.0

    # parameters of Gaussian process to generate pseuso SNR
    tau = 0.8
    smooth_width = 5.0
    inten_kernel = 1.0
    
    coords = np.arange(0,n_V)[:,None]

    dist2 = np.square(coords-coords.T)

    inten_tile = np.tile(inten,[n_V,1])
    inten_diff2 = (inten_tile-inten_tile.T)**2

    K = np.exp(-dist2/smooth_width**2/2.0 -inten_diff2/inten_kernel**2/2.0) * tau**2 + np.eye(n_V)*tau**2*0.001

    L = np.linalg.cholesky(K)
    snr = np.exp(np.dot(L,np.random.randn(n_V))) * snr_level
    sqrt_v = noise_level*snr
    betas_simulated = np.dot(L_full,np.random.randn(n_C,n_V)) * sqrt_v
    signal = np.dot(design.design_task,betas_simulated)

    # Adding noise to signal as data
    Y = signal + noise


    scan_onsets = np.linspace(0,design.n_TR,num=5)


    # Test fitting with GP prior.
    brsa = BRSA(GP_space=True,GP_inten=True,verbose=False,n_iter = 200,auto_nuisance=False)

    # We also test that it can detect baseline regressor included in the design matrix for task conditions
    wrong_design = np.insert(design.design_task, 0, 1, axis=1)
    with pytest.raises(ValueError) as excinfo:
        brsa.fit(X=Y, design=wrong_design, scan_onsets=scan_onsets,
             coords=coords, inten=inten)
    assert 'Your design matrix appears to have included baseline time series.' in str(excinfo.value)
    # Now we fit with the correct design matrix.
    brsa.fit(X=Y, design=design.design_task, scan_onsets=scan_onsets,
             coords=coords, inten=inten)
    
    # Check that result is significantly correlated with the ideal covariance matrix
    u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)],
                              u_i[np.tril_indices_from(u_i)])[1]
    assert p < 0.01, "Fitted covariance matrix does not correlate with ideal covariance matrix!"
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_,snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)),0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_,noise_level)[1]
    assert p < 0.01, "Fitted noise level does not correlate with simulated noise level!"
    p = scipy.stats.pearsonr(brsa.rho_,rho1)[1]
    assert p < 0.01, "Fitted AR(1) coefficient does not correlate with simulated values!"


    # Test fitting with lower rank and without GP prior
    rank = n_C - 1
    n_nureg = 1
    brsa = BRSA(rank=rank,n_nureg=n_nureg)
    brsa.fit(X=Y, design=design.design_task, scan_onsets=scan_onsets)
    u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)],u_i[np.tril_indices_from(u_i)])[1]
    assert p < 0.01, "Fitted covariance matrix does not correlate with ideal covariance matrix!"
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_,snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)),0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_,noise_level)[1]
    assert p < 0.01, "Fitted noise level does not correlate with simulated noise level!"
    p = scipy.stats.pearsonr(brsa.rho_,rho1)[1]
    assert p < 0.01, "Fitted AR(1) coefficient does not correlate with simulated values!"

    assert not hasattr(brsa,'bGP_') and not hasattr(brsa,'lGPspace_') and not hasattr(brsa,'lGPinten_'),\
        'the BRSA object should not have parameters of GP if GP is not requested.'
    # GP parameters are not set if not requested
    assert brsa.beta0_.shape[0] == n_nureg, 'Shape of beta0 incorrect'
    p = scipy.stats.pearsonr(brsa.beta0_[0,:],np.mean(noise,axis=0))[1]
    assert p < 0.05, 'recovered beta0 does not correlate with the baseline of voxels.'

    # Test fitting with GP over just spatial coordinates.
    brsa = BRSA(GP_space=True)
    brsa.fit(X=Y, design=design.design_task, scan_onsets=scan_onsets, coords=coords)
    # Check that result is significantly correlated with the ideal covariance matrix
    u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)],u_i[np.tril_indices_from(u_i)])[1]
    assert p < 0.01, "Fitted covariance matrix does not correlate with ideal covariance matrix!"
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_,snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)),0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_,noise_level)[1]
    assert p < 0.01, "Fitted noise level does not correlate with simulated noise level!"
    p = scipy.stats.pearsonr(brsa.rho_,rho1)[1]
    assert p < 0.01, "Fitted AR(1) coefficient does not correlate with simulated values!"
    assert not hasattr(brsa,'lGPinten_'),\
        'the BRSA object should not have parameters of lGPinten_ if only smoothness in space is requested.'
Exemplo n.º 2
0
def test_fit():
    from brainiak.reprsimil.brsa import BRSA
    import brainiak.utils.utils as utils
    import scipy.stats
    import numpy as np
    import os.path
    np.random.seed(10)
    file_path = os.path.join(os.path.dirname(__file__), "example_design.1D")
    # Load an example design matrix
    design = utils.ReadDesign(fname=file_path)

    # concatenate it by 2 times, mimicking 2 runs of itenditcal timing
    n_run = 2
    design.design_task = np.tile(design.design_task[:, :-1], [n_run, 1])
    design.n_TR = design.n_TR * n_run

    # start simulating some data
    n_V = 50
    n_C = np.size(design.design_task, axis=1)
    n_T = design.n_TR

    noise_bot = 0.5
    noise_top = 5.0
    noise_level = np.random.rand(n_V) * (noise_top - noise_bot) + noise_bot
    # noise level is random.

    # AR(1) coefficient
    rho1_top = 0.8
    rho1_bot = -0.2
    rho1 = np.random.rand(n_V) * (rho1_top - rho1_bot) + rho1_bot

    # generating noise
    noise = np.zeros([n_T, n_V])
    noise[0, :] = np.random.randn(n_V) * noise_level / np.sqrt(1 - rho1**2)
    for i_t in range(1, n_T):
        noise[i_t, :] = noise[i_t - 1, :] * rho1 + \
            np.random.randn(n_V) * noise_level

    # ideal covariance matrix
    ideal_cov = np.zeros([n_C, n_C])
    ideal_cov = np.eye(n_C) * 0.6
    ideal_cov[0:4, 0:4] = 0.2
    for cond in range(0, 4):
        ideal_cov[cond, cond] = 2
    ideal_cov[5:9, 5:9] = 0.9
    for cond in range(5, 9):
        ideal_cov[cond, cond] = 1
    L_full = np.linalg.cholesky(ideal_cov)

    # generating signal
    snr_level = 5.0  # test with high SNR
    # snr = np.random.rand(n_V)*(snr_top-snr_bot)+snr_bot
    # Notice that accurately speaking this is not snr. the magnitude of signal
    # depends
    # not only on beta but also on x.
    inten = np.random.rand(n_V) * 20.0

    # parameters of Gaussian process to generate pseuso SNR
    tau = 1.0
    smooth_width = 5.0
    inten_kernel = 1.0

    coords = np.arange(0, n_V)[:, None]

    dist2 = np.square(coords - coords.T)

    inten_tile = np.tile(inten, [n_V, 1])
    inten_diff2 = (inten_tile - inten_tile.T)**2

    K = np.exp(-dist2 / smooth_width**2 / 2.0 - inten_diff2 / inten_kernel**2 /
               2.0) * tau**2 + np.eye(n_V) * tau**2 * 0.001

    L = np.linalg.cholesky(K)
    snr = np.exp(np.dot(L, np.random.randn(n_V))) * snr_level
    sqrt_v = noise_level * snr
    betas_simulated = np.dot(L_full, np.random.randn(n_C, n_V)) * sqrt_v
    signal = np.dot(design.design_task, betas_simulated)

    # Adding noise to signal as data
    Y = signal + noise + inten

    scan_onsets = np.linspace(0, design.n_TR, num=n_run + 1)

    # Test fitting with GP prior.
    brsa = BRSA(GP_space=True,
                GP_inten=True,
                n_iter=5,
                init_iter=10,
                auto_nuisance=False,
                tol=2e-3)

    # We also test that it can detect baseline regressor included in the
    # design matrix for task conditions
    wrong_design = np.insert(design.design_task, 0, 1, axis=1)
    with pytest.raises(ValueError) as excinfo:
        brsa.fit(X=Y,
                 design=wrong_design,
                 scan_onsets=scan_onsets,
                 coords=coords,
                 inten=inten)
    assert ('Your design matrix appears to have included baseline time series.'
            in str(excinfo.value))
    # Now we fit with the correct design matrix.
    brsa.fit(X=Y,
             design=design.design_task,
             scan_onsets=scan_onsets,
             coords=coords,
             inten=inten)

    # Check that result is significantly correlated with the ideal covariance
    # matrix
    u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)],
                              u_i[np.tril_indices_from(u_i)])[1]
    assert p < 0.01, (
        "Fitted covariance matrix does not correlate with ideal covariance "
        "matrix!")
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_, noise_level)[1]
    assert p < 0.01, (
        "Fitted noise level does not correlate with simulated noise level!")
    p = scipy.stats.pearsonr(brsa.rho_, rho1)[1]
    assert p < 0.01, (
        "Fitted AR(1) coefficient does not correlate with simulated values!")

    noise_new = np.zeros([n_T, n_V])
    noise_new[0, :] = np.random.randn(n_V) * noise_level / np.sqrt(1 - rho1**2)
    for i_t in range(1, n_T):
        noise_new[i_t, :] = noise_new[i_t - 1, :] * \
            rho1 + np.random.randn(n_V) * noise_level

    Y_new = signal + noise_new + inten
    ts, ts0 = brsa.transform(Y_new, scan_onsets=scan_onsets)
    p = scipy.stats.pearsonr(ts[:, 0], design.design_task[:, 0])[1]
    assert p < 0.01, (
        "Recovered time series does not correlate with true time series!")
    assert np.shape(ts) == (n_T, n_C) and np.shape(ts0) == (n_T, 1), (
        "Wrong shape in returned time series by transform function!")

    [score, score_null] = brsa.score(X=Y_new,
                                     design=design.design_task,
                                     scan_onsets=scan_onsets)
    assert score > score_null, (
        "Full model does not win over null model on data containing signal")

    [score, score_null] = brsa.score(X=noise_new + inten,
                                     design=design.design_task,
                                     scan_onsets=scan_onsets)
    assert score < score_null, (
        "Null model does not win over full model on data without signal")

    # Test fitting with lower rank, nuisance regressors and without GP prior
    rank = n_C - 1
    n_nureg = 1
    brsa = BRSA(rank=rank,
                n_nureg=n_nureg,
                tol=2e-3,
                n_iter=8,
                init_iter=4,
                auto_nuisance=True)
    brsa.fit(X=Y, design=design.design_task, scan_onsets=scan_onsets)
    # u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)],
                              u_i[np.tril_indices_from(u_i)])[1]
    assert p < 0.01, (
        "Fitted covariance matrix does not correlate with ideal covariance "
        "matrix!")
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_, noise_level)[1]
    assert p < 0.01, (
        "Fitted noise level does not correlate with simulated noise level!")
    p = scipy.stats.pearsonr(brsa.rho_, rho1)[1]
    assert p < 0.01, (
        "Fitted AR(1) coefficient does not correlate with simulated values!")

    assert (not hasattr(brsa, 'bGP_') and not hasattr(brsa, 'lGPspace_')
            and not hasattr(brsa, 'lGPinten_')), (
                "the BRSA object should not have parameters of GP if GP is "
                "not requested.")
    # GP parameters are not set if not requested
    assert brsa.beta0_.shape[0] == n_nureg + 1, 'Shape of beta0 incorrect'
    p = scipy.stats.pearsonr(brsa.beta0_[0, :], inten)[1]
    assert p < 0.01, (
        'recovered beta0 does not correlate with the baseline of voxels.')
    assert np.shape(
        brsa.L_) == (n_C,
                     rank), 'Cholesky factor should have shape of (n_C, rank)'

    # Test fitting with GP over just spatial coordinates.
    brsa = BRSA(GP_space=True,
                baseline_single=False,
                tol=2e-3,
                n_iter=4,
                init_iter=4)
    brsa.fit(X=Y,
             design=design.design_task,
             scan_onsets=scan_onsets,
             coords=coords)
    # Check that result is significantly correlated with the ideal covariance
    # matrix
    u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)],
                              u_i[np.tril_indices_from(u_i)])[1]
    assert p < 0.01, (
        "Fitted covariance matrix does not correlate with ideal covariance "
        "matrix!")
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_, noise_level)[1]
    assert p < 0.01, (
        "Fitted noise level does not correlate with simulated noise level!")
    p = scipy.stats.pearsonr(brsa.rho_, rho1)[1]
    assert p < 0.01, (
        "Fitted AR(1) coefficient does not correlate with simulated values!")
    assert not hasattr(brsa, 'lGPinten_'), (
        "the BRSA object should not have parameters of lGPinten_ if only "
        "smoothness in space is requested.")
Exemplo n.º 3
0
def test_fit():
    from brainiak.reprsimil.brsa import BRSA
    import brainiak.utils.utils as utils
    import scipy.stats
    import numpy as np
    import os.path
    np.random.seed(10)
    file_path = os.path.join(os.path.dirname(__file__), "example_design.1D")
    # Load an example design matrix
    design = utils.ReadDesign(fname=file_path)

    # concatenate it by 2 times, mimicking 2 runs of itenditcal timing
    n_run = 2
    design.design_task = np.tile(design.design_task[:, :-1], [n_run, 1])
    design.n_TR = design.n_TR * n_run

    # start simulating some data
    n_V = 50
    n_C = np.size(design.design_task, axis=1)
    n_T = design.n_TR

    noise_bot = 0.5
    noise_top = 5.0
    noise_level = np.random.rand(n_V) * (noise_top - noise_bot) + noise_bot
    # noise level is random.

    # AR(1) coefficient
    rho1_top = 0.8
    rho1_bot = -0.2
    rho1 = np.random.rand(n_V) * (rho1_top - rho1_bot) + rho1_bot

    # generating noise
    noise = np.zeros([n_T, n_V])
    noise[0, :] = np.random.randn(n_V) * noise_level / np.sqrt(1 - rho1**2)
    for i_t in range(1, n_T):
        noise[i_t, :] = noise[i_t - 1, :] * rho1 + \
            np.random.randn(n_V) * noise_level

    # ideal covariance matrix
    ideal_cov = np.zeros([n_C, n_C])
    ideal_cov = np.eye(n_C) * 0.6
    ideal_cov[0:4, 0:4] = 0.2
    for cond in range(0, 4):
        ideal_cov[cond, cond] = 2
    ideal_cov[5:9, 5:9] = 0.9
    for cond in range(5, 9):
        ideal_cov[cond, cond] = 1
    L_full = np.linalg.cholesky(ideal_cov)

    # generating signal
    snr_level = 5.0  # test with high SNR
    # snr = np.random.rand(n_V)*(snr_top-snr_bot)+snr_bot
    # Notice that accurately speaking this is not snr. the magnitude of signal
    # depends
    # not only on beta but also on x.
    inten = np.random.rand(n_V) * 20.0

    # parameters of Gaussian process to generate pseuso SNR
    tau = 1.0
    smooth_width = 5.0
    inten_kernel = 1.0

    coords = np.arange(0, n_V)[:, None]

    dist2 = np.square(coords - coords.T)

    inten_tile = np.tile(inten, [n_V, 1])
    inten_diff2 = (inten_tile - inten_tile.T)**2

    K = np.exp(-dist2 / smooth_width**2 / 2.0 - inten_diff2 /
               inten_kernel**2 / 2.0) * tau**2 + np.eye(n_V) * tau**2 * 0.001

    L = np.linalg.cholesky(K)
    snr = np.exp(np.dot(L, np.random.randn(n_V))) * snr_level
    sqrt_v = noise_level * snr
    betas_simulated = np.dot(L_full, np.random.randn(n_C, n_V)) * sqrt_v
    signal = np.dot(design.design_task, betas_simulated)

    # Adding noise to signal as data
    Y = signal + noise + inten

    scan_onsets = np.linspace(0, design.n_TR, num=n_run + 1)

    # Test fitting with GP prior.
    brsa = BRSA(GP_space=True, GP_inten=True, n_iter=5,
                init_iter=10, auto_nuisance=False, tol=2e-3)

    # We also test that it can detect baseline regressor included in the
    # design matrix for task conditions
    wrong_design = np.insert(design.design_task, 0, 1, axis=1)
    with pytest.raises(ValueError) as excinfo:
        brsa.fit(X=Y, design=wrong_design, scan_onsets=scan_onsets,
                 coords=coords, inten=inten)
    assert ('Your design matrix appears to have included baseline time series.'
            in str(excinfo.value))
    # Now we fit with the correct design matrix.
    brsa.fit(X=Y, design=design.design_task, scan_onsets=scan_onsets,
             coords=coords, inten=inten)

    # Check that result is significantly correlated with the ideal covariance
    # matrix
    u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)],
                              u_i[np.tril_indices_from(u_i)])[1]
    assert p < 0.01, (
        "Fitted covariance matrix does not correlate with ideal covariance "
        "matrix!")
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_, noise_level)[1]
    assert p < 0.01, (
        "Fitted noise level does not correlate with simulated noise level!")
    p = scipy.stats.pearsonr(brsa.rho_, rho1)[1]
    assert p < 0.01, (
        "Fitted AR(1) coefficient does not correlate with simulated values!")

    noise_new = np.zeros([n_T, n_V])
    noise_new[0, :] = np.random.randn(n_V) * noise_level / np.sqrt(1 - rho1**2)
    for i_t in range(1, n_T):
        noise_new[i_t, :] = noise_new[i_t - 1, :] * \
            rho1 + np.random.randn(n_V) * noise_level

    Y_new = signal + noise_new + inten
    ts, ts0 = brsa.transform(Y_new, scan_onsets=scan_onsets)
    p = scipy.stats.pearsonr(ts[:, 0], design.design_task[:, 0])[1]
    assert p < 0.01, (
        "Recovered time series does not correlate with true time series!")
    assert np.shape(ts) == (n_T, n_C) and np.shape(ts0) == (n_T, 1), (
        "Wrong shape in returned time series by transform function!")

    [score, score_null] = brsa.score(
        X=Y_new, design=design.design_task, scan_onsets=scan_onsets)
    assert score > score_null, (
        "Full model does not win over null model on data containing signal")

    [score, score_null] = brsa.score(X=noise_new + inten,
                                     design=design.design_task,
                                     scan_onsets=scan_onsets)
    assert score < score_null, (
        "Null model does not win over full model on data without signal")

    # Test fitting with lower rank, nuisance regressors and without GP prior
    rank = n_C - 1
    n_nureg = 1
    brsa = BRSA(rank=rank, n_nureg=n_nureg, tol=2e-3,
                n_iter=4, init_iter=4, auto_nuisance=True)
    brsa.fit(X=Y, design=design.design_task, scan_onsets=scan_onsets)
    # u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)], u_i[
                              np.tril_indices_from(u_i)])[1]
    assert p < 0.01, (
        "Fitted covariance matrix does not correlate with ideal covariance "
        "matrix!")
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_, noise_level)[1]
    assert p < 0.01, (
        "Fitted noise level does not correlate with simulated noise level!")
    p = scipy.stats.pearsonr(brsa.rho_, rho1)[1]
    assert p < 0.01, (
        "Fitted AR(1) coefficient does not correlate with simulated values!")

    assert (not hasattr(brsa, 'bGP_')
            and not hasattr(brsa, 'lGPspace_')
            and not hasattr(brsa, 'lGPinten_')
            ), ("the BRSA object should not have parameters of GP if GP is "
                "not requested.")
    # GP parameters are not set if not requested
    assert brsa.beta0_.shape[0] == n_nureg + 1, 'Shape of beta0 incorrect'
    p = scipy.stats.pearsonr(brsa.beta0_[0, :], inten)[1]
    assert p < 0.01, (
        'recovered beta0 does not correlate with the baseline of voxels.')
    assert np.shape(brsa.L_) == (
        n_C, rank), 'Cholesky factor should have shape of (n_C, rank)'

    # Test fitting with GP over just spatial coordinates.
    brsa = BRSA(GP_space=True, baseline_single=False,
                tol=2e-3, n_iter=4, init_iter=4)
    brsa.fit(X=Y, design=design.design_task,
             scan_onsets=scan_onsets, coords=coords)
    # Check that result is significantly correlated with the ideal covariance
    # matrix
    u_b = brsa.U_
    u_i = ideal_cov
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b)], u_i[
                              np.tril_indices_from(u_i)])[1]
    assert p < 0.01, (
        "Fitted covariance matrix does not correlate with ideal covariance "
        "matrix!")
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simulated SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"
    p = scipy.stats.pearsonr(brsa.sigma_, noise_level)[1]
    assert p < 0.01, (
        "Fitted noise level does not correlate with simulated noise level!")
    p = scipy.stats.pearsonr(brsa.rho_, rho1)[1]
    assert p < 0.01, (
        "Fitted AR(1) coefficient does not correlate with simulated values!")
    assert not hasattr(brsa, 'lGPinten_'), (
        "the BRSA object should not have parameters of lGPinten_ if only "
        "smoothness in space is requested.")
Exemplo n.º 4
0
def test_fit():
    from brainiak.reprsimil.brsa import BRSA
    import brainiak.utils.utils as utils
    import scipy.stats
    import numpy as np
    import os.path
    np.random.seed(10)
    file_path = os.path.join(os.path.dirname(__file__), "example_design.1D")
    # Load an example design matrix
    design = utils.ReadDesign(fname=file_path)
    # concatenate it by 4 times, mimicking 4 runs of itenditcal timing
    design.design_used = np.tile(design.design_used[:, 0:17], [4, 1])
    design.n_TR = design.n_TR * 4

    # start simulating some data
    n_V = 300
    n_C = np.size(design.design_used, axis=1)
    n_T = design.n_TR

    noise_bot = 0.5
    noise_top = 1.5
    noise_level = np.random.rand(n_V) * (noise_top - noise_bot) + noise_bot
    # noise level is random.

    # AR(1) coefficient
    rho1_top = 0.8
    rho1_bot = -0.2
    rho1 = np.random.rand(n_V) * (rho1_top - rho1_bot) + rho1_bot

    # generating noise
    noise = np.zeros([n_T, n_V])
    noise[0, :] = np.random.randn(n_V) * noise_level / np.sqrt(1 - rho1**2)
    for i_t in range(1, n_T):
        noise[i_t, :] = noise[i_t -
                              1, :] * rho1 + np.random.randn(n_V) * noise_level

    # ideal covariance matrix
    ideal_cov = np.zeros([n_C, n_C])
    ideal_cov = np.eye(n_C) * 0.6
    ideal_cov[0, 0] = 0.2
    ideal_cov[5:9, 5:9] = 0.6
    for cond in range(5, 9):
        ideal_cov[cond, cond] = 1
    idx = np.where(np.sum(np.abs(ideal_cov), axis=0) > 0)[0]
    L_full = np.linalg.cholesky(ideal_cov)

    # generating signal
    snr_level = 5.0  # test with high SNR
    # snr = np.random.rand(n_V)*(snr_top-snr_bot)+snr_bot
    # Notice that accurately speaking this is not snr. the magnitude of signal depends
    # not only on beta but also on x.
    inten = np.random.randn(n_V) * 20.0

    # parameters of Gaussian process to generate pseuso SNR
    tau = 0.8
    smooth_width = 5.0
    inten_kernel = 1.0

    coords = np.arange(0, n_V)[:, None]

    dist2 = np.square(coords - coords.T)

    inten_tile = np.tile(inten, [n_V, 1])
    inten_diff2 = (inten_tile - inten_tile.T)**2

    K = np.exp(-dist2 / smooth_width**2 / 2.0 - inten_diff2 / inten_kernel**2 /
               2.0) * tau**2 + np.eye(n_V) * tau**2 * 0.001

    L = np.linalg.cholesky(K)
    snr = np.exp(np.dot(L, np.random.randn(n_V))) * snr_level
    sqrt_v = noise_level * snr
    betas_simulated = np.dot(L_full, np.random.randn(n_C, n_V)) * sqrt_v
    signal = np.dot(design.design_used, betas_simulated)

    # Adding noise to signal as data
    Y = signal + noise

    scan_onsets = np.linspace(0, design.n_TR, num=5)

    # Test fitting with GP prior.
    brsa = BRSA(GP_space=True, GP_inten=True, verbose=False, n_iter=200)

    brsa.fit(X=Y,
             design=design.design_used,
             scan_onsets=scan_onsets,
             coords=coords,
             inten=inten)

    # Check that result is significantly correlated with the ideal covariance matrix
    u_b = brsa.U_[1:, 1:]
    u_i = ideal_cov[1:, 1:]
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b, k=-1)],
                              u_i[np.tril_indices_from(u_i, k=-1)])[1]
    assert p < 0.01, "Fitted covariance matrix does not correlate with ideal covariance matrix!"
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simualted SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"

    # Test fitting without GP prior.
    brsa = BRSA()
    brsa.fit(X=Y, design=design.design_used, scan_onsets=scan_onsets)

    # Check that result is significantly correlated with the ideal covariance matrix
    u_b = brsa.U_[1:, 1:]
    u_i = ideal_cov[1:, 1:]
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b, k=-1)],
                              u_i[np.tril_indices_from(u_i, k=-1)])[1]
    assert p < 0.01, "Fitted covariance matrix does not correlate with ideal covariance matrix!"
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simualted SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"
    assert not hasattr(brsa,'bGP_') and not hasattr(brsa,'lGPspace_') and not hasattr(brsa,'lGPinten_'),\
        'the BRSA object should not have parameters of GP if GP is not requested.'
    # GP parameters are not set if not requested

    # Test fitting with GP over just spatial coordinates.
    brsa = BRSA(GP_space=True)
    brsa.fit(X=Y,
             design=design.design_used,
             scan_onsets=scan_onsets,
             coords=coords)
    # Check that result is significantly correlated with the ideal covariance matrix
    u_b = brsa.U_[1:, 1:]
    u_i = ideal_cov[1:, 1:]
    p = scipy.stats.spearmanr(u_b[np.tril_indices_from(u_b, k=-1)],
                              u_i[np.tril_indices_from(u_i, k=-1)])[1]
    assert p < 0.01, "Fitted covariance matrix does not correlate with ideal covariance matrix!"
    # check that the recovered SNRs makes sense
    p = scipy.stats.pearsonr(brsa.nSNR_, snr)[1]
    assert p < 0.01, "Fitted SNR does not correlate with simualted SNR!"
    assert np.isclose(np.mean(np.log(brsa.nSNR_)), 0), "nSNR_ not normalized!"
    assert not hasattr(brsa,'lGPinten_'),\
        'the BRSA object should not have parameters of lGPinten_ if only smoothness in space is requested.'