Is there any possibility to pass Simulator parameters (https://www.nengo.ai/nengo/v3.1.0/backend-api.html#nengo.Simulator) to GUI’s Simulator (eg. change dt or optimize)?
On the surface, no, this is not possible. NengoGUI currently does not support changing the default parameters of it’s reference simulator. We are aware of this issue, and have several issues open on our (NengoGUI’s) github to address this.
However, NengoGUI does support the use of custom Simulator objects, and we can use this to “hack” this functionality into NengoGUI. The idea is as follows:
- Create a subclass of the default
nengo.Simulatorwith your own defaults:
import nengo class Simulator(nengo.Simulator): def __init__(self, network): super().__init__(network, <custom nengo.Simulator settings>)
Create a python package with your custom
- Pip install your custom python package into your Python environment.
nengo -b <custom_package>to get it to use your custom
As a proof-of-concept example, I’ve created a custom python package called
my-sim and uploaded it here.
To use it:
- Unzip the zip file. You should see the folder structure:
my-sim +-- my_sim | +-- __init__.py | +-- simulator.py +-- setup.py
- Change directory to the
my-simdirectory, the pip install it:
cd my-sim pip install -e .
- Check that you can access the
my-simpackage within your Python environment:
python > import my_sim # Note the underscore here
nengo -b my_sim
- I’ve modified the custom Nengo simulator to use a
dtof 0.1s, and if you run the Nengo simulation, this should be apparent (especially if you pull up a spike plot)
Wow, sneaky way of doing this!
Thank you a lot for a quick solution
I am thankful to have a bunch of dedicated devs to bounce ideas off of to quickly come up with solutions to the variety of problems that users bring up on the forums.
This is a colossal (and unsupported) hack but if you want something quick and dirty that doesn’t involve setting up an extra package or changing anything outside of your script itself, you can add this to your script:
import importlib import sys pkg_name = "my_backend" spec = importlib.machinery.ModuleSpec(pkg_name, None) my_backend = importlib.util.module_from_spec(spec) my_backend.Simulator = lambda *args, **kwargs: nengo.Simulator(*args, dt=0.1, **kwargs) sys.modules[pkg_name] = my_backend __page__.settings.backend = pkg_name
dt is being changed to
0.1 for sake of example (you can change the simulator invocation however you’d like here).
The way this works is by dynamically creating a new module and adding it to
sys.modules so that NengoGUI picks up on it when it tries to import this backend simulator. The last line is yet another hack that overrides the NengoGUI page settings to tell it to use this other backend.
So thanks also to the devs!
Wow, I didn’t know that there is a possibility in Python, to “pretend” that some package exists. I think this option is a bit easier to use, due to no need of creating the actual package, especially if I want to check something quickly.
The solution works great with other backends also - checked with OCL Simulator.
Thank you guys for both solutions