def test_iqr_scaling():
    # Check that the scaling of IQR works
    # Test with simplest possible array
    arr = np.arange(101)  # percentile same as value
    # iqr = 50
    exp_lo = 25 - 100
    exp_hi = 75 + 100
    indices, thresholds = diagnostics.iqr_outliers(arr, 2)
    assert_array_equal(indices, [])
    assert_equal(thresholds, (exp_lo, exp_hi))
    # Add outliers - but these aren't big enough now
    arr[0] = -51
    arr[1] = 151
    indices, thresholds = diagnostics.iqr_outliers(arr, 2)
    assert_array_equal(indices, [])
    # Add outliers - that are big enough
    arr[0] = -76
    arr[1] = 176
    arr[100] = 1  # replace lost value to keep centiles same
    indices, thresholds = diagnostics.iqr_outliers(arr, 2)
    assert_array_equal(indices, [0, 1])
def test_iqr_outliers():
    # Test with simplest possible array
    arr = np.arange(101)  # percentile same as value
    # iqr = 50
    exp_lo = 25 - 75
    exp_hi = 75 + 75
    indices, thresholds = diagnostics.iqr_outliers(arr)
    assert_array_equal(indices, [])
    assert_equal(thresholds, (exp_lo, exp_hi))
    # Reverse, same values
    indices, thresholds = diagnostics.iqr_outliers(arr[::-1])
    assert_array_equal(indices, [])
    assert_equal(thresholds, (exp_lo, exp_hi))
    # Add outliers
    arr[0] = -51
    arr[1] = 151
    arr[100] = 1  # replace lost value to keep centiles same
    indices, thresholds = diagnostics.iqr_outliers(arr)
    assert_array_equal(indices, [0, 1])
    assert_equal(thresholds, (exp_lo, exp_hi))
    # Reversed, then the indices are reversed
    indices, thresholds = diagnostics.iqr_outliers(arr[::-1])
    assert_array_equal(indices, [99, 100])
    assert_equal(thresholds, (exp_lo, exp_hi))
Beispiel #3
0
#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
for i in run_img_result.keys():
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')
fobj.close()
"""
Plot all these on the same plot:

* The volume standard deviation values;
* The outlier points from the std values, marked on the plot with an 'o'
  marker;
* A horizontal dashed line at the lower IRQ threshold;
* A horizontal dashed line at the higher IRQ threshold;

Extra points for a good legend to the plot.
Beispiel #5
0
"""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)









*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")
fobj.close()

"""
Plot all these on the same plot:

* The volume standard deviation values;
* The outlier points from the std values, marked on the plot with an 'o'
  marker;
* A horizontal dashed line at the lower IRQ threshold;
* A horizontal dashed line at the higher IRQ threshold;

Extra points for a good legend to the plot.
Beispiel #7
0
"""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)
    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))
    plt.xlabel('time')
    plt.legend(fontsize=11, \
    ncol=2, loc=9, borderaxespad=0.2)
    plt.savefig(project_path+'fig/outliers/%s_vol_std.png' %str(name))
    plt.close()
Beispiel #9
0
    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))
    plt.xlabel('time')
    plt.legend(fontsize=11, \
    ncol=2, loc=9, borderaxespad=0.2)
    plt.savefig(project_path + 'fig/outliers/%s_vol_std.png' % str(name))
    plt.close()
Beispiel #10
0
#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
for i in run_img_result.keys():