def test_calcMRSS(): # Like above, create a test matrix of regressors X = np.ones((6, 4)) X[:, 0] = np.array([1,2,3,4,5,6]) X[:, 1] = np.array([2,4,6,9,10,12]) X[:, 2] = np.linspace(-1,1,6) X[:, 3] = X[:,2]**2 Y = np.array([10,12,14,15,17,20]) # Create linear regression object regr = linear_model.LinearRegression() # Train the model using the training sets regr.fit(X, Y) pred = regr.predict(X) test_MRSS = np.mean(np.sum((pred - Y)**2/(Y.shape[-1]-4))) #--------------------------------------------------------------------------# # my function my_MRSS = calcMRSS(Y, X[:,0], X[:,1], X[:,2], X[:,3]) #--------------------------------------------------------------------------# # assert assert (abs(test_MRSS-my_MRSS) < .0001) #--------------------------------------------------------------------------# # standard X_s = np.ones((6, 2)) X_s[:, 0] = np.array([1,2,3,4,5,6]) X_s[:, 1] = np.array([2.5,4.1,6.3,9.7,10.1,12.3]) Y_s = np.array([10,12,14,15,17,20]) # Create linear regression object regr_s = linear_model.LinearRegression() # Train the model using the training sets regr_s.fit(X_s, Y_s) pred_s = regr_s.predict(X_s) test_MRSS_s = np.mean(np.sum((pred_s - Y_s)**2/(Y_s.shape[-1]-3))) # my function my_MRSS_s = calcMRSS(Y_s, X_s[:,0], X_s[:,1]) #--------------------------------------------------------------------------# # assert assert (abs(test_MRSS_s-my_MRSS_s) < .0001)
def test_calcMRSS(): # Like above, create a test matrix of regressors X = np.ones((6, 4)) X[:, 0] = np.array([1,2,3,4,5,6]) X[:, 1] = np.array([2,4,6,9,10,12]) X[:, 2] = np.linspace(-1,1,6) X[:, 3] = X[:,2]**2 Y = np.array([10,12,14,15,17,20]) # Create linear regression object regr = linear_model.LinearRegression() # Train the model using the training sets regr.fit(X, Y) pred = regr.predict(X) test_MRSS = np.mean(np.sum((pred - Y)**2/(Y.shape[-1]-4))) #--------------------------------------------------------------------------# # my function my_MRSS = calcMRSS(Y, X[:,0], X[:,1], X[:,2], X[:,3]) #--------------------------------------------------------------------------# # assert assert (abs(test_MRSS-my_MRSS) < .0001)
def test_calcMRSS(): # Like above, create a test matrix of regressors X = np.ones((6, 4)) X[:, 0] = np.array([1, 2, 3, 4, 5, 6]) X[:, 1] = np.array([2, 4, 6, 9, 10, 12]) X[:, 2] = np.linspace(-1, 1, 6) X[:, 3] = X[:, 2] ** 2 Y = np.array([10, 12, 14, 15, 17, 20]) # Create linear regression object regr = linear_model.LinearRegression() # Train the model using the training sets regr.fit(X, Y) pred = regr.predict(X) test_MRSS = np.mean(np.sum((pred - Y) ** 2 / (Y.shape[-1] - 4))) # --------------------------------------------------------------------------# # my function my_MRSS = calcMRSS(Y, X[:, 0], X[:, 1], X[:, 2], X[:, 3]) # --------------------------------------------------------------------------# # assert assert abs(test_MRSS - my_MRSS) < 0.0001
+ ` j ` + "/cond004.txt" ) 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 ) 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) d_shape = data_full.shape[:3] mea = calcMRSS(data_full, gain_full, loss_full, linear_full, quad_full) X, Y, beta = calcBeta(data_full, gain_full, loss_full, linear_full, quad_full) # ------------------------------------------------------------------------------# # Take the 40,000 voxel fitted = X.dot(beta[:, 40000]) residuals = Y[:, 40000] - fitted qqplot(residuals, saveit=True) res_var(fitted, residuals, name="fitted", saveit=True) # possibly transform data: X_log, Y_log, beta_log = calcBeta(np.log(data_full + 1), gain_full, loss_full, linear_full, quad_full) residuals_log = Y_log[:, 40000] - X_log.dot(beta_log[:, 40000]) qqplot(residuals_log, saveit=True) res_var(X_log.dot(beta_log[:, 40000]), residuals_log, name="fitted_log", saveit=True) vox_pos = np.unravel_index(40000, d_shape) print("Voxel used: " + str(vox_pos))
gain_full = np.empty([0,]) loss_full = np.empty([0,]) linear_full = np.empty([0,]) quad_full = np.empty([0,]) for j in range(1,4): direct='ds005/sub0'+str(i).zfill(2)+'/BOLD/task001_run00'+`j`+'/' boldname = direct+'bold.nii.gz' img=nib.load(boldname) data=img.get_data() data=smooth_spatial(data) run = j behav_cond = 'ds005/sub0'+str(i).zfill(2)+'/behav/task001_run00'+`j`+'/behavdata.txt' task_cond1 = 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/task001_run00'+`j`+'/cond001.txt' task_cond2 = 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/task001_run00'+`j`+'/cond002.txt' task_cond3 = 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/task001_run00'+`j`+'/cond003.txt' task_cond4 = 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/task001_run00'+`j`+'/cond004.txt' 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) 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) mea=calcMRSS(data_full, gain_full, loss_full, linear_full, quad_full, threshold) X, Y, beta=calcBeta(data_full, gain_full, loss_full, linear_full, quad_full, threshold) # write='ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/sub'+`i`+'_beta.txt' # np.savetxt(write, beta)