Esempio n. 1
0
def test_normality():
    # Generate some 4-d random uniform data. 
    # The first 3 dimensions are like voxels, the last like time. 
    np.random.seed(159)
    sim_resids = np.random.rand(2, 2, 2, 200)
    # Force one of the time courses to be standard normal. 
    sim_resids[0,0,0] = np.random.randn(200)
    
    # Do Shaprio-Wilk and Kruskal-Wallis
    sw_3d = check_sw(sim_resids)
    kw_3d = check_kw(sim_resids)
    
    assert(sw_3d[0,0,0] > 0.05)
    assert(sw_3d[1,0,0] < 0.05)
    
    assert(kw_3d[0,0,0] > 0.05)
Esempio n. 2
0
def test_normality():
    # Generate some 4-d random uniform data.
    # The first 3 dimensions are like voxels, the last like time.
    np.random.seed(159)
    sim_resids = np.random.rand(2, 2, 2, 200)
    # Force one of the time courses to be standard normal.
    sim_resids[0, 0, 0] = np.random.randn(200)

    # Do Shaprio-Wilk and Kruskal-Wallis
    sw_3d = check_sw(sim_resids)
    kw_3d = check_kw(sim_resids)

    assert (sw_3d[0, 0, 0] > 0.05)
    assert (sw_3d[1, 0, 0] < 0.05)

    assert (kw_3d[0, 0, 0] > 0.05)
Esempio n. 3
0
def test_normality():
    # Generate some 4-d random uniform data. 
    # The first 3 dimensions are like voxels, the last like time. 
    np.random.seed(159)
    sim_resids = np.random.rand(2, 2, 2, 200)
    # Force one of the time courses to be standard normal. 
    sim_resids[0,0,0] = np.random.randn(200)
    
    # Do Shaprio-Wilk. 
    sw_3d = check_sw(sim_resids) # 4-d residuals, 3-d p-values
    sw_1d = check_sw_masked(sim_resids.reshape((-1, sim_resids.shape[-1]))) # 2-d residuals, 1-d p-values
    # Do Kruskal-Wallis.
    kw_3d = check_kw(sim_resids)
    
    assert(sw_3d[0,0,0] > 0.05)
    assert(sw_3d[1,0,0] < 0.05)

    # Two Shaprio-Wilk functions should do the same thing over arrays of different dimensions. 
    assert(sw_3d[0,0,0] == sw_1d[0])
    
    assert(kw_3d[0,0,0] > 0.05)
Esempio n. 4
0
def test_normality():
    # Generate some 4-d random uniform data.
    # The first 3 dimensions are like voxels, the last like time.
    np.random.seed(159)
    sim_resids = np.random.rand(2, 2, 2, 200)
    # Force one of the time courses to be standard normal.
    sim_resids[0, 0, 0] = np.random.randn(200)

    # Do Shaprio-Wilk.
    sw_3d = check_sw(sim_resids)  # 4-d residuals, 3-d p-values
    sw_1d = check_sw_masked(sim_resids.reshape(
        (-1, sim_resids.shape[-1])))  # 2-d residuals, 1-d p-values
    # Do Kruskal-Wallis.
    kw_3d = check_kw(sim_resids)

    assert (sw_3d[0, 0, 0] > 0.05)
    assert (sw_3d[1, 0, 0] < 0.05)

    # Two Shaprio-Wilk functions should do the same thing over arrays of different dimensions.
    assert (sw_3d[0, 0, 0] == sw_1d[0])

    assert (kw_3d[0, 0, 0] > 0.05)
# Load our normality functions. 
from normality import check_sw

# Load masking and visualization functions.
from Image_Visualizing import make_mask, present_3d

# Progress bar
toolbar_width=len(sub_list)
sys.stdout.write("GLM, :  ")
sys.stdout.write("[%s]" % (" " * toolbar_width))
sys.stdout.flush()
sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after '['

for i in sub_list:
    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')
Esempio n. 6
0
np.savetxt(condition_location + "cond_all.txt", cond_all)

neural_prediction = events2neural(condition_location + "cond_all.txt", TR,
                                  n_vols)
convolved = np.convolve(neural_prediction,
                        hrf_at_trs)  # hrf_at_trs sample data
N = len(neural_prediction)  # N == n_vols == 173
M = len(hrf_at_trs)  # M == 12
np_hrf = convolved[:N]

###################
# From GLM function
###################

np_B, np_X = glm(data, np_hrf)

####################################
# GLM Diagnostics (to get residuals)
###################################

np_MRSS, np_fitted, np_residuals = glm_diagnostics(np_B, np_X, data)

###########################
#Shapiro-Wilks on Residuals
###########################
#Shapiro-Wilks: tests the null hypothesis that the data was
#drawn from a normal distribution.

sw_pvals = check_sw(np_residuals)
print(np.mean(sw_pvals > 0.05))
# Progress bar
toolbar_width = len(sub_list)
sys.stdout.write("Shaprio-Wilk test for normality, :  ")
sys.stdout.write("[%s]" % (" " * toolbar_width))
sys.stdout.flush()
sys.stdout.write("\b" *
                 (toolbar_width + 1))  # return to start of line, after '['

# Set up lists to store proportion of p-values above 0.05.
unmasked_prop = []  # Unmasked (all voxels)
masked_prop = []  # Masked.

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([])

####################################
# GLM Diagnostics (to get residuals)
###################################

np_MRSS, np_fitted, np_residuals = glm_diagnostics(np_B, np_X, data)

###########################
#Shapiro-Wilks on Residuals
###########################
# Shapiro-Wilks: tests the null hypothesis that the data was 
# drawn from a normal distribution.

# Using 4-d residuals.
sw_pvals = check_sw(np_residuals)
print("Proportion of voxels with p-value above 0.05 (unmasked): "+str(np.mean(sw_pvals > 0.05)))


# Load mask.
mask = nib.load(pathtodata+'/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("Proportion of voxels with p-value above 0.05 (masked): "+str(np.mean(pvals_in_brain > 0.05)))

plt.imshow(present_3d(sw_pvals), cmap=plt.get_cmap('gray'))
plt.savefig(location_of_images+'sub001sw.png')
plt.close()
plt.imshow(present_3d(masked_pvals), cmap=plt.get_cmap('gray'))