Passing Simulator parameters to GUI

Hi,
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:

  1. Create a subclass of the default nengo.Simulator with your own defaults:
import nengo

class Simulator(nengo.Simulator):
    def __init__(self, network):
        super().__init__(network, <custom nengo.Simulator settings>)
  1. Create a python package with your custom Simulator class.
  2. Pip install your custom python package into your Python environment.
  3. Call nengo with nengo -b <custom_package> to get it to use your custom Simulator class.

As a proof-of-concept example, I’ve created a custom python package called my-sim and uploaded it here.
To use it:

  1. Unzip the zip file. You should see the folder structure:
my-sim
  +-- my_sim
  |     +-- __init__.py
  |     +-- simulator.py
  +-- setup.py
  1. Change directory to the my-sim directory, the pip install it:
cd my-sim
pip install -e .
  1. Check that you can access the my-sim package within your Python environment:
python
> import my_sim  # Note the underscore here
  1. Start nengo with the my_sim simulator:
nengo -b my_sim
  1. I’ve modified the custom Nengo simulator to use a dt of 0.1s, and if you run the Nengo simulation, this should be apparent (especially if you pull up a spike plot)
1 Like

Wow, sneaky way of doing this!
Thank you a lot for a quick solution :smiley:

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. :smiley:

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

where 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! :smiley:

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 :slight_smile: