dbbs-lab / bsb-core

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

ERROR: 'NestAdapter' object has no attribute 'is_prepared' #738

Closed LorenzoDeToni closed 10 months ago

LorenzoDeToni commented 1 year ago

I'm trying to create a new network, I wrote the .json file. In Python I write:

I expected to get the simulation of the network but I stumbled in an error like is reported in the image. photo

Helveg commented 1 year ago

Hello and welcome Lorenzo, what version of the BSB are you using? Could you try installing bsb==4.0.0a55?

LorenzoDeToni commented 1 year ago

Hi, thank you for your answer, I already have that version.

Helveg commented 1 year ago

Oh, my bad, I see that the NEST simulation code hasn't been released yet. If you are interested in this feature, please install the BSB from the git repository:

git clone git@github.com:dbbs-lab/bsb
cd bsb
git checkout feature/nest3
pip install -e .

If you need any further help please let me know (much of the documentation is still missing)

LorenzoDeToni commented 1 year ago

Hi, sorry ho bother, am I doing something wrong with the repository cloning o do I need some kind of permission? 1 I am able to clone it with https, but in the end it gives me an error.

2 3

4

Thank you for your help!

Helveg commented 1 year ago

Sorry, I missed your last reply! Is this still an issue? It seems like your Python environment is unable to write to some folders, you should create a virtual environment, this will solve your immediate problem. An alternative is to use pip install . instead, keep in mind that you would then have to repeat this command every time you change the git source code.

LorenzoDeToni commented 11 months ago

Hi Robin, let's start with a good new, with bsb4.0.0a55 and nest3.4 I'm currently able to run bsb till the scaffold generation. However I have another issue when I try to run the simulation. image image image

It's seem like bsb has problem when calling nest features like neuron parameters and recording devices. Do you have any suggestion? I can't find any material on the bsb website related to this issue. Thanks in advances, best whises. Lorenzo

Helveg commented 11 months ago

Hi Lorenzo, good to hear there's some progress! Could you post this error as code formatted text? It is hard to work with these images. Post both the full code (and configuration), and full error log as code blocks and I will take a look!

From what I can tell so far it seems to be a bug that should be easy to reproduce :)

I can't find any material on the bsb website related to this issue.

Yes, sorry, the documentation is still missing, but starting from october up to february it will be the main focus of the development team to complete the documentation! 🎉

LorenzoDeToni commented 11 months ago

Hi Robin, here is the transcript.

import numpy as np
from bsb.core import Scaffold
from bsb.config import from_json
import bsb.options
bsb.options.verbosity = 2
from bsb.core import from_storage
from bsb.config import Configuration
from bsb.plotting import plot_network
from bsb.morphologies import Morphology
import json
import os
import scipy.io as sio

config = from_json("CPG_bsb4.json")

scaffold = Scaffold(config)

scaffold.compile(clear=True)
        Compiling the following strategies:  ...
        Clearing data
        Starting placement strategies:  ...
        Starting connectivity strategies:  ...
        Runtime: 0.25578784942626953 2
plot_network(scaffold,cubic=False)

scaffold.run_simulation("prova")
        Importing  NEST...

                       -- N E S T --
         Copyright (C) 2004 The NEST Initiative

         Version: 3.4
         Built: Mar  7 2023 05:58:31

         his program is provided AS IS and comes with
         NO WARRANTY. See the file LICENSE for details.

         Problems or suggestions?
         Visit https://www.nest-simulator.org/

          Type 'nest.help()' to find out more about NEST.

          Installing  NEST modules...
         Creating neurons...
         Creating connections...
          A_to_B:     100%|█████████████████████████████████| 10/10  [00:00<00:00, 57.33it/s]
          Creating devices...
          ------------------------------------------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[6], line 1
----> 1 scaffold.run_simulation("prova")

File ~/bsb/bsb/profiling.py:157, in meter.<locals>.decorated(*args, **kwargs)
    155     return r
    156 else:
--> 157     return f(*args, **kwargs)

File ~/bsb/bsb/core.py:420, in Scaffold.run_simulation(self, simulation_name, quit)
    418 simulation = self.get_simulation(simulation_name)
    419 adapter = get_simulation_adapter(simulation.simulator)
--> 420 return adapter.simulate(simulation)

File ~/bsb/bsb/simulators/nest/adapter.py:77, in NestAdapter.simulate(self, simulation)
     75 try:
     76     self.reset_kernel()
---> 77     return super().simulate(simulation)
     78 finally:
     79     self.reset_kernel()

File ~/bsb/bsb/simulation/adapter.py:10, in SimulatorAdapter.simulate(self, simulation)
      6 """
      7 Simulate the given simulation.
      8 """
      9 with simulation.scaffold.storage.read_only():
---> 10     data = self.prepare(simulation)
     11     for hook in simulation.post_prepare:
     12         hook(self, simulation, data)

File ~/bsb/bsb/simulators/nest/adapter.py:93, in NestAdapter.prepare(self, simulation, comm)
     91     self.connect_neurons(simulation)
     92     report("Creating devices...", level=2)
---> 93     self.create_devices(simulation)
     94     return self.simdata[simulation]
     95 except Exception:

File ~/bsb/bsb/simulators/nest/adapter.py:187, in NestAdapter.create_devices(self, simulation)
    185 simdata = self.simdata[simulation]
    186 for device_model in simulation.devices.values():
--> 187     device_model.implement(self, simulation, simdata)

File ~/bsb/bsb/simulators/nest/devices/spike_recorder.py:14, in SpikeRecorder.implement(self, adapter, simulation, simdata)
     11 def implement(self, adapter, simulation, simdata):
     12     import nest
---> 14     nodes = self.get_target_nodes(adapter, simulation, simdata)
     15     device = self.register_device(simdata, nest.Create("spike_recorder"))
     16     self.connect_to_nodes(device, nodes)

File ~/bsb/bsb/simulators/nest/device.py:33, in NestDevice.get_target_nodes(self, adapter, simulation, simdata)
     27 else:
     28     node_collector = (
     29         simdata.populations[model][targets]
     30         for model, targets in simdata.populations.items()
     31         if not self.targetting.cell_models or model in self.targetting.cell_models
     32     )
---> 33 return sum(node_collector, start=adapter.nest.NodeCollection())

File ~/bsb/bsb/simulators/nest/device.py:31, in <genexpr>(.0)
     24     node_collector = self.targetting.get_targets(
     25         adapter, simulation, simdata
     26     ).values()
     27 else:
     28     node_collector = (
     29         simdata.populations[model][targets]
     30         for model, targets in simdata.populations.items()
---> 31         if not self.targetting.cell_models or model in self.targetting.cell_models
     32     )
     33 return sum(node_collector, start=adapter.nest.NodeCollection())

AttributeError: 'NoneType' object has no attribute 'cell_models'

Thanks for your help. Lorenzo

Helveg commented 11 months ago

Can you show me the simulations configuration you have in CPG_bsb4.json?

LorenzoDeToni commented 11 months ago
"simulations": {
    "prova": {
      "simulator": "nest",
      "duration": 3000,
      "default_neuron_model": "ht_neuron",
      "default_synapse_model": "static_synapse",
      "cell_models": {
        "A": {
          },
        "B": {
        },
        "C":{
        },
        "D":{
        },
        "E":{
        }
    },
    "connection_models": {
        "A_to_B": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": 50.0,
            "delay": 320.0
          },
          "synapse":{
            "weight": 50.0,
            "delay": 320.0
            }
        },
        "A_to_B": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": -20.0,
            "delay": 320.0
          },
          "synapse":{
            "weight": -20.0,
            "delay": 320.0
            }
        },
        "A_to_C": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": 50.0,
            "delay": 1.0
          },
          "synapse":{
            "weight": 50.0,
            "delay": 1.0
            }
        },
        "B_to_A": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": -50.0,
            "delay": 1.0
          },
          "synapse":{
            "weight": -50.0,
            "delay": 1.0
            }
        },
        "A_to_D": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": 2.0,
            "delay": 200.0
          },
          "synapse":{
            "weight": 2.0,
            "delay": 200.0
            }
        },
        "B_to_D": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": -8.0,
            "delay": 200.0
          },
          "synapse":{
            "weight": -8.0,
            "delay": 200.0
            }
        },
        "A_to_E": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": 30.0,
            "delay": 5.0
          },
          "synapse":{
            "weight": 30.0,
            "delay": 5.0
            }
        },
        "B_to_A": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": -100.0,
            "delay": 1.0
          },
          "synapse":{
            "weight": -100.0,
            "delay": 1.0
            }
        },
        "A_to_E": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": -100.0,
            "delay": 50.0
          },
          "synapse":{
            "weight": -100.0,
            "delay": 50.0
            }
        },
        "E_to_A": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": 2.0,
            "delay": 1.0
          },
          "synapse":{
            "weight": 2.0,
            "delay": 1.0
            }
        },
        "B_to_C": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": -4.0,
            "delay": 1.0
          },
          "synapse":{
            "weight": -4.0,
            "delay": 1.0
            }
        },
        "C_to_B": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": 10.0,
            "delay": 300.0
          },
          "synapse":{
            "weight": 10.0,
            "delay": 300.0
            }
        },
        "D_to_A": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": 10.0,
            "delay": 1.0
          },
          "synapse":{
            "weight": 10.0,
            "delay": 1.0
            }
        },
        "E_to_D": {
          "synapse_model": "static_synapse",
          "connection": {
            "weight": 2.0,
            "delay": 200.0
          },
          "synapse":{
            "weight": 2.0,
            "delay": 200.0
            }
        }
      },
      "devices": {
        "record_stimulus_spikes": {
          "device": "spike_recorder",
          "delay": 2.0
        }
      }
}
}
Helveg commented 11 months ago

Fixed it! Install bsb==4.0.0a57 to get the latest version where this shouldn't be an issue anymore.

PS: The default_neuron_model and default_synapse_model don't exist anymore, the default neuron model is iaf_psc_alpha and the default synapse is static_synapse. If you need different ones you have to give a neuron_model in each cell_model.

Helveg commented 11 months ago

Also, Lorenzo, I'm currently writing my dissertation on this software project. From the filename I'm guessing you're working on Central Pattern Generators? May I include a short description of what you're trying to model with the framework in my dissertation?

LorenzoDeToni commented 10 months ago

Hi Robin, Yes sure you can write about my project, I do work on central patterns generators (I am Lorenzo De Toni from NiLab, @ UNIMORE Jonathan Mapelli Lab) After I've installed the version you suggested, the device section is working (no error), however I still get errors compiling the cell parameters (like Vm...), it's seems like there is a mismatch between bsb version and the syntax indicated in the website. Here the error I get when I try to give some parameters to my cell model:

CastError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 config = from_json("PROVAbsbAB.json")

File ~/.local/lib/python3.10/site-packages/bsb/config/__init__.py:259, in parser_factory.<locals>.parser_method(self, file, data, path)
    257         data = f.read()
    258 tree, meta = parser().parse(data, path=path or file)
--> 259 return _from_parsed(self, name, tree, meta, file)

File ~/.local/lib/python3.10/site-packages/bsb/config/__init__.py:243, in _from_parsed(self, parser_name, tree, meta, file)
    240     from ._config import _bootstrap_components
    242     _bootstrap_components(tree["components"])
--> 243 conf = self.Configuration(tree)
    244 conf._parser = parser_name
    245 conf._meta = meta

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:65, in make_metaclass.<locals>.ConfigArgRewrite.__call__(meta_subject, _parent, _key, *args, **kwargs)
     63     raise ValueError(f"Unexpected positional argument '{primer}'")
     64 # Call the base class's new with internal arguments
---> 65 instance = meta_subject.__new__(
     66     meta_subject, *args, _parent=_parent, _key=_key, **kwargs
     67 )
     68 instance._config_pos_init = getattr(instance, "_config_pos_init", False)
     69 # Call the end user's __init__ with the rewritten arguments, if one is defined

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:218, in compile_new.<locals>.__new__(_cls, _parent, _key, *args, **kwargs)
    216 if root:
    217     instance._config_isfinished = False
--> 218 instance.__post_new__(**kwargs)
    219 if _cls is not ncls:
    220     instance.__init__(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:318, in wrap_root_postnew.<locals>.__post_new__(self, _parent, _key, *args, **kwargs)
    316     post_new(self, *args, _parent=None, _key=None, **kwargs)
    317 except (CastError, RequirementError) as e:
--> 318     _bubble_up_exc(e, self._meta)
    319 self._config_isfinished = True
    320 _resolve_references(self)

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:337, in _bubble_up_exc(exc, meta)
    335     node = ""
    336 attr = f".{exc.attr}" if hasattr(exc, "attr") and exc.attr else ""
--> 337 errr.wrap(type(exc), exc, append=node + attr)

File ~/.local/lib/python3.10/site-packages/errr/__init__.py:7, in wrap(err_type, *args, **kwargs)
      6 def wrap(err_type, *args, **kwargs):
----> 7     return err_type.wrap(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/errr/exception.py:132, in DetailedException.wrap(err_type, e, prepend, append, *details)
    130 err = err_type(msg, *details)
    131 err.__traceback__ = e.__traceback__
--> 132 raise err from None

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:316, in wrap_root_postnew.<locals>.__post_new__(self, _parent, _key, *args, **kwargs)
    314 with warnings.catch_warnings(record=True) as log:
    315     try:
--> 316         post_new(self, *args, _parent=None, _key=None, **kwargs)
    317     except (CastError, RequirementError) as e:
    318         _bubble_up_exc(e, self._meta)

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:288, in compile_postnew.<locals>.__post_new__(self, _parent, _key, **kwargs)
    286         attr.flag_pristine(self)
    287     else:
--> 288         setattr(self, name, value)
    289         attr.flag_dirty(self)
    290 for key, value in leftovers.items():

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:803, in ConfigurationDictAttribute.__set__(self, instance, value, _key)
    799 def __set__(self, instance, value, _key=None):
    800     _setattr(
    801         instance,
    802         self.attr_name,
--> 803         self.fill(value, _parent=instance, _key=_key or self.attr_name),
    804     )

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:812, in ConfigurationDictAttribute.fill(self, value, _parent, _key)
    810 _cfgdict._config_attr = self
    811 _cfgdict._elem_type = self.child_type
--> 812 _cfgdict.update(value or builtins.dict())
    813 return _cfgdict

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:753, in cfgdict.update(self, other)
    751 def update(self, other):
    752     for ckey, value in other.items():
--> 753         self[ckey] = value

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:697, in cfgdict.__setitem__(self, key, value)
    695     _unset_nodes(self[key])
    696 try:
--> 697     value = self._elem_type(value, _parent=self, _key=key)
    698 except (RequirementError, CastError) as e:
    699     if not (hasattr(e, "node") and e.node):

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:65, in make_metaclass.<locals>.ConfigArgRewrite.__call__(meta_subject, _parent, _key, *args, **kwargs)
     63     raise ValueError(f"Unexpected positional argument '{primer}'")
     64 # Call the base class's new with internal arguments
---> 65 instance = meta_subject.__new__(
     66     meta_subject, *args, _parent=_parent, _key=_key, **kwargs
     67 )
     68 instance._config_pos_init = getattr(instance, "_config_pos_init", False)
     69 # Call the end user's __init__ with the rewritten arguments, if one is defined

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:218, in compile_new.<locals>.__new__(_cls, _parent, _key, *args, **kwargs)
    216 if root:
    217     instance._config_isfinished = False
--> 218 instance.__post_new__(**kwargs)
    219 if _cls is not ncls:
    220     instance.__init__(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:288, in compile_postnew.<locals>.__post_new__(self, _parent, _key, **kwargs)
    286         attr.flag_pristine(self)
    287     else:
--> 288         setattr(self, name, value)
    289         attr.flag_dirty(self)
    290 for key, value in leftovers.items():

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:803, in ConfigurationDictAttribute.__set__(self, instance, value, _key)
    799 def __set__(self, instance, value, _key=None):
    800     _setattr(
    801         instance,
    802         self.attr_name,
--> 803         self.fill(value, _parent=instance, _key=_key or self.attr_name),
    804     )

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:812, in ConfigurationDictAttribute.fill(self, value, _parent, _key)
    810 _cfgdict._config_attr = self
    811 _cfgdict._elem_type = self.child_type
--> 812 _cfgdict.update(value or builtins.dict())
    813 return _cfgdict

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:753, in cfgdict.update(self, other)
    751 def update(self, other):
    752     for ckey, value in other.items():
--> 753         self[ckey] = value

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:697, in cfgdict.__setitem__(self, key, value)
    695     _unset_nodes(self[key])
    696 try:
--> 697     value = self._elem_type(value, _parent=self, _key=key)
    698 except (RequirementError, CastError) as e:
    699     if not (hasattr(e, "node") and e.node):

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:65, in make_metaclass.<locals>.ConfigArgRewrite.__call__(meta_subject, _parent, _key, *args, **kwargs)
     63     raise ValueError(f"Unexpected positional argument '{primer}'")
     64 # Call the base class's new with internal arguments
---> 65 instance = meta_subject.__new__(
     66     meta_subject, *args, _parent=_parent, _key=_key, **kwargs
     67 )
     68 instance._config_pos_init = getattr(instance, "_config_pos_init", False)
     69 # Call the end user's __init__ with the rewritten arguments, if one is defined

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:218, in compile_new.<locals>.__new__(_cls, _parent, _key, *args, **kwargs)
    216 if root:
    217     instance._config_isfinished = False
--> 218 instance.__post_new__(**kwargs)
    219 if _cls is not ncls:
    220     instance.__init__(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/bsb/config/_make.py:288, in compile_postnew.<locals>.__post_new__(self, _parent, _key, **kwargs)
    286         attr.flag_pristine(self)
    287     else:
--> 288         setattr(self, name, value)
    289         attr.flag_dirty(self)
    290 for key, value in leftovers.items():

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:653, in ConfigurationListAttribute.__set__(self, instance, value, _key)
    652 def __set__(self, instance, value, _key=None):
--> 653     _setattr(instance, self.attr_name, self.fill(value, _parent=instance))

File ~/.local/lib/python3.10/site-packages/bsb/config/_attrs.py:666, in ConfigurationListAttribute.fill(self, value, _parent, _key)
    664 _cfglist._elem_type = self.child_type
    665 if isinstance(value, builtins.dict):
--> 666     raise CastError(f"Dictionary `{value}` given where list is expected.")
    667 _cfglist.extend(value or builtins.list())
    668 if self.size is not None and len(_cfglist) != self.size:

CastError: Dictionary `<parsed json config '{'V_m': -70.0}' at '/simulations/prova/cell_models/B/parameters'>` given where list is expected. in {root}.simulations.prova.cell_models.B
Helveg commented 10 months ago

Hi! The public documentation is definitely outdated, I think you can just change parameters to constants. In the new simulation features parameters is a list of Parameter, which can map generated BSB data to properties for the individual NEST nodes. When you set Vm for the entire node collection, it's considered a constant that you'd put in constants. Hope this helps.

Helveg commented 10 months ago

@LorenzoDeToni just checking in if everything is running smoothly now? Keep in mind that this is all very new stuff, and if you need any missing component, just open an issue with a feature request, or ask a question on StackOverflow tagged with bsb-framework and I'll make sure to reply and help out wherever I can 😄

LorenzoDeToni commented 10 months ago

Hi Robin, thank for your help, yes now everything it's ok and the simulation is running, but I've a problem in the visualisation of the result of the device, I don't know how to open the hdf5 file, do you have any suggest?

Helveg commented 10 months ago

The .hdf5 file is the network reconstruction file; it contains the cell positions, morphologies, connections, ... The simulation results should be stored in a .nio file.

from bsb.core import from_storage

my_network = from_storage("my_file.hdf5")
from neo.io import NixIO

data = NixIO("my_file.nio").read()

Each library contains their own functions that you can use to read the data. For the BSB (which can get you the structural data), try playing around with the my_network.get_placement_set("my_cell_type"), it has a .load_positions(), load_rotations, load_morphologies function. There's also the my_network.get_connectivity_set("my_conn_type").load_connections().all()

For Neo you can read their docs, they have a very consistent data model, the data should be in .blocks[0].segments[0].spiketrains or .blocks[0].segments[0].analogsignals

Helveg commented 10 months ago

Since you no longer face any technical issues, I'll close this thread, and we can continue in a question/answer format on StackOverflow! That way the questions and answers are easily publicly available to everyone! I will transcribe your questions there and post example links here for future reference.

(Make sure to tag your question with the bsb-framework tag, then I receive an email notification)

Helveg commented 10 months ago

I have transcribed your last comment as 2 separate questions on StackOverflow: