color or color_list procedures in PlotShape

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
roybens
Posts: 48
Joined: Fri Mar 14, 2008 7:57 am

color or color_list procedures in PlotShape

Post by roybens »

Hi,
I am trying to plot the morphology of a neuron with a different color for each section. I am trying to use the PlotShape (really nice it is working with matplotlib now).


The code i am running is:

Code: Select all

from neuron import h
from matplotlib import pyplot
mosinit_hoc = '/global/homes/r/roybens/Cori/NeuronInverter/NeuronStuff/hoc_templates/L5_TTPC1_cADpyr232_1/mosinit.hoc'

def plot_shape():
    h.load_file(mosinit_hoc)
    ps = h.PlotShape(False)  # False tells h.PlotShape not to use NEURON's gui
    #ps.color_list(h.cell.apical,3)
    for curr_sec in h.cell.apical:
        ps.color(curr_sec,4)
    ax = ps.plot(pyplot)
    pyplot.show()
ps = plot_shape()
I tried to change the order of ax = ps.plot(pyplot) and to use the color_list function as well (commented out)
But it seems like none of the sections are colored (although the sectionlist is not empty)
What am i doing wrong?
Thanks!

ramcdougal
Posts: 214
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: color or color_list procedures in PlotShape

Post by ramcdougal »

Unfortunately the .plot for both matplotlib and plotly (available in 8.0 development version) does not currently support the .color or .color_list values and instead always color based on the value of some variable (v, by default).

(Note that you can use .mark as in the documentation to highlight specific locations, but I gather you want to highlight regions instead.)

One solution is of course to temporarily change the value of some variable (e.g. v) to be e.g. high in the apical dendrites and low elsewhere.

There is an undocumented method, _do_plot, of the .plot return objects that can help (matplotlib only for now, plotly works slightly differently). It requires 4 arguments: a low value, a high value, a list of sections, a variable to use for color coding -- or None -- and passes any keyword arguments to matplotlib. Thus, for example, if you've already plotted the whole shapeplot, you could color the h.apic sections red with:

Code: Select all

ps._do_plot(0, 1, h.apic, None, color='red')
(0 and 1 are the low and high values, respectively, but they're ignored since the variable is None.)

Here's a full working example and output:

Code: Select all

import urllib.request
from matplotlib import pyplot
from matplotlib.colors import ListedColormap
from neuron import h

# load a morphology from the web
with urllib.request.urlopen(
    "https://senselab.med.yale.edu/modeldb/getModelFile?model=87284&AttrID=23&s=yes&file=/%2fCA1_abeta%2fc91662.ses"
) as response:
    with open('c91662.ses', 'wb') as f:
        f.write(response.read())

# load the morphology into NEURON
h.load_file('c91662.ses')

# get matplotlib plotshape with default coloring (for v)
ps = h.PlotShape(False).plot(pyplot)

# now go back and highlight the apics
ps._do_plot(0, 1, h.apic, None, color='red')

pyplot.show()
Image

roybens
Posts: 48
Joined: Fri Mar 14, 2008 7:57 am

Re: color or color_list procedures in PlotShape

Post by roybens »

This is very helpful,
I will try both methods, and report back if I get into any problems.
Thanks ramcdougal!

roybens
Posts: 48
Joined: Fri Mar 14, 2008 7:57 am

Re: color or color_list procedures in PlotShape

Post by roybens »

Hi ramcdougal,
I am probably doing something wrong or there is a problem in my environment (though for any other purpose except this it is working totally fine)
So i tried the code you sent on both my local machine (running windows NEURON 7.7) and i get the following error:

Code: Select all

runfile('E:/GitHub/MoreGits/Cori/NeuronInverter/NeuronStuff/plot_morpho.py', wdir='E:/GitHub/MoreGits/Cori/NeuronInverter/NeuronStuff')
Reloaded modules: neuron, neuron.hoc37, neuron.hclass3, neuron.psection, neuron.gui2, neuron.gui2.plotshape, neuron.gui2.utilities
Traceback (most recent call last):

  File "E:\GitHub\MoreGits\Cori\NeuronInverter\NeuronStuff\plot_morpho.py", line 25, in <module>
    ps._do_plot(0, 1, h.apic, None, color='red')

  File "c:\nrn\lib\python\neuron\__init__.py", line 756, in _do_plot
    lines[line] = '%s at %s' % (val, seg)

UnboundLocalError: local variable 'val' referenced before assignment
When i am running this on our cluster through docker i get the following (the image is working very well for every other NEURON application)

Code: Select all

$ python3 plot_morpho.py
Traceback (most recent call last):
  File "plot_morpho.py", line 17, in <module>
    ps = h.PlotShape(False).plot(pyplot)
  File "/project/projectdirs/m2043/roybens/neuron7_7/nrn/lib/python/neuron/__init__.py", line 796, in __call__
    return _do_plot_on_matplotlib_figure(fig)
  File "/project/projectdirs/m2043/roybens/neuron7_7/nrn/lib/python/neuron/__init__.py", line 778, in _do_plot_on_matplotlib_figure
    _lines = result._do_plot(lo, hi, secs, variable, *args, **kwargs)
  File "/project/projectdirs/m2043/roybens/neuron7_7/nrn/lib/python/neuron/__init__.py", line 764, in _do_plot
    col = cmap(int(255 * (val - val_min) / (val_range)))
  File "/global/homes/r/roybens/.local/lib/python3.6/site-packages/matplotlib/colors.py", line 561, in __call__
    rgba = lut.take(xa, axis=0, mode='clip')
TypeError: Cannot cast array data from dtype('O') to dtype('int64') according to the rule 'safe'
I assume I am missing something any ideas?

ramcdougal
Posts: 214
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: color or color_list procedures in PlotShape

Post by ramcdougal »

Hmmm... looks like that example depends on a change introduced about a month ago... so it's available in the "pip install" version for mac/linux or the windows nightly build. (Don't try to pip install on Windows, that will give a VERY old version of NEURON.)

Sorry about that. So I guess the best solution for 7.7 is to set an indicator value to different values in the different regions and choose a colormap that displays the difference clearly.

If you want to use something like the above in 7.7... I guess you could borrow the code for _PlotShapePlot and use that instead of doing an h.PlotShape.plot

roybens
Posts: 48
Joined: Fri Mar 14, 2008 7:57 am

Re: color or color_list procedures in PlotShape

Post by roybens »

Thanks so much!
This is very helpful.

Post Reply