def test_present(): # Read in the image data. data = np.arange(100000) data = data.reshape((100,100,10)) full=present_3d(data) assert full.shape == (400,300)
#plt.close() #print("Initial plot with NO MASK (using old bh function) done.") print("# ==== BEGIN varying the Q value = .005 (FDR) ==== #") Q = .005 significant_pvals = bh_procedure(p_vals, Q) # Reshape significant_pvals reshaped_sig_p = np.reshape(significant_pvals, data.shape[:-1]) slice_reshaped_sig_p = reshaped_sig_p[...,7] masked_data = make_mask(original_slice, reshaped_sig_p, fit=False) plt.imshow(present_3d(masked_data)) plt.clim(0, 1600) plt.colorbar() plt.title('Slice with Significant p-values (Q = .005)') plt.savefig(location_of_images+"significant_p_slice1.png") plt.close() print("# ==== END plot with Q = .005 done. ==== #") print("# ==== BEGIN varying the Q value = .05 (FDR) ==== #") Q = .05 significant_pvals = bh_procedure(p_vals, Q) # Reshape significant_pvals reshaped_sig_p = np.reshape(significant_pvals, data.shape[:-1]) slice_reshaped_sig_p = reshaped_sig_p[...,7]
final_bh = np.mean(bh_mean, axis = 3) np.save("../data/bh_t_beta/bh_all.npy",bh_mean) np.save("../data/bh_t_beta/final_bh_average.npy", final_bh) final_t = np.mean(t_mean, axis = 3) np.save("../data/bh_t_beta/final_t_average.npy", final_t) np.save("../data/bh_t_beta/t_all.npy",t_mean) final_beta = np.mean(beta_mean, axis = 3) np.save("../data/bh_t_beta/final_beta_average.npy", final_beta) np.save("../data/bh_t_beta/beta_all.npy",beta_mean) # plot/save the result (BH) plt.imshow(present_3d(final_bh), interpolation = 'nearest', cmap = 'seismic') plt.title("Mean BH Value Across 25 Subjects with Q = .25") plt.colorbar() plt.savefig("../../images/bh_mean_final.png") plt.close() # plot/save the result (t) plt.imshow(present_3d(final_t), interpolation = 'nearest', cmap = 'seismic') plt.title("Mean t_grouping Value Across 25 Subjects with proportion = .1") plt.colorbar() plt.savefig("../../images/tgroup_mean_final.png") plt.close() # plot/save the result (beta) plt.imshow(present_3d(final_beta), interpolation = 'nearest', cmap = 'seismic') plt.title("Mean beta_grouping Value Across 25 Subjects with proportion = .2")
residuals = np.load(residual_data+i+"_residual.npy") sw_pvals = check_sw(residuals) print(i+" proportion of voxels with p-value above 0.05 (unmasked): "+str(np.mean(sw_pvals > 0.05))) mask = nib.load(path_to_data+i+'/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() masked_pvals = make_mask(sw_pvals, mask_data, fit=True) pvals_in_brain = sw_pvals.ravel()[masked_pvals.ravel() != 0] print(i+" proportion of voxels with p-value above 0.05 (masked): "+str(np.mean(pvals_in_brain > 0.05))) sys.stdout.write("-") sys.stdout.flush() # Save image plots of masked and unmasked p-values for a single subject. plt.imshow(present_3d(sw_pvals), cmap=plt.get_cmap('gray')) plt.savefig(location_of_images+i+'sw.png') plt.close() plt.imshow(present_3d(masked_pvals), cmap=plt.get_cmap('gray')) plt.savefig(location_of_images+i+'swmasked.png') plt.close() sys.stdout.write("\n")
data = img.get_data() data = data[...,6:] # Knock off the first 6 observations. ####################### # a. (my) smoothing # ####################### # Kind of arbitrary chosen time time = 7 original_slice = data[..., 7] # full width at half maximum (FWHM) fwhm = 1.5 smoothed_slice = smoothvoxels(data, fwhm, time) # visually compare original_slice to smoothed_slice plt.imshow(present_3d(smoothed_slice)) plt.colorbar() plt.title('Smoothed Slice') plt.clim(0,1600) plt.savefig(location_of_images+"smoothed_slice.png") plt.close() plt.imshow(present_3d(original_slice)) plt.colorbar() plt.title('Original Slice') plt.clim(0,1600) plt.savefig(location_of_images+"original_slice.png") plt.close()
]: t_mean = np.zeros((64, 64, 34, 24)) count = 0 for i in sub_list: t_stat = np.load(t_data + i + model) mask = nib.load(path_to_data + i + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() t_mean[..., count] = make_mask(t_stat, mask_data, fit=True) count += 1 t_mean = np.mean(t_mean, axis=3) final = present_3d(t_mean) plt.imshow(final, interpolation='nearest', cmap='seismic') plt.title("Mean T-Statistic Value Across 25 Subjects") zero_out = max(abs(np.min(final)), np.max(final)) plt.clim(-zero_out, zero_out) plt.colorbar() plt.show() # # ##################################### # ########## Clustering########## # ##################################### # # data_new = t_mean[...,20:23] # X = np.reshape(data_new, (-1, 1)) #
sys.stdout.write("\b" * (toolbar_width+1)) for i in desired_index: name=sub_list[i] # the mask for each subject path_to_data = project_path + "data/ds009/" + name brain = nib.load(path_to_data + '/anatomy/inplane001_brain.nii.gz') brain=brain.get_data() ########################### # Benjamini Hochberg Plot # ########################### plt.imshow(present_3d(brain[::2,::2,:]),cmap="gray") upper= np.percentile(np.ravel(brain[::2,::2,:]),95) plt.colorbar() if bRIGHT: plt.clim(0,upper) overlap=present_3d(bh_all[...,i]) overlap[overlap==0]=np.nan overlap[-1,-1]=0 # to make the output correct plt.imshow(overlap,cmap="bwr",alpha=.7) plt.xticks([]) plt.yticks([]) plt.title(name+ ", Benjamini Hochberg") plt.savefig("../../images/"+name+"_bh_overlay.png") plt.close()
behav_suffix = "/behav/task001_run001/behavdata.txt" smooth_data = final_data + 'smooth/' hrf_data = final_data + 'hrf/' sys.path.append(location_of_functions) from Image_Visualizing import present_3d, make_mask, present_3d_options i="sub001" brain=nib.load(path_to_data+i+'/BOLD/task001_run001/bold.nii.gz') data=brain.get_data() brain_hi= nib.load(path_to_data + i+'/anatomy/inplane001_brain.nii.gz') joy_hi=brain_hi.get_data() joy=data[...,7] plt.imshow(present_3d(data[...,7]),cmap="gray",interpolation="nearest") plt.figure() plt.imshow(present_3d_options(joy,2),cmap="gray",interpolation="nearest") plt.title("2") plt.figure() plt.imshow(present_3d_options(joy,1),cmap="gray",interpolation="nearest") plt.title("1") plt.figure() plt.imshow(present_3d_options(joy,0),cmap="gray",interpolation="nearest") plt.title("0")
mask[mask>0]=1 t_vals=t t_vals_3d=t_vals.reshape(data.shape[:-1]) pro=[.25,.1,.1,.05,.025] folks=[1,1,5,5,10] plt.close() for i in np.arange(5): start,cutoff=t_grouping_neighbor(t_vals_3d,mask,pro[i],prop=True,neighbors= folks[i],abs_on=True) plt.imshow(present_3d(2*start-1),interpolation='nearest',cmap="seismic") plt.title("T statistics " +str(pro[i])+" proportion \n (cutoff=" + str(cutoff)+") , neighbors: " + str(folks[i])) plt.colorbar() plt.savefig(location_of_images+str(pro[i])+"_" + str(folks[i])+"_t.png") plt.close() ################## # Beta # ################## b1 = B[1] #cutoff = .6 b1_vals_3d=b1.reshape(data.shape[:-1]) pro=[.25,.1,.1,.05,.025] folks=[1,1,5,5,10]
for i in sub_list: residuals = np.load(residual_data + i + "_residual.npy") sw_pvals = check_sw(residuals) unmasked_prop.append(np.mean(sw_pvals > 0.05)) mask = nib.load(path_to_data + i + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() masked_pvals = make_mask(sw_pvals, mask_data, fit=True) masked_pvals[masked_pvals > 1] = 1 pvals_in_brain = sw_pvals.ravel()[masked_pvals.ravel() != 0] masked_prop.append(np.mean(pvals_in_brain > 0.05)) if (i[-3:] == "010"): # Save image plots of unmasked p-values for subject 10. plt.imshow(present_3d(sw_pvals), cmap=plt.get_cmap('gray')) plt.colorbar() plt.xticks([]) plt.yticks([]) plt.title("p-values for " + i + " (Unmasked Data)") plt.savefig(location_of_images + i + 'sw.png') plt.close() # Save image plots of masked p-values for a single subject. plt.imshow(present_3d(masked_pvals), cmap=plt.get_cmap('gray')) plt.colorbar() plt.xticks([]) plt.yticks([]) plt.title("p-values for " + i + " (Masked Data)") plt.savefig(location_of_images + i + 'swmasked.png') plt.close()
location_of_functions = project_path + "code/utils/functions/" sys.path.append(location_of_functions) from Image_Visualizing import present_3d # mean_across for all the process outputs for each subject final_bh = np.load("../data/bh_t_beta/final_bh_average.npy") final_t = np.load("../data/bh_t_beta/final_t_average.npy") final_beta = np.load("../data/bh_t_beta/final_beta_average.npy") ##################################### # Benjamini Hochberg Plots Q = 0.25 # ##################################### desired_image = present_3d(final_bh) desired_image[320:, 256:] = .5 plt.imshow(desired_image, interpolation='nearest', cmap='gray') plt.title("Mean BH Value Across 24 Subjects with Q = .25") plt.xticks([]) plt.yticks([]) plt.colorbar() plt.savefig("../../images/bh_mean_final.png") plt.close() ###################################### # T-statistic Plots Proportion = 0.1 # ###################################### desired_image = present_3d(final_t)
#plt.savefig(location_of_images+"OLD_significant_p_slice_NOMASK.png") #plt.close() #print("Initial plot with NO MASK (using old bh function) done.") print("# ==== BEGIN varying the Q value = .005 (FDR) ==== #") Q = .005 significant_pvals = bh_procedure(p_vals, Q) # Reshape significant_pvals reshaped_sig_p = np.reshape(significant_pvals, data.shape[:-1]) slice_reshaped_sig_p = reshaped_sig_p[..., 7] masked_data = make_mask(original_slice, reshaped_sig_p, fit=False) plt.imshow(present_3d(masked_data)) plt.clim(0, 1600) plt.colorbar() plt.title('Slice with Significant p-values (Q = .005)') plt.savefig(location_of_images + "significant_p_slice1.png") plt.close() print("# ==== END plot with Q = .005 done. ==== #") print("# ==== BEGIN varying the Q value = .05 (FDR) ==== #") Q = .05 significant_pvals = bh_procedure(p_vals, Q) # Reshape significant_pvals reshaped_sig_p = np.reshape(significant_pvals, data.shape[:-1]) slice_reshaped_sig_p = reshaped_sig_p[..., 7]
TR, n_vols) convolved = np.convolve(neural_prediction, hrf_at_trs) # hrf_at_trs sample N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 np_hrf = convolved[:N] B, t, df, p = t_stat(data, np_hrf, np.array([0, 1])) #Simple mask function mask = nib.load(pathtodata + i + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() t_mean[..., int(i[-1])] = make_mask(np.reshape(t, (64, 64, 34)), mask_data, fit=True) final = present_3d(np.mean(t_mean, axis=3)) ####################### # Plot the results # ####################### plt.imshow(final, interpolation='nearest', cmap='seismic') plt.title("Mean T-Statistic Value Across 25 Subjects") zero_out = max(abs(np.min(final)), np.max(final)) plt.clim(-zero_out, zero_out) plt.colorbar() plt.savefig("../../../images/hypothesis_testing.png") plt.close()
data = img.get_data() data = data[..., 6:] # Knock off the first 6 observations. ####################### # a. (my) smoothing # ####################### # Kind of arbitrary chosen time time = 7 original_slice = data[..., 7] # full width at half maximum (FWHM) fwhm = 1.5 smoothed_slice = smoothvoxels(data, fwhm, time) # visually compare original_slice to smoothed_slice plt.imshow(present_3d(smoothed_slice)) plt.colorbar() plt.title('Smoothed Slice') plt.clim(0, 1600) plt.savefig(location_of_images + "smoothed_slice.png") plt.close() plt.imshow(present_3d(original_slice)) plt.colorbar() plt.title('Original Slice') plt.clim(0, 1600) plt.savefig(location_of_images + "original_slice.png") plt.close()
t_vals = t t_vals_3d = t_vals.reshape(data.shape[:-1]) pro = [.25, .1, .1, .05, .025] folks = [1, 1, 5, 5, 10] plt.close() for i in np.arange(5): start, cutoff = t_grouping_neighbor(t_vals_3d, mask, pro[i], prop=True, neighbors=folks[i], abs_on=True) plt.imshow(present_3d(2 * start - 1), interpolation='nearest', cmap="seismic") plt.title("T statistics " + str(pro[i]) + " proportion \n (cutoff=" + str(cutoff) + ") , neighbors: " + str(folks[i])) plt.colorbar() plt.savefig(location_of_images + str(pro[i]) + "_" + str(folks[i]) + "_t.png") plt.close() ################## # Beta # ################## b1 = B[1] #cutoff = .6 b1_vals_3d = b1.reshape(data.shape[:-1])
############################################ # a. Pick a good voxel to compare against # ############################################ from glm import glm from Image_Visualizing import present_3d beta_np, X_np = glm(data, conv_np) # beta_2,X_2=glm(data,conv_2) not correct shape beta_3, X_3 = glm(data, conv_3) beta_4, X_4 = glm(data, conv_4_30) #beta_5,X_5=glm(data,conv_5) # non-np are stronger/more clear plt.imshow(present_3d(beta_np[..., 1]), cmap="gray", interpolation="nearest") plt.imshow(present_3d(beta_3[..., 1]), cmap="gray", interpolation="nearest") plt.imshow(present_3d(beta_4[..., 1]), cmap="gray", interpolation="nearest") #plt.imshow(present_3d(beta_5[...,1]),cmap="gray",interpolation="nearest") plt.imshow(beta_4[..., 2, 1], cmap="gray", interpolation="nearest") plt.colorbar() plt.close() # From visual analysis # In the regression has a really high beta_1 value at: # beta_my[41,47,2,1] (voxel data[41,47,2] ) # lets use the comparisons (I know that is not good practice to check created # X based on betas based on X) ###########################################
from Image_Visualizing import present_3d # mean_across for all the process outputs for each subject final_bh = np.load("../data/bh_t_beta/final_bh_average.npy") final_t = np.load("../data/bh_t_beta/final_t_average.npy") final_beta = np.load("../data/bh_t_beta/final_beta_average.npy") ##################################### # Benjamini Hochberg Plots Q = 0.25 # ##################################### desired_image = present_3d(final_bh) desired_image[320:, 256:] = 0.5 plt.imshow(desired_image, interpolation="nearest", cmap="gray") plt.title("Mean BH Value Across 24 Subjects with Q = .25") plt.xticks([]) plt.yticks([]) plt.colorbar() plt.savefig("../../images/bh_mean_final.png") plt.close() ###################################### # T-statistic Plots Proportion = 0.1 # ######################################
plt.savefig(location_of_images + "NOMASK_significant_p_slice.png") plt.close() print("# ==== END No Mask, bh_procedure ==== #") print("# ==== BEGIN varying the Q value = .005 (FDR) ==== #") Q = .005 significant_pvals = bh_procedure(p_vals, Q) # Reshape significant_pvals reshaped_sig_p = np.reshape(significant_pvals, data.shape[:-1]) slice_reshaped_sig_p = reshaped_sig_p[..., 7] masked_data = make_mask(original_slice, reshaped_sig_p, fit=False) plt.imshow(present_3d(masked_data)) plt.clim(0, 1600) plt.colorbar() plt.title('Slice with Significant p-values (Q = .005)') plt.savefig(location_of_images + "significant_p_slice1.png") plt.close() print("# ==== END plot with Q = .005 done. ==== #") print("# ==== BEGIN varying the Q value = .05 (FDR) ==== #") Q = .05 significant_pvals = bh_procedure(p_vals, Q) # Reshape significant_pvals reshaped_sig_p = np.reshape(significant_pvals, data.shape[:-1]) slice_reshaped_sig_p = reshaped_sig_p[..., 7]
from glm import glm from Image_Visualizing import present_3d beta_np,X_np=glm(data,conv_np) # beta_2,X_2=glm(data,conv_2) not correct shape beta_3,X_3=glm(data,conv_3) beta_4,X_4=glm(data,conv_4_30) #beta_5,X_5=glm(data,conv_5) # non-np are stronger/more clear plt.imshow(present_3d(beta_np[...,1]),cmap="gray",interpolation="nearest") plt.imshow(present_3d(beta_3[...,1]),cmap="gray",interpolation="nearest") plt.imshow(present_3d(beta_4[...,1]),cmap="gray",interpolation="nearest") #plt.imshow(present_3d(beta_5[...,1]),cmap="gray",interpolation="nearest") plt.imshow(beta_4[...,2,1],cmap="gray",interpolation="nearest") plt.colorbar() plt.close() # From visual analysis # In the regression has a really high beta_1 value at: # beta_my[41,47,2,1] (voxel data[41,47,2] ) # lets use the comparisons (I know that is not good practice to check created # X based on betas based on X)
############################ # Condition 2 vs Time Plot # ############################ plt.plot(X_np[:,2]) plt.title("Condition 2 (pop) time predictions") plt.xlabel("Time") plt.ylabel("Hemoglobin response") plt.savefig(location_of_images+'cond2_time.png') plt.close() ############################ # Condition 2 brain image # ############################ plt.imshow(present_3d(B_np[...,2]),interpolation='nearest', cmap='seismic') # instead of cmap="gray" plt.title("Condition 2 (pop) beta Brain Image") plt.colorbar() zero_out=max(abs(np.min(present_3d(B_np[...,2]))),np.max(present_3d(B_np[...,2]))) plt.clim(-zero_out,zero_out) plt.savefig(location_of_images+'mr_cond2_beta_brain.png') plt.close() ############################ # Condition 3 vs Time Plot # ############################ plt.plot(X_np[:,3]) plt.title("Condition 3 (save) time predictions") plt.xlabel("Time")
N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 np_hrf=convolved[:N] B,t,df,p = t_stat(data, np_hrf, np.array([0,1])) #Simple mask function mask = nib.load(pathtodata+i+'/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() t_mean[...,int(i[-1])] = make_mask(np.reshape(t,(64,64,34)), mask_data, fit=True) final = present_3d(np.mean(t_mean,axis=3)) ####################### # Plot the results # ####################### plt.imshow(final,interpolation='nearest', cmap='seismic') plt.title("Mean T-Statistic Value Across 25 Subjects") zero_out=max(abs(np.min(final)),np.max(final)) plt.clim(-zero_out,zero_out) plt.colorbar() plt.savefig("../../../images/hypothesis_testing.png") plt.close()
behav_suffix = "/behav/task001_run001/behavdata.txt" smooth_data = final_data + 'smooth/' hrf_data = final_data + 'hrf/' sys.path.append(location_of_functions) from Image_Visualizing import present_3d, make_mask, present_3d_options i = "sub001" brain = nib.load(path_to_data + i + '/BOLD/task001_run001/bold.nii.gz') data = brain.get_data() brain_hi = nib.load(path_to_data + i + '/anatomy/inplane001_brain.nii.gz') joy_hi = brain_hi.get_data() joy = data[..., 7] plt.imshow(present_3d(data[..., 7]), cmap="gray", interpolation="nearest") plt.figure() plt.imshow(present_3d_options(joy, 2), cmap="gray", interpolation="nearest") plt.title("2") plt.figure() plt.imshow(present_3d_options(joy, 1), cmap="gray", interpolation="nearest") plt.title("1") plt.figure() plt.imshow(present_3d_options(joy, 0), cmap="gray", interpolation="nearest") plt.title("0") plt.close() plt.imshow(present_3d_options(joy_hi, 2), cmap="gray", interpolation="nearest") plt.title("2") plt.savefig(location_of_images + "kent_brain_2.png")
t_mean = np.zeros((64, 64, 34,24)) #loop through each person's T-statistic count=0 for i in sub_list: t_stat = np.load(t_data+i+"_tstat.npy") mask = nib.load(path_to_data+i+'/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() t_mean[...,count] = make_mask(t_stat, mask_data, fit=True) count+=1 t_mean = np.mean(t_mean,axis=3) final = present_3d(t_mean) plt.imshow(final,interpolation='nearest', cmap='seismic') plt.title("Mean T-Statistic Value Across 25 Subjects") zero_out=max(abs(np.min(final)),np.max(final)) plt.clim(-zero_out,zero_out) plt.colorbar() plt.show() #Cluster data_new = t_mean[...,10:15] X = np.reshape(data_new, (-1, 1)) connectivity = grid_to_graph(n_x= data_new.shape[0], n_y = data_new.shape[1], n_z = data_new.shape[2])
bh_all[bh_all!=1]=np.nan t_all[t_all!=1]=np.nan beta_all[beta_all!=1]=np.nan for i, name in enumerate(sub_list): # the mask for each subject path_to_data = project_path + "data/ds009/" + name brain = nib.load(path_to_data + '/anatomy/inplane001_brain.nii.gz') brain=brain.get_data() # bh plt.imshow(present_3d(brain[::2,::2,:]),cmap="gray") upper= np.percentile(np.ravel(brain[::2,::2,:]),95) plt.colorbar() #plt.clim(0,upper) overlap=present_3d(bh_all[...,i]) overlap[overlap==0]=np.nan overlap[-1,-1]=0 # to make the output correct plt.imshow(overlap,cmap="Blues",alpha=.5) plt.savefig("../../images/"+name+"_bh_overlay.png") plt.close() # t plt.imshow(present_3d(brain[::2,::2,:]),cmap="gray")
p_3d = np.load("../data/p-values/"+name+"_pvalue_fourier.npy") t_3d = np.load("../data/t_stat/"+name+"_tstat_fourier.npy") beta_3d = np.load("../data/betas/"+name+"_beta_fourier.npy") X = np.load("../data/X/"+name+"_covX.npy") mask = nib.load(path_to_data + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() rachels_ones = np.ones((64, 64, 34)) fitted_mask = make_mask(rachels_ones, mask_data, fit = True) fitted_mask[fitted_mask>0]=1 plt.imshow(present_3d(beta_3d*fitted_mask),cmap="seismic") plt.colorbar() plt.clim(-np.max(abs(beta_3d)),np.max(abs(beta_3d))) plt.title(name+" beta values") plt.yticks([]) plt.xticks([]) plt.savefig(location_of_images+name+"_"+"beta_fourier.png") plt.close() plt.imshow(present_3d(t_3d*fitted_mask),cmap="seismic") plt.colorbar() plt.clim(-np.max(abs(t_3d)),np.max(abs(t_3d))) plt.title(name+" t values") plt.yticks([]) plt.xticks([])