def design_matrix(subject, run, TR = 2.5): data = bold_data(subject,run) vol_shape, n_trs = data.shape[:-1], data.shape[-1] tr_times = np.arange(0,30,TR) hrf_at_trs = hrf(tr_times) col = 0 X = np.ones((n_trs,14)) #Smoothed and masked data mean_data = np.mean(data,axis=-1) masked, mask = median_otsu(mean_data,2,1) # smooth_data = gaussian_filter(data,[2,2,2,0]) # Y = smooth_data[mask].T #omitted smoothing for now Y = data[mask].T #Adding onsets to design matrix for i in list_cond_file(subject,run): neural_prediction = events2neural_fixed(i, TR, n_trs) convolved = convolve(neural_prediction, hrf_at_trs) X[:,col] = convolved col = col+1 ##PCA Y_demeaned = Y - np.mean(Y,axis=1).reshape([-1,1]) unscaled_cov = Y_demeaned.dot(Y_demeaned.T) U, S, V = npl.svd(unscaled_cov) X[:,8] = U[:,0] X[:,9:11] = U[:,6:8] linear_drift = np.linspace(-1,1,n_trs) X[:,11] = linear_drift quadratic_drift = linear_drift ** 2 quadratic_drift -= np.mean(quadratic_drift) X[:,12]= quadratic_drift betas = npl.pinv(X).dot(Y) betas_vols = np.zeros(vol_shape+(14,)) betas_vols[mask,:] = betas.T projections = U.T.dot(Y_demeaned) projection_vols = np.zeros(data.shape) projection_vols[mask,:] = projections.T return X, Y, betas_vols, mask, U, Y_demeaned, mean_data, projection_vols
def design_matrix(subject, run, TR=2.5): data = bold_data(subject, run) vol_shape, n_trs = data.shape[:-1], data.shape[-1] tr_times = np.arange(0, 30, TR) hrf_at_trs = hrf(tr_times) col = 0 X = np.ones((n_trs, 14)) #Smoothed and masked data mean_data = np.mean(data, axis=-1) masked, mask = median_otsu(mean_data, 2, 1) # smooth_data = gaussian_filter(data,[2,2,2,0]) # Y = smooth_data[mask].T #omitted smoothing for now Y = data[mask].T #Adding onsets to design matrix for i in list_cond_file(subject, run): neural_prediction = events2neural_fixed(i, TR, n_trs) convolved = convolve(neural_prediction, hrf_at_trs) X[:, col] = convolved col = col + 1 ##PCA Y_demeaned = Y - np.mean(Y, axis=1).reshape([-1, 1]) unscaled_cov = Y_demeaned.dot(Y_demeaned.T) U, S, V = npl.svd(unscaled_cov) X[:, 8] = U[:, 0] X[:, 9:11] = U[:, 6:8] linear_drift = np.linspace(-1, 1, n_trs) X[:, 11] = linear_drift quadratic_drift = linear_drift**2 quadratic_drift -= np.mean(quadratic_drift) X[:, 12] = quadratic_drift betas = npl.pinv(X).dot(Y) betas_vols = np.zeros(vol_shape + (14, )) betas_vols[mask, :] = betas.T projections = U.T.dot(Y_demeaned) projection_vols = np.zeros(data.shape) projection_vols[mask, :] = projections.T return X, Y, betas_vols, mask, U, Y_demeaned, mean_data, projection_vols
from convolve import * import pyfits filename = "sub600.fits" psf = pyfits.open(filename)[0].data.copy().T psf /= psf.sum() # filename='sub1000.fits' # img = pyfits.open(filename)[0].data.copy().T img = psf im = convolve(img, psf) pyfits.PrimaryHDU(im.T).writeto("cov_psf_psf.fits", clobber=True)
# TR is 2,5 second TR = 2.5 tr_times = np.arange(0, 30, TR) # The number of the voxel is 121 n_vols = 121 all_tr_times = np.arange(121) * TR for i in list_every_cond('sub001', 'task001_run001'): neural = events2neural(cond_path('sub001', 'task001_run001', i), TR, n_vols) convolved = convolve(neural, hrf(tr_times)) plt.plot(all_tr_times, neural) plt.plot(all_tr_times, convolved) plt.close() # There are 8 categories, and 121 features each categories. X = np.ones((n_vols, 8 + 1)) #There are 8 categories, so the data matrix has 9 colomns. #The order of the column is house, scrambledpix, cat, shoe, bottle, scissors, chair, face. # Let the first column starts from index 0 col = 0 # create data matrix
hrf_at_trs = hrf(tr_times) X = np.ones((n_trs,14)) X_np = np.ones((n_trs,14)) mean_data = np.mean(data,axis=-1) masked, mask = median_otsu(mean_data,2,1) Y = data[mask].T col = 0 pred = 0 #Adding onsets to design matrix for i in list_cond_file(subject,run): neural_prediction = events2neural_fixed(i, TR, n_trs) convolved = convolve(neural_prediction, hrf_at_trs) X[:,col] = convolved X_np[:,pred] = neural_prediction col = col + 1 pred = pred + 1 plt.plot(all_tr_times ,X_np[:,:8]) plt.savefig('../../images/block.png') plt.close() plt.plot(all_tr_times ,X_np[:,:8]) plt.plot(all_tr_times, X[:,:8]) plt.savefig('../../images/block_and_hdr.png') plt.close() #II. Design
hrf_at_trs = hrf(tr_times) X = np.ones((n_trs,14)) X_np = np.ones((n_trs,14)) mean_data = np.mean(data,axis=-1) masked, mask = median_otsu(mean_data,2,1) Y = data[mask].T col = 0 pred = 0 #Adding onsets to design matrix for i in list_cond_file(subject,run): neural_prediction = events2neural_fixed(i, TR, n_trs) convolved = convolve(neural_prediction, hrf_at_trs) X[:,col] = convolved X_np[:,pred] = neural_prediction col = col + 1 pred = pred + 1 plt.plot(all_tr_times ,X_np[:,:8]) plt.savefig('block.png') plt.close() plt.plot(all_tr_times ,X_np[:,:8]) plt.plot(all_tr_times, X[:,:8]) plt.savefig('block_and_hdr.png') plt.close() #II. Design