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") plt.close() # To test significance of betas: # Create contrast matrix for each beta: c_mat = np.diag(np.array(np.ones((dct_design_mat.shape[1],)))) # t statistics and p values # Length is the number of voxels after masking t_mat = np.ones((dct_design_mat.shape[1], y.shape[1])) p_mat = np.ones((dct_design_mat.shape[1], y.shape[1])) for i in range(0, dct_design_mat.shape[1], 1): t, p = linear_modeling.t_stat(X, c_mat[:, i], beta, MRSS, df) t_mat[i, :] = t p_mat[i, :] = p # Reshape t values t_val = np.zeros(vol_shape + (t_mat.shape[0],)) t_val[in_brain_mask, :] = t_mat.T # Reshape p values p_val = np.zeros(vol_shape + (p_mat.shape[0],)) p_val[in_brain_mask, :] = p_mat.T # Visualizing t values for the middle slice fig, axes = plt.subplots(nrows=2, ncols=3) for i, ax in zip(range(0, 7, 1), axes.flat): im = ax.imshow(t_val[:, :, 45, i], cmap="RdYlBu") fig.subplots_adjust(right=0.85)
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],)) t_vols[mask, :] = t_value.T p_vols = np.ones(vol_shape + (p_value.shape[0],)) p_vols[mask, :] = p_value.T #======================================================================================================== # P value for single voxel plt.figure(0) plt.plot(range(p_value.shape[1]), p_value[0,:]) plt.xlabel('voxel') plt.ylabel('P-value') plt.title('P-value for single voxel') line = plt.axhline(0.01, ls='--', color = 'red')
fig.colorbar(im, cax=cax) plt.savefig("../../../data/maps/dct_beta.png") plt.close() # To test significance of betas: # Create contrast matrix for each beta: c_mat = np.diag(np.array(np.ones((dct_design_mat.shape[1], )))) # t statistics and p values # Length is the number of voxels after masking t_mat = np.ones((dct_design_mat.shape[1], y.shape[1])) p_mat = np.ones(( dct_design_mat.shape[1], y.shape[1], )) for i in range(0, dct_design_mat.shape[1], 1): t, p = linear_modeling.t_stat(X, c_mat[:, i], beta, MRSS, df) t_mat[i, :] = t p_mat[i, :] = p # Reshape t values t_val = np.zeros(vol_shape + (t_mat.shape[0], )) t_val[in_brain_mask, :] = t_mat.T # Reshape p values p_val = np.zeros(vol_shape + (p_mat.shape[0], )) p_val[in_brain_mask, :] = p_mat.T # Visualizing t values for the middle slice fig, axes = plt.subplots(nrows=2, ncols=3) for i, ax in zip(range(0, 7, 1), axes.flat): im = ax.imshow(t_val[:, :, 45, i], cmap='RdYlBu') fig.subplots_adjust(right=0.85)