Example #1
0
def test_fetch_skeleton():
    c = Client(NEUPRINT_SERVER, DATASET)
    orig_df = c.fetch_skeleton(5813027016, False)
    healed_df = c.fetch_skeleton(5813027016, True)

    assert len(orig_df) == len(healed_df)
    assert (healed_df['link'] == -1).sum() == 1
    assert healed_df['link'].iloc[0] == -1
Example #2
0
def test_members():
    set_default_client(None)
    assert default_client() is None
    c = Client(NEUPRINT_SERVER, DATASET)
    assert c.server == f'https://{NEUPRINT_SERVER}'
    assert c.dataset == DATASET

    assert default_client() is c

    df = c.fetch_custom("MATCH (m:Meta) RETURN m.primaryRois as rois")
    assert isinstance(df, pd.DataFrame)
    assert df.columns == ['rois']
    assert len(df) == 1
    assert isinstance(df['rois'].iloc[0], list)

    assert isinstance(c.fetch_available(), list)
    assert isinstance(c.fetch_help(), str)
    assert c.fetch_server_info() is True
    assert isinstance(c.fetch_version(), str)
    assert isinstance(c.fetch_database(), dict)
    assert isinstance(c.fetch_datasets(), dict)
    assert isinstance(c.fetch_db_version(), str)
    assert isinstance(c.fetch_profile(), dict)
    assert isinstance(c.fetch_token(), str)
    assert isinstance(c.fetch_daily_type(), tuple)
    assert isinstance(c.fetch_roi_completeness(), pd.DataFrame)
    assert isinstance(c.fetch_roi_connectivity(), pd.DataFrame)
    assert isinstance(c.fetch_roi_mesh('AB(R)'), bytes)
    assert isinstance(c.fetch_skeleton(EXAMPLE_BODY), pd.DataFrame)
Example #3
0
    neuronIDs = np.arange(len(bodyIds))
    np.random.shuffle(neuronIDs)
    selectedIDs = neuronIDs[0:params['max_neurons']]
    bodyIds = bodyIds.iloc[selectedIDs]

# Get skeletons
skeletonFile = '%s_skeletons.pkl' % (analysedRegion)
randomColor = lambda: '#' + ''.join(
    ['0123456789ABCDEF'[np.random.randint(16)] for i in range(6)])
print('Getting skeletons...')
if not (os.path.isfile(skeletonFile)):
    skeletons = []
    # max_number = 100
    for i, bodyId in tqdm(enumerate(bodyIds)):
        try:
            s = c.fetch_skeleton(bodyId, format='pandas')
            s['bodyId'] = bodyId
            s['color'] = randomColor()
            skeletons.append(s)
        except:
            print('; Skeleton %s cannot be downloaded' % (i))
    savePickle(skeletonFile, skeletons)
else:
    skeletons = loadPickle(skeletonFile)

# Load MB neurons for comparison with PN neurons
if analysedRegion == 'PN':
    getRandGrey = lambda: '#' + 'ABCDEF'[np.random.randint(low=0, high=6)] * 6
    mb_skeletons = loadPickle('MB_skeletons.pkl')
    neuronIDs = np.arange(len(mb_skeletons))
    np.random.shuffle(neuronIDs)
    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFuZHJldy5saW5Ac2hlZmZpZWxkLmFjLnVrIiwibGV2ZWwiOiJub2F1dGgiLCJpbWFnZS11cmwiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS0vQUF1RTdtQmp2LXlGaUlDWjdFQ3AwUHhzYmNVSHNDclppTU96eWtHbnFSemk_c3o9NTA_c3o9NTAiLCJleHAiOjE3NjE0MzE3Mzh9.kfurqTReeqmGsFHQdkKKGhTcaw3JR-FnpxCpMpS7uOA'
)
print(c.fetch_version())

print("Loading volume")
with h5py.File('hemibrain-v1.1-primary-roi-segmentation.h5', 'r') as f:
    roi_vol_scale_5 = f['volume-256nm'][:]

KCs = SegmentCriteria(type="^KC.*", regex=True)
KCneurons, KCroicounts = neuprint.queries.fetch_neurons(KCs)
KCids = KCneurons['bodyId']
KCidsarray = np.asarray(KCids)
np.savetxt("KCids_v1.1.csv", KCidsarray, delimiter=",", fmt="%u")
for i in range(0, len(KCids)):
    print(f"Fetching skeleton for KC {i}")
    KC = SegmentCriteria(bodyId=KCids[i])
    KCskel = c.fetch_skeleton(KCids[i], format='pandas', heal=True)
    print(f"Extracting ROI for each skeleton point for KC {i}")
    extract_labels_from_volume(KCskel,
                               roi_vol_scale_5,
                               vol_scale=5,
                               label_names=c.primary_rois)
    KCskel.rename(columns={'label_name': 'roi'}, inplace=True)
    KCskel.to_csv(path_or_buf="KCskel" + str(i) + ".csv")
    KCinputs = neuprint.queries.fetch_synapse_connections(target_criteria=KC)
    print(f"Found {len(KCinputs)} inputs to KC {i} bodyId {KCids[i]}")
    KCinputs.to_csv(path_or_buf="KCinputs" + str(i) + ".csv")
    KCoutputs = neuprint.queries.fetch_synapse_connections(source_criteria=KC)
    print(f"Found {len(KCoutputs)} outputs from KC {i} bodyId {KCids[i]}")
    KCoutputs.to_csv(path_or_buf="KCoutputs" + str(i) + ".csv")
Example #5
0
import os

import neuprint
from neuprint import Client, queries, SegmentCriteria

c = Client(
    'neuprint.janelia.org',
    dataset='hemibrain:v1.1',
    token=
    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFuZHJldy5saW5Ac2hlZmZpZWxkLmFjLnVrIiwibGV2ZWwiOiJub2F1dGgiLCJpbWFnZS11cmwiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS0vQUF1RTdtQmp2LXlGaUlDWjdFQ3AwUHhzYmNVSHNDclppTU96eWtHbnFSemk_c3o9NTA_c3o9NTAiLCJleHAiOjE3NjE0MzE3Mzh9.kfurqTReeqmGsFHQdkKKGhTcaw3JR-FnpxCpMpS7uOA'
)
print(c.fetch_version())

KCs = SegmentCriteria(type="^KC.*", regex=True)
APL = SegmentCriteria(bodyId=425790257)
print("Searching for KC to APL synapses")
KCtoAPL = neuprint.queries.fetch_synapse_connections(source_criteria=KCs,
                                                     target_criteria=APL)
print(f"Found {len(KCtoAPL)} KC to APL synapses")
print("Saving KC to APL synapses to csv")
KCtoAPL.to_csv(path_or_buf="KCtoAPLv1.1.csv")
print("Searching for APL to KC synapses")
APLtoKC = neuprint.queries.fetch_synapse_connections(source_criteria=APL,
                                                     target_criteria=KCs)
print("Saving APL to KC synapses to csv")
APLtoKC.to_csv(path_or_buf="APLtoKCv1.1.csv")
print(f"Found {len(APLtoKC)} APL to KC synapses")

APLskel = c.fetch_skeleton(425790257, format='pandas', heal=True)
APLskel.to_csv(path_or_buf="APLskelv1.1.csv")
Example #6
0
def set_picture_variables(b_clear, b_neuropil, b_neuron, b_xyz, b_search,
                          b_synapse, neuropil_names, neuron_names, xyz_names,
                          select_color, syn_roi, syn_query_neuron, upstream_id,
                          up_w, downstream_id, down_w, history_data,
                          upload_neuron_list, upload_xyz_list,
                          syn_neuron_source_brain, syn_neuron_version,
                          syn_neuron_label, syn_xyz_source_brain,
                          syn_xyz_version, syn_xyz_label, syn_random,
                          upload_synapse_list):
    print("1st", select_color)
    ####deal_with_clear
    if b_clear:
        if b_clear > history_data['NeuronSyn']['syn_button_clear']:
            history_data['NeuronSyn']['syn_button_clear'] = b_clear
            ## synapse

            if os.path.isfile(eflyplot_environment.brain_space.tmp_path +
                              "search_button_click_record.pickle"):
                os.remove(eflyplot_environment.brain_space.tmp_path +
                          "search_button_click_record.pickle")
            ## neuron
            if os.path.isfile(eflyplot_environment.brain_space.tmp_path +
                              "syn_neuron.pickle"):
                os.remove(eflyplot_environment.brain_space.tmp_path +
                          "syn_neuron.pickle")
            ## xyz
            if os.path.isfile(eflyplot_environment.brain_space.tmp_path +
                              "syz_xyz.pickle"):
                os.remove(eflyplot_environment.brain_space.tmp_path +
                          "syn_xyz.pickle")
            ## figure_all
            if os.path.isfile(eflyplot_environment.brain_space.tmp_path +
                              "syz_3d.pickle"):
                os.remove(eflyplot_environment.brain_space.tmp_path +
                          "syn_3d.pickle")
            return eFlyPlot_layout.initialize_eFlyPlot_history(), dict(rgb={
                'r': 255,
                'g': 255,
                'b': 255,
                'a': 1
            }), [], [], []
    ####deal_with_xyz
    if b_xyz:
        print("xyz", xyz_names)
        if b_xyz > history_data['NeuronSyn']['syn_button_add_xyz']:
            history_data['NeuronSyn']['syn_button_add_xyz'] = b_xyz
            if xyz_names:
                xyz_names = xyz_names.split(" ")
            else:
                xyz_names = []
            if upload_xyz_list:
                xyz_names = xyz_names + upload_xyz_list
            final_xyz_names = []
            for xyz_name in xyz_names:
                if xyz_name.find(".txt") == -1:
                    final_xyz_names.append(f"{xyz_name}.txt")
                else:
                    final_xyz_names.append(f"{xyz_name}")
            print(f"setting {final_xyz_names}")
            if syn_random == 'u':
                color = set_color(select_color)
            for xyz_name in final_xyz_names:
                if not os.path.isfile(
                        eflyplot_environment.brain_space.xyz_path + xyz_name):
                    print(f"<Warning> cannot open {xyz_name}")
                    continue
                if syn_random == 's':
                    select_color = set_color(select_color)
                elif syn_random == 'u':
                    select_color = color
                elif syn_random == 'i':
                    select_color = dict(
                        rgb={
                            'r': rd.randint(0, 255),
                            'g': rd.randint(0, 255),
                            'b': rd.randint(0, 255),
                            'a': 1
                        })
                history_data['NeuronSyn']['xyz_name'][xyz_name] = select_color
            return history_data, dict(rgb={
                'r': 255,
                'g': 255,
                'b': 255,
                'a': 1
            }), [], [], []
    ####deal_with_neuron
    if b_neuron:
        if b_neuron > history_data['NeuronSyn']['syn_button_add_neuron']:
            print('neuron', neuron_names)
            history_data['NeuronSyn']['syn_button_add_neuron'] = b_neuron
            if neuron_names:
                neuron_names = neuron_names.split(" ")
            else:
                neuron_names = []
            final_neuron_names = []
            if neuron_names:
                for neuron_name in neuron_names:
                    if neuron_name.find(".swc") == -1:
                        final_neuron_names.append(
                            f"{neuron_name}_{syn_neuron_source_brain}_{syn_neuron_version}_{syn_neuron_label}.swc"
                        )
                    else:
                        final_neuron_names.append(
                            f"{neuron_name}_{syn_neuron_source_brain}_{syn_neuron_version}_{syn_neuron_label}"
                        )
            if upload_neuron_list:
                final_neuron_names = final_neuron_names + upload_neuron_list
            print(f"setting {neuron_names}")
            if syn_random == "Uniform random color":
                select_color = dict(
                    rgb={
                        'r': rd.randint(0, 255),
                        'g': rd.randint(0, 255),
                        'b': rd.randint(0, 255),
                        'a': 1
                    })
                color = set_color(select_color)
            elif syn_random == "Selected color":
                color = set_color(select_color)
            for neuron_name in final_neuron_names:
                print("acquiring neuron..........")
                print(
                    f'{eflyplot_environment.brain_space.skeleton_path}{neuron_name}'
                )
                if not os.path.isfile(
                        f'{eflyplot_environment.brain_space.skeleton_path}{neuron_name}'
                ):
                    try:
                        c = Client('neuprint.janelia.org',
                                   dataset=f'hemibrain:{syn_neuron_version}',
                                   token=eflyplot_environment.brain_space.
                                   neuprint_tool.Token)
                        c.fetch_skeleton(
                            copy.deepcopy(neuron_name).split("_")[0],
                            export_path=
                            f"{eflyplot_environment.brain_space.skeleton_path}{neuron_name}",
                            format="swc")
                    except:
                        continue

                if syn_random == "Individual random color":
                    color = set_color(
                        dict(
                            rgb={
                                'r': rd.randint(0, 255),
                                'g': rd.randint(0, 255),
                                'b': rd.randint(0, 255),
                                'a': 1
                            }))
                history_data['NeuronSyn']['neuron_name'][neuron_name] = color
                print(history_data['NeuronSyn']['neuron_name'][neuron_name])
            return history_data, dict(rgb={
                'r': 255,
                'g': 255,
                'b': 255,
                'a': 1
            }), [], [], []

    ####deal_with_neuropil
    if b_neuropil is not None:
        if b_neuropil > history_data['NeuronSyn']['syn_button_add_neuropil']:
            history_data['NeuronSyn']['syn_button_add_neuropil'] = b_neuropil
            '''
            若有在history,就不更動,避免改到過去設定好的顏色
            但若是history有,現在沒有
            '''
            if syn_random == "Uniform random color":
                select_color = dict(
                    rgb={
                        'r': rd.randint(0, 255),
                        'g': rd.randint(0, 255),
                        'b': rd.randint(0, 255),
                        'a': 1
                    })
                color = set_color(select_color)
            elif syn_random == "Selected color":
                color = set_color(select_color)
            for neuropil in neuropil_names:
                if neuropil not in history_data['NeuronSyn']['neuropil']:
                    if syn_random == "Individual random color":
                        color = set_color(
                            dict(
                                rgb={
                                    'r': rd.randint(0, 255),
                                    'g': rd.randint(0, 255),
                                    'b': rd.randint(0, 255),
                                    'a': 1
                                }))
                    history_data['NeuronSyn']['neuropil'][neuropil] = color

                # elif neuropil in history_data['neuropil'] andhistory_data['neuropil'].index(neuropil)
            for neuropil in history_data['NeuronSyn']['neuropil']:
                if neuropil not in neuropil_names:
                    del (history_data['NeuronSyn']['neuropil'][neuropil])
            return history_data, dict(rgb={
                'r': 255,
                'g': 255,
                'b': 255,
                'a': 1
            }), [], [], []

    ####deal with search
    if b_search:
        if b_search > history_data['NeuronSyn']['syn_button_submit']:
            history_data['NeuronSyn']['syn_button_submit'] = b_search
            if not upstream_id:
                upstream_id = "noinfo"
            if not up_w:
                up_w = 0
            if not downstream_id:
                downstream_id = "noinfo"
            if not down_w:
                down_w = 0
            if not syn_query_neuron:
                syn_query_neuron = "noinfo"
            if not syn_roi:
                syn_roi = "noinfo"
            connection = f"{upstream_id} {up_w} {downstream_id} {down_w} {syn_query_neuron} {syn_roi}"
            color = set_color(select_color)
            history_data['NeuronSyn']['syn_search_query'][connection] = color
            return history_data, dict(rgb={
                'r': 255,
                'g': 255,
                'b': 255,
                'a': 1
            }), [], [], []

    if b_synapse:
        if b_synapse > history_data['NeuronSyn']['syn_button_add_synapse']:
            history_data['NeuronSyn']['syn_button_add_synapse'] = b_synapse
            if upload_synapse_list:
                final_synapses = upload_synapse_list
            if syn_random == "Uniform random color":
                select_color = dict(
                    rgb={
                        'r': rd.randint(0, 255),
                        'g': rd.randint(0, 255),
                        'b': rd.randint(0, 255),
                        'a': 1
                    })
                color = set_color(select_color)
            elif syn_random == "Selected color":
                color = set_color(select_color)
            for synapse_file in final_synapses:
                if syn_random == "Individual random color":
                    color = set_color(
                        dict(
                            rgb={
                                'r': rd.randint(0, 255),
                                'g': rd.randint(0, 255),
                                'b': rd.randint(0, 255),
                                'a': 1
                            }))
                history_data['NeuronSyn']['upload-synapse_data'][
                    synapse_file] = color
        return history_data, dict(rgb={
            'r': 255,
            'g': 255,
            'b': 255,
            'a': 1
        }), [], [], []
    return history_data, dict(rgb={
        'r': 255,
        'g': 255,
        'b': 255,
        'a': 1
    }), [], [], []