design[:, 3] = convo

# reshape data to 2D
vol_shape, n_time = data.shape[:-1], data.shape[-1]
# shape_2d = (n_time, np.product(vol_shape)) # (133, 902629)

# Smoothing raw data set
mean_data = np.mean(data, -1)
plt.figure(2)
plt.hist(np.ravel(mean_data), bins=100)
line = plt.axvline(8000, ls='--', color = 'red')
mask = mean_data > 8000
smooth_data = linear_modeling.smoothing(data, mask)

# Block linear regression
betas_hat, s2, df = linear_modeling.beta_est(smooth_data, design) #(4, 194287)
np.savetxt('../../../data/beta/' + f2 + '_betas_hat_block.txt', betas_hat.T, newline='\r\n')

# Filling back to raw data shape
beta_vols = np.zeros(vol_shape + (betas_hat.shape[0],)) #(91, 109, 91, 4)
beta_vols[mask] = betas_hat.T

# set regions outside mask as missing with np.nan
mean_data[~mask] = np.nan
beta_vols[~mask] = np.nan

# T-test on null hypothesis, assume only input variance of beta3 [0,0,0,1]
t_value, p_value = linear_modeling.t_stat(design, [0,1,1,1], betas_hat, s2, df) #(1, 194287) (1, 194287)

# Filling T, P value back to raw data shape (91, 109, 91)
t_vols = np.zeros(vol_shape + (t_value.shape[0],))
######### we take the mean volume (over time), and do a histogram of the values
mean_vol = np.mean(data, axis=-1)
# mask out the outer-brain noise using mean volumes over time.
in_brain_mask = mean_vol > 8000
# We can use this 3D mask to index into our 4D dataset.
# This selects all the voxel time-courses for voxels within the brain
# (as defined by the mask)
y = linear_modeling.smoothing(data, in_brain_mask)


######### Lastly, do t test on betas:
X = dct_design_mat
np.savetxt("../../../data/design_matrix/full_dct_design_mat.txt", X)

beta, errors, MRSS, df = linear_modeling.beta_est(y, X)
print("The mean MRSS across all voxels in mixed design is " + str(np.mean(MRSS)))
np.savetxt("../../../data/beta/" + f1 + "_betas_hat_full_dct.txt", beta, newline="\r\n")

# Visualizing betas for the middle slice
# First reshape
b_vols = np.zeros(vol_shape + (beta.shape[0],))
b_vols[in_brain_mask, :] = beta.T
# Then plot them on the same plot with uniform scale
fig, axes = plt.subplots(nrows=2, ncols=3)
for i, ax in zip(range(0, 7, 1), axes.flat):
    im = ax.imshow(b_vols[:, :, 45, i], cmap="gray")
fig.subplots_adjust(right=0.85)
cax = fig.add_axes([0.9, 0.15, 0.03, 0.7])
fig.colorbar(im, cax=cax)
plt.savefig("../../../data/maps/dct_beta.png")
Example #3
0
np.savetxt('../../../data/maps/design_mat_filter.txt', design_mat_filter)



#################### temporal filtering
#### Subtract Gaussian weighted running line fit to smooth
mean_vol = np.mean(data, axis=-1)
in_brain_mask = mean_vol > 5000

y = np.loadtxt('../../../data/maps/y.txt')
y_mat = np.zeros(y.shape)
for i in range(y.shape[1]):
    y_mat[:,i] = y[:,i] - gaussian_smooth(y[:,i], 15, 8)[:n_trs]
X = design_mat_filter

beta, MRSS, df = linear_modeling.beta_est(y_mat,X)

b_vols = np.zeros(vol_shape + (beta.shape[0],))
b_vols[in_brain_mask, :] = beta.T
# Then plot them on the same plot with uniform scale
fig, axes = plt.subplots(nrows=3, ncols=4)
for i, ax in zip(range(0,design_mat_filter.shape[1],1), axes.flat):
    im = ax.imshow(b_vols[:, :, 45, i], cmap = 'gray')
fig.subplots_adjust(right=0.85)
cax = fig.add_axes([0.9, 0.15, 0.03, 0.7])
fig.colorbar(im, cax=cax)
plt.show()


c_mat = np.diag(np.array(np.ones((design_mat_filter.shape[1],))))
# t statistics and p values
Example #4
0
############## we take the mean volume (over time)
mean_vol = np.mean(data, axis=-1)

# mask out the outer-brain noise using mean volumes over time.
in_brain_mask = mean_vol > 8000
# We can use this 3D mask to index into our 4D dataset.
# This selects all the voxel time-courses for voxels within the brain
# (as defined by the mask)
############## Spatially smoothing the raw data
y = linear_modeling.smoothing(data, in_brain_mask)

############## Lastly, do t test on betas:
X = design_mat

############## Get RSS from full model
_, _, MRSS, df = linear_modeling.beta_est(y, X)
RSS = MRSS * df

############## Test beta1 + beta4 + beta5 = 0 (block design)
index1 = np.array([0, 3, 4])
X_1 = np.delete(X, index1, axis=1)
_, _, MRSS1, df1 = linear_modeling.beta_est(y, X_1)
RSS1 = MRSS1 * df1

############## Test beta2 + beta3 = 0 (event related design)
index2 = np.array([1, 2])
X_2 = np.delete(X, index2, axis=1)
_, _, MRSS2, df2 = linear_modeling.beta_est(y, X_2)
RSS2 = MRSS2 * df2

############## Compare RSS
n_vol = np.product(vol_shape)
data = np.reshape(data,(n_trs, n_vol))

#---------------------------------------------------------------------------
#---------------------------------------------------------------------------
# Block Design using original data
start = np.loadtxt('../../../data/convo/' + f1 + '_conv001.txt')[4:]
end = np.loadtxt('../../../data/convo/' + f1 + '_conv004.txt')[4:]
convo = np.loadtxt('../../../data/convo/' + f1 + '_conv005.txt')[4:]
# Building design X matrix
design = np.ones((len(convo), 4))
design[:, 1] = start
design[:, 2] = end
design[:, 3] = convo
X = design
beta, errors, RSS, df = linear_modeling.beta_est(data, X)

pval = normal_assumption.sw(errors)

# smoothing
fmri_img = image.smooth_img('../../../data/sub001/BOLD/task001_run001/filtered_func_data_mni.nii.gz', fwhm=6)
mean_img = image.mean_img(fmri_img)
# Thresholding
p_val = np.ones(vol_shape + (pval.shape[1],))
p_val[in_brain_mask, :] = pval

log_p_values = -np.log10(p_val[..., 0])
log_p_values[np.isnan(log_p_values)] = 0.
log_p_values[log_p_values > 10.] = 10.
log_p_values[log_p_values < -np.log10(0.05/137)] = 0
plot_stat_map(nib.Nifti1Image(log_p_values, img.get_affine()),
Example #6
0
plt.close()

######### we take the mean volume (over time), and do a histogram of the values
mean_vol = np.mean(data, axis=-1)
# mask out the outer-brain noise using mean volumes over time.
in_brain_mask = mean_vol > 8000
# We can use this 3D mask to index into our 4D dataset.
# This selects all the voxel time-courses for voxels within the brain
# (as defined by the mask)
y = linear_modeling.smoothing(data, in_brain_mask)

######### Lastly, do t test on betas:
X = dct_design_mat
np.savetxt('../../../data/design_matrix/full_dct_design_mat.txt', X)

beta, errors, MRSS, df = linear_modeling.beta_est(y, X)
print('The mean MRSS across all voxels in mixed design is ' +
      str(np.mean(MRSS)))
np.savetxt('../../../data/beta/' + f1 + '_betas_hat_full_dct.txt',
           beta,
           newline='\r\n')

# Visualizing betas for the middle slice
# First reshape
b_vols = np.zeros(vol_shape + (beta.shape[0], ))
b_vols[in_brain_mask, :] = beta.T
# Then plot them on the same plot with uniform scale
fig, axes = plt.subplots(nrows=2, ncols=3)
for i, ax in zip(range(0, 7, 1), axes.flat):
    im = ax.imshow(b_vols[:, :, 45, i], cmap='gray')
fig.subplots_adjust(right=0.85)