def test_calcAnova():
    # Test dataset
    t_data = np.reshape(np.random.normal(0,1,8), (1,1,1,8))
    run_group = np.array([1, 2, 1, 3, 3, 2, 2, 1])
    # split into runs
    d1 = np.reshape(t_data, (-1, 8)).T[:,0][run_group == 1]
    d2 = np.reshape(t_data, (-1, 8)).T[:,0][run_group == 2]
    d3 = np.reshape(t_data, (-1, 8)).T[:,0][run_group == 3]
    groups = np.array([d1,d2,d3])
    def ANOVA(G):
        # variation within groups
        SSD_W = 0
        for g in G:
            SSD_W += np.sum([(i-np.mean(g))**2 for i in g])
        # a bit awkward, just flattening the list of lists
        # to get the mean and N
        T = list()
        for g in G:
            T.extend(g)
        m = np.mean(T)
    
        # variation between groups (X for 'cross')
        SSD_X = 0
        for g in G:
            SSD_X += len(g)*(np.mean(g)-m)**2
        N = len(T)  
        k = len(G)  
        MS_W = SSD_W*1.0/(N-k)
        MS_X = SSD_X*1.0/(k-1)
        F_stat = MS_X/MS_W
        pval = 1-stats.f.cdf(F_stat, k-1, N-k)
        return np.array([F_stat, pval])
    
    test_anova = ANOVA(groups)
    # My function
    my_anova = calcAnov(t_data, run_group).ravel()
    my_anova_thrs = calcAnov(t_data, run_group, -40000).ravel()
    my_anova_thrs1 = calcAnov(t_data, run_group, 10).ravel()
    # Assert
    assert_allclose(test_anova, my_anova)
    assert_allclose(test_anova, my_anova_thrs)
    assert (test_anova != my_anova_thrs1).any()
예제 #2
0
        parameters = merge_cond(behav_cond, task_cond1, task_cond2, task_cond3, task_cond4)
        neural_prediction = events2neural_extend(parameters,TR, n_vols)
        gain, loss, linear_dr, quad_dr = getRegressor(TR, n_vols, hrf_at_trs, neural_prediction)
        data, gain, loss, linear_dr, quad_dr = deleteOutliers(data, gain, loss, linear_dr, quad_dr, i, run, dvars_out, fd_out)
        run_count[j-1] = data.shape[3]     ## dummy variable indicating the groups
        data_full = np.concatenate((data_full,data),axis=3)
        gain_full = np.concatenate((gain_full,gain),axis=0)
        loss_full = np.concatenate((loss_full,loss),axis=0)
        linear_full = np.concatenate((linear_full,linear_dr),axis=0)
        quad_full = np.concatenate((quad_full,quad_dr),axis=0)
        
    run_group = np.concatenate((np.repeat(1, run_count[0]), 
                                np.repeat(2, run_count[1]), np.repeat(3, run_count[2])), axis=0)
    thrshd = 400 ## set a threshold to idenfity the voxels inside the brain
    print "calculating parameters of subject "+str(i)
    beta = calcBetaLme(data_full, gain_full, loss_full, linear_full, quad_full, run_group, thrshd)
    sig_level = 0.05
    sig_gain_prop[i-1], sig_loss_prop[i-1] = calcSigProp(beta, sig_level)
    write=pathtofolder + 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/sub0'+str(i).zfill(2)+'_lme_beta.txt'
    np.savetxt(write, beta)
    anov_test = calcAnov(data_full, run_group, thrshd)
    anov_prop[i-1] = anovStat(anov_test)

write=pathtofolder + 'ds005/models/lme_sig_gain_prop.txt'
np.savetxt(write,  sig_gain_prop)
write=pathtofolder + 'ds005/models/lme_sig_loss_prop.txt'
np.savetxt(write,  sig_loss_prop)
write=pathtofolder + 'ds005/models/anova_prop.txt'
np.savetxt(write,  anov_prop)

예제 #3
0
        parameters = merge_cond(behav_cond, task_cond1, task_cond2, task_cond3, task_cond4)
        neural_prediction = events2neural_extend(parameters,TR, n_vols)
        gain, loss, linear_dr, quad_dr = getRegressor(TR, n_vols, hrf_at_trs, neural_prediction)
        data, gain, loss, linear_dr, quad_dr = deleteOutliers(data, gain, loss, linear_dr, quad_dr, i, run, dvars_out, fd_out)
        run_count[j-1] = data.shape[3]     ## dummy variable indicating the groups
        data_full = np.concatenate((data_full,data),axis=3)
        gain_full = np.concatenate((gain_full,gain),axis=0)
        loss_full = np.concatenate((loss_full,loss),axis=0)
        linear_full = np.concatenate((linear_full,linear_dr),axis=0)
        quad_full = np.concatenate((quad_full,quad_dr),axis=0)
        
    run_group = np.concatenate((np.repeat(1, run_count[0]), 
                                np.repeat(2, run_count[1]), np.repeat(3, run_count[2])), axis=0)
    thrshd = 400 ## set a threshold to idenfity the voxels inside the brain
    print "calculating parameters of subject "+str(i)
    beta = calcBetaLme(data_full, gain_full, loss_full, linear_full, quad_full, run_group, thrshd)
    sig_level = 0.05
    sig_gain_prop[i-1], sig_loss_prop[i-1] = calcSigProp(beta, sig_level)
    write=pathtofolder + 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/sub0'+str(i).zfill(2)+'_lme_beta.txt'
    np.savetxt(write, beta)
    anov_test = calcAnov(data_full, run_group)
    anov_prop[i-1] = anovStat(anov_test)

write=pathtofolder + 'ds005/models/lme_sig_gain_prop.txt'
np.savetxt(write,  sig_gain_prop)
write=pathtofolder + 'ds005/models/lme_sig_loss_prop.txt'
np.savetxt(write,  sig_loss_prop)
write=pathtofolder + 'ds005/models/anova_prop.txt'
np.savetxt(write,  anov_prop)