def persistence_image_add(Z2, Z1, new_fig=True, subplot=111, xlims=None, ylims=None, norm=True, vmin=0, vmax=2., cmap=jet_map, **kwargs): """Takes as input two images as exported from the gaussian kernel plotting function, and plots their addition. """ if xlims is None or xlims is None: xlims, ylims = ((0, 100), (0, 100)) addition = analysis.get_image_add_data(Z1, Z2, norm=norm) fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot) ax.imshow(_np.rot90(addition), vmin=vmin, vmax=vmax, cmap=cmap, interpolation='bilinear', extent=xlims + ylims) kwargs['xlim'] = xlims kwargs['ylim'] = ylims return _cm.plot_style(fig=fig, ax=ax, **kwargs)
def diagram(ph, new_fig=True, subplot=False, color='b', alpha=1.0, edgecolors='black', s=30, **kwargs): """ Generates a 2d figure (ph diagram) of the persistent homology of a tree. """ # Initialization of matplotlib figure and axes. fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot) bounds_max = _np.max(_np.max(ph)) bounds_min = _np.min(_np.min(ph)) _cm.plt.plot([bounds_min, bounds_max], [bounds_min, bounds_max], c='black') ax.scatter(_np.array(ph)[:, 0], _np.array(ph)[:, 1], c=color, alpha=alpha, edgecolors=edgecolors, s=s) kwargs['title'] = kwargs.get('title', 'Persistence diagram') kwargs['xlabel'] = kwargs.get('xlabel', 'End radial distance from soma') kwargs['ylabel'] = kwargs.get('ylabel', 'Start radial distance from soma') return _cm.plot_style(fig=fig, ax=ax, **kwargs)
def all_trunks3d(nrn, new_fig=True, new_axes=True, subplot=False, neurite_type='all', N=10, **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) 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: trunk3d(temp_tree, N=N, **kwargs) kwargs['title'] = kwargs.get('title', nrn.name) kwargs['xlim'] = kwargs.get( 'xlim', [nrn.soma.get_center()[0] - 2. * N, nrn.soma.get_center()[0] + 2. * N]) kwargs['ylim'] = kwargs.get( 'ylim', [nrn.soma.get_center()[1] - 2. * N, nrn.soma.get_center()[1] + 2. * N]) kwargs['zlim'] = kwargs.get( 'zlim', [nrn.soma.get_center()[2] - 2. * N, nrn.soma.get_center()[2] + 2. * N]) return _cm.plot_style(fig=fig, ax=ax, **kwargs)
def all_trunks(nrn, plane='xy', new_fig=True, subplot=False, hadd=0.0, vadd=0.0, neurite_type='all', N=10, **kwargs): '''Generates a 2d figure of the neuron, that contains a soma and a list of trees. Parameters ---------- neuron: Neuron neurom.Neuron 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) kwargs['new_fig'] = False kwargs['subplot'] = subplot soma(nrn.soma, plane=plane, hadd=hadd, vadd=vadd, **kwargs) 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: trunk(temp_tree, N=N, **kwargs) kwargs['title'] = kwargs.get('title', nrn.name) kwargs['xlabel'] = kwargs.get('xlabel', plane[0]) kwargs['ylabel'] = kwargs.get('ylabel', plane[1]) kwargs['xlim'] = kwargs.get( 'xlim', [nrn.soma.get_center()[0] - 2. * N, nrn.soma.get_center()[0] + 2. * N]) kwargs['ylim'] = kwargs.get( 'ylim', [nrn.soma.get_center()[1] - 2. * N, nrn.soma.get_center()[1] + 2. * N]) return _cm.plot_style(fig=fig, ax=ax, **kwargs)
def population3d(pop, new_fig=True, new_axes=True, subplot=False, **kwargs): '''Generates a figure of the population, that contains the pop somata and a set of list of trees. Parameters ---------- pop: Population neurom.Population 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 h = [] v = [] d = [] for nrn in pop.neurons: # soma3d(nrn.soma, **kwargs) for temp_tree in nrn.neurites: 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', '') 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)
def soma(sm, plane='xy', new_fig=True, subplot=False, hadd=0.0, vadd=0.0, **kwargs): '''Generates a 2d figure of the soma. Parameters ---------- soma: Soma neurom.Soma object ''' treecolor = kwargs.get('treecolor', None) outline = kwargs.get('outline', True) 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) # Definition of the tree color depending on the tree type. treecolor = _cm.get_color(treecolor, tree_type='soma') # Plot the outline of the soma as a circle, is outline is selected. if not outline: soma_circle = _cm.plt.Circle(sm.get_center() + [hadd, vadd, 0.0], sm.get_diameter() / 2., color=treecolor, alpha=_get_default('alpha', **kwargs)) ax.add_artist(soma_circle) else: horz = getattr(sm, plane[0]) + hadd vert = getattr(sm, plane[1]) + vadd horz = _np.append( horz, horz[0]) + hadd # To close the loop for a soma viewer. vert = _np.append( vert, vert[0]) + vadd # To close the loop for a soma viewer. _cm.plt.plot(horz, vert, color=treecolor, alpha=_get_default('alpha', **kwargs), linewidth=_get_default('linewidth', **kwargs)) kwargs['title'] = kwargs.get('title', 'Soma view') kwargs['xlabel'] = kwargs.get('xlabel', plane[0]) kwargs['ylabel'] = kwargs.get('ylabel', plane[1]) return _cm.plot_style(fig=fig, ax=ax, **kwargs)
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, **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) if colorbar: _cm.plt.colorbar(plots) # soma(neu.soma, new_fig=False) return _cm.plot_style(fig=fig, ax=ax, **kwargs)
def diagram_enhanced(ph, new_fig=True, subplot=False, alpha=1.0, valID=2, cmap=jet_map, edgecolors='black', s=30, **kwargs): """ Generates a 2d figure (diagram) of the persistent homology of a tree enhanced by a parameter encodes in ph[valID] """ # Initialization of matplotlib figure and axes. fig, ax = cm.get_figure(new_fig=new_fig, subplot=subplot) val_max = np.max(ph, axis=0)[valID] # Hack for colorbar creation Z = [[0, 0], [0, 0]] levels = np.linspace(0.0, val_max, 200) CS3 = plt.contourf(Z, levels, cmap=cmap) def sort_ph_enhanced(ph, valID): """ Sorts barcode according to length. """ ph_sort = [p[:valID + 1] + [np.abs(p[0] - p[1])] for p in ph] ph_sort.sort(key=lambda x: x[valID + 1]) return ph_sort ph_sort = sort_ph_enhanced(ph, valID) bounds_max = np.max(np.max(ph_sort)) bounds_min = np.min(np.min(ph_sort)) plt.plot([bounds_min, bounds_max], [bounds_min, bounds_max], c='black') colors = [cmap(p[valID] / val_max) for p in ph_sort] ax.scatter(np.array(ph_sort)[:, 0], np.array(ph_sort)[:, 1], c=colors, alpha=alpha, edgecolors=edgecolors, s=s) kwargs['title'] = kwargs.get('title', 'Persistence diagram') kwargs['xlabel'] = kwargs.get('xlabel', 'End radial distance from soma') kwargs['ylabel'] = kwargs.get('ylabel', 'Start radial distance from soma') plt.ylim([-1, len(ph_sort)]) plt.colorbar(CS3) return cm.plot_style(fig=fig, ax=ax, **kwargs)
def persistence_image(ph, new_fig=True, subplot=111, xlims=None, ylims=None, masked=False, colorbar=False, norm_factor=None, threshold=0.01, vmin=None, vmax=None, cmap=jet_map, bw_method=None, **kwargs): '''Plots the gaussian kernel of the ph diagram that is given. ''' if xlims is None or xlims is None: xlims, ylims = analysis.get_limits(ph, coll=False) # pylint: disable=unexpected-keyword-arg Zn = analysis.get_persistence_image_data(ph, norm_factor=norm_factor, bw_method=bw_method, xlims=xlims, ylims=ylims) fig, ax = cm.get_figure(new_fig=new_fig, subplot=subplot) if masked: Zn = np.ma.masked_where((threshold > Zn), Zn) cax = ax.imshow(np.rot90(Zn), vmin=vmin, vmax=vmax, cmap=cmap, interpolation='bilinear', extent=xlims + ylims) if colorbar: plt.colorbar(cax) kwargs['xlim'] = xlims kwargs['ylim'] = ylims kwargs['title'] = kwargs.get('title', 'Persistence image') kwargs['xlabel'] = kwargs.get('xlabel', 'End radial distance from soma') kwargs['ylabel'] = kwargs.get('ylabel', 'Start radial distance from soma') return Zn, cm.plot_style(fig=fig, ax=ax, **kwargs)
def histogram_stepped_population(ph_list, new_fig=True, subplot=False, color='b', alpha=0.7, **kwargs): '''Extracts and plots the stepped histogram of a list of persistence diagrams. The histogram is normalized acording to the number of persistence diagrams. ''' fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot) hist_data = analysis.histogram_stepped(analysis.collapse(ph_list)) ax.fill_between(hist_data[0][:-1], 0, hist_data[1] / len(ph_list), color=color, alpha=alpha) return _cm.plot_style(fig=fig, ax=ax, **kwargs)
def histogram_stepped(ph, new_fig=True, subplot=False, color='b', alpha=0.7, **kwargs): '''Extracts and plots the stepped histogram of a persistent homology array. ''' fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot) hist_data = analysis.histogram_stepped(ph) ax.fill_between(hist_data[0][:-1], 0, hist_data[1], color=color, alpha=alpha) return _cm.plot_style(fig=fig, ax=ax, **kwargs)
def histogram_horizontal(ph, new_fig=True, subplot=False, bins=100, color='b', alpha=0.7, **kwargs): '''Extracts and plots the binned histogram of a persistent homology array. ''' fig, ax = cm.get_figure(new_fig=new_fig, subplot=subplot) hist_data = analysis.histogram_horizontal(ph, num_bins=bins) ax.fill_between(hist_data[0][:-1], 0, hist_data[1], color=color, alpha=alpha) return cm.plot_style(fig=fig, ax=ax, **kwargs)
def start_length_diagram(ph, new_fig=True, subplot=False, color='b', alpha=1.0, **kwargs): '''Plots the transformed ph diagram that represents lengths and starting points of a component. ''' ph_transformed = transform_ph_to_length(ph) fig, ax = cm.get_figure(new_fig=new_fig, subplot=subplot) for p in ph_transformed: ax.scatter(p[0], p[1], c=color, edgecolors='black', alpha=alpha) kwargs['title'] = kwargs.get('title', 'Transformed Persistence diagram') kwargs['xlabel'] = kwargs.get('xlabel', 'Start of the component') kwargs['ylabel'] = kwargs.get('ylabel', 'Length of the component') return cm.plot_style(fig=fig, ax=ax, **kwargs)
def persistence_image_average(ph_list, new_fig=True, subplot=111, xlims=None, ylims=None, norm_factor=1.0, vmin=None, vmax=None, cmap=jet_map, weighted=False, **kwargs): """Merges a list of ph diagrams and plots their respective average image. """ # pylint: disable=unexpected-keyword-arg av_imgs = analysis.get_average_persistence_image(ph_list, xlims=xlims, ylims=ylims, norm_factor=norm_factor, weighted=weighted) if xlims is None or xlims is None: xlims, ylims = analysis.get_limits(ph_list) if vmin is None: vmin = np.min(av_imgs) if vmax is None: vmax = np.max(av_imgs) fig, ax = cm.get_figure(new_fig=new_fig, subplot=subplot) ax.imshow(np.rot90(av_imgs), vmin=vmin, vmax=vmax, cmap=cmap, interpolation='bilinear', extent=xlims + ylims) kwargs['xlim'] = xlims kwargs['ylim'] = ylims kwargs['title'] = kwargs.get('title', 'Average persistence image') kwargs['xlabel'] = kwargs.get('xlabel', 'End radial distance from soma') kwargs['ylabel'] = kwargs.get('ylabel', 'Start radial distance from soma') return av_imgs, cm.plot_style(fig=fig, ax=ax, **kwargs)
def barcode_enhanced(ph, new_fig=True, subplot=False, linewidth=1.2, valID=2, cmap=jet_map, **kwargs): """ Generates a 2d figure (barcode) of the persistent homology of a tree enhanced by a parameter encodes in ph[valID] """ # Initialization of matplotlib figure and axes. fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot) val_max = _np.max(ph, axis=0)[valID] # Hack for colorbar creation Z = [[0, 0], [0, 0]] levels = _np.linspace(0.0, val_max, 200) CS3 = _cm.plt.contourf(Z, levels, cmap=cmap) def sort_ph_enhanced(ph, valID): """ Sorts barcode according to length. """ ph_sort = [p[:valID + 1] + [_np.abs(p[0] - p[1])] for p in ph] ph_sort.sort(key=lambda x: x[valID + 1]) return ph_sort ph_sort = sort_ph_enhanced(ph, valID) for ip, p in enumerate(ph_sort): ax.plot(p[:2], [ip, ip], c=cmap(p[valID] / val_max), linewidth=linewidth) kwargs['title'] = kwargs.get('title', 'Barcode of p.h.') kwargs['xlabel'] = kwargs.get('xlabel', 'Lifetime') _cm.plt.ylim([-1, len(ph_sort)]) _cm.plt.colorbar(CS3) return _cm.plot_style(fig=fig, ax=ax, **kwargs)
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)
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)
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)
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)
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)
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)
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)