How to plot the 3D morphology of a neuron using 'plot_morphology()'

Description of problem
Hi everyone. I tried to see the 3D morphology of a pyramid cell downloaded from NeuroMorpho.Org. I used plot_morphology with plot_3d=False and successfully drew a 2D morphology. But when I altered False to True, an error returned, see in Traceback. I have no idea about it since I also import the mayavi package as suggested in User’s guide.

Minimal code to reproduce problem

from brian2 import *
from brian2tools import*
from mayavi import*
morpho = Morphology.from_file('neuromorpho//allen cell types//CNG version//H16-06-013-05-06-02_576010060_m_DendriteAxon.CNG.swc')

figure()
plot_morphology(morpho, plot_3d=True, show_compartments= True, show_diameter=True)
show()

What you have aready tried
Checked any package was imported if necessary. Altered plot_3d=False to True to see the 3D image but failed.

Expected output (if relevant)
A 3D morphology should be like this (in User’s guide)

Actual output (if relevant)
A 2D morphology is available, rather than 3D.

Full traceback of error (if relevant)

Traceback (most recent call last):
  File "d:\一些文档啥的\2023学年\chengxu\pyramid.py", line 9, in <module>
    plot_morphology(morpho, plot_3d=True, show_compartments= True, show_diameter=True)
  File "D:\python\envs\brain\Lib\site-packages\brian2tools\plotting\morphology.py", line 304, in plot_morphology
    import mayavi.mlab as mayavi
  File "D:\python\envs\brain\Lib\site-packages\mayavi\mlab.py", line 15, in <module>
    from mayavi.core.common import process_ui_events
  File "D:\python\envs\brain\Lib\site-packages\mayavi\core\common.py", line 21, in <module>
    from pyface import api as pyface
  File "D:\python\envs\brain\Lib\site-packages\pyface\api.py", line 13, in <module>
    from .about_dialog import AboutDialog
  File "D:\python\envs\brain\Lib\site-packages\pyface\about_dialog.py", line 15, in <module>
    from .toolkit import toolkit_object
  File "D:\python\envs\brain\Lib\site-packages\pyface\toolkit.py", line 23, in <module>
    toolkit = toolkit_object = find_toolkit("pyface.toolkits")
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\python\envs\brain\Lib\site-packages\pyface\base_toolkit.py", line 285, in find_toolkit
    plugin for plugin in importlib_metadata.entry_points()[entry_point]
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "D:\python\envs\brain\Lib\importlib\metadata\__init__.py", line 287, in __getitem__
    raise KeyError(name)
KeyError: 'pyface.toolkits'

Hi @czx. I’ve never seen this error, but it looks like a problem in the mayavi installation. Can you run mayavi2 (cf. Installation — mayavi 4.8.2 documentation)?

I tried typing ‘matavi2’ in the cmd window as described in that documentation, and a similar error was returned, as follows. Should I reinstall mayavi ?

Traceback (most recent call last):
  File "D:\python\envs\brain\Lib\importlib\metadata\__init__.py", line 285, in __getitem__
    return next(iter(self.select(name=name)))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\envs\brain\Scripts\mayavi2-script.py", line 6, in <module>
    from mayavi.scripts.mayavi2 import main
  File "D:\python\envs\brain\Lib\site-packages\mayavi\scripts\mayavi2.py", line 462, in <module>
    from mayavi.plugins.app import Mayavi, setup_logger
  File "D:\python\envs\brain\Lib\site-packages\mayavi\plugins\app.py", line 19, in <module>
    from .mayavi_workbench_application import MayaviWorkbenchApplication
  File "D:\python\envs\brain\Lib\site-packages\mayavi\plugins\mayavi_workbench_application.py", line 13, in <module>
    from envisage.ui.workbench.api import WorkbenchApplication
  File "D:\python\envs\brain\Lib\site-packages\envisage\ui\workbench\api.py", line 13, in <module>
    from .workbench import Workbench
  File "D:\python\envs\brain\Lib\site-packages\envisage\ui\workbench\workbench.py", line 14, in <module>
    import pyface.workbench.api as pyface
  File "D:\python\envs\brain\Lib\site-packages\pyface\workbench\api.py", line 13, in <module>
    from .editor import Editor
  File "D:\python\envs\brain\Lib\site-packages\pyface\workbench\editor.py", line 15, in <module>
    from pyface.toolkit import toolkit_object
  File "D:\python\envs\brain\Lib\site-packages\pyface\toolkit.py", line 23, in <module>
    toolkit = toolkit_object = find_toolkit("pyface.toolkits")
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\python\envs\brain\Lib\site-packages\pyface\base_toolkit.py", line 285, in find_toolkit
    plugin for plugin in importlib_metadata.entry_points()[entry_point]
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "D:\python\envs\brain\Lib\importlib\metadata\__init__.py", line 287, in __getitem__
    raise KeyError(name)
KeyError: 'pyface.toolkits'

Unfortunately, I cannot help much with this. Did you follow the installation instructions on Installation — mayavi 4.8.2 documentation ?

1 Like

I degraded my Python version from 3.12 to 3.7, and reinstalled mayavi according to the documentation. Now the problem seems to be solved. I think it’s a version mismatch. Thank you!

Happy to hear you solved that issue. Just be aware that Python 3.7 is very old (or was that a typo?) – e.g. the most recent version of Brian2 requires Python 3.10.