def main_segments(swc_matirx): neuron = Neuron(swc_matirx) branch_order = tree_util.branch_order(neuron.parent_index) distance_from_parent = neuron.distance_from_parent() main, parent_main_point, neural, euclidean = \ neuron.get_neural_and_euclid_lenght_main_point(branch_order, distance_from_parent) branch_branch, branch_die, die_die, initial_with_branch = \ neuron.branching_type(main, parent_main_point) ind_main = nodes_laying_toward_soma( neuron.parent_index, np.array(np.append(branch_die, die_die))) main_seg = np.zeros(len(neuron.parent_index)) main_seg[ind_main] = 1 return main_seg.astype(int)
def cut_from_end_nodes(neuron, number): current_swc = rescale_neuron_in_unit_box(neuron) current_neuron = Neuron(input_file=current_swc, input_format="Matrix of swc") current_swc = McNeuron.swc_util.get_swc_matrix(current_neuron) for i in range(current_neuron.n_node - number): row = np.random.choice(np.where(current_neuron.branch_order == 0)[0]) current_swc = np.delete(current_swc, row, axis=0) higher_index = np.where(current_swc[:, 6] > row)[0] current_swc[higher_index, 6] = current_swc[higher_index, 6] - 1 current_neuron = Neuron(input_file=current_swc, input_format="Matrix of swc") current_swc = rescale_neuron_in_unit_box(current_neuron) current_neuron = Neuron(input_file=current_swc, input_format="Matrix of swc") return current_neuron
def generate_data(path, scale_depth, n_camrea, kappa): """ input ----- path : list list of all the pathes of swc. each element of the list should be a string. scale_depth : float in the interval [0,1] a value to differentiate between the background and gray level in the image. n_camera : int number of different angles to set the six images. For each angle, six images will be generated (up,down and four sides) kappa : float The width of the distribution that the angles come from. Large value for kappa results in the angles close to x aixs kappa = 1 is equvalent to the random angle. output ------ Data : list of length """ Data = [] for i in range(len(path)): print(path[i]) neuron = Neuron(file_format='swc without attributes', input_file=path[i]) if (len(neuron.nodes_list) != 0): for j in range(n_camrea): D = np.asarray( make_six_images(neuron, scale_depth, np.array([i, j]), kappa)) Data.append(D) return Data
def extract_main_neuron_from_swc(matrix, num=300): a, b = np.unique(matrix[:, 6], return_counts=True) (I, ) = np.where(b == 2) branch_point = a[I] end_point = np.setxor1d(np.arange(0, matrix.shape[0]), matrix[4:, 6]) I = np.union1d(branch_point, end_point) leng = matrix.shape[0] lengi = len(I) I = np.union1d(I, np.arange(0, n, int(n / (leng - lengi - 1)))) random_point = np.setxor1d(np.arange(3, matrix.shape[0]), I) I = np.append(I, random_point[-(num - len(I)):-1]) I = np.sort(I) I = np.array(I, dtype=int) I[0] = 0 K = matrix[:, 6] K = np.array(K, dtype=int) J = parent_id_for_extract2(K, I) J = J + 1 J[0] = -1 n = len(J) I = I - 1 I = I[1:] I = np.append(I, matrix.shape[0] - 1) new_matrix = np.zeros([n, 7]) new_matrix[:, 0] = np.arange(0, n) new_matrix[:, 1] = matrix[I, 1] new_matrix[:, 2] = matrix[I, 2] new_matrix[:, 3] = matrix[I, 3] new_matrix[:, 4] = matrix[I, 4] new_matrix[:, 5] = matrix[I, 5] new_matrix[:, 6] = J neuron = Neuron(input_format='Matrix of swc', input_file=new_matrix) return neuron
def neuron_with_selected_nodes(neuron, selected_index): """ Giving back a new neuron made up with the selected_index nodes of self. if node A is parent (or grand parent) of node B in the original neuron, it is the same for the new neuron. Parameters ---------- selected_index: numpy array the index of nodes from original neuron for making new neuron. Returns ------- Neuron: the subsampled neuron. """ parent = parent_id(neuron, selected_index) # making the list of nodes n_list = [] for i in range(selected_index.shape[0]): n = Node() n.xyz = neuron.nodes_list[selected_index[i]].xyz n.r = neuron.nodes_list[selected_index[i]].r n.type = neuron.nodes_list[selected_index[i]].type n_list.append(n) # adjusting the childern and parents for the nodes. for i in np.arange(1, selected_index.shape[0]): j = parent[i] n_list[i].parent = n_list[j] n_list[j].add_child(n_list[i]) return Neuron(file_format='only list of nodes', input_file=n_list)
def read_file(self, input_format, input_file): if (input_format == 'Matrix of swc'): self.index = np.array([]) index = -1 for neuron in input_file: try: index += 1 m = subsample.fast_straigthen_subsample_swc( neuron, self.len_subsampling) n = Neuron(input_file=m) n.set_features() self.index = np.append(self.index, index) self.database.append(n) print(index) except: print('ERROR IN:') print(index)
def prune_subsample(neuorn, number): main_point = subsample_main_nodes() Nodes = main_point.nodes_list rm = (main_point.n_node - number)/2. for remove in range(int(rm)): b, m = find_sharpest_fork(Nodes) remove_pair_adjust_parent(Nodes, b) return Neuron(file_format = 'only list of nodes', input_file = Nodes)
def check_neuron(neuron): """ Check the features of the neurons. Parameters: ----------- neuron: Neuron The neuron to be checked. Returns: warnings if some features are not correct. """ cor = 1 n = Neuron(input_file=get_swc_matrix(neuron), input_format='Matrix of swc without Node class') n.fit() list_features = neuron.features.keys() for f in range(len(list_features)): # if list_features[f] == 'curvature': # print(n.features[list_features[f]]) # print(neuron.features[list_features[f]]) # print(n.features[list_features[f]] - neuron.features[list_features[f]]) if len(n.features[list_features[f]]) - \ len(neuron.features[list_features[f]]) != 0: print("The size of feature " + list_features[f] + " is not calculated correctly.") cor = 0 else: a = n.features[list_features[f]] - neuron.features[ list_features[f]] if list_features[f] == 'branch_angle' or list_features[ f] == 'side_branch_angle': a = np.sort(n.features[list_features[f]], axis=0) - \ np.sort(neuron.features[list_features[f]], axis=0) a = a**2 # if list_features[f] == 'curvature': # print(n.features[list_features[f]]) # print(neuron.features[list_features[f]]) # print(a.sum()) if (a.sum() > 0.): print("The feature " + list_features[f] + " is not calculated correctly.") cor = 0
def topological_depth(swc_matirx): neuron = Neuron(swc_matirx) branch_order = tree_util.branch_order(neuron.parent_index) distance_from_parent = neuron.distance_from_parent() main, parent_main_point, neural, euclidean = \ neuron.get_neural_and_euclid_lenght_main_point(branch_order, distance_from_parent) reg_neuron = Neuron(subsample.regular_subsample(swc_matirx)) topo_depth = np.zeros(swc_matirx.shape[0]) depth_main = neuron_util.dendogram_depth(reg_neuron.parent_index) topo_depth[main] = depth_main for i in range(1, swc_matirx.shape[0]): b = True par = i while b: (index, ) = np.where(main == par) if len(index) != 0: topo_depth[i] = topo_depth[main[index]] b = False par = neuron.parent_index[par] return topo_depth
def make_swc_from_prufer_and_locations(data): # the prufer code and the location are given. parents_code = np.array(decode_prufer(list(data['morphology']))) location = data['geometry'] M = np.zeros([len(parents_code), 7]) M[:, 0] = np.arange(1, len(parents_code)+1) M[0, 1] = 1 M[1:, 1] = 2 M[1:, 2:5] = location parents_code[1:] = parents_code[1:] + 1 M[:, 6] = parents_code return Neuron(file_format='Matrix of swc', input_file=M)
def mesoscale_subsample(neuron, number): main_point = subsample_main_nodes(neuron) Nodes = main_point.nodes_list rm = (main_point.n_node - number) / 2. for remove in range(int(rm)): b, m = find_sharpest_fork(neuron, Nodes) remove_pair_adjust_parent(neuron, Nodes, b) neuron = Neuron(input_format='only list of nodes', input_file=Nodes) if (neuron.n_node > number): (I, ) = np.where(neuron.branch_order == 0) neuron = remove_node(neuron, I[0]) return neuron
def swc_to_neuron(matrix): """ Return the Neuron object from swc matrix. Parameters ---------- matrix: numpy array numpy array of the size n_nodes*7. Return ------ Neuron: Neuron a neuron obj with the given swc format. """ return Neuron(file_format='Matrix of swc', input_file=matrix)
def get_2d_image(path, size, dpi, background, show_width): neu = Neuron(file_format='swc without attributes', input_file=path) depth = neu.location[2, :] p = neu.location[0:2, :] widths = 5 * neu.diameter widths[0:3] = 0 m = min(depth) M = max(depth) depth = background * ((depth - m) / (M - m)) colors = [] lines = [] patches = [] for i in range(neu.n_soma): x1 = neu.location[0, i] y1 = neu.location[1, i] r = 1 * neu.diameter[i] circle = Circle((x1, y1), r, color=str(depth[i]), ec='none', fc='none') patches.append(circle) pa = PatchCollection(patches, cmap=matplotlib.cm.gray) pa.set_array(depth[0] * np.zeros(neu.n_soma)) for i in range(len(neu.nodes_list)): colors.append(str(depth[i])) j = neu.parent_index[i] lines.append([(p[0, i], p[1, i]), (p[0, j], p[1, j])]) if (show_width): lc = mc.LineCollection(lines, colors=colors, linewidths=widths) else: lc = mc.LineCollection(lines, colors=colors) fig, ax = plt.subplots() ax.add_collection(lc) ax.add_collection(pa) fig.set_size_inches([size + 1, size + 1]) fig.set_dpi(dpi) plt.axis('off') plt.xlim((min(p[0, :]), max(p[0, :]))) plt.ylim((min(p[1, :]), max(p[1, :]))) plt.draw() data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, )) border = (dpi / 2) return np.squeeze(data[border:-border, border:-border, 0])
def evolution_with_increasing_node(neuron): for i in range(10): m = neuron_util.get_swc_matrix(neuron) m = m[40 * i:, :] I = m[:, 6] I = I - 40 * i I[I < 0] = -1 m[:, 6] = I n = Neuron(input_file=m, input_format='Matrix of swc without Node class') lc = get_segment_collection(n) fig = plt.figure(figsize=(4, 4)) fig, ax = plt.subplots() ax.add_collection(lc) plt.axis('off') plt.xlim((-6, 6)) plt.ylim((-6, 6)) #plt.draw() plt.show()
def read_file(self, input_format, input_file): if(input_format == 'Matrix of swc'): self.index = np.array([]) index = -1 for neuron in input_file: try: index += 1 m = subsample.fast_straigthen_subsample_swc(neuron, self.len_subsampling) n = Neuron(input_format=input_format, input_file=m) n.fit() self.index = np.append(self.index, index) self.database.append(n) print index except: print('ERROR IN:') print index if(input_format == 'Matrix of swc without Node class'): self.index = np.array([]) index = -1 for neuron in input_file: try: index += 1 m = subsample.fast_straigthen_subsample_swc(neuron, self.len_subsampling) n = Neuron(input_format=input_format, input_file=m) self.index = np.append(self.index, index) self.database.append(n) print index except: print('ERROR IN:') print index if(input_format == 'swc'): for neuron in input_file: n = Neuron(input_format=input_format, input_file=neuron) purne_n, dis = subsample.straight_subsample_with_fixed_number(n, self.n_subsampling) self.database.append(purne_n)
def register_from_file(self, database): self.database = [] for i in range(self.n): n = Neuron(file_format = 'swc', input_file = database[i]) self.database.append(n)
def star_neuron(wing_number=3, node_on_each_wings=4, spherical=False, length=10): """ Make a star-wise neuron. The location of the root is origin. Parameters: wing_number: int The number of blades that the neuron should have node_on_each_wings: int The number of nodes on each warnings Return: ------- neuron: Neuron The desire neuron """ nodes_list = [] root = Node() root.r = 1. root.node_type = 1 root.xyz = np.array([0, 0, 0], dtype=float) nodes_list.append(root) for i in range(0): soma = Node() soma.r = .2 soma.node_type = 1 soma.xyz = np.array([0, 0, 0], dtype=float) nodes_list.append(soma) root.add_child(soma) soma.parent = root angle = 2 * np.pi / wing_number for j in range(wing_number): rand_vec = np.random.randn(3) rand_vec = rand_vec / np.sqrt(sum(rand_vec**2)) for i in range(node_on_each_wings): node = Node() node.r = .2 node.node_type = 2 if spherical: x = rand_vec[0] * length * (i + 1) y = rand_vec[1] * length * (i + 1) z = rand_vec[2] * length * (i + 1) else: x = np.sin(j * angle) * length * (i + 1) y = np.cos(j * angle) * length * (i + 1) z = 0. node.xyz = np.array([x, y, z], dtype=float) # +0*np.random.rand(3) if i == 0: root.add_child(node) node.parent = root nodes_list.append(node) else: nodes_list[-1:][0].add_child(node) node.parent = nodes_list[-1:][0] nodes_list.append(node) neuron = Neuron(input_format='only list of nodes', input_file=nodes_list) return neuron