#START DOING GLM
    for j in range(data.shape[2]):

        data_slice = data[:, :, j, :]
        mask_slice = mask_data[:, :, j]
        data_slice = data_slice.reshape((-1, int(num_TR)))
        mask_slice = np.ravel(mask_slice)

        data_slice = data_slice[mask_slice == 1]

        # all conditions in 1 roof (cond_all)
        X = np.ones((n_vols, 15))
        X[:, 1] = hrf_matrix_all[:, j]  # 1 more
        X[:, 2] = np.linspace(-1, 1, num=X.shape[0])  #drift
        X[:, 3:9] = fourier_creation(X.shape[0], 3)[:, 1:]  # six more
        X[:, 9:] = pca_addition

        # all conditions seperate (cond1, cond2, cond3)
        X_cond = np.ones((n_vols, 17))
        X_cond[:, 1] = hrf_matrix_1[:, j]  # 1 more
        X_cond[:, 2] = hrf_matrix_2[:, j]  # 1 more
        X_cond[:, 3] = hrf_matrix_3[:, j]  # 1 more
        X_cond[:, 4] = np.linspace(-1, 1, num=X.shape[0])  #drift # one
        X_cond[:, 5:11] = fourier_creation(X.shape[0], 3)[:, 1:]  # six more
        X_cond[:, 11:] = pca_addition

        #START CREATING MODELS

        ###################
        #   MODEL 1       #
    #START DOING GLM
    for j in range(data.shape[2]):

        data_slice = data[:,:,j,:]
        mask_slice = mask_data[:,:,j]
        data_slice = data_slice.reshape((-1, int(num_TR)))
        mask_slice = np.ravel(mask_slice)

        data_slice = data_slice[mask_slice == 1]

        # all conditions in 1 roof (cond_all)
        X = np.ones((n_vols,15))
        X[:, 1] = hrf_matrix_all[:, j] # 1 more
        X[:, 2] = np.linspace(-1, 1, num = X.shape[0]) #drift
        X[:, 3:9] = fourier_creation(X.shape[0], 3)[:, 1:] # six more
        X[:, 9:] = pca_addition

        # all conditions seperate (cond1, cond2, cond3)
        X_cond = np.ones((n_vols,17))
        X_cond[:, 1] = hrf_matrix_1[:, j] # 1 more
        X_cond[:, 2] = hrf_matrix_2[:, j] # 1 more
        X_cond[:, 3] = hrf_matrix_3[:, j] # 1 more
        X_cond[:, 4] = np.linspace(-1, 1, num = X.shape[0]) #drift # one 
        X_cond[:, 5:11] = fourier_creation(X.shape[0], 3)[:, 1:] # six more
        X_cond[:, 11:] = pca_addition


    #START CREATING MODELS

        ###################
Beispiel #3
0
    mask_data = make_mask(np.ones(data.shape[:-1]), mask_data, fit = True)
    mask_data = mask_data != 0
    mask_data = mask_data.astype(int)


    
    #Run per slice in order to correct for time
    for j in range(data.shape[2]):
        
        data_slice = data[:, :, j, :]
        
        #Create design matrix
        X = np.ones((n_vols, 9))
        X[:, 1] = convolve[:, j]
        X[:, 2] = np.linspace(-1, 1, num = X.shape[0]) #drift
        X[:, 3:] = fourier_creation(n_vols, 3)[:, 1:]
        
        
        beta, t, df, p = t_stat_mult_regression(data_slice, X)
        
        #take only first coefficient      
        t = t[1, :]
        p = p[1, :]
        
        MRSS, fitted, residuals = glm_diagnostics(beta, X, data_slice)

        beta = beta[:, 1]
        
        #insert into the proper slice
        beta_final[:, :, j] = beta.reshape(data_slice.shape[:-1])
        t_final[:, :, j] = t.reshape(data_slice.shape[:-1])
img = nib.load(smooth_data+ i +"_bold_smoothed.nii")
data = img.get_data().astype(float)
    
n_vols = data.shape[-1]    
convolve = np.loadtxt(hrf_data+i+"_hrf.txt")

residual_final = np.zeros((data.shape))
t_final = np.zeros((data.shape[:-1]))


data_slice = data[:,:,j,:]
        
X = np.ones((n_vols,7))
X[:,1] = convolve[:,j]
X[:,2]=np.linspace(-1,1,num=X.shape[0]) #drift
X[:,3:]=fourier_creation(X.shape[0],2)[:,1:]

beta,t,df,p = t_stat_mult_regression(data_slice, X)


MRSS, fitted, residuals = glm_diagnostics(beta, X, data_slice)




plt.scatter(fitted[30,40,:],residuals[30,40,:])
plt.title("Subject 001, voxel: [30,40,15]")
plt.xlabel("Fitted Values")
plt.ylabel("Residuals")
plt.savefig(location_of_images+'Fitted_v_Residuals.png')
plt.close()
Beispiel #5
0
    img = nib.load(smooth_data + i + "_bold_smoothed.nii")
    data = img.get_data()

    n_vols = data.shape[-1]
    convolve = np.loadtxt(hrf_data + i + "_hrf.txt")

    residual_final = np.zeros((data.shape))
    t_final = np.zeros((data.shape[:-1]))

    for j in range(data.shape[2]):

        data_slice = data[:, :, j, :]
        X = np.ones((n_vols, 6))
        X[:, 1] = convolve[:, j]
        X[:, 2] = np.linspace(-1, 1, num=X.shape[0])  #drift
        X[:, 3:] = fourier_creation(X.shape[0], 3)[:, 1:]

        beta, t, df, p = t_stat_mult_regression(data_slice, X)

        t = t[1, :]

        MRSS, fitted, residuals = glm_diagnostics(beta, X, data_slice)

        t_final[:, :, j] = t.reshape(data_slice.shape[:-1])

        residual_final[:, :, j, :] = residuals.reshape(data_slice.shape)

        np.save("../data/glm/t_stat/" + i + "_tstat.npy", t_final)
        np.save("../data/glm/residual/" + i + "_residual.npy", residual_final)

    sys.stdout.write("-")
Beispiel #6
0
t_final = np.zeros((data.shape[:-1]))

data_slice = data[:, :, j, :]

#Prepare mask for PCA
mask = nib.load(path_to_data + i + '/anatomy/inplane001_brain_mask.nii.gz')
mask_data = mask.get_data()
mask_data = make_mask(np.ones(data.shape[:-1]), mask_data, fit=True)
mask_data = mask_data != 0
mask_data = mask_data.astype(int)

#Create design matrix
X = np.ones((n_vols, 9))
X[:, 1] = convolve[:, j]
X[:, 2] = np.linspace(-1, 1, num=X.shape[0])  #drift
X[:, 3:] = fourier_creation(n_vols, 3)[:, 1:]

beta, t, df, p = t_stat_mult_regression(data_slice, X)

MRSS, fitted, residuals = glm_diagnostics(beta, X, data_slice)

###########################
# Fitted VS Residual Plot #
###########################

plt.scatter(fitted[30, 40, :], residuals[30, 40, :])
min_max = (np.min(fitted[30, 40, :]), np.max(fitted[30, 40, :]))

plt.plot([min_max[0], min_max[1]], [0, 0], color="k")

plt.xlim(min_max[0], min_max[1])
Beispiel #7
0
    #Run GLM Per slice

    for j in range(data.shape[2]):

        data_slice = data[:, :, j, :]
        mask_slice = mask_data[:, :, j]
        data_slice = data_slice.reshape((-1, num_TR))
        mask_slice = np.ravel(mask_slice)

        data_slice = data_slice[mask_slice == 1]

        # all conditions in 1 roof (cond_all)
        X = np.ones((n_vols, 13))
        X[:, 1] = hrf_matrix_all[:, j]  # 1 more
        X[:, 2] = np.linspace(-1, 1, num=X.shape[0])  #drift # one
        X[:, 3:7] = fourier_creation(X.shape[0], 2)[:, 1:]  # four more
        X[:, 7:] = pca_addition

        # all conditions seperate (cond1,cond2,cond3)
        X_cond = np.ones((n_vols, 15))
        X_cond[:, 1] = hrf_matrix_1[:, j]
        X_cond[:, 2] = hrf_matrix_2[:, j]
        X_cond[:, 3] = hrf_matrix_3[:, j]
        X_cond[:, 4] = np.linspace(-1, 1, num=X.shape[0])  #drift # one
        X_cond[:, 5:9] = fourier_creation(X.shape[0], 2)[:, 1:]  # four more
        X_cond[:, 9:] = pca_addition

        #START CREATING MODELS

        ###################
        #   MODEL 1       #
    #START DOING GLM
    for j in range(data.shape[2]):

        data_slice = data[:,:,j,:]
        mask_slice = mask_data[:,:,j]
        data_slice = data_slice.reshape((-1,num_TR))
        mask_slice = np.ravel(mask_slice)

        data_slice = data_slice[mask_slice==1]

        # all conditions in 1 roof (cond_all)
        X = np.ones((n_vols,13))
        X[:,1] = hrf_matrix_all[:,j] # 1 more
        X[:,2] = np.linspace(-1,1,num=X.shape[0]) #drift # one 
        X[:,3:7] = fourier_creation(X.shape[0],2)[:,1:] # four more
        X[:,7:] = pca_addition

        # all conditions seperate (cond1,cond2,cond3)
        X_cond = np.ones((n_vols,15))
        X_cond[:,1] = hrf_matrix_1[:,j] # 1 more
        X_cond[:,2] = hrf_matrix_2[:,j] # 1 more
        X_cond[:,3] = hrf_matrix_3[:,j] # 1 more
        X_cond[:,4] = np.linspace(-1,1,num=X.shape[0]) #drift # one 
        X_cond[:,5:9] = fourier_creation(X.shape[0],2)[:,1:] # four more
        X_cond[:,9:] = pca_addition


    #START CREATING MODELS

        ###################