Beispiel #1
0
def plot_tree(ax, tree, plane='xy',
              diameter_scale=_DIAMETER_SCALE, linewidth=_LINEWIDTH,
              color=None, alpha=_ALPHA, realistic_diameters=False):
    """Plots a 2d figure of the tree's segments.

    Args:
        ax(matplotlib axes): on what to plot
        tree(neurom.core.Tree or neurom.core.Neurite): plotted tree
        plane(str): Any pair of 'xyz'
        diameter_scale(float): Scale factor multiplied with segment diameters before plotting
        linewidth(float): all segments are plotted with this width, but only if diameter_scale=None
        color(str or None): Color of plotted values, None corresponds to default choice
        alpha(float): Transparency of plotted values
        realistic_diameters(bool): scale linewidths with axis data coordinates

    Note:
        If the tree contains one single point the plot will be empty
        since no segments can be constructed.
    """
    plane0, plane1 = _plane2col(plane)

    section_segment_list = [(section, segment)
                            for section in iter_sections(tree)
                            for segment in iter_segments(section)]
    colors = [_get_color(color, section.type) for section, _ in section_segment_list]

    if realistic_diameters:
        def _get_rectangle(x, y, linewidth):
            """Draw  a rectangle to represent a secgment."""
            x, y = np.array(x), np.array(y)
            diff = y - x
            angle = np.arctan2(diff[1], diff[0]) % (2 * np.pi)
            return Rectangle(x - linewidth / 2. * np.array([-np.sin(angle), np.cos(angle)]),
                             np.linalg.norm(diff),
                             linewidth,
                             np.rad2deg(angle))

        segs = [_get_rectangle((seg[0][plane0], seg[0][plane1]),
                               (seg[1][plane0], seg[1][plane1]),
                               2 * segment_radius(seg) * diameter_scale)
                for _, seg in section_segment_list]

        collection = PatchCollection(segs, alpha=alpha, facecolors=colors)

    else:
        segs = [((seg[0][plane0], seg[0][plane1]),
                 (seg[1][plane0], seg[1][plane1]))
                for _, seg in section_segment_list]

        linewidth = _get_linewidth(
            tree,
            diameter_scale=diameter_scale,
            linewidth=linewidth,
        )
        collection = LineCollection(segs, colors=colors, linewidth=linewidth, alpha=alpha)

    ax.add_collection(collection)
Beispiel #2
0
def _get_linewidth(tree, linewidth, diameter_scale):
    '''calculate the desired linewidth based on tree contents

    If diameter_scale exists, it is used to scale the diameter of each of the segments
    in the tree
    If diameter_scale is None, the linewidth is used.
    '''
    if diameter_scale is not None and tree:
        linewidth = [2 * segment_radius(s) * diameter_scale
                     for s in iter_segments(tree)]
    return linewidth
Beispiel #3
0
def test_segment_radius():
    assert mm.segment_radius(((0, 0, 0, 4), (0, 0, 0, 6))) == 5
Beispiel #4
0
def test_segment_radius():
    nt.ok_(mm.segment_radius(((0,0,0,4),(0,0,0,6))) == 5)
Beispiel #5
0
    # p[COLS.R] yields the radius for point p.
    # Note: this includes duplicated points at beginning of
    # non-trunk sections
    print('Mean radius of points:',
          np.mean([s.points[:, COLS.R] for s in nm.iter_sections(nrn)]))

    # get mean radius of neurite points in cell.
    # p[COLS.R] yields the radius for point p.
    # Note: this includes duplicated points at beginning of
    # non-trunk sections
    pts = [p[COLS.R] for s in nrn.sections[1:] for p in s.points]
    print('Mean radius of points:', np.mean(pts))

    # get mean radius of segments
    print('Mean radius of segments:',
          np.mean(list(mm.segment_radius(s) for s in nm.iter_segments(nrn))))

    # get stats for the segment taper rate, for different types of neurite
    for ttype in NEURITES:
        ttt = ttype
        seg_taper_rate = [
            mm.segment_taper_rate(s)
            for s in nm.iter_segments(nrn,
                                      neurite_filter=tree_type_checker(ttt))
        ]

        print('Segment taper rate (',
              ttype,
              '):\n  mean=',
              np.mean(seg_taper_rate),
              ', std=',