BlueBrain / TMD

A python package for the topological analysis of neurons.
https://tmd.readthedocs.io/en/latest/
Other
28 stars 21 forks source link

KeyError for a neuron while computing persistence diagram #61

Open dokato opened 2 years ago

dokato commented 2 years ago

When calling get_persistence_diagram on my neuron ( swc file attached) that looks like this:

Screenshot 2022-11-02 at 17 18 16

I get the following error:

In [18]: perd = tmd.methods.get_persistence_diagram(neuron.neurites[0])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [18], in <cell line: 1>()
----> 1 perd = tmd.methods.get_persistence_diagram(neuron.neurites[0])

File ~/anaconda3/envs/testnavis/lib/python3.9/site-packages/tmd/Topology/methods.py:102, in get_persistence_diagram(tree, feature, **kwargs)
    100 def get_persistence_diagram(tree, feature='radial_distances', **kwargs):
    101     '''Method to extract ph from tree that contains mutlifurcations'''
--> 102     ph, _ = tree_to_property_barcode(
    103         tree,
    104         filtration_function=_filtration_function(feature, **kwargs),
    105         property_class=NoProperty
    106     )
    107     return ph

File ~/anaconda3/envs/testnavis/lib/python3.9/site-packages/tmd/Topology/methods.py:62, in tree_to_property_barcode(tree, filtration_function, property_class)
     59 while len(alives) > 1:
     60     for alive in alives:
---> 62         p = parents[alive]
     63         c = children[p]
     65         if np.alltrue(active[c]):
KeyError: 15

In fact this is a neuron exported from the navis library.

> import navis
> n = navis.example_neurons(1)
> navis.write_swc(n,'nrn.swc')

nrn.swc.zip

lidakanari commented 2 years ago

Hi @dokato,

I'm not sure if that is causing your issue, but I noticed a few things that are unexpected in the file. I assume this structure is a neuron. However the section labels (i.e. the second row of section types) are not what I would expect. For example you have some parts of the tree labeled as soma, most nodes are undefined (0) and some are bifurcations etc. In order to make your file work with the software I suggest you make the types as per the expected swc standard: soma:1, axon:2, basal:3, apical:4. There is no need to label bifurcations, terminations, the software will recognize these automatically. I would propose that you fix this in your file and then you can try again the process. I am checking the connectivity of your nodes in the meantime to see if there are other issues that could be improved.

Best, Lida

dokato commented 2 years ago

Thank you @lidakanari for you reply. As you might have noticed I didn't create this swc file myself, I exported that from the other python library. Do you suggest that there is an issue with the data format of navis exporter then?

lidakanari commented 2 years ago

Hi @dokato, I'm not very familiar with the exporter, but I looked at the example data they provide and they all have the same issue with the section types, for example:

https://github.com/navis-org/navis/blob/master/navis/data/swc/1734350788.swc

So I don't think the issue is the writer, rather the examples are not in the expected format.

Maybe you can download one cell from neuromorpho.org, in which data are more standardized. If you just want an example cell to try things on this might be a good start. Otherwise, you can work on the specific file and fix the section types manually.