コード例 #1
0
ファイル: test_pymaid.py プロジェクト: BlenderCN-Org/pyMaid
    def setUp(self):
        self.rm = pymaid.CatmaidInstance(config_test.server_url,
                                         config_test.http_user,
                                         config_test.http_pw,
                                         config_test.token)

        self.n = pymaid.get_neuron(config_test.test_skids[0],
                                   remote_instance=self.rm)
コード例 #2
0
ファイル: old.py プロジェクト: malei-pku/navis
    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('annotation:%s' % config_test.test_annotations[
            0], remote_instance=self.rm)
コード例 #3
0
    def __get_instance(self, credentials):
        with open(credentials) as fp:
            config = configparser.ConfigParser()
            config.readfp(fp)
            user = config.get("Credentials", "user")
            password = config.get("Credentials", "password")
            token = config.get("Credentials", "token")

            rm = pymaid.CatmaidInstance(self.api_url, token, user, password)
        return rm
コード例 #4
0
 def __init__(self,
              neuprint_endpoint=None,
              neuprint_dataset=None,
              neuprint_token=None,
              catmaid_endpoint=None):
     if neuprint_endpoint and neuprint_dataset and neuprint_token:
         self.neuprint_client = neuprint.Client(neuprint_endpoint,
                                                dataset=neuprint_dataset,
                                                token=neuprint_token)
     elif catmaid_endpoint:
         self.rm = pymaid.CatmaidInstance(catmaid_endpoint, '', '', '')
         #no self?
     else:
         self.neuprint_client = None
     self.vc = VfbConnect(neo_endpoint="http://kb.p2.virtualflybrain.org")
コード例 #5
0
ファイル: test_pymaid.py プロジェクト: BlenderCN-Org/pyMaid
    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[0:2],
                                    remote_instance=self.rm)

        self.n = self.nl[0]
        self.n.reroot(self.n.soma)

        # Get some random leaf node
        self.leaf_id = self.n.nodes[self.n.nodes.type == 'end'].sample(
            1).iloc[0].treenode_id
        self.slab_id = self.n.nodes[self.n.nodes.type == 'slab'].sample(
            1).iloc[0].treenode_id
コード例 #6
0
ファイル: old.py プロジェクト: malei-pku/navis
    def setUp(self):
        self.rm = pymaid.CatmaidInstance(
            config_test.server_url,
            config_test.http_user,
            config_test.http_pw,
            config_test.token)

        self.n = pymaid.get_neuron(config_test.test_skids[0],
                                   remote_instance=self.rm)

        self.cn_table = pymaid.get_partners(config_test.test_skids[0],
                                            remote_instance=self.rm)

        self.nB = pymaid.get_neuron(self.cn_table.iloc[0].skeleton_id,
                                    remote_instance=self.rm)

        self.adj = pymaid.adjacency_matrix(
            self.cn_table[self.cn_table.relation == 'upstream'].iloc[:10].skeleton_id.values)
コード例 #7
0
ファイル: connections.py プロジェクト: htem/pymaid_addons
def connect_to_catmaid(config_filename='default_connection.json'):
    if config_filename.lower() in connection_nicknames:
        config_filename = connection_nicknames[config_filename.lower()]

    if not os.path.exists(config_filename):
        config_filename = os.path.join(config_dir, config_filename)
    try:
        with open(config_filename, 'r') as f:
            configs = json.load(f)
    except:
        print(f'ERROR: No {config_filename} file found, or file improperly'
              ' formatted. See catmaid_configs_virtualflybrain_FAFB.json for'
              ' an example config file that works.')
        raise

    catmaid_http_username = configs.get('catmaid_http_username', None)
    catmaid_http_password = configs.get('catmaid_http_password', None)

    # --Source project-- #
    if all([
            configs.get('source_catmaid_url', None),
            configs.get('source_catmaid_account_to_use', None),
            configs.get('source_project_id', None)
    ]):
        source_project = pymaid.CatmaidInstance(
            configs['source_catmaid_url'],
            configs['catmaid_account_api_keys'][
                configs['source_catmaid_account_to_use']],
            http_user=catmaid_http_username,
            http_password=catmaid_http_password,
            make_global=True)
        source_project.project_id = configs['source_project_id']
        try:
            print_project_name(
                source_project,
                'Source project: ' + configs['source_catmaid_url'])
        except:
            raise Exception(f'The API key provided in {config_filename}'
                            ' does not appear to have access to project'
                            f' {source_project.project_id}. Please provide'
                            ' a different API key or project ID.')
    else:
        raise ValueError('The following fields must appear in'
                         f' {config_filename} and not be null:'
                         " 'source_catmaid_url',"
                         " 'source_catmaid_account_to_use',"
                         " and 'source_project_id'")

    # --Target project-- #
    # target_project is only used by upload_or_update_neurons, and may be
    # ommitted from the config file when you only want to do read-only
    # operations.
    if all([
            configs.get('target_catmaid_url', None),
            configs.get('target_catmaid_account_to_use', None),
            configs.get('target_project_id', None)
    ]):
        target_project = pymaid.CatmaidInstance(
            configs['target_catmaid_url'],
            configs['catmaid_account_api_keys'][
                configs['target_catmaid_account_to_use']],
            http_user=catmaid_http_username,
            http_password=catmaid_http_password,
            make_global=False)
        target_project.project_id = configs['target_project_id']
        print_project_name(target_project,
                           'Target project: ' + configs['target_catmaid_url'])

    elif any([
            configs.get('target_catmaid_url', None),
            configs.get('target_catmaid_account_to_use', None),
            configs.get('target_project_id', None)
    ]):
        print('WARNING: You have configured some target project variables but'
              ' not all. The following fields must appear in'
              f" {config_filename} and not be null: 'target_catmaid_url',"
              " 'target_catmaid_account_to_use', and 'target_project_id'."
              ' Continuing without a target project.')

    try:
        return source_project, target_project
    except:
        return source_project
コード例 #8
0
ファイル: old.py プロジェクト: malei-pku/navis
 def setUp(self):
     self.rm = pymaid.CatmaidInstance(config_test.server_url,
                                      config_test.http_user,
                                      config_test.http_pw,
                                      config_test.token)
コード例 #9
0
ファイル: pymaid.py プロジェクト: tliu68/maggot_connectome
def start_instance(log=False):
    if not log:
        logging.getLogger("pymaid").setLevel(logging.WARNING)
    return pymaid.CatmaidInstance(*catmaid_args)
コード例 #10
0
ファイル: pymaid.py プロジェクト: tliu68/maggot_connectome
import json
import logging
from pathlib import Path

import pymaid

base_path = Path(__file__).parent
try:
    file_path = (base_path / "./pymaid_credentials.json").resolve()
    with open(file_path, "r") as f:
        cred_dict = json.load(f)
    url = cred_dict["url"]
    token = cred_dict["token"]
    name = cred_dict["name"]
    pwd = cred_dict["pwd"]
    catmaid_args = (url, token, name, pwd)
except FileNotFoundError:
    msg = "Catmaid credentials not found - Pymaid functionality will be unavailable"
    raise UserWarning(msg)


def start_instance(log=False):
    if not log:
        logging.getLogger("pymaid").setLevel(logging.WARNING)
    return pymaid.CatmaidInstance(*catmaid_args)


if __name__ == "__main__":
    rm = pymaid.CatmaidInstance(*catmaid_args)
コード例 #11
0
ファイル: Plot_nx.py プロジェクト: markuspleijzier/AdultEM
import pymaid
import time
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import pandas as ps

#A requirement of this code is access to CATMAID.
#This is for API access to the CATMAID servers and to download skeleton information
server = 
http_user = 
http_pw = 
token = 


pymaid.CatmaidInstance( server, http_user, http_pw, token)




#This is an integer number that is unique to your neuron of interest
Neuron_1_skeleton_id_number = 
Neuron_1 = pymaid.get_neuron(Neuron_skeleton_id_number)

Neuron_2_skeleton_id_number = 
Neuron_2 = pymaid.get_neuron(Neuron_2_skeleton_id_number) 

#This function downsamples the neuron - it removes large stretches of skeleton that do not have any branch points.
#When the argument preserve_cn_treenodes = True, this preserves the treenodes where connectors (pre/postsynapses)
#have been placed. Downsampling is used to reduce the computational time, as some 3D reconstructed neurons can become 
#very large. 
コード例 #12
0
def get_branches(all_pids,
                 noi,
                 conn_data_path=None,
                 ignore_tags=False,
                 branch_threshold=0.05):
    """ Input:  list of all project ids
                list of neurons of interest
                path to conn_data_per_neuron folder
                ignore 'not a branch' tags
                % of main branch for threshold to consider branch
        Output: 
    """

    project_data = {}
    on_branch_per_project = {}

    if conn_data_path != None:
        for project in all_pids:
            project_data[project] = pd.read_csv(conn_data_path + str(project) +
                                                '/' + str(project) + '.csv')
            project_data[project]['neuron'] = project_data[project][
                'neuron'].str.split('(').str[0]
            on_branch_per_project[project] = []

        fixed_outputs = []
        for sublist in project_data[project].outputs:
            fixed_outputs.append(literal_eval(sublist))
        project_data[project]['outputs'] = fixed_outputs

    fullBranchList = pd.DataFrame(columns=[
        'leafnode', 'length', 'dist_from_root', 'neurName', 'project',
        'n_conns'
    ])

    for project in all_pids:
        # open an instance of CATMAID containing data https://zhencatmaid.com
        catmaid = pymaid.CatmaidInstance(
            server='https://zhencatmaid.com/',
            api_token='c48243e19b85edf37345ced8049ce5d6c5802412',
            project_id=project)

        if conn_data_path != None:
            curr_project = project_data[project]

        for neurName in noi:
            print('Working on ' + neurName + ' in project ' + str(project))
            try:
                catNeur = pymaid.get_neuron(neurName)
            except:
                print(neurName + " not found in project " + str(project))
                continue

            if isinstance(catNeur, pymaid.CatmaidNeuron):
                catNeur = [catNeur]

            for neur in catNeur:
                if neur.n_nodes < 10:
                    continue

                skid = neur.id

                if pymaid.find_nodes(tags=['nerve_ring_starts'],
                                     skeleton_ids=skid).empty:
                    continue

                catNeurnumpy = neur.nodes[[
                    "node_id", "parent_id", "x", "y", "z"
                ]].to_numpy()

                skTree = bf.build_tree(neur)
                nr_subtree = bf.crop_tree_nr(skTree, skid)

                for i in range(0, len(nr_subtree)):
                    strneurName = bf.strip_neurName(
                        list(pymaid.get_names(skid).values())[0])

                    if conn_data_path != None:
                        connsList = curr_project.loc[
                            curr_project['neuron'].isin([strneurName])]
                    else:
                        connsList = None

                    bl_output = bf.get_branchList(nr_subtree[i], neur,
                                                  branch_threshold)
                    branchList = bl_output[0]
                    pathList = bl_output[1]
                    trunk = bl_output[2]
                    trunklen = bf.cable_length(trunk[-1], catNeurnumpy,
                                               trunk[0])
                    lengthTemp = []
                    connTemp = []
                    for path in pathList:
                        lengthTemp.append(
                            bf.cable_length(path[0], catNeurnumpy, trunk[0]))
                        connTemp.append(
                            sum_Conns_on_Branch(path, neur, connsList))
                    branchList['length'] = branchList['length'] / trunklen
                    branchList['dist_from_root'] = [
                        i / trunklen for i in lengthTemp
                    ]
                    branchList['neurName'] = strneurName
                    branchList['project'] = project
                    branchList['n_conns'] = connTemp
                    if not ignore_tags:
                        try:
                            branchList = branchList[
                                ~branchList['leafnode'].astype(float).
                                astype(int).isin(neur.tags['not a branch'])]
                        except:
                            pass
                    fullBranchList = fullBranchList.append(branchList)
    return fullBranchList
コード例 #13
0
def get_connection_list(all_pids,noi,confidence = 5):
    """ Input:  list of all project ideas to get list for
                list of neurons of interest
        Output: list of all connections
    """
    fullConnsList = pd.DataFrame(columns = ['connector_id',
                                            'length',
                                            'dist_from_root',
                                            'neuron',
                                            'project',
                                            'type',])

    for project in all_pids:    
        # open an instance of CATMAID containing data https://zhencatmaid.com
        catmaid = pymaid.CatmaidInstance(server = 'https://zhencatmaid.com/',
                                            api_token='c48243e19b85edf37345ced8049ce5d6c5802412',
                                            project_id = project)

        for neurName in noi:
            print('Working on ' + neurName + ' in project ' + str(project))
            try:
                catNeur = pymaid.get_neuron(neurName)
            except:
                print(neurName + " not found in project " + str(project))
                continue

            if isinstance(catNeur, pymaid.CatmaidNeuron):
                catNeur = [catNeur]

            for neur in catNeur:
                if neur.n_nodes < 10:
                    continue

                skid = neur.id
                
                if pymaid.find_nodes(tags=['nerve_ring_starts'],skeleton_ids=skid).empty:
                    continue

                catNeurnumpy = neur.nodes[["node_id","parent_id","x","y","z"]].to_numpy()

                skTree = bf.build_tree(neur)
                nr_subtree = bf.crop_tree_nr(skTree,skid)

                # define and filter connections
                connectors = neur.connectors
                filt_conns = connectors[connectors.type.isin([0,1])].reset_index(drop = True)

                for i in range(len(nr_subtree)):
                    if len(nr_subtree) > 1:
                        strneurName = bf.strip_neurName(list(pymaid.get_names(skid).values())[0]) + "(" + str(i) + ")"
                    else: 
                        strneurName = bf.strip_neurName(list(pymaid.get_names(skid).values())[0])
                    bl_output = bf.get_branchList(nr_subtree[i],neur)
                    trunk = bl_output[2]
                    
                    connsList = pd.DataFrame(columns = ['connector_id',
                                                    'length',
                                                    'dist_from_root',
                                                    'neuron',
                                                    'project',
                                                    'type',
                                                    'inputs',
                                                    'outputs'])
                    
                    filt_conns.connector_id = filt_conns.connector_id.astype(str)
                    filt_conns2 = pd.DataFrame(columns = filt_conns.columns)
                    inputsClean = []
                    outputsClean = []

                    for connector in filt_conns.iterrows():
                        connector = connector[1]
                        if connector.node_id in nr_subtree[i]:
                            if connector.type == 1:
                                if check_confidence(catmaid,project,connector.connector_id,skid,'input'):
                                    inputsClean.append(clean_inputs(catmaid,project,connector))
                                    outputsClean.append(clean_outputs(catmaid,project,connector))
                                    filt_conns2 = filt_conns2.append(connector)
                            if connector.type == 0:
                                if check_confidence(catmaid,project,connector.connector_id,skid,'output'):
                                    inputsClean.append(clean_inputs(catmaid,project,connector))
                                    outputsClean.append(clean_outputs(catmaid,project,connector))
                                    filt_conns2 = filt_conns2.append(connector)
                    filt_conns2 = filt_conns2.reset_index(drop = True)

                    lengthTemp = []
                    distTemp = []
                    for node in filt_conns2.node_id:                    
                        lengthTemp.append(cf.get_norm_length(node,catNeurnumpy,trunk))
                        distTemp.append(cf.get_norm_dist(node,catNeurnumpy,trunk))

                    connsList.connector_id = filt_conns2.connector_id
                    connsList.project = project
                    connsList.neuron = strneurName
                    connsList.length = lengthTemp
                    connsList.dist_from_root = distTemp
                    connsList.type = filt_conns2.type
                    connsList.inputs = inputsClean
                    connsList.outputs = outputsClean
                    fullConnsList = fullConnsList.append(connsList).reset_index(drop = True)

    return fullConnsList
コード例 #14
0
ファイル: cytoscape.py プロジェクト: zzephyr00/CRN_graphing
from py2cytoscape.data.cyrest_client import CyRestClient

from IPython.display import Image

import networkx as nx
import pandas as pd

import pymaid

rm = pymaid.CatmaidInstance('server_url', 'api_token', 'http_user',
                            'http_password')

print('pymaid version:', pymaid.__version__)