def merge_clusters(labels_a_weights, n, labels_b, k): """ The merge strategy is described in Section C: Voting Procedure in the paper Detecting Regions of Interest in fMRI, found in the link http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1006726 Parameters ---------- labels_a_weights : 3D np array: [labels_3d_index_x, labels_3d_index_y, labels_3d_index_z]. This is the weights of the primary volume. This is the outcome of merging n volumes of labels together n : no. of volumes already merged into labels_a labels_b : 3D np array: [labels_3d_index_x, labels_3d_index_y, labels_3d_index_z]. We merge this volume of labels into labels_a k : no. of clusters Returns ------- labels_3d : array has the same shape as img_data.shape. Each elem is the cluster label of the merged data. """ assert labels_a_weights.shape[1:] == labels_b.shape shape = labels_b.shape # separate all points to their respective clusters cluster_to_point_map_b = [set() for i in range(k)] for i in vol_index_iter(shape): cluster_to_point_map_b[labels_b[i]].add(i) # compute a k * k matrix A with A[i, j] indicating similarity between cluster i in a and cluster j in b inter_cluster_similarity = np.zeros((k, k)) - 1 for a_i, b_i in product(range(k), range(k)): if a_i != b_i: inter_cluster_similarity[(a_i, b_i)] = weighted_similarity( labels_a_weights[a_i], cluster_to_point_map_b[b_i]) # based on the inter-cluster-similarity matrix, decide cluster mapping, matching the most similar a-b pair of clusters first. cluster_map = {} while len(cluster_map) < k: a_index, b_index = np.unravel_index( np.argmax(inter_cluster_similarity), (k, k)) cluster_map[a_index] = b_index inter_cluster_similarity[a_index, :] = -float('inf') inter_cluster_similarity[:, b_index] = -float('inf') # update weights on the primary volume for i, j in cluster_map.items(): for index in vol_index_iter(shape): if labels_a_weights[i][index] == 0: continue labels_a_weights[i][index] += n / (n + 1) new_cluster_index = i if index in cluster_to_point_map_b[ j] else find_cluster(cluster_to_point_map_b, index) labels_a_weights[new_cluster_index][index] += 1 / (n + 1)
def merge_clusters(labels_a_weights, n, labels_b, k): """ The merge strategy is described in Section C: Voting Procedure in the paper Detecting Regions of Interest in fMRI, found in the link http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1006726 Parameters ---------- labels_a_weights : 3D np array: [labels_3d_index_x, labels_3d_index_y, labels_3d_index_z]. This is the weights of the primary volume. This is the outcome of merging n volumes of labels together n : no. of volumes already merged into labels_a labels_b : 3D np array: [labels_3d_index_x, labels_3d_index_y, labels_3d_index_z]. We merge this volume of labels into labels_a k : no. of clusters Returns ------- labels_3d : array has the same shape as img_data.shape. Each elem is the cluster label of the merged data. """ assert labels_a_weights.shape[1:] == labels_b.shape shape = labels_b.shape # separate all points to their respective clusters cluster_to_point_map_b = [set() for i in range(k)] for i in vol_index_iter(shape): cluster_to_point_map_b[labels_b[i]].add(i) # compute a k * k matrix A with A[i, j] indicating similarity between cluster i in a and cluster j in b inter_cluster_similarity = np.zeros((k, k)) - 1 for a_i, b_i in product(range(k), range(k)): if a_i != b_i: inter_cluster_similarity[(a_i, b_i)] = weighted_similarity(labels_a_weights[a_i], cluster_to_point_map_b[b_i]) # based on the inter-cluster-similarity matrix, decide cluster mapping, matching the most similar a-b pair of clusters first. cluster_map = {} while len(cluster_map) < k: a_index, b_index = np.unravel_index(np.argmax(inter_cluster_similarity), (k, k)) cluster_map[a_index] = b_index inter_cluster_similarity[a_index,:] = -float('inf') inter_cluster_similarity[:, b_index] = -float('inf') # update weights on the primary volume for i, j in cluster_map.items(): for index in vol_index_iter(shape): if labels_a_weights[i][index] == 0: continue labels_a_weights[i][index] += n / (n + 1) new_cluster_index = i if index in cluster_to_point_map_b[j] else find_cluster(cluster_to_point_map_b, index) labels_a_weights[new_cluster_index][index] += 1 / (n + 1)
def pad_boundary_per_image(data, brain_mask, pad_thickness): brain_points, non_brain_points = [], [] for i in vol_index_iter(data.shape): if brain_mask[i]: brain_points.append(i) else: non_brain_points.append(i) tree = cKDTree(np.array(brain_points)) non_brain_points = [i for i in vol_index_iter(data.shape) if (not brain_mask[i])] neighbors_list = tree.query_ball_point(non_brain_points, pad_thickness) for list_i, neighbors in enumerate(neighbors_list): i = non_brain_points[list_i] if len(neighbors) != 0: data[i] = np.mean([data[brain_points[j]] for j in neighbors])
def __init__(self, in_brain_mask, dist_from_center): points = [ i for i in vol_index_iter(in_brain_mask.shape) if in_brain_mask[i] ] self.tree = cKDTree(np.array(points)) self.points = points self.dist_from_center = dist_from_center
def weighted_similarity(cluster_a_weights, cluster_b): shape = cluster_a_weights.shape accu_weight = 0 for i in vol_index_iter(shape): if i in cluster_b: accu_weight += cluster_a_weights[i] return accu_weight / np.sum(cluster_a_weights)
def correlation_map(data, cond_filename): tr_times = np.arange(0, 30, TR) convolved = conv_main(data.shape[-1], cond_filename, TR) corrs = np.zeros((data.shape[:-1])) for i in general_utils.vol_index_iter(data.shape[:-1]): corrs[i] = np.corrcoef(data[i], convolved)[1,0] return corrs
def correlation_map(data, cond_filename): tr_times = np.arange(0, 30, TR) convolved = conv_main(data.shape[-1], cond_filename, TR) corrs = np.zeros((data.shape[:-1])) for i in general_utils.vol_index_iter(data.shape[:-1]): corrs[i] = np.corrcoef(data[i], convolved)[1, 0] return corrs
def correlation_map_without_convoluation(data, cond_filename): n_trs = data.shape[-1] + 5 time_course = events2neural_rounded(cond_filename, TR, n_trs) time_course=time_course[5:] correlations = np.zeros(data.shape[:-1]) for i in general_utils.vol_index_iter(data.shape[:-1]): vox_values = data[i] correlations[i] = np.corrcoef(time_course, vox_values)[1, 0]
def correlation_map_without_convoluation(data, cond_filename): n_trs = data.shape[-1] + 5 time_course = events2neural_rounded(cond_filename, TR, n_trs) time_course = time_course[5:] correlations = np.zeros(data.shape[:-1]) for i in general_utils.vol_index_iter(data.shape[:-1]): vox_values = data[i] correlations[i] = np.corrcoef(time_course, vox_values)[1, 0]
def plot_cluster_3d(labels_3d, cluster_index): Xs, Ys, Zs = [], [], [] for i, j, k in vol_index_iter(labels_3d.shape): if labels_3d[i, j, k] == cluster_index: Xs.append(i) Ys.append(j) Zs.append(k) fig = plt.figure() ax = Axes3D(fig) ax.scatter(Xs, Ys, Zs) ax.show()
def correlation_map(data, cond_filename): """ Generate a cross-correlation per voxel between BOLD signals and a convolved gamma baseline function. Assume that the first 5 images are already dropped. """ convolved = conv_main(data.shape[-1] + 5, cond_filename, TR)[5:] corrs = np.zeros((data.shape[:-1])) for i in gu.vol_index_iter(data.shape[:-1]): r = np.corrcoef(data[i], convolved)[1,0] if np.isnan(r): r = 0 corrs[i] = r return corrs
def correlation_map(data, cond_filename): """ Generate a cross-correlation per voxel between BOLD signals and a convolved gamma baseline function. Assume that the first 5 images are already dropped. """ convolved = conv_main(data.shape[-1] + 5, cond_filename, TR)[5:] corrs = np.zeros((data.shape[:-1])) for i in gu.vol_index_iter(data.shape[:-1]): r = np.corrcoef(data[i], convolved)[1, 0] if np.isnan(r): r = 0 corrs[i] = r return corrs
def correlation_map_without_convoluation(data, cond_filename): """ Generate a cross-correlation per voxel between BOLD signals and a square wave baseline function, which represents the boolean array of the on-off time course. Assume that the first 5 images are already dropped. """ n_trs = data.shape[-1] + 5 time_course = events2neural_rounded(cond_filename, TR, n_trs)[5:] correlations = np.zeros(data.shape[:-1]) for i in gu.vol_index_iter(data.shape[:-1]): vox_values = data[i] r = np.corrcoef(time_course, vox_values)[1, 0] if np.isnan(r): r = 0 correlations[i] = r return correlations
def __init__(self, in_brain_mask, dist_from_center): points = [i for i in vol_index_iter(in_brain_mask.shape) if in_brain_mask[i]] self.tree = cKDTree(np.array(points)) self.points = points self.dist_from_center = dist_from_center
def assign_points(labels_weights, shape): labels = np.zeros((shape)) for x, y, z in vol_index_iter(shape): labels[x, y, z] = np.argmax(labels_weights[:, x, y, z]) return labels
def form_initial_weights(labels, n_clusters): shape = labels.shape result_labels_weights = np.zeros((n_clusters, ) + shape) for i, j, k in vol_index_iter(shape): result_labels_weights[labels[i, j, k], i, j, k] = 1 return result_labels_weights
def form_initial_weights(labels, n_clusters): shape = labels.shape result_labels_weights = np.zeros((n_clusters,) + shape) for i, j, k in vol_index_iter(shape): result_labels_weights[labels[i, j, k], i, j, k] = 1 return result_labels_weights
def assign_points(labels_weights, shape): labels = np.zeros((shape)) for x,y,z in vol_index_iter(shape): labels[x,y,z] = np.argmax(labels_weights[:,x,y,z]) return labels