Value error when drawing morphology of a downloaded spatial neuron

Description of problem
Hi everyone. I tried to use Morphology.from_file to load a neuron morphology file from NeuroMorpho.Org, and call plot_morphology to draw it. However, except for the neuron named H16-06-013-05-06-02_576010060_m_DendriteAxon.CNG.swc, when plotted other neurons such as 485184849_reconstruction.CNG.swc, a value error raised just like below.

Minimal code to reproduce problem

from brian2 import *
from brian2tools import*
from mayavi import*

morpho = Morphology.from_file('neuromorpho//allen cell types bascket//CNG version//485184849_reconstruction.CNG.swc')

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

What you have aready tried
I checked the format of these 2 files but found no difference. The line reported the error in Morphology was also checked but it seemed to be normal as well. It was not the problem with file path either.

Expected output (if relevant)

Actual output (if relevant)

Full traceback of error (if relevant)

File "D:\python\envs\test\lib\site-packages\brian2\spatialneuron\morphology.py", line 1082, in _replace_three_point_soma
    raise ValueError(f"Cannot replace '3-point-soma' by a single "
ValueError: Unknown format code 'd' for object of type 'float'

Hi @czx. Thanks for testing out all these tools, they don’t get used by that many people, so bugs don’t get caught as early as for other parts of the code. The issue you are seeing is only partially Brian’s fault. SWC files on Neuromorpho.org can have different formats for somas, and in the standardized format they convert all of these formats into the “Three-point soma representation” (see https://neuromorpho.org/SomaFormat.html). Briefly, this means that in the SWC file a soma is always represented as two cylinders, and all other compartments (dendrites, axon) connect to the connection point in the middle of these compartments. Without going into the details: Brian supports a Soma as a special case, and therefore does not need the workaround as two cylinders. On loading an swc file with the three-point format, it tries to convert it into a “normal”, spherical soma.

Now, for the failing swc file that you mentioned above, you should be getting the following error message:

ValueError: Cannot replace '3-point-soma' by a single point, the second and third points should be positioned one radius away from the first point. Distances are 6.701um and 6.689um, respectively, while the radius is 6.704um.

The reason why you are getting a different error is that there is a bug in the code for the error message itself… I will fix this right away, it is a trivial fix.

As the error message states, the SWC file does not correctly use the three-point-format. In the file, the three soma points are:

1 0 0 0 6.704 -1
2 1 1.95 6.39 0.52 6.704 1
3 1 -1.94 -6.38 -0.52 6.704 1

This is not correct, since the endpoints of the two cylinders should both be exactly one radius away from the center, but as you can see their distances are slightly different. This is probably due to some rounding errors in the conversion tool on the neuromorpho.org website. Brian accepts some error, but not as much – we should probably be less strict, though, since as you noticed many files on neuromorpho.org have this issue. To fix this issue, the easiest solution is to delete the second and third line (i.e. the lines starting with 2 and 3 respectively), this will be directly interpreted as a spherical soma by Brian. Alternatively, you can replace it by something like

1 1 0 0 0 6.704 -1
2 1 1.95 6.39 0.52 6.704 1
3 1 -1.95 -6.39 -0.52 6.704 1

which will also work – both should give you exactly the same morphology in Brian.

Hope that clears things up a bit !

Thanks for your detailed reply ! I tried both of the modifying method but it still didn’t work…

File "D:\python\envs\test\lib\site-packages\brian2\spatialneuron\morphology.py", line 1082, in _replace_three_point_soma
    raise ValueError(f"Cannot replace '3-point-soma' by a single "
ValueError: Unknown format code 'd' for object of type 'float'

Does that bug still exist? I’m a new hand in computational neuroscience, thus I may encounter many issues that trouble you to help with :sweat_smile:

Hi @czx. Could you double-check that you loaded the correct file, etc.? I tried both variants, and they worked. In case it wasn’t clear, the bug in Brian I was mentioning above is just that the error message is not displayed correctly (as in your case). The corrected files load without error message and are therefore not affected by the bug. Here are the two manually corrected files for reference:
485184849_reconstruction.CNG-fixed1.swc (400.2 KB)
485184849_reconstruction.CNG-fixed3.swc (400.2 KB)

Hi @mstimberg. I double checked my code and I’m sure that the file was loaded correctly. The corrected files sent by you worked normally. However, although I replaced the content in the original file with the corrected one and loaded it, the error still raised. The only difference between them was just their names. Thus I’m still confused about it.

Um, I’m as confused as you are…

That’s all right, I’ll try it again. Thanks for your patience and suggenstion!

Hi again @czx . Regardless of the weird issue with the files you were seeing earlier, I have just added a few changes to Brian that should make the SWC parsing more robust and accept most (all?) files from neuromorpho.org. It also fixes the bug in the error message.

I will do a new “proper” release soon that will include the fix. Until then, you might want to test it directly with the development version, which you can install from the pypi test server like this:

pip install -i https://test.pypi.org/simple/ Brian2==2.7.0.post12

This requires that the dependencies are already installed. If you do the install from scratch (e.g. in a new environment), you’d have to add

--extra-index-url https://pypi.org/simple

to the above command.

1 Like

Hi @mstimberg. Thank you for sharing the development version. The weird issue was a mistake on my part, and let’s ignore it. I tried to install the development version by Anaconda in a new environment , using the command

pip install -i https://test.pypi.org/simple/ Brian2==2.7.0.post12--extra-index-url https://pypi.org/simple

However, the package seemed cannot be unpacked just like below:
image
I tried several python versions from 3.10 to 3.12 but still failed. Is there any other dependencies needed to be installed (which I didn’t see in the installation guide of Brian)? Or is it due to any other problem?

Hi @czx. There is a space missing in your command between the Brian version and the --extra-index-url. The command should be

pip install -i https://test.pypi.org/simple/ Brian2==2.7.0.post12 --extra-index-url https://pypi.org/simple

Apologies if that wasn’t clear from my earlier comment!

1 Like

Hi @mstimberg. Now it successfully works. I should have been more careful when using the command…Anyway thank you very much for your sharing and patience!

1 Like

These changes are now part of the regular 2.7.1 release: Release notes — Brian 2 2.7.1 documentation