How to run repeated simulationsΒΆ

Let us imagine that you want to run a list of simulations but only want to change one or many parameters in between these simulations. For the sake of this example, we suppose that you are using the reconstruction from the NEST tutorial. We want here to run 10 iterations of the basal_activity simulation, only changing the rate parameter from the background_noise Poisson generator.

You could copy paste all these simulations in your configuration file or use the import mechanism, but this is a tedious task and prone to error.

Instead, you can load your simulation configuration from your Scaffold object and modify it on the fly:

from bsb import from storage

scaffold = from_storage("network.hdf5")
config_sim = scaffold.config.simulations["basal_activity"]

nb_repetitions = 10
for i in range(nb_repetitions):
        # Set the stimulation rate on the `general_noise` device
        input_rate = i * 10.0
        config_sim.devices["general_noise"].rate = input_rate
        # Run the simulation and collect the results
        results = scaffold.run_simulation("basal_activity")

        # save the results in a separate nio file
        output_file = f"simulation-results/my_simulation_results_{input_rate}Hz.nio"
        results.write(output_file, "ow")

Note

In the above example, note that BSB will load your whole simulation parameters on NEST 10 times, which might take a while.

Let us say now that you want to directly modify a NEST variable. Then you will have to use the SimulatorAdapter related to NEST, the NestAdapter.

To run a simulation directly with its adapter, you need to run this following methods:

NEST additionally need you to reset its kernel in between simulations so you should run also the adapter.reset_kernel() before prepare.

In between prepare and run you have a full access to the simulation backend in case you want to modify them directly:

from bsb_nest import NestAdapter

from bsb import from_storage

scaffold = from_storage("network.hdf5")
simulation = scaffold.get_simulation("basal_activity")
adapter = NestAdapter()

nb_repetitions = 10

for i in range(nb_repetitions):
    # Set the stimulation rate on the `general_noise` device
    input_rate = i * 10.0
    simulation.devices["general_noise"].rate = input_rate
    # Clear NEST
    adapter.reset_kernel()
    # Let the adapter translate the simulation config into
    # simulator specific instructions
    simulation_backend = adapter.prepare(simulation)
    # You have free access to the `simulation_backend` here, to tweak
    # or augment the framework's instructions.

    # ...

    # Let the adapter run the simulation and collect the output.
    results = adapter.run(simulation)
    results = adapter.collect(results)
    results = results[0]  # here we only run one simulation
    # Organize the Neo data file into your data workflow by tagging it,
    # renaming it, moving it, giving it metadata, ...
    output_file = f"simulation-results/my_simulation_results_{input_rate}Hz.nio"
    results.write(output_file, "ow")