def get_radius_pruned_neurons_by_skid(skids, radius_to_keep=PRIMARY_NEURITE_RADIUS, keep_larger_radii=True): neurons = pymaid.get_neuron(skids, remote_instance=source_project) if type(neurons) is pymaid.core.CatmaidNeuron: neurons = pymaid.core.CatmaidNeuronList(neurons) for neuron in neurons: if 'radius' in neuron.neuron_name: raise Exception('Radius pruning was requested for' f' "{neuron.neuron_name}". You probably didn\'t' ' mean to do this since it was already pruned.' ' Abort!') if keep_larger_radii: navis.subset_neuron(neuron, neuron.nodes.node_id.values[ neuron.nodes.radius >= radius_to_keep], inplace=True) else: navis.subset_neuron(neuron, neuron.nodes.node_id.values[neuron.nodes.radius == radius_to_keep], inplace=True) if neuron.n_skeletons > 1: navis.plot2d(neuron) raise Exception(f'You cut {neuron.neuron_name} into two fragments.' " That's not supposed to happen.") neuron.annotations.append('pruned to nodes with radius 500') neuron.neuron_name = neuron.neuron_name + f' - radius {radius_to_keep}' return neurons
def plot_morpho(self, figsize, save_path=None, alpha=1, color=None, volume=None, vol_color = (250, 250, 250, .05), azim=-90, elev=-90, dist=6, xlim3d=(-4500, 110000), ylim3d=(-4500, 110000), linewidth=1.5, connectors=False): # recommended volume for L1 dataset, 'PS_Neuropil_manual' neurons = pymaid.get_neurons(self.skids) if(color==None): color = self.color if(volume!=None): neuropil = pymaid.get_volume(volume) neuropil.color = vol_color fig, ax = navis.plot2d([neurons, neuropil], method='3d_complex', color=color, linewidth=linewidth, connectors=connectors, cn_size=2, alpha=alpha) if(volume==None): fig, ax = navis.plot2d([neurons], method='3d_complex', color=color, linewidth=linewidth, connectors=connectors, cn_size=2, alpha=alpha) ax.azim = azim ax.elev = elev ax.dist = dist ax.set_xlim3d(xlim3d) ax.set_ylim3d(ylim3d) plt.show() if(save_path!=None): fig.savefig(f'{save_path}.png', format='png', dpi=300, transparent=True)
def plot_celltype(path, pairids, n_rows, n_cols, celltypes, pairs_path, plot_pairs=True, connectors=False, cn_size=0.25, color=None, names=False, plot_padding=[0,0]): pairs = Promat.get_pairs(pairs_path) # pull specific cell type identities celltype_ct = [Celltype(f'{pairid}-ipsi-bi', Promat.get_paired_skids(pairid, pairs)) for pairid in pairids] celltype_ct = Celltype_Analyzer(celltype_ct) celltype_ct.set_known_types(celltypes) members = celltype_ct.memberships() # link identities to official celltype colors celltype_identities = [np.where(members.iloc[:, i]==1.0)[0][0] for i in range(0, len(members.columns))] if(plot_pairs): celltype_ct = [Celltype(celltypes[celltype_identities[i]].name.replace('s', ''), Promat.get_paired_skids(pairid, pairs), celltypes[celltype_identities[i]].color) if celltype_identities[i]<17 else Celltype(f'{pairid}', Promat.get_paired_skids(pairid, pairs), '#7F7F7F') for i, pairid in enumerate(pairids)] if(plot_pairs==False): celltype_ct = [Celltype(celltypes[celltype_identities[i]].name.replace('s', ''), pairid, celltypes[celltype_identities[i]].color) if celltype_identities[i]<17 else Celltype('Other', pairid, '#7F7F7F') for i, pairid in enumerate(pairids)] # plot neuron morphologies neuropil = pymaid.get_volume('PS_Neuropil_manual') neuropil.color = (250, 250, 250, .05) n_rows = n_rows n_cols = n_cols alpha = 1 fig = plt.figure(figsize=(n_cols*2, n_rows*2)) gs = plt.GridSpec(n_rows, n_cols, figure=fig, wspace=plot_padding[0], hspace=plot_padding[1]) axs = np.empty((n_rows, n_cols), dtype=object) for i, skids in enumerate([x.skids for x in celltype_ct]): if(color!=None): col = color else: col = celltype_ct[i].color neurons = pymaid.get_neurons(skids) inds = np.unravel_index(i, shape=(n_rows, n_cols)) ax = fig.add_subplot(gs[inds], projection="3d") axs[inds] = ax navis.plot2d(x=[neurons, neuropil], connectors=connectors, cn_size=cn_size, color=col, alpha=alpha, ax=ax, method='3d_complex') ax.azim = -90 ax.elev = -90 ax.dist = 6 ax.set_xlim3d((-4500, 110000)) ax.set_ylim3d((-4500, 110000)) if(names): ax.text(x=(ax.get_xlim()[0] + ax.get_xlim()[1])/2 - ax.get_xlim()[1]*0.05, y=ax.get_ylim()[1]*4/5, z=0, s=celltype_ct[i].name, transform=ax.transData, color=col, alpha=1) fig.savefig(f'{path}.png', format='png', dpi=300, transparent=True)
def _special_mbon_split(n, treenode_info, output_path): skid = int(n.skeleton_id) axon_starts = list( pymaid.find_nodes(tags="mw axon start", skeleton_ids=skid)["node_id"]) axon_ends = list( pymaid.find_nodes(tags="mw axon end", skeleton_ids=skid)["node_id"]) axon_splits = axon_starts + axon_ends fragments = navis.cut_skeleton(n, axon_splits) axons = [] dendrites = [] for fragment in fragments: root = fragment.root if "mw axon start" in fragment.tags and root in fragment.tags[ "mw axon start"]: axons.append(fragment) elif "mw axon end" in fragment.tags and root in fragment.tags[ "mw axon end"]: dendrites.append(fragment) elif "soma" in fragment.tags and root in fragment.tags["soma"]: dendrites.append(fragment) else: raise UserWarning( f"Something weird happened when splitting special neuron {skid}" ) for a in axons: axon_treenodes = a.nodes.node_id.values _append_labeled_nodes(treenode_info, axon_treenodes, "axon") for d in dendrites: dendrite_treenodes = d.nodes.node_id.values _append_labeled_nodes(treenode_info, dendrite_treenodes, "dendrite") fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111, projection="3d") navis.plot2d(axons, color="red", ax=ax) navis.plot2d(dendrites, color="blue", ax=ax) plt.savefig( output_path / f"weird-mbon-{skid}.png", facecolor="w", transparent=False, bbox_inches="tight", pad_inches=0.3, dpi=300, ) fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111, projection="3d") navis.plot2d(axons, color="red", ax=ax) navis.plot2d(dendrites, color="blue", ax=ax) ax.azim = -90 ax.elev = 0 plt.savefig( output_path / f"weird-mbon-{skid}-top.png", facecolor="w", transparent=False, bbox_inches="tight", pad_inches=0.3, dpi=300, )