def t_grouping_neighbor(t_3d, mask, cutoff, neighbors=None, prop=False, abs_on=False, binary=True, off_value=0, masked_value=.5): """ Masks a 3d array, does t_binary_grouping, and does neighboring Parameters ---------- t_3d: t-value of the betas 3d numpy array mask: a 3d numpy array of 0s and 1s that has the same shape as t_3d cutoff: the limit for the false discovery rate neighbors: number of neighbors for neighbor smoothing (must have binary be true) prop: logical~ if the cutoff is a proportion or a value abs_on: logical~ if we want to take absolute value of the t input binary: if binary, then off_value is ignored and 0 is used as the off_value, 1 as the on value off_value: the value of those not selected Returns ------- output_3d: a 3d numpy array same size as the t_3d with either: (1) binary on_off values for inside the mask and "masked_value" for values outside mask or (2) t values to the accepted values, and "off_values" for lost values, and "masked_value" for values outside mask. MOREOVER, it can have had neighbor smoothing applied the binary case cutoff: the limit for the false discovery rate """ if neighbors != None and binary == False: return False t_1d = masking_reshape_start(t_3d, mask) t_1d = np.ravel(t_1d) zero_one, cutoff = t_binary_grouping(t_1d, cutoff, prop, abs_on) if not binary: t_1d = t_1d * zero_one + off_value * (1 - zero_one) else: t_1d = zero_one output_3d = masking_reshape_end(t_1d, mask, masked_value) if neighbors != None: output_3d = neighbor_smoothing(output_3d, neighbors) return output_3d, cutoff
def test_1(): # just checks masking_reshape_start # 3d happy=np.arange(27) happy=happy.reshape((3,3,3)) mask=np.zeros((3,3,3)) mask[:2,1:,2:]=1 joy=masking_reshape_start(happy,mask) assert(np.all(joy == np.ravel(happy[:2,1:,2:]))) # 4d happy4=np.arange(27*4) happy4=happy4.reshape((3,3,3,4)) mask4=np.zeros((3,3,3)) mask4[:2,1:,2:]=1 joy4=masking_reshape_start(happy4,mask4) assert(np.all(np.ravel(joy4) == np.ravel(happy4[:2,1:,2:,:])))
def t_grouping_neighbor(t_3d, mask, cutoff, neighbors = None, prop = False, abs_on = False, binary = True, off_value = 0, masked_value = .5): """ Masks a 3d array, does t_binary_grouping, and does neighboring Parameters ---------- t_3d: t-value of the betas 3d numpy array mask: a 3d numpy array of 0s and 1s that has the same shape as t_3d cutoff: the limit for the false discovery rate neighbors: number of neighbors for neighbor smoothing (must have binary be true) prop: logical~ if the cutoff is a proportion or a value abs_on: logical~ if we want to take absolute value of the t input binary: if binary, then off_value is ignored and 0 is used as the off_value, 1 as the on value off_value: the value of those not selected Returns ------- output_3d: a 3d numpy array same size as the t_3d with either: (1) binary on_off values for inside the mask and "masked_value" for values outside mask or (2) t values to the accepted values, and "off_values" for lost values, and "masked_value" for values outside mask. MOREOVER, it can have had neighbor smoothing applied the binary case cutoff: the limit for the false discovery rate """ if neighbors != None and binary == False: return False t_1d = masking_reshape_start(t_3d, mask) t_1d = np.ravel(t_1d) zero_one, cutoff = t_binary_grouping(t_1d, cutoff, prop, abs_on) if not binary: t_1d = t_1d*zero_one + off_value*(1 - zero_one) else: t_1d = zero_one output_3d = masking_reshape_end(t_1d, mask, masked_value) if neighbors != None: output_3d = neighbor_smoothing(output_3d, neighbors) return output_3d, cutoff
def test_2(): # checks BOTH masking_reshape_start and masking_reshape_end # 3d happy=np.arange(27) happy=happy.reshape((3,3,3)) mask=np.zeros((3,3,3)) mask[:2,1:,2:]=1 joy=masking_reshape_start(happy,mask) output=masking_reshape_end(joy,mask,off_value=0) test=np.zeros((3,3,3)) test[:2,1:,2:]=happy[:2,1:,2:] assert(np.all(test==output))
n_vols = num_TR hrf_matrix_all = np.loadtxt("../data/hrf/" + i + "_hrf_all.txt") hrf_matrix_1 = np.loadtxt("../data/hrf/" + i + "_hrf_1.txt") hrf_matrix_2 = np.loadtxt("../data/hrf/" + i + "_hrf_2.txt") hrf_matrix_3 = np.loadtxt("../data/hrf/" + i + "_hrf_3.txt") mask = nib.load(path_to_data + i + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() mask_data = make_mask(np.ones(data.shape[:-1]), mask_data, fit=True) mask_data = mask_data != 0 mask_data = mask_data.astype(int) # PCA to_2d = masking_reshape_start(data, mask_data) X_pca = to_2d - np.mean(to_2d, 0) - np.mean(to_2d, 1)[:, None] cov = X_pca.T.dot(X_pca) U, S, V = npl.svd(cov) pca_addition = U[:, :6] # ~40% coverage #START DOING GLM for j in range(data.shape[2]): data_slice = data[:, :, j, :] mask_slice = mask_data[:, :, j] data_slice = data_slice.reshape((-1, int(num_TR))) mask_slice = np.ravel(mask_slice)
hrf_matrix_all = np.loadtxt("../data/hrf/" + i + "_hrf_all.txt") hrf_matrix_1 = np.loadtxt("../data/hrf/" + i + "_hrf_1.txt") hrf_matrix_2 = np.loadtxt("../data/hrf/" + i + "_hrf_2.txt") hrf_matrix_3 = np.loadtxt("../data/hrf/" + i + "_hrf_3.txt") mask = nib.load(path_to_data + i + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() mask_data = make_mask(np.ones(data.shape[:-1]), mask_data, fit = True) mask_data = mask_data != 0 mask_data = mask_data.astype(int) # PCA to_2d = masking_reshape_start(data, mask_data) X_pca = to_2d - np.mean(to_2d,0) - np.mean(to_2d,1)[:, None] cov = X_pca.T.dot(X_pca) U, S, V = npl.svd(cov) pca_addition = U[:, :6] # ~40% coverage #START DOING GLM for j in range(data.shape[2]): data_slice = data[:,:,j,:] mask_slice = mask_data[:,:,j]
print("# ==== BEGIN mask_phase_2_dimension_change plotting (LOL) ==== #") Q = .45 mask = nib.load(pathtodata + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() rachels_ones = np.ones(data.shape[:-1]) fitted_mask = make_mask(rachels_ones, mask_data, fit=True) fitted_mask[fitted_mask > 0] = 1 mask_new = mask_data[::2, ::2, :] assert (mask_new.shape == fitted_mask.shape) p_vals_3d = p_vals.reshape(data.shape[:-1]) # call masking_reshape_start function, reshaped to 1d output to_1d = masking_reshape_start(p_vals_3d, mask_new) # call bh_procedure where Q = .45 bh_1d = bh_procedure(to_1d, Q) # call masking_reshape_end function, to reshape back into 3d shape to_3d = 2 * masking_reshape_end(bh_1d, mask_new, .5) - 1 # plot this plt.imshow(present_3d(to_3d), interpolation='nearest', cmap='seismic') plt.title(str(Q) + " with masked p-values") plt.savefig(location_of_images + "masked_pval_" + str(Q) + ".png") plt.figure() plt.close() print("# ==== END mask_phase_2_dimension_change plotting ==== #")
print("# ==== BEGIN mask_phase_2_dimension_change plotting (LOL) ==== #") Q = .45 mask = nib.load(pathtodata + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() #print(data.shape) rachels_ones = np.ones(data.shape[:-1]) fitted_mask = make_mask(rachels_ones, mask_data, fit = True) fitted_mask[fitted_mask > 0] = 1 mask_new = mask_data[::2, ::2, :] assert(mask_new.shape == fitted_mask.shape) p_vals_3d = p_vals.reshape(data.shape[:-1]) # call masking_reshape_start function, reshaped to 1d output to_1d = masking_reshape_start(p_vals_3d, mask_new) # call bh_procedure where Q = .45 bh_1d=bh_procedure(to_1d, Q) # call masking_reshape_end function, to reshape back into 3d shape to_3d = 2*masking_reshape_end(bh_1d, mask_new, .5) - 1 # plot this plt.imshow(present_3d(to_3d),interpolation='nearest', cmap='seismic') plt.title(str(Q) + " with masked p-values") #plt.colorbar() plt.savefig(location_of_images + "masked_pval_" + str(Q) + ".png") plt.figure() plt.close() print("# ==== END mask_phase_2_dimension_change plotting ==== #")