def test_calcBetaLme(): # Test data with large n = 1500 X = np.ones((2000, 4)) X[:, 0] = np.random.normal(0, 1, 2000) X[:, 1] = np.random.normal(2, 2, 2000) X[:, 2] = np.linspace(-1,1,2000) X[:, 3] = X[:,2]**2 Y = np.random.normal(3,1,2000) # Create linear regression object regr = linear_model.LinearRegression() # Train the model using the training sets regr.fit(X, Y) test_betas = regr.coef_ # My function, should produce same results if groups are all the same: lme = calcBetaLme(Y, X[:,0], X[:,1], X[:,2], X[:,3], np.repeat(1,2000)) lme_thrs = calcBetaLme(Y, X[:,0], X[:,1], X[:,2], X[:,3], np.repeat(1,2000), -40000) lme_thrs1 = calcBetaLme(Y, X[:,0], X[:,1], X[:,2], X[:,3], np.repeat(1,2000), 10) # Compare betas my_betas = lme.ravel()[[0,2]] my_betas_thrs = lme_thrs.ravel()[[0,2]] my_betas_thrs1 = lme_thrs1.ravel()[[0,2]] assert max(abs(my_betas - test_betas[:2])) < 0.005 assert max(abs(my_betas_thrs - test_betas[:2])) < 0.005 assert (test_betas != my_betas_thrs1)
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)
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)