def test_vol_std(): # We make a fake 4D image shape_3d = (2, 3, 4) V = np.prod(shape_3d) T = 10 # The number of 3D volumes # Make a 2D array that we will reshape to 4D arr_2d = np.random.normal(size=(V, T)) expected_stds = np.std(arr_2d, axis=0) # Reshape to 4D arr_4d = np.reshape(arr_2d, shape_3d + (T,)) actual_stds = diagnostics.vol_std(arr_4d) assert_almost_equal(expected_stds, actual_stds)
sub001_task_run #Get the BOLD path sub001_path_BOLD = path_bold('sub001') #Get the each data of each task run run_img_result = { x: sub001_path_BOLD + "/" + x + '/bold.nii.gz' for x in sub001_task_run } #Plot the standard deviation for each run for i in run_img_result.keys(): img = nib.load(run_img_result[i]) data = img.get_data() vol_stds = dg.vol_std(data) outliers, thresholds = dg.iqr_outliers(vol_stds) N = len(vol_stds) x = np.arange(N) plt.plot(vol_stds, label='vol std') plt.plot(x[outliers], vol_stds[outliers], 'o', label='outliers') plt.plot([0, N], [thresholds[0], thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [thresholds[1], thresholds[1]], ':', label='IQR hi') plt.xlabel('Voxel') plt.ylabel('Volume standard deviation') plt.title('Volume Standard Deviation_' + i) plt.legend(fontsize=8) plt.savefig(i + '_vol_std.png') plt.close() # plot the RMS difference values
""" * Load the image as an image object * Load the image data from the image * Drop the first four volumes, as we know these are outliers """ img = nib.load('ds114_sub009_t2r1.nii') data = img.get_data() data = data[..., 4:] """ Use your vol_std function to get the volume standard deviation values for the remaining 169 volumes. Write these 169 values out to a text file. *IMPORTANT* - this text file MUST be called 'vol_std_values.txt' """ volstd = diagnostics.vol_std(data) fobj = open('vol_std_values.txt', 'wt') for i in volstd: fobj.write(str(i) + '\n') fobj.close() """ Use the iqr_outlier detection routine to get indices of outlier volumes. Write these indices out to a text file. *IMPORTANT* - this text file MUST be called 'vol_std_outliers.txt' """ outliers_index, thres = diagnostics.iqr_outliers(volstd) fobj = open('vol_std_outliers.txt', 'wt') for i in outliers_index: fobj.write(str(i) + '\n')
"""take out the volumne outliers in an image""" import nibabel as nib import matplotlib.pyplot as plt import numpy as np import numpy.linalg as npl from itertools import product import diagnostics reload(diagnostics) img=nib.load('ds005/sub001/BOLD/task001_run001/bold.nii') data=img.get_data() """get std""" volstd = diagnostics.vol_std(data) outliers_index, thres = diagnostics.iqr_outliers(volstd)
for image_path in images_paths: name = image_path[0] img = nib.load(image_path[1]) data_int = img.get_data() data = data_int.astype(float) vol_shape = data.shape[-1] mean_vol = np.mean(data, axis=-1) # Take the mean value over time and plot an histogram mean_vol = np.mean(data, axis=-1) plt.hist(np.ravel(mean_vol), bins=100) plt.title('Histogram of mean values of voxels accross time\n' + str(name)) plt.savefig(project_path+'fig/histograms/' + str(name) + '_hist.png') plt.xlabel('Mean value of voxels accross time') plt.close() # Outliers vol_stds = diagnostics.vol_std(data) assert len(vol_stds) == vol_shape #Plot data to see outliers np.savetxt(project_path+'txt_output/outliers/vol_std_' \ + str(name) + '.txt', vol_stds) outliers, thresholds = diagnostics.iqr_outliers(vol_stds) np.savetxt(project_path+'txt_output/outliers/vol_std_outliers_' \ + str(name) + '.txt', outliers) N = len(vol_stds) x = np.arange(N) plt.plot(vol_stds, label='voxels std ' + str(name)) plt.plot(x[outliers], vol_stds[outliers], 'o', label='outliers') plt.plot([0, N], [thresholds[0], thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [thresholds[1], thresholds[1]], ':', label='IQR hi') plt.title('voxels std ' + str(name))
for image_path in images_paths: name = image_path[0] img = nib.load(image_path[1]) data_int = img.get_data() data = data_int.astype(float) vol_shape = data.shape[-1] mean_vol = np.mean(data, axis=-1) # Take the mean value over time and plot an histogram mean_vol = np.mean(data, axis=-1) plt.hist(np.ravel(mean_vol), bins=100) plt.title('Histogram of mean values of voxels accross time\n' + str(name)) plt.savefig(project_path + 'fig/histograms/' + str(name) + '_hist.png') plt.xlabel('Mean value of voxels accross time') plt.close() # Outliers vol_stds = diagnostics.vol_std(data) assert len(vol_stds) == vol_shape #Plot data to see outliers np.savetxt(project_path+'txt_output/outliers/vol_std_' \ + str(name) + '.txt', vol_stds) outliers, thresholds = diagnostics.iqr_outliers(vol_stds) np.savetxt(project_path+'txt_output/outliers/vol_std_outliers_' \ + str(name) + '.txt', outliers) N = len(vol_stds) x = np.arange(N) plt.plot(vol_stds, label='voxels std ' + str(name)) plt.plot(x[outliers], vol_stds[outliers], 'o', label='outliers') plt.plot([0, N], [thresholds[0], thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [thresholds[1], thresholds[1]], ':', label='IQR hi') plt.title('voxels std ' + str(name))
#Extract each task sub001_task_run = open_bold('sub001') sub001_task_run #Get the BOLD path sub001_path_BOLD = path_bold('sub001') #Get the each data of each task run run_img_result = {x: sub001_path_BOLD + "/" + x + '/bold.nii.gz' for x in sub001_task_run} #Plot the standard deviation for each run for i in run_img_result.keys(): img = nib.load(run_img_result[i]) data = img.get_data() vol_stds = dg.vol_std(data) outliers, thresholds = dg.iqr_outliers(vol_stds) N = len(vol_stds) x = np.arange(N) plt.plot(vol_stds, label='vol std') plt.plot(x[outliers], vol_stds[outliers], 'o', label='outliers') plt.plot([0, N], [thresholds[0], thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [thresholds[1], thresholds[1]], ':', label='IQR hi') plt.xlabel('Voxel') plt.ylabel('Volume standard deviation') plt.title('Volume Standard Deviation_' + i) plt.legend(fontsize = 8) plt.savefig( i + '_vol_std.png') plt.close() # plot the RMS difference values