# Value error when drawing morphology of a downloaded spatial neuron

**URL:** <https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238>\
**Category:** Support\
**Tags:** multicompartment, morphology, swc\
**Created:** [14 June 2024 09:39 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238 "2024-06-14T09:39:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![czx](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@czx](https://brian.discourse.group/u/czx)\
**Post date:** [14 June 2024 09:39 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/1 "2024-06-14T09:39:19Z")

</div>

Description of problem  
Hi everyone. I tried to use `Morphology.from_file` to load a neuron morphology file from _[NeuroMorpho.Org](http://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

```auto
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)

```auto
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'

```

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [14 June 2024 14:05 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/2 "2024-06-14T14:05:47Z")

</div>

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](http://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](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:

```pytb
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:

```auto
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](http://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](http://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

```auto
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 !

---

<div class="post-metadata">

**Author:** ![czx](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@czx](https://brian.discourse.group/u/czx)\
**Post date:** [15 June 2024 06:33 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/3 "2024-06-15T06:33:45Z")

</div>

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

```auto
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 😅

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [17 June 2024 08:05 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/4 "2024-06-17T08:05:04Z")

</div>

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](https://brian.discourse.group/uploads/short-url/8HHAGneV7CUYqBby19hFYkcSF0M.swc) (400.2 KB)  
[485184849\_reconstruction.CNG-fixed3.swc](https://brian.discourse.group/uploads/short-url/zCZw4h7oCfWcfVODYTtwBFBr4PL.swc) (400.2 KB)

---

<div class="post-metadata">

**Author:** ![czx](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@czx](https://brian.discourse.group/u/czx)\
**Post date:** [17 June 2024 08:59 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/5 "2024-06-17T08:59:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [17 June 2024 16:52 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/6 "2024-06-17T16:52:33Z")

</div>

Um, I’m as confused as you are…

---

<div class="post-metadata">

**Author:** ![czx](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@czx](https://brian.discourse.group/u/czx)\
**Post date:** [18 June 2024 01:48 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/7 "2024-06-18T01:48:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [25 June 2024 15:26 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/8 "2024-06-25T15:26:17Z")

</div>

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](http://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:

```auto
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

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

```

to the above command.

---

<div class="post-metadata">

**Author:** ![czx](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@czx](https://brian.discourse.group/u/czx)\
**Post date:** [26 June 2024 08:28 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/9 "2024-06-26T08:28:11Z")

</div>

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

```auto
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](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/98d18696a940f6d5a4c433719742c71a1069e871.png)  
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?

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [26 June 2024 08:32 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/10 "2024-06-26T08:32:56Z")

</div>

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![czx](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@czx](https://brian.discourse.group/u/czx)\
**Post date:** [26 June 2024 08:39 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/11 "2024-06-26T08:39:34Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [2 July 2024 14:53 UTC](https://brian.discourse.group/t/value-error-when-drawing-morphology-of-a-downloaded-spatial-neuron/1238/12 "2024-07-02T14:53:35Z")

</div>

These changes are now part of the regular 2.7.1 release: [Release notes — Brian 2 2.7.1 documentation](https://brian2.readthedocs.io/en/stable/introduction/release_notes.html)
