In [1]:
import pymaid
import matplotlib.pyplot as plt
rm = pymaid.CatmaidInstance('server_url', 'api_token', 'http_user', 'http_password')
nl = pymaid.get_neurons('annotation:glomerulus DA1 right excitatory')
# Access single attribute: e.g. cable lengths [um]
nl.cable_length
Out[1]:
In [2]:
# .. or get a full summary as pandas DataFrame
df = nl.summary()
df.head()
Out[2]:
In [3]:
# Reroot a single neuron to its soma
nl[0].soma
Out[3]:
In [4]:
# .soma returns the treenode ID of the soma (if existing) and can be used to reroot
nl[0].reroot(nl[0].soma)
In [5]:
# You can also perform this operation on the entire CatmaidNeuronList
nl.reroot( nl.soma )
In [6]:
# Downsample by "skipping" N nodes (here: 10)
nl_downsampled = nl.downsample( 10, inplace=False )
# More elaborate: resample to given resolution in nanometers (here: 1000nm = 1um)
nl_resampled = nl.resample( 1000, inplace=False )
In [7]:
import matplotlib.pyplot as plt
# Plot an original neuron first
fig, ax = nl[0].plot2d(color='red')
# Shift the downsampled and resampled versions slightly and plot
n_ds = nl_downsampled[0].copy()
n_rs = nl_resampled[0].copy()
n_ds.nodes.x += 10000
n_rs.nodes.x += 20000
fig, ax = n_ds.plot2d(color='blue', ax=ax)
fig, ax = n_rs.plot2d(color='green', ax=ax)
plt.show()
In [8]:
# Cut a neuron in two using either a treenode ID or (in this case) a node tag
distal, proximal = pymaid.cut_neuron( nl[0], cut_node='SCHLEGEL_LH' )
# Plot neuron fragments
fig, ax = distal.plot2d(color='red', method='2d', connectors=False)
fig, ax = proximal.plot2d(color='blue', method='2d', connectors=False, ax=ax)
# Annotate cut point
cut_coords = distal.nodes.set_index('treenode_id').loc[ distal.root, ['x','y'] ].values[0]
ax.annotate('cut point', xy=(cut_coords[0], -cut_coords[1]),
xytext=(cut_coords[0], -cut_coords[1]-20000), va='center', ha='center',
arrowprops=dict(facecolor='black', shrink=0.01, width=1),
)
plt.show()
In [9]:
n = nl[0].prune_distal_to('SCHLEGEL_LH', inplace=False)
# Plot original neuron in black
fig, ax = nl[0].plot2d(color='black', method='2d', connectors=False, linestyle=(0, (5, 10)))
# Plot pruned neuron in red
fig, ax = n.plot2d(color='red', method='2d', connectors=False, ax=ax)
# Annotate cut point
ax.annotate('cut point', xy=(cut_coords[0], -cut_coords[1]),
xytext=(cut_coords[0], -cut_coords[1]-20000), va='center', ha='center',
arrowprops=dict(facecolor='black', shrink=0.01, width=1),
)
plt.show()
In [10]:
# To undo, simply reload the neuron from server
nl[0].reload()
In [11]:
# These operations can also be performed on a collection of neurons
n = nl[:5].prune_distal_to('SCHLEGEL_LH', inplace=False)
# Plot original neurons in black
fig, ax = nl[:5].plot2d(color='black', method='2d', connectors=False, linestyle=(0, (5, 10)))
# Plot pruned neurons in red
fig, ax = n.plot2d(color='red', method='2d', connectors=False, ax=ax)
# Annotate cut point
ax.annotate('cut point', xy=(cut_coords[0], -cut_coords[1]),
xytext=(cut_coords[0], -cut_coords[1]-20000), va='center', ha='center',
arrowprops=dict(facecolor='black', shrink=0.01, width=1),
)
plt.show()
In [12]:
# Again, let's undo
nl.reload()
nl.reroot(nl.soma)
In [13]:
# Something more sophisticated: pruning by strahler index
n = nl[:5].prune_by_strahler( to_prune = [1,2,3], inplace=False )
# Plot original neurons in black
fig, ax = nl[:5].plot2d(color='black', method='2d', connectors=False)
# Plot pruned neurons in red
fig, ax = n.plot2d(color='red', method='2d', connectors=False, ax=ax, linewidth=1.5)
# Annotate cut point
ax.annotate('cut point', xy=(cut_coords[0], -cut_coords[1]),
xytext=(cut_coords[0], -cut_coords[1]-20000), va='center', ha='center',
arrowprops=dict(facecolor='black', shrink=0.01, width=1),
)
plt.show()
In [14]:
# Again, let's undo
nl.reload()
In [15]:
# Get a volume
lh = pymaid.get_volume('LH_R')
# Prune by volume
nl_lh = nl.prune_by_volume(lh, inplace=False)
#nl_lh.summary().head()
In [16]:
# Set color of volume
lh['color'] = (250,250,250,.2)
# Plot neurons that have some cable left and the volume
fig, ax = pymaid.plot2d([nl_lh[nl_lh.cable_length > 10], lh],
method='3d',
connectors=False,
linewidth=2)
ax.dist=6
plt.show()