def test_segment_iteration(): nt.assert_equal(list(val_iter(isegment(REF_TREE))), [(0, 11),(11, 111),(11, 112), (0, 12),(12, 121),(121,1211), (1211,12111),(1211,12112),(12, 122)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[0]))), [(0, 11), (11, 111),(11, 112)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[0].children[0]))), [(11, 111)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[0].children[1]))), [(11, 112)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[1]))), [(0, 12), (12, 121), (121, 1211), (1211, 12111), (1211, 12112), (12, 122)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[1].children[0]))), [(12, 121), (121, 1211), (1211, 12111), (1211, 12112)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[1].children[1]))), [(12, 122)])
def test_segment_iteration(): nt.assert_equal(list(val_iter(isegment(REF_TREE))), [(0, 11), (11, 111), (11, 112), (0, 12), (12, 121), (121, 1211), (1211, 12111), (1211, 12112), (12, 122)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[0]))), [(0, 11), (11, 111), (11, 112)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[0].children[0]))), [(11, 111)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[0].children[1]))), [(11, 112)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[1]))), [(0, 12), (12, 121), (121, 1211), (1211, 12111), (1211, 12112), (12, 122)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[1].children[0]))), [(12, 121), (121, 1211), (1211, 12111), (1211, 12112)]) nt.assert_equal(list(val_iter(isegment(REF_TREE.children[1].children[1]))), [(12, 122)])
def calculate_and_plot_end_to_end_distance(path): '''Calculate and plot the end-to-end distance vs the number of segments for an increasingly larger part of a given path. Note that the plots are not very meaningful for bifurcating trees.''' end_to_end_distance = [morphmath.point_dist(segment[1], path.value) for segment in tree.val_iter(tree.isegment(path))] make_end_to_end_distance_plot(np.arange(len(end_to_end_distance)) + 1, end_to_end_distance, path.type)
def neurite_centre_of_mass(neurite): '''Calculate and return centre of mass of a neurite.''' centre_of_mass = np.zeros(3) total_volume = 0 for segment in tree.val_iter(tree.isegment(neurite)): seg_volume = morphmath.segment_volume(segment) centre_of_mass = centre_of_mass + seg_volume * segment_centre_of_mass(segment) total_volume += seg_volume return centre_of_mass / total_volume
def radius_of_gyration(neurite): '''Calculate and return radius of gyration of a given neurite.''' centre_mass = neurite_centre_of_mass(neurite) sum_sqr_distance = 0 N = 0 for segment in tree.val_iter(tree.isegment(neurite)): sum_sqr_distance = sum_sqr_distance + distance_sqr(centre_mass, segment) N += 1 return np.sqrt(sum_sqr_distance / N)
def neurite_centre_of_mass(neurite): '''Calculate and return centre of mass of a neurite.''' centre_of_mass = np.zeros(3) total_volume = 0 for segment in tree.val_iter(tree.isegment(neurite)): seg_volume = morphmath.segment_volume(segment) centre_of_mass = centre_of_mass + seg_volume * segment_centre_of_mass( segment) total_volume += seg_volume return centre_of_mass / total_volume
def radius_of_gyration(neurite): '''Calculate and return radius of gyration of a given neurite.''' centre_mass = neurite_centre_of_mass(neurite) sum_sqr_distance = 0 N = 0 for segment in tree.val_iter(tree.isegment(neurite)): sum_sqr_distance = sum_sqr_distance + distance_sqr( centre_mass, segment) N += 1 return np.sqrt(sum_sqr_distance / N)
def nonzero_segment_lengths(neuron, threshold=0.0): '''Check presence of neuron segments with length not above threshold Arguments: neuron: Neuron object whose segments will be tested threshold: value above which a segment length is considered to be non-zero Return: list of (first_id, second_id) of zero length segments ''' l = [[s for s in val_iter(isegment(t)) if segment_length(s) <= threshold] for t in neuron.neurites] return [(i[0][COLS.ID], i[1][COLS.ID]) for i in chain(*l)]
def test_segment_upstream_iteration(): leaves = [l for l in ileaf(REF_TREE2)] ref_paths = [[(1111, 11111), (111, 1111), (11, 111), (0, 11)], [(1111, 11112), (111, 1111), (11, 111), (0, 11)], [(1111, 11113), (111, 1111), (11, 111), (0, 11)], [(11, 112), (0, 11)], [(1211, 12111), (121, 1211), (12, 121), (0, 12)], [(1211, 12112), (121, 1211), (12, 121), (0, 12)], [(12, 122), (0, 12)]] for l, ref in zip(leaves, ref_paths): nt.assert_equal([s for s in val_iter(isegment(l, iupstream))], ref)
def _check_trees(trees): for t in trees: nt.ok_(len(list(tree.ileaf(t))) == 11) nt.ok_(len(list(tree.iforking_point(t))) == 10) nt.ok_(len(list(tree.ipreorder(t))) == 211) nt.ok_(len(list(tree.ipostorder(t))) == 211) nt.ok_(len(list(tree.isegment(t))) == 210) leaves = [l for l in tree.ileaf(t)] # path length from each leaf to root node. branch_order = [21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 111] for i, l in enumerate(leaves): nt.ok_(len(list(tree.iupstream(l))) == branch_order[i])
def i_segment_radial_dist(pos, tree): '''Return an iterator of radial distances of tree segments to a given point The radial distance is the euclidian distance between the mid-point of the segment and the point in question. Parameters: pos: origin to which disrances are measured. It must have at least 3 components. The first 3 components are (x, y, z). tree: tree of raw data rows. ''' return tr.imap_val(lambda s: mm.segment_radial_dist(s, pos), tr.isegment(tree))
def test_segment_upstream_iteration(): leaves = [l for l in ileaf(REF_TREE2)] ref_paths = [ [(1111, 11111), (111, 1111), (11, 111), (0, 11)], [(1111, 11112), (111, 1111), (11, 111), (0, 11)], [(1111, 11113), (111, 1111), (11, 111), (0, 11)], [(11, 112), (0, 11)], [(1211, 12111), (121, 1211), (12, 121), (0, 12)], [(1211, 12112), (121, 1211), (12, 121), (0, 12)], [(12, 122), (0, 12)] ] for l, ref in zip(leaves, ref_paths): nt.assert_equal([s for s in val_iter(isegment(l, iupstream))], ref)
def tree3d(tr, new_fig=True, new_axes=True, subplot=False, **kwargs): '''Generates a figure of the tree in 3d. Parameters: tr: Tree neurom.Tree object Options: linewidth: float \ Defines the linewidth of the tree, \ if diameter is set to False. \ Default value is 1.2. alpha: float \ Defines throughe transparency of the tree. \ 0.0 transparent through 1.0 opaque. \ Default value is 0.8. treecolor: str or None \ Defines the color of the tree. \ If None the default values will be used, \ depending on the type of tree: \ Basal dendrite: "red" \ Axon : "blue" \ Apical dendrite: "purple" \ Undefined tree: "black" \ Default value is None. new_fig: boolean \ Defines if the tree will be plotted \ in the current figure (False) \ or in a new figure (True) \ Default value is True. subplot: matplotlib subplot value or False \ If False the default subplot 111 will be used. \ For any other value a matplotlib subplot \ will be generated. \ Default value is False. diameter: boolean If True the diameter, scaled with diameter_scale factor, \ will define the width of the tree lines. \ If False use linewidth to select the width of the tree lines. \ Default value is True. diameter_scale: float \ Defines the scale factor that will be multiplied \ with the diameter to define the width of the tree line. \ Default value is 1. white_space: float \ Defines the white space around \ the boundary box of the morphology. \ Default value is 1. Returns: A 3D matplotlib figure with a tree view. ''' from mpl_toolkits.mplot3d.art3d import Line3DCollection # Initialization of matplotlib figure and axes. fig, ax = common.get_figure(new_fig=new_fig, new_axes=new_axes, subplot=subplot, params={'projection': '3d'}) # Data needed for the viewer: x,y,z,r bounding_box = get_bounding_box(tr) def _seg_3d(seg): '''2d coordinates needed for the plotting of a segment''' horz = getattr(COLS, 'X') vert = getattr(COLS, 'Y') depth = getattr(COLS, 'Z') parent_point = seg[0] child_point = seg[1] horz1 = parent_point[horz] horz2 = child_point[horz] vert1 = parent_point[vert] vert2 = child_point[vert] depth1 = parent_point[depth] depth2 = child_point[depth] return ((horz1, vert1, depth1), (horz2, vert2, depth2)) segs = [_seg_3d(seg) for seg in val_iter(isegment(tr))] linewidth = get_default('linewidth', **kwargs) # Definition of the linewidth according to diameter, if diameter is True. if get_default('diameter', **kwargs): # TODO: This was originally a numpy array. Did it have to be one? linewidth = [2 * d * get_default('diameter_scale', **kwargs) for d in i_segment_radius(tr)] # Plot the collection of lines. collection = Line3DCollection(segs, color=common.get_color(get_default('treecolor', **kwargs), get_tree_type(tr)), linewidth=linewidth, alpha=get_default('alpha', **kwargs)) ax.add_collection3d(collection) kwargs['title'] = kwargs.get('title', 'Tree 3d-view') kwargs['xlabel'] = kwargs.get('xlabel', 'X') kwargs['ylabel'] = kwargs.get('ylabel', 'Y') kwargs['zlabel'] = kwargs.get('zlabel', 'Z') kwargs['xlim'] = kwargs.get('xlim', [bounding_box[0][0] - get_default('white_space', **kwargs), bounding_box[1][0] + get_default('white_space', **kwargs)]) kwargs['ylim'] = kwargs.get('ylim', [bounding_box[0][1] - get_default('white_space', **kwargs), bounding_box[1][1] + get_default('white_space', **kwargs)]) kwargs['zlim'] = kwargs.get('zlim', [bounding_box[0][2] - get_default('white_space', **kwargs), bounding_box[1][2] + get_default('white_space', **kwargs)]) return common.plot_style(fig=fig, ax=ax, **kwargs)
def i_segment_area(tree): ''' return an iterator of tree segment areas ''' return tr.imap_val(mm.segment_area, tr.isegment(tree))
def i_segment_volume(tree): ''' return an iterator of tree segment volumes ''' return tr.imap_val(mm.segment_volume, tr.isegment(tree))
def i_segment_radius(tree): ''' return an iterator of tree segment radii ''' return tr.imap_val(mm.segment_radius, tr.isegment(tree))
def tree3d(tr, new_fig=True, new_axes=True, subplot=False, **kwargs): """Generates a figure of the tree in 3d. Parameters: tr: Tree neurom.Tree object Options: linewidth: float \ Defines the linewidth of the tree, \ if diameter is set to False. \ Default value is 1.2. alpha: float \ Defines throughe transparency of the tree. \ 0.0 transparent through 1.0 opaque. \ Default value is 0.8. treecolor: str or None \ Defines the color of the tree. \ If None the default values will be used, \ depending on the type of tree: \ Basal dendrite: "red" \ Axon : "blue" \ Apical dendrite: "purple" \ Undefined tree: "black" \ Default value is None. new_fig: boolean \ Defines if the tree will be plotted \ in the current figure (False) \ or in a new figure (True) \ Default value is True. subplot: matplotlib subplot value or False \ If False the default subplot 111 will be used. \ For any other value a matplotlib subplot \ will be generated. \ Default value is False. diameter: boolean If True the diameter, scaled with diameter_scale factor, \ will define the width of the tree lines. \ If False use linewidth to select the width of the tree lines. \ Default value is True. diameter_scale: float \ Defines the scale factor that will be multiplied \ with the diameter to define the width of the tree line. \ Default value is 1. white_space: float \ Defines the white space around \ the boundary box of the morphology. \ Default value is 1. Returns: A 3D matplotlib figure with a tree view. """ from mpl_toolkits.mplot3d.art3d import Line3DCollection # Initialization of matplotlib figure and axes. fig, ax = common.get_figure(new_fig=new_fig, new_axes=new_axes, subplot=subplot, params={"projection": "3d"}) # Data needed for the viewer: x,y,z,r bounding_box = get_bounding_box(tr) def _seg_3d(seg): """2d coordinates needed for the plotting of a segment""" horz = getattr(COLS, "X") vert = getattr(COLS, "Y") depth = getattr(COLS, "Z") parent_point = seg[0] child_point = seg[1] horz1 = parent_point[horz] horz2 = child_point[horz] vert1 = parent_point[vert] vert2 = child_point[vert] depth1 = parent_point[depth] depth2 = child_point[depth] return ((horz1, vert1, depth1), (horz2, vert2, depth2)) segs = [_seg_3d(seg) for seg in val_iter(isegment(tr))] linewidth = get_default("linewidth", **kwargs) # Definition of the linewidth according to diameter, if diameter is True. if get_default("diameter", **kwargs): # TODO: This was originally a numpy array. Did it have to be one? linewidth = [2 * d * get_default("diameter_scale", **kwargs) for d in i_segment_radius(tr)] # Plot the collection of lines. collection = Line3DCollection( segs, color=common.get_color(get_default("treecolor", **kwargs), get_tree_type(tr)), linewidth=linewidth, alpha=get_default("alpha", **kwargs), ) ax.add_collection3d(collection) kwargs["title"] = kwargs.get("title", "Tree 3d-view") kwargs["xlabel"] = kwargs.get("xlabel", "X") kwargs["ylabel"] = kwargs.get("ylabel", "Y") kwargs["zlabel"] = kwargs.get("zlabel", "Z") kwargs["xlim"] = kwargs.get( "xlim", [ bounding_box[0][0] - get_default("white_space", **kwargs), bounding_box[1][0] + get_default("white_space", **kwargs), ], ) kwargs["ylim"] = kwargs.get( "ylim", [ bounding_box[0][1] - get_default("white_space", **kwargs), bounding_box[1][1] + get_default("white_space", **kwargs), ], ) kwargs["zlim"] = kwargs.get( "zlim", [ bounding_box[0][2] - get_default("white_space", **kwargs), bounding_box[1][2] + get_default("white_space", **kwargs), ], ) return common.plot_style(fig=fig, ax=ax, **kwargs)
def n_segments(tree): """ Return number of segments in tree """ return sum(1 for _ in tr.isegment(tree))
def i_segment_length(tree): ''' return an iterator of tree segment lengths ''' return tr.imap_val(mm.segment_length, tr.isegment(tree))
def tree3d(tr, new_fig=True, new_axes=True, subplot=False, **kwargs): ''' Generates a figure of the tree in 3d. If the tree contains one single point the plot will be empty \ since no segments can be constructed. Parameters: tr: Tree \ neurom.Tree object linewidth: float \ Defines the linewidth of the tree, \ if diameter is set to False. \ Default value is 1.2. alpha: float \ Defines throughe transparency of the tree. \ 0.0 transparent through 1.0 opaque. \ Default value is 0.8. treecolor: str or None \ Defines the color of the tree. \ If None the default values will be used, \ depending on the type of tree: \ Basal dendrite: "red" \ Axon : "blue" \ Apical dendrite: "purple" \ Undefined tree: "black" \ Default value is None. new_fig: boolean \ Defines if the tree will be plotted \ in the current figure (False) \ or in a new figure (True) \ Default value is True. subplot: matplotlib subplot value or False \ If False the default subplot 111 will be used. \ For any other value a matplotlib subplot \ will be generated. \ Default value is False. diameter: boolean \ If True the diameter, scaled with diameter_scale factor, \ will define the width of the tree lines. \ If False use linewidth to select the width of the tree lines. \ Default value is True. diameter_scale: float \ Defines the scale factor that will be multiplied \ with the diameter to define the width of the tree line. \ Default value is 1. white_space: float \ Defines the white space around \ the boundary box of the morphology. \ Default value is 1. Returns: A 3D matplotlib figure with a tree view. ''' from mpl_toolkits.mplot3d.art3d import Line3DCollection # Initialization of matplotlib figure and axes. fig, ax = common.get_figure(new_fig=new_fig, new_axes=new_axes, subplot=subplot, params={'projection': '3d'}) # Data needed for the viewer: x,y,z,r bounding_box = get_bounding_box(tr) def _seg_3d(seg): '''2d coordinates needed for the plotting of a segment''' horz = getattr(COLS, 'X') vert = getattr(COLS, 'Y') depth = getattr(COLS, 'Z') parent_point = seg[0] child_point = seg[1] horz1 = parent_point[horz] horz2 = child_point[horz] vert1 = parent_point[vert] vert2 = child_point[vert] depth1 = parent_point[depth] depth2 = child_point[depth] return ((horz1, vert1, depth1), (horz2, vert2, depth2)) segs = [_seg_3d(seg) for seg in val_iter(isegment(tr))] linewidth = get_default('linewidth', **kwargs) # Definition of the linewidth according to diameter, if diameter is True. if get_default('diameter', **kwargs): # TODO: This was originally a numpy array. Did it have to be one? linewidth = [2 * d * get_default('diameter_scale', **kwargs) for d in iter_neurites(tr, segment_radius)] if len(linewidth) == 0: linewidth = get_default('linewidth', **kwargs) # Plot the collection of lines. collection = Line3DCollection(segs, color=common.get_color(get_default('treecolor', **kwargs), get_tree_type(tr)), linewidth=linewidth, alpha=get_default('alpha', **kwargs)) ax.add_collection3d(collection) kwargs['title'] = kwargs.get('title', 'Tree 3d-view') kwargs['xlabel'] = kwargs.get('xlabel', 'X') kwargs['ylabel'] = kwargs.get('ylabel', 'Y') kwargs['zlabel'] = kwargs.get('zlabel', 'Z') kwargs['xlim'] = kwargs.get('xlim', [bounding_box[0][0] - get_default('white_space', **kwargs), bounding_box[1][0] + get_default('white_space', **kwargs)]) kwargs['ylim'] = kwargs.get('ylim', [bounding_box[0][1] - get_default('white_space', **kwargs), bounding_box[1][1] + get_default('white_space', **kwargs)]) kwargs['zlim'] = kwargs.get('zlim', [bounding_box[0][2] - get_default('white_space', **kwargs), bounding_box[1][2] + get_default('white_space', **kwargs)]) return common.plot_style(fig=fig, ax=ax, **kwargs)
def tree(tr, plane='xy', new_fig=True, subplot=False, **kwargs): ''' Generates a 2d figure of the tree's segments. \ If the tree contains one single point the plot will be empty \ since no segments can be constructed. Parameters: tr: Tree \ neurom.Tree object plane: str \ Accepted values: Any pair of of xyz \ Default value is 'xy'.treecolor linewidth: float \ Defines the linewidth of the tree, \ if diameter is set to False. \ Default value is 1.2. alpha: float \ Defines throughe transparency of the tree. \ 0.0 transparent through 1.0 opaque. \ Default value is 0.8. treecolor: str or None \ Defines the color of the tree. \ If None the default values will be used, \ depending on the type of tree: \ Basal dendrite: "red" \ Axon : "blue" \ Apical dendrite: "purple" \ Undefined tree: "black" \ Default value is None. new_fig: boolean \ Defines if the tree will be plotted \ in the current figure (False) \ or in a new figure (True) \ Default value is True. subplot: matplotlib subplot value or False \ If False the default subplot 111 will be used. \ For any other value a matplotlib subplot \ will be generated. \ Default value is False. diameter: boolean If True the diameter, scaled with diameter_scale factor, \ will define the width of the tree lines. \ If False use linewidth to select the width of the tree lines. \ Default value is True. diameter_scale: float \ Defines the scale factor that will be multiplied \ with the diameter to define the width of the tree line. \ Default value is 1. limits: list or boolean \ List of type: [[xmin, ymin, zmin], [xmax, ymax, zmax]] \ If False the figure will not be scaled. \ If True the figure will be scaled according to tree limits. \ Default value is False. white_space: float \ Defines the white space around \ the boundary box of the morphology. \ Default value is 1. Returns: A 2D matplotlib figure with a tree view, at the selected plane. ''' if plane not in ('xy', 'yx', 'xz', 'zx', 'yz', 'zy'): return None, 'No such plane found! Please select one of: xy, xz, yx, yz, zx, zy.' # Initialization of matplotlib figure and axes. fig, ax = common.get_figure(new_fig=new_fig, subplot=subplot) # Data needed for the viewer: x,y,z,r bounding_box = get_bounding_box(tr) def _seg_2d(seg): '''2d coordinates needed for the plotting of a segment''' horz = getattr(COLS, plane[0].capitalize()) vert = getattr(COLS, plane[1].capitalize()) parent_point = seg[0] child_point = seg[1] horz1 = parent_point[horz] horz2 = child_point[horz] vert1 = parent_point[vert] vert2 = child_point[vert] return ((horz1, vert1), (horz2, vert2)) segs = [_seg_2d(seg) for seg in val_iter(isegment(tr))] linewidth = get_default('linewidth', **kwargs) # Definition of the linewidth according to diameter, if diameter is True. if get_default('diameter', **kwargs): scale = get_default('diameter_scale', **kwargs) # TODO: This was originally a numpy array. Did it have to be one? linewidth = [2 * d * scale for d in iter_neurites(tr, segment_radius)] if len(linewidth) == 0: linewidth = get_default('linewidth', **kwargs) # Plot the collection of lines. collection = LineCollection(segs, color=common.get_color(get_default('treecolor', **kwargs), get_tree_type(tr)), linewidth=linewidth, alpha=get_default('alpha', **kwargs)) ax.add_collection(collection) kwargs['title'] = kwargs.get('title', 'Tree view') kwargs['xlabel'] = kwargs.get('xlabel', plane[0]) kwargs['ylabel'] = kwargs.get('ylabel', plane[1]) kwargs['xlim'] = kwargs.get('xlim', [bounding_box[0][getattr(COLS, plane[0].capitalize())] - get_default('white_space', **kwargs), bounding_box[1][getattr(COLS, plane[0].capitalize())] + get_default('white_space', **kwargs)]) kwargs['ylim'] = kwargs.get('ylim', [bounding_box[0][getattr(COLS, plane[1].capitalize())] - get_default('white_space', **kwargs), bounding_box[1][getattr(COLS, plane[1].capitalize())] + get_default('white_space', **kwargs)]) return common.plot_style(fig=fig, ax=ax, **kwargs)
def path_length(tree): '''Get the path length from a sub-tree to the root node''' return np.sum(s for s in tr.imap_val(mm.segment_length, tr.isegment(tree, tr.iupstream)))
def segments(self): '''Returns segment iterator ''' return isegment(self._tree)
centre_mass = neurite_centre_of_mass(neurite) sum_sqr_distance = 0 N = 0 for segment in tree.val_iter(tree.isegment(neurite)): sum_sqr_distance = sum_sqr_distance + distance_sqr(centre_mass, segment) N += 1 return np.sqrt(sum_sqr_distance / N) def mean_rad_of_gyration(neurites): '''Calculate mean radius of gyration for set of neurites.''' return np.mean([radius_of_gyration(n) for n in neurites]) if __name__ == '__main__': # load a neuron from an SWC file filename = 'test_data/swc/Neuron.swc' nrn = ezy.load_neuron(filename) # for every neurite, print (number of segments, radius of gyration, neurite type) print([(sum(1 for _ in tree.isegment(nrte)), radius_of_gyration(nrte), nrte.type) for nrte in nrn.neurites]) # print mean radius of gyration per neurite type print('Mean radius of gyration for axons: ', mean_rad_of_gyration(n for n in nrn.neurites if n.type == ezy.TreeType.axon)) print('Mean radius of gyration for basal dendrites: ', mean_rad_of_gyration(n for n in nrn.neurites if n.type == ezy.TreeType.basal_dendrite)) print('Mean radius of gyration for apical dendrites: ', mean_rad_of_gyration(n for n in nrn.neurites if n.type == ezy.TreeType.apical_dendrite))
N += 1 return np.sqrt(sum_sqr_distance / N) def mean_rad_of_gyration(neurites): '''Calculate mean radius of gyration for set of neurites.''' return np.mean([radius_of_gyration(n) for n in neurites]) if __name__ == '__main__': # load a neuron from an SWC file filename = 'test_data/swc/Neuron.swc' nrn = ezy.load_neuron(filename) # for every neurite, print (number of segments, radius of gyration, neurite type) print([(sum(1 for _ in tree.isegment(nrte)), radius_of_gyration(nrte), nrte.type) for nrte in nrn.neurites]) # print mean radius of gyration per neurite type print( 'Mean radius of gyration for axons: ', mean_rad_of_gyration(n for n in nrn.neurites if n.type == ezy.TreeType.axon)) print( 'Mean radius of gyration for basal dendrites: ', mean_rad_of_gyration(n for n in nrn.neurites if n.type == ezy.TreeType.basal_dendrite)) print( 'Mean radius of gyration for apical dendrites: ', mean_rad_of_gyration(n for n in nrn.neurites if n.type == ezy.TreeType.apical_dendrite))
def tree(tr, plane='xy', new_fig=True, subplot=False, **kwargs): '''Generates a 2d figure of the tree. Parameters: tr: Tree \ neurom.Tree object Options: plane: str \ Accepted values: Any pair of of xyz \ Default value is 'xy'.treecolor linewidth: float \ Defines the linewidth of the tree, \ if diameter is set to False. \ Default value is 1.2. alpha: float \ Defines throughe transparency of the tree. \ 0.0 transparent through 1.0 opaque. \ Default value is 0.8. treecolor: str or None \ Defines the color of the tree. \ If None the default values will be used, \ depending on the type of tree: \ Basal dendrite: "red" \ Axon : "blue" \ Apical dendrite: "purple" \ Undefined tree: "black" \ Default value is None. new_fig: boolean \ Defines if the tree will be plotted \ in the current figure (False) \ or in a new figure (True) \ Default value is True. subplot: matplotlib subplot value or False \ If False the default subplot 111 will be used. \ For any other value a matplotlib subplot \ will be generated. \ Default value is False. diameter: boolean If True the diameter, scaled with diameter_scale factor, \ will define the width of the tree lines. \ If False use linewidth to select the width of the tree lines. \ Default value is True. diameter_scale: float \ Defines the scale factor that will be multiplied \ with the diameter to define the width of the tree line. \ Default value is 1. limits: list or boolean \ List of type: [[xmin, ymin, zmin], [xmax, ymax, zmax]] \ If False the figure will not be scaled. \ If True the figure will be scaled according to tree limits. \ Default value is False. white_space: float \ Defines the white space around \ the boundary box of the morphology. \ Default value is 1. Returns: A 2D matplotlib figure with a tree view, at the selected plane. ''' if plane not in ('xy', 'yx', 'xz', 'zx', 'yz', 'zy'): return None, 'No such plane found! Please select one of: xy, xz, yx, yz, zx, zy.' # Initialization of matplotlib figure and axes. fig, ax = common.get_figure(new_fig=new_fig, subplot=subplot) # Data needed for the viewer: x,y,z,r bounding_box = get_bounding_box(tr) def _seg_2d(seg): '''2d coordinates needed for the plotting of a segment''' horz = getattr(COLS, plane[0].capitalize()) vert = getattr(COLS, plane[1].capitalize()) parent_point = seg[0] child_point = seg[1] horz1 = parent_point[horz] horz2 = child_point[horz] vert1 = parent_point[vert] vert2 = child_point[vert] return ((horz1, vert1), (horz2, vert2)) segs = [_seg_2d(seg) for seg in val_iter(isegment(tr))] linewidth = get_default('linewidth', **kwargs) # Definition of the linewidth according to diameter, if diameter is True. if get_default('diameter', **kwargs): scale = get_default('diameter_scale', **kwargs) # TODO: This was originally a numpy array. Did it have to be one? linewidth = [2 * d * scale for d in i_segment_radius(tr)] # Plot the collection of lines. collection = LineCollection(segs, color=common.get_color(get_default('treecolor', **kwargs), get_tree_type(tr)), linewidth=linewidth, alpha=get_default('alpha', **kwargs)) ax.add_collection(collection) kwargs['title'] = kwargs.get('title', 'Tree view') kwargs['xlabel'] = kwargs.get('xlabel', plane[0]) kwargs['ylabel'] = kwargs.get('ylabel', plane[1]) kwargs['xlim'] = kwargs.get('xlim', [bounding_box[0][getattr(COLS, plane[0].capitalize())] - get_default('white_space', **kwargs), bounding_box[1][getattr(COLS, plane[0].capitalize())] + get_default('white_space', **kwargs)]) kwargs['ylim'] = kwargs.get('ylim', [bounding_box[0][getattr(COLS, plane[1].capitalize())] - get_default('white_space', **kwargs), bounding_box[1][getattr(COLS, plane[1].capitalize())] + get_default('white_space', **kwargs)]) return common.plot_style(fig=fig, ax=ax, **kwargs)
'''Calculate and plot the end-to-end distance vs the number of segments for an increasingly larger part of a given path. Note that the plots are not very meaningful for bifurcating trees.''' end_to_end_distance = [morphmath.point_dist(segment[1], path.value) for segment in tree.val_iter(tree.isegment(path))] make_end_to_end_distance_plot(np.arange(len(end_to_end_distance)) + 1, end_to_end_distance, path.type) if __name__ == '__main__': # load a neuron from an SWC file filename = 'test_data/swc/Neuron_3_random_walker_branches.swc' nrn = ezy.load_neuron(filename) # print mean end-to-end distance per neurite type print('Mean end-to-end distance for axons: ', mean_end_to_end_dist(n for n in nrn.neurites if n.type == ezy.TreeType.axon)) print('Mean end-to-end distance for basal dendrites: ', mean_end_to_end_dist(n for n in nrn.neurites if n.type == ezy.TreeType.basal_dendrite)) print('Mean end-to-end distance for apical dendrites: ', mean_end_to_end_dist(n for n in nrn.neurites if n.type == ezy.TreeType.apical_dendrite)) print 'End-to-end distance per neurite (nb segments, end-to-end distance, neurite type):' for nrte in nrn.neurites: # plot end-to-end distance for increasingly larger parts of neurite calculate_and_plot_end_to_end_distance(nrte) # print (number of segments, end-to-end distance, neurite type) print(sum(1 for _ in tree.isegment(nrte)), path_end_to_end_distance(nrte), nrte.type)