in_brain_img = make_mask_filtered_data(image_path[1],mask_path) print("brain image filtered\n") else: print("Loading filtered brain image for: ") print(str(name)) in_brain_img = nib.load(md) print("brain image loaded\n") data_int = in_brain_img.get_data() data = data_int.astype(float) mean_data = np.mean(data, axis=-1) template = nib.load(template_path) template_data_int = template.get_data() template_data = template_data_int.astype(float) Transpose = False in_brain_mask = (mean_data - 0.0) < 0.01 plt.imshow(plot_mosaic(template_data, transpose=Transpose),\ cmap='gray', alpha=1) else: img = nib.load(image_path[1]) data_int = img.get_data() data = data_int.astype(float) mean_data = np.mean(data, axis=-1) in_brain_mask = mean_data > thres Transpose = True plt.contour(plot_mosaic(in_brain_mask, transpose=Transpose), \ cmap='gray' , alpha=1) # Smoothing with Gaussian filter smooth_data = smoothing(data,1,range(data.shape[-1])) # Selecting the voxels in the brain in_brain_tcs = smooth_data[in_brain_mask, :]
else: img = nib.load(image_path[1]) data_int = img.get_data() data = data_int.astype(float) mean_data = np.mean(data, axis=-1) in_brain_mask = mean_data > thres Transpose = True # Smoothing with Gaussian filter smooth_data = smoothing(data,1,range(data.shape[-1])) # Selecting the voxels in the brain in_brain_tcs = smooth_data[in_brain_mask, :] #in_brain_tcs = data[in_brain_mask, :] vol_shape = data.shape[:-1] # Plotting the voxels in the brain plt.imshow(plot_mosaic(mean_data, transpose=Transpose), cmap='gray', alpha=1) plt.colorbar() plt.contour(plot_mosaic(in_brain_mask, transpose=Transpose),colors='blue') plt.title('In brain voxel mean values' + '\n' + (d_path['type'] + str(name))) plt.savefig(project_path+'fig/BOLD/%s_mean_voxels_countour.png'\ %(d_path['type'] + str(name))) #plt.show() plt.clf() # Convolution with 1 to 4 conditions convolved = np.zeros((240,5)) for i in range(1,5): #convolved = np.loadtxt(\ # '../../../txt_output/conv_normal/%s_conv_00%s_canonical.txt'\ # %(str(name),str(i))) convolved[:,i] = np.loadtxt(\
for mydict in [task, gain, loss]: mydict['mean'] = sum([task[sub] for sub in subject_list])/(len(subject_list)) mydict['mean_reshape'] = mydict['mean'].reshape(91,109,91) mydict['mean_mask'] = (mydict['mean_reshape'] - 0.0) < 0.01 mydict['mean_reshape_plot'] = mydict['mean_reshape'] mydict['mean_reshape_plot'][~mydict['mean_mask']] = np.nan print("Creating plots for the voxel mean accross subjects analysis") task_mean = task['mean'] gain_mean = gain['mean'] loss_mean = loss['mean'] in_brain_task = task['mean_mask'] in_brain_gain = gain['mean_mask'] in_brain_loss = loss['mean_mask'] plt.title('In brain activated voxels - \nmean across ' + str(len(subject_list)) +' subjects on TASK condition', fontsize=12) plt.imshow(plot_mosaic(template_data, transpose=False), cmap='gray', alpha=1) plt.imshow(plot_mosaic(task['mean_reshape_plot'], transpose=False), \ cmap='seismic', alpha=1, vmin=task['mean_reshape_plot'].min(), vmax= task['mean_reshape_plot'].max()) plt.colorbar() plt.savefig(dirs[1]+'/mean_task.png') #plt.show() plt.clf() print(" Plot for the TASK condition saved in " + dirs[1]+'/mean_task.png') plt.title('In brain activated voxels - \nmean across ' + str(len(subject_list)) + ' subjects on GAIN condition', fontsize=12) plt.imshow(plot_mosaic(template_data, transpose=False), cmap='gray', alpha=1) plt.imshow(plot_mosaic(gain['mean_reshape_plot'], transpose=False), \ cmap='seismic', alpha=1, vmin=gain['mean_reshape_plot'].min(), vmax= gain['mean_reshape_plot'].max()) plt.colorbar() plt.savefig(dirs[1]+'/mean_gain.png') #plt.show()
+ 'task001_run%s%s/%s' %(r.zfill(3),d_path['feat'],\ d_path['bold_img_name'])) \ for r in run_list \ for s in subject_list] for image_path in images_paths: name = image_path[0] data_int = nib.load(image_path[1]).get_data() data = data_int.astype(float) mean_data = np.mean(data, axis=-1) # Plot if d_path['type'] == 'filtered': Transpose = False template_data_int = nib.load(template_path).get_data() template_data = template_data_int.astype(float) plt.imshow(\ plot_mosaic(template_data, transpose=Transpose), \ cmap='gray', alpha=1) else: in_brain_mask = mean_data > 375 Transpose = True plt.contour(\ plot_mosaic(in_brain_mask, transpose=Transpose), \ cmap='gray' , alpha=1) plt.imshow(\ plot_mosaic(mean_data, transpose=Transpose), cmap='gray', alpha=1) plt.colorbar() plt.title('Voxels mean values' + '\n' + (d_path['type'] + str(name))) plt.savefig(project_path+'fig/BOLD/%s_mean_voxels.png'\ %(d_path['type'] + str(name))) #plt.show() plt.clf()
data_path + 'sub%s/'%(s.zfill(3)) + d_path['run_path'] \ + 'task001_run%s%s/%s' %(r.zfill(3),d_path['feat'],\ d_path['bold_img_name'])) \ for r in run_list \ for s in subject_list] for image_path in images_paths: name = image_path[0] data_int = nib.load(image_path[1]).get_data() data = data_int.astype(float) mean_data = np.mean(data, axis=-1) # Plot if d_path['type']=='filtered': Transpose=False template_data = nib.load(template_path).get_data() plt.imshow(\ plot_mosaic(template_data, transpose=Transpose), \ cmap='gray', alpha=1) else: in_brain_mask = mean_data > 375 Transpose=True plt.contour(\ plot_mosaic(in_brain_mask, transpose=Transpose), \ cmap='gray' , alpha=1) plt.imshow(\ plot_mosaic(mean_data, transpose=Transpose), cmap='gray', alpha=1) plt.colorbar() plt.title('Voxels mean values' + '\n' + (d_path['type'] + str(name))) plt.savefig(project_path+'fig/BOLD/%s_mean_voxels.png'\ %(d_path['type'] + str(name))) #plt.show() plt.clf()
X_matrix = np.ones((data.shape[-1], p)) #build our design matrix for cond in range(1,5): convolved = np.loadtxt(txt_path + name + '_conv_' + str(cond).zfill(3) + '_high_res.txt') #convolved = np.loadtxt(txt_path + name + '_conv_' + str(cond).zfill(3) + '_canonical.txt') X_matrix[:,cond] = convolved linear_drift = np.linspace(-1, 1, n_trs) X_matrix[:,5] = linear_drift quadratic_drift = linear_drift ** 2 quadratic_drift -= np.mean(quadratic_drift) X_matrix[:,6] = quadratic_drift beta, t, df, p = t_stat(smooth_data, X_matrix) for cond in range(0,4): print("Starting test for condition " + str(cond+1)) t_newshape = np.reshape(t[cond,:],vol_shape) t_newshape[~in_brain_mask]=np.nan t_T = np.zeros(vol_shape) for z in range(vol_shape[2]): t_T[:, :, z] = t_newshape[:,:, z].T t_plot = plot_mosaic(t_T) plt.imshow(t_plot,interpolation='nearest', cmap='seismic') zero_out=max(abs(np.nanmin(t_T)),np.nanmax(t_T)) plt.title(name+'_t_statistics'+'_cond_'+'_%s'%(cond+1)) plt.clim(-zero_out,zero_out) plt.colorbar() plt.savefig(dirs[1]+'/'+ name +'_t-test_'+'cond'+str(cond+1)+'.png') plt.close() print("\nT-test analysis and plots done for selected subjects") print("See mosaic plots in project-epsilon/fig/t-test/")
else: img = nib.load(image_path[1]) data_int = img.get_data() data = data_int.astype(float) mean_data = np.mean(data, axis=-1) in_brain_mask = mean_data > thres Transpose = True # Smoothing with Gaussian filter smooth_data = smoothing(data, 1, range(data.shape[-1])) # Selecting the voxels in the brain in_brain_tcs = smooth_data[in_brain_mask, :] #in_brain_tcs = data[in_brain_mask, :] vol_shape = data.shape[:-1] # Plotting the voxels in the brain plt.imshow(plot_mosaic(mean_data, transpose=Transpose), cmap='gray', alpha=1) plt.colorbar() plt.contour(plot_mosaic(in_brain_mask, transpose=Transpose), colors='blue') plt.title('In brain voxel mean values' + '\n' + (d_path['type'] + str(name))) plt.savefig(project_path+'fig/BOLD/%s_mean_voxels_countour.png'\ %(d_path['type'] + str(name))) #plt.show() plt.clf() # Convolution with 1 to 4 conditions convolved = np.zeros((240, 5)) for i in range(1, 5): #convolved = np.loadtxt(\
gain_sum += gain[x] gain_mean = gain_sum / 16 gain_mean_reshape = gain_mean.reshape(91, 109, 91) loss_sum = gain[1] for x in range(2, 17): loss_sum += loss[x] loss_mean = loss_sum / 16 loss_mean_reshape = loss_mean.reshape(91, 109, 91) plt.title( 'In brain activated voxels - \nmean across 16 subjects on TASK condition', fontsize=12) plt.imshow(plot_mosaic(template_data, transpose=False), cmap='gray', alpha=1) plt.imshow(plot_mosaic(task_mean_reshape, transpose=False), cmap='seismic', alpha=1, vmin=task_mean_reshape.min(), vmax=task_mean_reshape.max()) plt.colorbar() plt.savefig(dirs[1] + '/mean_task.png') plt.clf() plt.title( 'In brain activated voxels - \nmean across 16 subjects on GAIN condition', fontsize=12) plt.imshow(plot_mosaic(template_data, transpose=False), cmap='gray', alpha=1) plt.imshow(plot_mosaic(gain_mean_reshape, transpose=False), cmap='seismic',
in_brain_img = make_mask_filtered_data(image_path[1], mask_path) print("brain image filtered\n") else: print("Loading filtered brain image for: ") print(str(name)) in_brain_img = nib.load(md) print("brain image loaded\n") data_int = in_brain_img.get_data() data = data_int.astype(float) mean_data = np.mean(data, axis=-1) template = nib.load(template_path) template_data_int = template.get_data() template_data = template_data_int.astype(float) Transpose = False in_brain_mask = (mean_data - 0.0) < 0.01 plt.imshow(plot_mosaic(template_data, transpose=Transpose),\ cmap='gray', alpha=1) else: img = nib.load(image_path[1]) data = img.get_data() mean_data = np.mean(data, axis=-1) in_brain_mask = mean_data > thres Transpose = True plt.contour(plot_mosaic(in_brain_mask, transpose=Transpose), \ cmap='gray' , alpha=1) # Smoothing with Gaussian filter smooth_data = smoothing(data, 1, range(data.shape[-1])) # Selecting the voxels in the brain in_brain_tcs = smooth_data[in_brain_mask, :] #in_brain_tcs = data[in_brain_mask, :]