예제 #1
0
def soma3d(sm, new_fig=True, new_axes=True, subplot=False, **kwargs):
    '''Generates a 3d figure of the soma.

    Parameters
    ----------
    soma: Soma
        neurom.Soma object
    '''
    treecolor = kwargs.get('treecolor', None)

    # Initialization of matplotlib figure and axes.
    fig, ax = _cm.get_figure(new_fig=new_fig,
                             new_axes=new_axes,
                             subplot=subplot,
                             params={'projection': '3d'})

    # Definition of the tree color depending on the tree type.
    treecolor = _cm.get_color(treecolor, tree_type='soma')

    center = sm.get_center()

    xs = center[0]
    ys = center[1]
    zs = center[2]

    # Plot the soma as a circle.
    fig, ax = _cm.plot_sphere(fig,
                              ax,
                              center=[xs, ys, zs],
                              radius=sm.get_diameter(),
                              color=treecolor,
                              alpha=_get_default('alpha', **kwargs))

    kwargs['title'] = kwargs.get('title', 'Soma view')
    kwargs['xlabel'] = kwargs.get('xlabel', 'X')
    kwargs['ylabel'] = kwargs.get('ylabel', 'Y')
    kwargs['zlabel'] = kwargs.get('zlabel', 'Z')

    return _cm.plot_style(fig=fig, ax=ax, **kwargs)
예제 #2
0
파일: plot.py 프로젝트: masht18/TMD
def barcode(ph,
            new_fig=True,
            subplot=False,
            color='b',
            linewidth=1.2,
            **kwargs):
    """
    Generates a 2d figure (barcode) of the persistent homology
    of a tree as it has been computed by
    Topology.get_persistent_homology method.
    """
    # Initialization of matplotlib figure and axes.
    fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot)
    ph_sort = analysis.sort_ph(ph)

    for ip, p in enumerate(ph_sort):
        ax.plot(p[:2], [ip, ip], c=color, linewidth=linewidth)

    kwargs['title'] = kwargs.get('title', 'Persistence barcode')
    kwargs['xlabel'] = kwargs.get('xlabel',
                                  'Lifetime: radial distance from soma')

    _cm.plt.ylim([-1, len(ph_sort)])
    return _cm.plot_style(fig=fig, ax=ax, **kwargs)
예제 #3
0
파일: view.py 프로젝트: vsukhor/TMD
def tree(tr,
         plane='xy',
         new_fig=True,
         subplot=False,
         hadd=0.0,
         vadd=0.0,
         **kwargs):
    '''Generates a 2d figure of the tree.

    Parameters
    ----------
    tr: Tree
        neurom.Tree object
    '''
    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 = _cm.get_figure(new_fig=new_fig, subplot=subplot)

    # Data needed for the viewer: x,y,z,r
    bounding_box = tr.get_bounding_box()

    def _seg_2d(seg, x_add=0.0, y_add=0.0):
        """2d coordinates required for the plotting of a segment"""

        horz = _utils.term_dict[plane[0]]
        vert = _utils.term_dict[plane[1]]

        horz1 = seg[0][horz] + x_add
        horz2 = seg[1][horz] + x_add
        vert1 = seg[0][vert] + y_add
        vert2 = seg[1][vert] + y_add

        return ((horz1, vert1), (horz2, vert2))

    segs = [_seg_2d(seg, hadd, vadd) for seg in tr.get_segments()]

    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)
        linewidth = [d * scale for d in tr.d]

    if tr.get_type() not in _utils.tree_type:
        treecolor = 'black'
    else:
        treecolor = _cm.get_color(_get_default('treecolor', **kwargs),
                                  _utils.tree_type[tr.get_type()])

    # Plot the collection of lines.
    collection = _LC(segs,
                     color=treecolor,
                     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])

    white_space = _get_default('white_space', **kwargs)

    kwargs['xlim'] = kwargs.get('xlim', [
        bounding_box[0][_utils.term_dict[plane[0]]] - white_space,
        bounding_box[1][_utils.term_dict[plane[0]]] + white_space
    ])
    kwargs['ylim'] = kwargs.get('ylim', [
        bounding_box[0][_utils.term_dict[plane[1]]] - white_space,
        bounding_box[1][_utils.term_dict[plane[1]]] + white_space
    ])

    return _cm.plot_style(fig=fig, ax=ax, **kwargs)
예제 #4
0
파일: view.py 프로젝트: vsukhor/TMD
def density_cloud(obj,
                  new_fig=True,
                  subplot=111,
                  new_axes=True,
                  neurite_type='all',
                  bins=100,
                  plane='xy',
                  color_map=blues_map,
                  alpha=0.8,
                  centered=True,
                  colorbar=True,
                  plot_neuron=False,
                  **kwargs):
    """
    View the neuron morphologies of a population as a density cloud.
    """
    x1 = []
    y1 = []

    if neurite_type == 'all':
        ntypes = 'neurites'
    else:
        ntypes = neurite_type

    fig, ax = _cm.get_figure(new_fig=new_fig,
                             new_axes=new_axes,
                             subplot=subplot)

    for neu in obj.neurons:
        for tr in getattr(neu, ntypes):
            if centered:
                dx = neu.soma.get_center()[0]
                dy = neu.soma.get_center()[1]
            else:
                dx = 0
                dy = 0
            x1 = x1 + list(getattr(tr, plane[0]) - dx)
            y1 = y1 + list(getattr(tr, plane[1]) - dy)

    H1, xedges1, yedges1 = _np.histogram2d(x1, y1, bins=(bins, bins))
    mask = H1 < 0.05
    H2 = _np.ma.masked_array(H1, mask)
    color_map.set_bad(color='white', alpha=None)

    plots = ax.contourf((xedges1[:-1] + xedges1[1:]) / 2,
                        (yedges1[:-1] + yedges1[1:]) / 2,
                        _np.transpose(H2),
                        cmap=color_map,
                        alhpa=alpha)

    kwargs['new_fig'] = False
    kwargs['subplot'] = subplot

    if plot_neuron:
        nrn = obj.neurons[0]
        h, v, _ = nrn.soma.get_center()
        soma(nrn.soma, plane='xy', hadd=-h, vadd=-v, **kwargs)
        for temp_tree in getattr(nrn, ntypes):
            tree(temp_tree,
                 plane='xy',
                 hadd=-h,
                 vadd=-v,
                 treecolor='r',
                 **kwargs)

    if colorbar:
        _cm.plt.colorbar(plots)
    # soma(neu.soma, new_fig=False)
    return _cm.plot_style(fig=fig, ax=ax, **kwargs)
예제 #5
0
파일: view.py 프로젝트: vsukhor/TMD
def neuron3d(nrn,
             new_fig=True,
             new_axes=True,
             subplot=False,
             neurite_type='all',
             **kwargs):
    '''Generates a figure of the neuron,
    that contains a soma and a list of trees.

    Parameters
    ----------
    neuron: Neuron
        neurom.Neuron object
    '''
    # Initialization of matplotlib figure and axes.
    fig, ax = _cm.get_figure(new_fig=new_fig,
                             new_axes=new_axes,
                             subplot=subplot,
                             params={'projection': '3d'})

    kwargs['new_fig'] = False
    kwargs['new_axes'] = False
    kwargs['subplot'] = subplot

    soma3d(nrn.soma, **kwargs)

    h = []
    v = []
    d = []

    to_plot = []

    if neurite_type == 'all':
        to_plot = nrn.neurites
    else:
        for neu_type in neurite_type:
            to_plot = to_plot + getattr(nrn, neu_type)

    for temp_tree in to_plot:

        bounding_box = temp_tree.get_bounding_box()

        h.append([
            bounding_box[0][_utils.term_dict['x']],
            bounding_box[1][_utils.term_dict['x']]
        ])
        v.append([
            bounding_box[0][_utils.term_dict['y']],
            bounding_box[1][_utils.term_dict['y']]
        ])
        d.append([
            bounding_box[0][_utils.term_dict['z']],
            bounding_box[1][_utils.term_dict['z']]
        ])

        tree3d(temp_tree, **kwargs)

    kwargs['title'] = kwargs.get('title', nrn.name)
    white_space = _get_default('white_space', **kwargs)
    kwargs['xlim'] = kwargs.get(
        'xlim', [_np.min(h) - white_space,
                 _np.max(h) + white_space])
    kwargs['ylim'] = kwargs.get(
        'ylim', [_np.min(v) - white_space,
                 _np.max(v) + white_space])
    kwargs['zlim'] = kwargs.get(
        'zlim', [_np.min(d) - white_space,
                 _np.max(d) + white_space])

    return _cm.plot_style(fig=fig, ax=ax, **kwargs)
예제 #6
0
파일: view.py 프로젝트: vsukhor/TMD
def trunk3d(tr, new_fig=True, new_axes=True, subplot=False, N=10, **kwargs):
    '''Generates a figure of the trunk in 3d.

    Parameters
    ----------
    tr: Tree
        neurom.Tree object
    '''
    # pylint: disable=import-outside-toplevel
    from mpl_toolkits.mplot3d.art3d import Line3DCollection

    # Initialization of matplotlib figure and axes.
    fig, ax = _cm.get_figure(new_fig=new_fig,
                             new_axes=new_axes,
                             subplot=subplot,
                             params={'projection': '3d'})

    def _seg_3d(seg):
        """3d coordinates needed for the plotting of a segment"""

        horz = _utils.term_dict['x']
        vert = _utils.term_dict['y']
        depth = _utils.term_dict['z']

        horz1 = seg[0][horz]
        horz2 = seg[1][horz]
        vert1 = seg[0][vert]
        vert2 = seg[1][vert]
        depth1 = seg[0][depth]
        depth2 = seg[1][depth]

        return ((horz1, vert1, depth1), (horz2, vert2, depth2))

    if len(tr.get_segments()) < N:
        N = len(tr.get_segments())

    segs = [_seg_3d(seg) for seg in tr.get_segments()[:N]]

    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)
        linewidth = [d * scale for d in tr.d]

    treecolor = _cm.get_color(_get_default('treecolor', **kwargs),
                              _utils.tree_type[tr.get_type()])

    # Plot the collection of lines.
    collection = Line3DCollection(segs,
                                  color=treecolor,
                                  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')

    return _cm.plot_style(fig=fig, ax=ax, **kwargs)
예제 #7
0
파일: view.py 프로젝트: vsukhor/TMD
def population(pop,
               plane='xy',
               new_fig=True,
               subplot=False,
               hadd=0.0,
               vadd=0.0,
               neurite_type='all',
               **kwargs):
    '''Generates a 2d figure of the population,
    that contains a soma and a list of trees.

    Parameters
    ----------
    pop: Population
        neurom.Population object

    Options
    -------
    plane: str
        Accepted values: Any pair of of xyz
        Default value is 'xy'

    linewidth: float
        Defines the linewidth of the tree and soma
        of the neuron, if diameter is set to False.
        Default value is 1.2.

    alpha: float
        Defines the transparency of the neuron.
        0.0 transparent through 1.0 opaque.
        Default value is 0.8.

    treecolor: str or None
        Defines the color of the trees.
        If None the default values will be used,
        depending on the type of tree:
        Soma: "black"
        Basal dendrite: "red"
        Axon : "blue"
        Apical dendrite: "purple"
        Undefined tree: "black"
        Default value is None.

    new_fig: boolean
        Defines if the neuron 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.

    Returns
    --------
    A 3D 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 = _cm.get_figure(new_fig=new_fig, subplot=subplot)

    kwargs['new_fig'] = False
    kwargs['subplot'] = subplot

    h = []
    v = []

    for nrn in pop.neurons:

        soma(nrn.soma, plane=plane, hadd=hadd, vadd=vadd, **kwargs)

        if neurite_type == 'all':
            neurite_list = ['basal', 'apical', 'axon']
        else:
            neurite_list = [neurite_type]

        for nt in neurite_list:
            for temp_tree in getattr(nrn, nt):

                bounding_box = temp_tree.get_bounding_box()

                h.append([
                    bounding_box[0][_utils.term_dict[plane[0]]] + hadd,
                    bounding_box[1][_utils.term_dict[plane[1]]] + vadd
                ])
                v.append([
                    bounding_box[0][_utils.term_dict[plane[0]]] + hadd,
                    bounding_box[1][_utils.term_dict[plane[1]]] + vadd
                ])

                tree(temp_tree, plane=plane, hadd=hadd, vadd=vadd, **kwargs)

    kwargs['title'] = kwargs.get('title', 'Neuron view')
    kwargs['xlabel'] = kwargs.get('xlabel', plane[0])
    kwargs['ylabel'] = kwargs.get('ylabel', plane[1])
    kwargs['xlim'] = kwargs.get('xlim', [_np.min(h), _np.max(h)])
    kwargs['ylim'] = kwargs.get('ylim', [_np.min(v), _np.max(v)])

    return _cm.plot_style(fig=fig, ax=ax, **kwargs)
예제 #8
0
파일: view.py 프로젝트: vsukhor/TMD
def neuron(nrn,
           plane='xy',
           new_fig=True,
           subplot=False,
           hadd=0.0,
           vadd=0.0,
           neurite_type='all',
           rotation=None,
           nosoma=False,
           new_axes=True,
           **kwargs):
    '''Generates a 2d figure of the neuron,
    that contains a soma and a list of trees.

    Parameters
    ----------
    neuron: Neuron
        neurom.Neuron object

    Options
    -------
    plane: str
        Accepted values: Any pair of of xyz
        Default value is 'xy'

    linewidth: float
        Defines the linewidth of the tree and soma
        of the neuron, if diameter is set to False.
        Default value is 1.2.

    alpha: float
        Defines the transparency of the neuron.
        0.0 transparent through 1.0 opaque.
        Default value is 0.8.

    treecolor: str or None
        Defines the color of the trees.
        If None the default values will be used,
        depending on the type of tree:
        Soma: "black"
        Basal dendrite: "red"
        Axon : "blue"
        Apical dendrite: "purple"
        Undefined tree: "black"
        Default value is None.

    new_fig: boolean
        Defines if the neuron 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.

    Returns
    --------
    A 3D 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 = _cm.get_figure(new_fig=new_fig,
                             subplot=subplot,
                             new_axes=new_axes)

    kwargs['new_fig'] = False
    kwargs['subplot'] = subplot

    if not nosoma:
        soma(nrn.soma, plane=plane, hadd=hadd, vadd=vadd, **kwargs)

    h = []
    v = []

    to_plot = []

    if rotation == 'apical':
        angle = _np.arctan2(nrn.apical[0].get_pca()[0],
                            nrn.apical[0].get_pca()[1])
        angle = _np.arctan2(rotation[1], rotation[0])

    if neurite_type == 'all':
        to_plot = nrn.neurites
    else:
        for neu_type in neurite_type:
            to_plot = to_plot + getattr(nrn, neu_type)

    for temp_tree in to_plot:
        if rotation is not None:
            temp_tree.rotate_xy(angle)

        bounding_box = temp_tree.get_bounding_box()

        h.append([
            bounding_box[0][_utils.term_dict[plane[0]]],
            bounding_box[1][_utils.term_dict[plane[0]]]
        ])
        v.append([
            bounding_box[0][_utils.term_dict[plane[1]]],
            bounding_box[1][_utils.term_dict[plane[1]]]
        ])

        tree(temp_tree, hadd=hadd, vadd=vadd, **kwargs)

    kwargs['title'] = kwargs.get('title', nrn.name)
    kwargs['xlabel'] = kwargs.get('xlabel', plane[0])
    kwargs['ylabel'] = kwargs.get('ylabel', plane[1])

    white_space = _get_default('white_space', **kwargs)
    kwargs['xlim'] = kwargs.get(
        'xlim',
        [_np.min(h) - white_space + hadd,
         _np.max(h) + white_space + hadd])
    kwargs['ylim'] = kwargs.get(
        'ylim',
        [_np.min(v) - white_space + vadd,
         _np.max(v) + white_space + vadd])

    return _cm.plot_style(fig=fig, ax=ax, **kwargs)