{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Generating CATMAID URLs\n", "-----------------------\n", "\n", "This example will demonstrate how to use pymaid to generate URLs to a set of connectors. You can use this, for example, to perform random sampling of up- or downstream partners." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import pymaid\n", "\n", "rm = pymaid.connect_catmaid()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "First get your neuron of interest:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "n = pymaid.get_neuron(16)" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "A simple example: Downstream sampling by connectors\n", "===================================================" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
treenode_idconnector_idrelationxyzURL
01272349991880438817164242217440https://neuropil.janelia.org/tracing/fafb/v14/...
12591979540437120160998211920https://neuropil.janelia.org/tracing/fafb/v14/...
22665983000437184162324214880https://neuropil.janelia.org/tracing/fafb/v14/...
32646983730437042162452214120https://neuropil.janelia.org/tracing/fafb/v14/...
42654984150436761163690214440https://neuropil.janelia.org/tracing/fafb/v14/...
\n", "
" ], "text/plain": [ " treenode_id connector_id relation x y z \\\n", "0 127234 999188 0 438817 164242 217440 \n", "1 2591 97954 0 437120 160998 211920 \n", "2 2665 98300 0 437184 162324 214880 \n", "3 2646 98373 0 437042 162452 214120 \n", "4 2654 98415 0 436761 163690 214440 \n", "\n", " URL \n", "0 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "1 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "2 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "3 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "4 https://neuropil.janelia.org/tracing/fafb/v14/... " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get all connectors\n", "cn = n.connectors\n", "\n", "# Add a column with the URL to coordinates\n", "cn['URL'] = pymaid.url_to_coordinates(cn, \n", " stack_id=5,\n", " active_node_id=cn.connector_id.values)\n", "cn.head()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "In above example we are using ``stack_id=5``. This is the ID of the image stack for this project and is encoded in the URL as ``sid0=5``.\n", "\n", "Now you can export this DataFrame. Two quick examples.\n", "\n", "To CSV:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "cn.to_csv('filename.csv')" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "To clipboard:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "cn.to_clipboard()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Randomizing\n", "===========\n", "Randomizing the order e.g. for random sampling of downstream partners is straight forward:\n", "\n", "Subset to presynapses:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "pre = cn[cn.relation == 0]" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Randomize order:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
treenode_idconnector_idrelationxyzURL
86810350265103502690358255151135167240https://neuropil.janelia.org/tracing/fafb/v14/...
142310366582103665810336551159297177360https://neuropil.janelia.org/tracing/fafb/v14/...
78510345534103455330375162146617170760https://neuropil.janelia.org/tracing/fafb/v14/...
59710337834103378350385421134986173200https://neuropil.janelia.org/tracing/fafb/v14/...
159110370697103706950341188153709169440https://neuropil.janelia.org/tracing/fafb/v14/...
\n", "
" ], "text/plain": [ " treenode_id connector_id relation x y z \\\n", "868 10350265 10350269 0 358255 151135 167240 \n", "1423 10366582 10366581 0 336551 159297 177360 \n", "785 10345534 10345533 0 375162 146617 170760 \n", "597 10337834 10337835 0 385421 134986 173200 \n", "1591 10370697 10370695 0 341188 153709 169440 \n", "\n", " URL \n", "868 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "1423 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "785 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "597 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "1591 https://neuropil.janelia.org/tracing/fafb/v14/... " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ran = pre.sample(frac=1)\n", "ran.head()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "A more complex example: sampling inputs by treenode\n", "===================================================\n", "\n", "First we have to get the presynaptic treenode IDs:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO : Data for 412 of 412 unique connector IDs retrieved (pymaid)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
connector_idpresynaptic_topostsynaptic_topresynaptic_to_nodepostsynaptic_to_node
0474311203840[7911576, 8638, 16, 6483677]474312[2736161, 474295, 124991, 21109788]
157390554753[7634, 279469, 30358, 17238, 16, 597080, 39182...573904[573902, 1482460, 1670909, 2875768, 127266, 28...
2133533255153[8408275, 16]1326548[27245140, 125650]
31482374203840[10379190, 9052688, 279469, 597085, 597100, 59...1277055[2875773, 30223253, 1482362, 2875771, 2875774,...
41510194288558[16, 203840]1510193[124597, 1510195]
\n", "
" ], "text/plain": [ " connector_id presynaptic_to \\\n", "0 474311 203840 \n", "1 573905 54753 \n", "2 1335332 55153 \n", "3 1482374 203840 \n", "4 1510194 288558 \n", "\n", " postsynaptic_to presynaptic_to_node \\\n", "0 [7911576, 8638, 16, 6483677] 474312 \n", "1 [7634, 279469, 30358, 17238, 16, 597080, 39182... 573904 \n", "2 [8408275, 16] 1326548 \n", "3 [10379190, 9052688, 279469, 597085, 597100, 59... 1277055 \n", "4 [16, 203840] 1510193 \n", "\n", " postsynaptic_to_node \n", "0 [2736161, 474295, 124991, 21109788] \n", "1 [573902, 1482460, 1670909, 2875768, 127266, 28... \n", "2 [27245140, 125650] \n", "3 [2875773, 30223253, 1482362, 2875771, 2875774,... \n", "4 [124597, 1510195] " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "post_details = pymaid.get_connector_details(n.postsynapses)\n", "post_details.head()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If you have connectors without a presynaptic treenode, you will have to fix that and rerun above code before moving on!" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
connector_idpresynaptic_topostsynaptic_topresynaptic_to_nodepostsynaptic_to_node
40120041670None[4954519, 16]None[20041667, 18719343]
\n", "
" ], "text/plain": [ " connector_id presynaptic_to postsynaptic_to presynaptic_to_node \\\n", "401 20041670 None [4954519, 16] None \n", "\n", " postsynaptic_to_node \n", "401 [20041667, 18719343] " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "post_details[post_details.presynaptic_to_node.isnull()]" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Next we need to get the coordinates for those treenodes." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
node_idxyz
010362880367320.0152292.0177760.0
118804742466093.0234873.038560.0
218802702466727.0233599.033640.0
310342419381419.0140309.0180280.0
410315798345150.0159327.0186640.0
\n", "
" ], "text/plain": [ " node_id x y z\n", "0 10362880 367320.0 152292.0 177760.0\n", "1 18804742 466093.0 234873.0 38560.0\n", "2 18802702 466727.0 233599.0 33640.0\n", "3 10342419 381419.0 140309.0 180280.0\n", "4 10315798 345150.0 159327.0 186640.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Now get the locations\n", "locs = pymaid.get_node_location(post_details.presynaptic_to_node.values)\n", "locs.head()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Now we have to merge the locations into the connector Dataframe" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
connector_idpresynaptic_topostsynaptic_topostsynaptic_to_nodexyz
4371247265239202541[8680461, 16, 13782][28458449, 1786, 22231380]438062.0140370.0203160.0
4371727265176202541[30571, 40637, 16, 4205169][6428950, 447292, 1819, 7265200]437010.0141171.0204400.0
474312474311203840[7911576, 8638, 16, 6483677][2736161, 474295, 124991, 21109788]421574.0127771.0194720.0
57390457390554753[7634, 279469, 30358, 17238, 16, 597080, 39182...[573902, 1482460, 1670909, 2875768, 127266, 28...434460.0165646.0216160.0
12755762873212203840[595239, 16][2873213, 127687]436480.0147325.0221360.0
\n", "
" ], "text/plain": [ " connector_id presynaptic_to \\\n", "437124 7265239 202541 \n", "437172 7265176 202541 \n", "474312 474311 203840 \n", "573904 573905 54753 \n", "1275576 2873212 203840 \n", "\n", " postsynaptic_to \\\n", "437124 [8680461, 16, 13782] \n", "437172 [30571, 40637, 16, 4205169] \n", "474312 [7911576, 8638, 16, 6483677] \n", "573904 [7634, 279469, 30358, 17238, 16, 597080, 39182... \n", "1275576 [595239, 16] \n", "\n", " postsynaptic_to_node x \\\n", "437124 [28458449, 1786, 22231380] 438062.0 \n", "437172 [6428950, 447292, 1819, 7265200] 437010.0 \n", "474312 [2736161, 474295, 124991, 21109788] 421574.0 \n", "573904 [573902, 1482460, 1670909, 2875768, 127266, 28... 434460.0 \n", "1275576 [2873213, 127687] 436480.0 \n", "\n", " y z \n", "437124 140370.0 203160.0 \n", "437172 141171.0 204400.0 \n", "474312 127771.0 194720.0 \n", "573904 165646.0 216160.0 \n", "1275576 147325.0 221360.0 " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cn_merged = pd.merge(post_details.set_index('presynaptic_to_node'),\n", " locs.set_index('node_id'),\n", " left_index=True, right_index=True)\n", "cn_merged.head()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Let's add the url to the presynaptic node and some additional info: the name of the presynaptic neuron and whether it already has a soma" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
connector_idpresynaptic_topostsynaptic_topostsynaptic_to_nodexyzurlneuron_namehas_soma
4371247265239202541[8680461, 16, 13782][28458449, 1786, 22231380]438062.0140370.0203160.0https://neuropil.janelia.org/tracing/fafb/v14/...putative Gad1-F-600202 40613 AJTrue
4371727265176202541[30571, 40637, 16, 4205169][6428950, 447292, 1819, 7265200]437010.0141171.0204400.0https://neuropil.janelia.org/tracing/fafb/v14/...putative Gad1-F-600202 40613 AJTrue
474312474311203840[7911576, 8638, 16, 6483677][2736161, 474295, 124991, 21109788]421574.0127771.0194720.0https://neuropil.janelia.org/tracing/fafb/v14/...APL 203841 MR JSLTrue
57390457390554753[7634, 279469, 30358, 17238, 16, 597080, 39182...[573902, 1482460, 1670909, 2875768, 127266, 28...434460.0165646.0216160.0https://neuropil.janelia.org/tracing/fafb/v14/...neuron 54754False
12755762873212203840[595239, 16][2873213, 127687]436480.0147325.0221360.0https://neuropil.janelia.org/tracing/fafb/v14/...APL 203841 MR JSLTrue
\n", "
" ], "text/plain": [ " connector_id presynaptic_to \\\n", "437124 7265239 202541 \n", "437172 7265176 202541 \n", "474312 474311 203840 \n", "573904 573905 54753 \n", "1275576 2873212 203840 \n", "\n", " postsynaptic_to \\\n", "437124 [8680461, 16, 13782] \n", "437172 [30571, 40637, 16, 4205169] \n", "474312 [7911576, 8638, 16, 6483677] \n", "573904 [7634, 279469, 30358, 17238, 16, 597080, 39182... \n", "1275576 [595239, 16] \n", "\n", " postsynaptic_to_node x \\\n", "437124 [28458449, 1786, 22231380] 438062.0 \n", "437172 [6428950, 447292, 1819, 7265200] 437010.0 \n", "474312 [2736161, 474295, 124991, 21109788] 421574.0 \n", "573904 [573902, 1482460, 1670909, 2875768, 127266, 28... 434460.0 \n", "1275576 [2873213, 127687] 436480.0 \n", "\n", " y z \\\n", "437124 140370.0 203160.0 \n", "437172 141171.0 204400.0 \n", "474312 127771.0 194720.0 \n", "573904 165646.0 216160.0 \n", "1275576 147325.0 221360.0 \n", "\n", " url \\\n", "437124 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "437172 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "474312 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "573904 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "1275576 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "\n", " neuron_name has_soma \n", "437124 putative Gad1-F-600202 40613 AJ True \n", "437172 putative Gad1-F-600202 40613 AJ True \n", "474312 APL 203841 MR JSL True \n", "573904 neuron 54754 False \n", "1275576 APL 203841 MR JSL True " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cn_merged['url'] = pymaid.url_to_coordinates(cn_merged, stack_id=5)\n", "cn_merged['neuron_name'] = cn_merged.presynaptic_to.astype(str).map(pymaid.get_names(cn_merged.presynaptic_to.values))\n", "cn_merged['has_soma'] = cn_merged.presynaptic_to.map(pymaid.has_soma(cn_merged.presynaptic_to.values))\n", "\n", "cn_merged.head()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "OK, nice. Let's rearrange and get rid of superfluous columns:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
presynaptic_toneuron_namehas_somaconnector_idxyzurl
437124202541putative Gad1-F-600202 40613 AJTrue7265239438062.0140370.0203160.0https://neuropil.janelia.org/tracing/fafb/v14/...
437172202541putative Gad1-F-600202 40613 AJTrue7265176437010.0141171.0204400.0https://neuropil.janelia.org/tracing/fafb/v14/...
474312203840APL 203841 MR JSLTrue474311421574.0127771.0194720.0https://neuropil.janelia.org/tracing/fafb/v14/...
57390454753neuron 54754False573905434460.0165646.0216160.0https://neuropil.janelia.org/tracing/fafb/v14/...
1275576203840APL 203841 MR JSLTrue2873212436480.0147325.0221360.0https://neuropil.janelia.org/tracing/fafb/v14/...
\n", "
" ], "text/plain": [ " presynaptic_to neuron_name has_soma \\\n", "437124 202541 putative Gad1-F-600202 40613 AJ True \n", "437172 202541 putative Gad1-F-600202 40613 AJ True \n", "474312 203840 APL 203841 MR JSL True \n", "573904 54753 neuron 54754 False \n", "1275576 203840 APL 203841 MR JSL True \n", "\n", " connector_id x y z \\\n", "437124 7265239 438062.0 140370.0 203160.0 \n", "437172 7265176 437010.0 141171.0 204400.0 \n", "474312 474311 421574.0 127771.0 194720.0 \n", "573904 573905 434460.0 165646.0 216160.0 \n", "1275576 2873212 436480.0 147325.0 221360.0 \n", "\n", " url \n", "437124 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "437172 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "474312 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "573904 https://neuropil.janelia.org/tracing/fafb/v14/... \n", "1275576 https://neuropil.janelia.org/tracing/fafb/v14/... " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cn_merged = cn_merged[['presynaptic_to', 'neuron_name', 'has_soma', 'connector_id', 'x', 'y', 'z', 'url']]\n", "cn_merged.head()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Now you can randomize this table and start sampling:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "cn_merged.sample(frac=1).to_clipboard()" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "These are the relevant functions:\n", "\n", ".. autosummary::\n", " \n", " pymaid.url_to_coordinates\n", " pymaid.get_node_location" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }