Page 1 of 1

Problem with h.PlotShape

Posted: Tue Jun 25, 2024 5:07 pm
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.

Re: Problem with h.PlotShape

Posted: Wed Jun 26, 2024 1:41 pm
by ted
Instead of matplotlib and pyplot, try plotly.

Code: Select all

import plotly
 . . .
ps = h.PlotShape(False)
ps.plot(plotly).show()

Re: Problem with h.PlotShape

Posted: Sat Jun 29, 2024 9:41 pm
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.

Re: Problem with h.PlotShape

Posted: Mon Jul 01, 2024 1:47 pm
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?

Re: Problem with h.PlotShape

Posted: Mon Jul 01, 2024 3:07 pm
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?

Re: Problem with h.PlotShape

Posted: Mon Jul 01, 2024 3:12 pm
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.

Re: Problem with h.PlotShape

Posted: Wed Jul 03, 2024 9:56 pm
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.

Re: Problem with h.PlotShape

Posted: Tue Jul 16, 2024 3:25 am
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,