Problem with h.PlotShape

Using the graphical user interface to build and exercise models. Includes customizing the GUI by writing a little bit of hoc or Python
Post Reply
VENOM
Posts: 6
Joined: Tue Jun 18, 2024 12:02 pm

Problem with h.PlotShape

Post by VENOM »

Hi NEURON community:
I am experiencing issues with h.PlotShape. Following the tutorial https://nrn.readthedocs.io/en/8.2.4/tut ... ick-1.html
I wrote this code on Ubuntu 22.04 python 3.10.12 and NEURON 8.2.4 (in a virtual environment):

Code: Select all

from neuron import h
from neuron.units import ms, mV, µm
h.load_file("stdrun.hoc")

class BallAndStick:
    def __init__(self, gid):
        self._gid = gid # identifier gid
        self.soma = h.Section(name="soma", cell=self)
        self.dend = h.Section(name="dend", cell=self)
        self.dend.connect(self.soma) # connect the sections
        # different from connect soma to dend; instead it means dendrite begins where soma ends
        self.soma.L = self.soma.diam = 12.6157 * µm
        self.dend.L = 200 * µm
        self.dend.diam = 1 * µm
    def __repr__(self):
        return "BallAndStick[{}]".format(self._gid)

my_cell = BallAndStick(0)
h.topology()
my_cell.soma(0.5).area() # only returns areas of segments. since there is only one segment, this is whole area

import matplotlib
matplotlib.use('QT5Agg')  # Use 'Qt5Agg' or another backend if preferred
import matplotlib.pyplot as plt

h.PlotShape(False).plot(plt)
However, no plot was shown. Even if I force the plot to "show" using

Code: Select all

plt.show()
it was just a blank canvas with nothing on it.

Before using Linux, I was on Anaconda Windows11 Python 3.11, NEURON 8.2.4; the code and error message was exactly same as viewtopic.php?t=4449
I reinstalled Windows, reset the whole system, checked my hardware (Windows Memory Diagnostic), but the error persists.

It would be much appreciated if anyone could help. I will provide any detail needed to solve this problem and test all suggested codes you provide.
ted
Site Admin
Posts: 6353
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Problem with h.PlotShape

Post by ted »

Instead of matplotlib and pyplot, try plotly.

Code: Select all

import plotly
 . . .
ps = h.PlotShape(False)
ps.plot(plotly).show()
VENOM
Posts: 6
Joined: Tue Jun 18, 2024 12:02 pm

Re: Problem with h.PlotShape

Post by VENOM »

Hi Ted:

I tried, here is the code

Code: Select all

from neuron import h
from neuron.units import ms, mV, µm
h.load_file("stdrun.hoc")
import plotly

class BallAndStick:
    def __init__ (self, gid):
        self._gid = gid # identifier gid
        self.soma = h.Section(name="soma", cell=self)
        self.dend = h.Section(name="dend", cell=self)
        self.dend.connect(self.soma) # connect the sections
        # different from connect soma to dend; instead it means dendrite begins where soma ends
        self.soma.L = self.soma.diam = 12.6157 * µm
        self.dend.L = 200 * µm
        self.dend.diam = 1 * µm
    def __repr__(self):
        return "BallAndStick[{}]".format(self._gid)

my_cell = BallAndStick (0)
h.topology()
my_cell.soma(0.5).area()

ps = h.PlotShape(False)
ps.plot(plotly).show()
Again I did not see any display. Could it be Linux is not GUI based and I should enable something?
In addition, first time I ran it it gave me error message

Code: Select all

Traceback (most recent call last):
  File "/home/venom/BasicCell.py", line 24, in <module>
    ps.plot(plotly).show()
  File "/home/venom/NEURON/lib/python3.10/site-packages/neuron/__init__.py", line 1239, in __call__
    return _do_plot_on_plotly()
  File "/home/venom/NEURON/lib/python3.10/site-packages/neuron/__init__.py", line 1196, in _do_plot_on_plotly
    from matplotlib.pyplot import cm
ModuleNotFoundError: No module named 'matplotlib'
So I guess it is still dependent on matplotlib.

Also an error message

Code: Select all

Traceback (most recent call last):
  File "/home/venom/BasicCell.py", line 24, in <module>
    ps.plot(plotly).show()
  File "/home/venom/NEURON/lib/python3.10/site-packages/neuron/__init__.py", line 1239, in __call__
    return _do_plot_on_plotly()
  File "/home/venom/NEURON/lib/python3.10/site-packages/neuron/__init__.py", line 1232, in _do_plot_on_plotly
    return FigureWidgetWithNEURON(data=data, layout={"showlegend": False})
  File "/home/venom/NEURON/lib/python3.10/site-packages/plotly/missing_ipywidgets.py", line 13, in __init__
    raise ImportError(
ImportError: Please install ipywidgets>=7.0.0 to use the FigureWidget class
Which was dealt with

Code: Select all

pip install ipywidgets --upgrade
Not sure if they are relevant.
ted
Site Admin
Posts: 6353
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Problem with h.PlotShape

Post by ted »

Could it be Linux is not GUI based
?? I think you'd know by now if the OS and hardware on the machine on which you're running NEURON have GUI support. Are you executing NEURON on a laptop or desktop machine, or on some remote server? Assuming NOT the latter, what happens if you execute

neurondemo

in a terminal at the system prompt?
VENOM
Posts: 6
Joined: Tue Jun 18, 2024 12:02 pm

Re: Problem with h.PlotShape

Post by VENOM »

I know my Ubuntu can show plots.

In the first post, I used

Code: Select all

import matplotlib
matplotlib.use('QT5Agg')
For my code to display plot.

I was asking if your alternative method need anything like this. If not, the alternative code you suggested also does not work for me. Sorry for the confusion.

There is something interesting when I tried the same code on python3.8 and NEURON 8.0.1 (with both plt and plotly with compatible version). Both plt and plotly reported "segment fault". Does it provide any further information for debugging?
VENOM
Posts: 6
Joined: Tue Jun 18, 2024 12:02 pm

Re: Problem with h.PlotShape

Post by VENOM »

And I was able to implement this tutorial's code with the final plotting:
https://nrn.readthedocs.io/en/8.2.4/tut ... asics.html
So I assume the problem is not my system not displaying graph or matplotlib malfunctional.
VENOM
Posts: 6
Joined: Tue Jun 18, 2024 12:02 pm

Re: Problem with h.PlotShape

Post by VENOM »

Actually, I ignored the PlotShape function and ran the ring network tutorial and suddenly everything worked out fine. I don't know what happened, but it is worth a try.
tletutour
Posts: 1
Joined: Wed May 11, 2022 7:41 am

Re: Problem with h.PlotShape

Post by tletutour »

Hello,
I am also on Windows 11 and Neuron 8.2.2 and was using Python 3.11. h.PlotShape() seems to work for me when downgrading to Python 3.8. Curious to know if anyone can replicate this behavior.
Best,
Post Reply