dbbs-lab / bsb-core

The Brain Scaffold Builder
https://bsb.readthedocs.io
GNU General Public License v3.0
21 stars 16 forks source link

MorphologyDependencyNode tags values can be casted both as str or list of str. #737

Closed drodarie closed 1 year ago

drodarie commented 1 year ago

On branch main, MorphologyDependencyNode tags is a dictionary attribute:

tags = config.attr(type=types.dict(type=types.or_(str, types.list(str))))

The problem is that a string can be casted as a list of string and a list of string can be casted into a string and the or function takes the first cast that works. So for instance with the following python script:

from bsb.core import Configuration

tags={1: 'soma', 16: ['axon', 'AIS'], 17: ['axon', 'AIS', 'K'], 18: ['axon', 'axonmyelin'], 19: ['axon', 'nodes'], 20: ['dendrites', 'basal_dendrites'], 21: ['dendrites', 'pf_targets'], 22: ['dendrites', 'aa_targets']}

cfg= Configuration.default(
    name="Declive",
    storage=dict(engine="hdf5", root="network.hdf5"),
    morphologies=[
        {
            "file": "PurkinjeCell.swc",
            "tags": tags,
        },
    ],
)
print(cfg.morphologies[0].tags)

We obtain:

{1: 'soma', 16: "['axon', 'AIS']", 17: "['axon', 'AIS', 'K']", 18: "['axon', 'axonmyelin']", 19: "['axon', 'nodes']", 20: "['dendrites', 'basal_dendrites']", 21: "['dendrites', 'pf_targets']", 22: "['dendrites', 'aa_targets']"}

I don't know how to make sure that lists are treated in priority as lists and strings as strings.

Helveg commented 1 year ago

We could add a safe flag that checks the type before trying to cast it, like numpy does

Helveg commented 1 year ago

@drodarie you have a branch ready with a fix, but no PR is open. Did you forget it?

drodarie commented 1 year ago

No the tests do not pass and I am still trying to fix it before creating the pull request.