This notebook demonstrates the usage of nat
, elmr
or catnat
in Python. Pymaid
offers a few wrappers to use specific functions right away but more importantly it converts between R and Python data so that you can use pretty much any function without much effort.
For example, you could:
In [2]:
import pymaid
from pymaid import rmaid
# Omit http user and password if not required
remote_instance = pymaid.CatmaidInstance('server_url', 'api_token', 'http_user', 'http_password')
print('Tested with pymaid version {0}'.format(pymaid.__version__))
pymaid.rmaid
provides a function, data2py
, to convert general R data to Python data. More importantly, there are two functions, neuron2py
and neuron2r
, that allow you to convert back and forth between R and Python objects.
In [5]:
# Load a neuron using PyMaid
neuron_py = pymaid.get_neuron(16, remote_instance = remote_instance )
In [7]:
neuron_py
Out[7]:
In [8]:
#Convert to R
neuron_r = rmaid.neuron2r( neuron_py )
neuron_r
Out[8]:
Let's try it the other way around. Rmaid
offers a wrapper to initialise R's catmaid library:
In [9]:
# Here we use credentials stored in the alread existing <remote_instance> but we could also just pass those by hand
# see help(rmaid.init_rcatmaid)
rcatmaid = rmaid.init_rcatmaid( remote_instance = remote_instance )
rcatmaid
behaves pretty much like a normal Python module such as pymaid
:
In [10]:
#Check which functions are available
dir(rcatmaid)
Out[10]:
Note that '.' (dots) from R have been replaced with '_' (underscore). For example read.neurons.catmaid
is in Python read_neurons_catmaid
:
In [11]:
neuron_rcatmaid = rcatmaid.read_neuron_catmaid(16)
neuron_rcatmaid
Out[11]:
In [12]:
#Convert to Python
neuron_conv = rmaid.neuron2py(neuron_rcatmaid)
neuron_conv
Out[12]:
Note how neuron_name
is NA
? Some data that is stored in Python neurons does not exists in R and vice versa. Nodes, skeleton_id, connectors and tags, however, are always preserved.
In [1]:
from rpy2.robjects.packages import importr
In [3]:
nat = importr('nat')
# Look at the last 20 functions
dir(nat)[-20:]
Out[3]:
Let us use some basic nat
function like prune_strahler
In [15]:
pruned = nat.prune_strahler( neuron_r )
In [16]:
pruned_py = rmaid.neuron2py( pruned )
pruned_py
Out[16]:
Now we will plot the pruned vs non-pruned neuron.
Please note that the cell with the 3D plot will be empty when you first open this notebook. You've got to run the code!
In [17]:
import plotly.offline as pyoff
#Need to initialize plotly for interactive rendering with Jupyter notebooks
from plotly.offline import init_notebook_mode
init_notebook_mode(connected=True)
In [20]:
fig = pymaid.plot3d( [neuron_py,pruned_py], backend='plotly', width=1000 )
pyoff.iplot(fig)
In [ ]: