示例#1
0
    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)
示例#2
0
    def setUp(self):
        self.rm = pymaid.CatmaidInstance(config_test.server_url,
                                         config_test.http_user,
                                         config_test.http_pw,
                                         config_test.token)

        self.nl = pymaid.get_neuron(config_test.test_skids,
                                    remote_instance=self.rm)

        self.vol = pymaid.get_volume(config_test.test_volume)
示例#3
0
def plot_3view(
    data,
    axs,
    palette=None,
    connectors=False,
    connectors_only=False,
    label_by=None,
    alpha=1,
    s=1,
    row_title=None,
    **kws,
):
    volumes = [pymaid.get_volume(v) for v in volume_names]
    for i, view in enumerate(views):
        ax = axs[i]
        if label_by is None:
            pymaid.plot2d(
                data,
                color=palette,
                ax=ax,
                method="3d",
                connectors=connectors,
                connectors_only=connectors_only,
                **kws,
            )
        else:
            uni_labels = np.unique(data[label_by])
            for ul in uni_labels:
                temp_data = data[data[label_by] == ul]
                color = palette[ul]
                scatter_kws = dict(s=s, alpha=alpha, color=color)
                pymaid.plot2d(
                    temp_data[["x", "y", "z"]],
                    ax=ax,
                    method="3d",
                    connectors=connectors,
                    connectors_only=connectors_only,
                    scatter_kws=scatter_kws,
                    **kws,
                )
        set_view_params(ax, **view_dict[view])
        plot_volumes(volumes, ax)
        if row_title is not None:
            ax = axs[0]
            ax.text2D(
                x=0,
                y=0.5,
                s=row_title,
                ha="right",
                va="center",
                color="grey",
                rotation=90,
                transform=ax.transAxes,
            )
示例#4
0
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)
示例#5
0
文件: old.py 项目: malei-pku/navis
    def test_connectivity_filter(self):
        dist, prox = pymaid.cut_neuron(
            self.n, pymaid.find_main_branchpoint(self.n))

        vol = pymaid.get_volume(config_test.test_volume)

        # Connectivity table by neuron
        self.assertIsInstance(pymaid.filter_connectivity(self.cn_table, prox),
                              pd.DataFrame)
        # Adjacency matrix by neuron
        self.assertIsInstance(pymaid.filter_connectivity(self.adj, prox),
                              pd.DataFrame)

        # Connectivity table by volume
        self.assertIsInstance(pymaid.filter_connectivity(self.cn_table, vol),
                              pd.DataFrame)
        # Adjacency matrix by volume
        self.assertIsInstance(pymaid.filter_connectivity(self.adj, vol),
                              pd.DataFrame)
示例#6
0
文件: old.py 项目: malei-pku/navis
 def test_get_volume(self):
     self.assertIsInstance(pymaid.get_volume(
         config_test.test_volume, remote_instance=self.rm), pymaid.Volume)
示例#7
0
pns_ms = neurogenesis.init_from_skid_list(fafb_c, pn_skids)

import pickle
path = local_path + "data/pn_bouton_clusters/"

with open(path + "pns_ms.pkl", 'wb') as f:
    pickle.dump(pns_ms, f, -1)
    
nl = [pymaid.get_neuron([str(i) for i in j]) for j in [pn_skids[:40], pn_skids[40:80], pn_skids[80:]]]
pns_pm = nl[0] + nl[1] + nl [2]

with open(path + "pns_pm.pkl", 'wb') as f:
    pickle.dump(pns_pm, f, -1)

ca = pymaid.get_volume('MB_CA_R')

with open(path + "ca.pkl", 'wb') as f:
    pickle.dump(ca, f, -1)

df = pd.read_excel(local_path + 'data/180613-pn_subtypes.xlsx')


# for loading the pickle pymaid neuronlist data
path = local_path + "data/pn_bouton_clusters/"
with open(path + "pns_pm.pkl", 'rb') as f:
    pns_pm = pickle.load(f)

    
with open(path + "pns_ms.pkl", 'rb') as f:
    pns_ms = pickle.load(f)
示例#8
0
    axs = np.empty((n_row, n_col), dtype="O")

    connection_types = ["axon", "dendrite", "unsplittable"]
    pal = sns.color_palette("deep", 5)
    colors = [pal[3], pal[0], pal[4]]  # red, blue, purple
    connection_colors = dict(zip(connection_types, colors))

    views = ["front", "side", "top"]
    view_params = [
        dict(azim=-90, elev=0, dist=5),
        dict(azim=0, elev=0, dist=5),
        dict(azim=-90, elev=90, dist=5),
    ]
    view_dict = dict(zip(views, view_params))

    volumes = [pymaid.get_volume(v) for v in volume_names]

    def set_view_params(ax, azim=-90, elev=0, dist=5):
        ax.azim = azim
        ax.elev = elev
        ax.dist = dist
        set_axes_equal(ax)

    def plot_volumes(ax):
        pymaid.plot2d(volumes, ax=ax, method="3d")
        for c in ax.collections:
            if isinstance(c, Poly3DCollection):
                c.set_alpha(0.03)

    def add_subplot(row, col, projection=None):
        ax = fig.add_subplot(gs[row, col], projection=projection)
示例#9
0
def get_volume_pruned_neurons_by_skid(skids,
                                      volume_id,
                                      mode='fele',
                                      resample=0,
                                      only_keep_largest_fragment=False,
                                      verbose=False,
                                      remote_instance=None):
    """
    mode : 'fele' -   Keep all parts of the neuron between its primary
                      neurite's First Entry to and Last Exit from the volume.
                      So if a segment of the primary neurite leaves and then
                      re-enters the volume, that segment is not removed.
           'strict' - All nodes outside the volume are pruned.
    resample : If set to a positive value, the neuron will be resampled before
               pruning to have treenodes placed every `resample` nanometers. If
               left at 0, resampling is not performed.
    In both cases, if a branch point is encountered before the first entry or
    last exit, that branch point is used as the prune point.
    """
    if remote_instance is None:
        remote_instance = source_project

    #if exit_volume_id is None:
    #    exit_volume_id = entry_volume_id

    neurons = pymaid.get_neuron(skids, remote_instance=source_project)
    if volume_id not in volumes:
        try:
            print(f'Pulling volume {volume_id} from project'
                  f' {remote_instance.project_id}.')
            volumes[volume_id] = pymaid.get_volume(
                volume_id, remote_instance=remote_instance)
        except:
            print(f"Couldn't find volume {volume_id} in project_id"
                  f" {remote_instance.project_id}! Exiting.")
            raise
    else:
        print(f'Loading volume {volume_id} from cache.')
    volume = volumes[volume_id]

    if type(neurons) is pymaid.core.CatmaidNeuron:
        neurons = pymaid.core.CatmaidNeuronList(neurons)

    if resample > 0:
        #TODO find the last node on the primary neurite and store its position
        neurons.resample(
            resample)  # This throws out radius info except for root
        #TODO find the new node closest to the stored node and set all nodes
        #between that node and root to have radius 500

    for neuron in neurons:
        if 'pruned by vol' in neuron.neuron_name:
            raise Exception(
                'Volume pruning was requested for '
                f' "{neuron.neuron_name}". You probably didn\'t mean to do'
                ' this since it was already pruned. Exiting.')
            continue
        print(f'Pruning neuron {neuron.neuron_name}')
        if mode == 'fele':
            """
            First, find the most distal primary neurite node. Then, walk
            backward until either finding a node within the volume or a branch
            point. Prune distal to one distal to that point (so it only gets
            the primary neurite and not the offshoot).
            Then, start from the primary neurite node that's a child of the
            soma node, and walk forward (how?) until finding a node within the
            volume or a branch point. Prune proximal to that.
            """
            nodes = neuron.nodes.set_index('node_id')
            # Find end of the primary neurite
            nodes['has_fat_child'] = False
            for tid in nodes.index:
                if nodes.at[tid, 'radius'] == PRIMARY_NEURITE_RADIUS:
                    parent = nodes.at[tid, 'parent_id']
                    nodes.at[parent, 'has_fat_child'] = True
            is_prim_neurite_end = (~nodes['has_fat_child']) & (
                nodes['radius'] == PRIMARY_NEURITE_RADIUS)
            prim_neurite_end = nodes.index[is_prim_neurite_end]
            if len(prim_neurite_end) is 0:
                raise ValueError(f"{neuron.neuron_name} doesn't look like a"
                                 "  motor neuron. Exiting.")
            elif len(prim_neurite_end) is not 1:
                raise ValueError('Multiple primary neurite ends for'
                                 f' {neuron.neuron_name}: {prim_neurite_end}.'
                                 '\nExiting.')

            nodes['is_in_vol'] = navis.in_volume(nodes, volume)

            # Walk backwards until at a point inside the volume, or at a branch
            # point
            current_node = prim_neurite_end[0]
            parent_node = nodes.at[current_node, 'parent_id']
            while (nodes.at[parent_node, 'type'] != 'branch'
                   and not nodes.at[parent_node, 'is_in_vol']):
                current_node = parent_node
                if verbose: print(f'Walk back to {current_node}')
                parent_node = nodes.at[parent_node, 'parent_id']
            if verbose: print(f'Pruning distal to {current_node}')
            neuron.prune_distal_to(current_node, inplace=True)

            # Start at the first primary neurite node downstream of root
            current_node = nodes.index[
                (nodes.parent_id == neuron.root[0])
                #& (nodes.radius == PRIMARY_NEURITE_RADIUS)][0]
                & (nodes.radius > 0)][0]
            #Walking downstream is a bit slow, but probably acceptable
            while (not nodes.at[current_node, 'is_in_vol']
                   and nodes.at[current_node, 'type'] == 'slab'):
                current_node = nodes.index[nodes.parent_id == current_node][0]
                if verbose: print(f'Walk forward to {current_node}')
            if not nodes.at[current_node, 'is_in_vol']:
                input('WARNING: Hit a branch before hitting the volume for'
                      f' neuron {neuron.neuron_name}. This is unusual.'
                      ' Press enter to acknowledge.')

            if verbose: print(f'Pruning proximal to {current_node}')
            neuron.prune_proximal_to(current_node, inplace=True)

        elif mode == 'strict':
            neuron.prune_by_volume(volume)  #This does in-place pruning

        if neuron.n_skeletons > 1:
            if only_keep_largest_fragment:
                print('Neuron has multiple disconnected fragments after'
                      ' pruning. Will only keep the largest fragment.')
                frags = morpho.break_fragments(neuron)
                neuron.nodes = frags[frags.n_nodes == max(
                    frags.n_nodes)][0].nodes
                #print(neuron)
            #else, the neuron will get healed and print a message about being
            #healed during upload_neuron, so nothing needs to be done here.

        if mode == 'fele':
            neuron.annotations.append(
                f'pruned (first entry, last exit) by vol {volume_id}')
        elif mode == 'strict':
            neuron.annotations.append(f'pruned (strict) by vol {volume_id}')

        neuron.neuron_name = neuron.neuron_name + f' - pruned by vol {volume_id}'

        if verbose: print('\n')

    return neurons