Skip to content

Commit 8177eeb

Browse files
committed
Defer NESTML compilation to sim.setup()
Previously each nestml_cell_type() / nestml_synapse_type() call compiled and installed a NEST module immediately, causing a collision when called more than once (second nest.Install() fails; single module name overwrites earlier models). Now both functions register a stub class in a module-level list and return it immediately — no compilation occurs. sim.setup() calls _compile_and_resolve(), which gathers all pending descriptions, invokes generate_nest_target() once for all of them, calls nest.Install() once, then resolves each stub class in place so it behaves identically to a native_cell_type() / native_synapse_type() class. Calling nestml_cell_type() or nestml_synapse_type() after setup() raises RuntimeError. Examples updated to register NESTML types before setup().
1 parent af5ea37 commit 8177eeb

5 files changed

Lines changed: 239 additions & 143 deletions

File tree

examples/nestml/stdp.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,27 @@
4141
if options.debug:
4242
init_logging(None, debug=True)
4343

44-
sim.setup(timestep=0.01, min_delay=1.0)
45-
46-
import pyNN
47-
nest = pyNN.nest
48-
from pyNN.utility import init_logging
49-
50-
# === Create the cell type from a NESTML definition
44+
# === Register the NESTML synapse (and co-generated neuron) before sim.setup()
5145

5246
current_dir = os.path.dirname(os.path.abspath(__file__))
53-
input_path = os.path.join(current_dir, "wb_cond_exp_neuron.nestml")
54-
# celltype_cls = sim.nestml.nestml_cell_type("wb_cond_exp_neuron", input_path)
55-
56-
synapse_type = sim.nestml.nestml_synapse_type("stdp_synapse", "stdp_synapse.nestml", "iaf_psc_exp_neuron.nestml")
47+
synapse_type = sim.nestml.nestml_synapse_type(
48+
"stdp_synapse",
49+
os.path.join(current_dir, "stdp_synapse.nestml"),
50+
postsynaptic_neuron_nestml_description=os.path.join(current_dir, "iaf_psc_exp_neuron.nestml"),
51+
)
5752
post_cell_type = synapse_type.postsynaptic_cell_type
5853

54+
sim.setup(timestep=0.01, min_delay=1.0)
5955

60-
# === Build and instrument the network =======================================
61-
62-
init_logging(logfile=None, debug=True)
6356

64-
# nest.setup()
57+
# === Build and instrument the network =======================================
6558

66-
p2 = nest.Population(10, nest.SpikeSourcePoisson())
67-
p1 = nest.Population(10, post_cell_type())
59+
p2 = sim.Population(10, sim.SpikeSourcePoisson())
60+
p1 = sim.Population(10, post_cell_type())
6861

69-
connector = nest.AllToAllConnector()
62+
connector = sim.AllToAllConnector()
7063

71-
# stdp_params = {'Wmax': 50.0, 'lambda': 0.015, 'w': 0.001}
72-
stdp_params = {}
73-
prj = nest.Projection(p2, p1, connector, receptor_type='excitatory', synapse_type=synapse_type(**stdp_params))
64+
prj = sim.Projection(p2, p1, connector, receptor_type='excitatory', synapse_type=synapse_type())
7465

7566
p1.record(["V_m", "I_syn_exc", "I_syn_inh"])
7667

examples/nestml/wang_buzsaki_current_injection.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
if options.debug:
4141
init_logging(None, debug=True)
4242

43-
sim.setup(timestep=0.01, min_delay=1.0)
44-
45-
46-
# === Create the cell type from a NESTML definition
43+
# === Register the NESTML cell type (must happen before sim.setup())
4744

4845
# note that the NESTML definition can also be stored in a separate file
4946

@@ -141,6 +138,8 @@
141138

142139
celltype_cls = sim.nestml.nestml_cell_type("wb_cond_exp", nestml_description)
143140

141+
sim.setup(timestep=0.01, min_delay=1.0)
142+
144143
# add some variability between neurons
145144
rng = NumpyRNG(seed=1309463846)
146145
rnd = lambda min, max: RandomDistribution("uniform", (min, max), rng=rng)

examples/nestml/wang_buzsaki_synaptic_input.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@
4141
if options.debug:
4242
init_logging(None, debug=True)
4343

44-
sim.setup(timestep=0.01, min_delay=1.0)
45-
46-
47-
# === Create the cell type from a NESTML definition
44+
# === Register the NESTML cell type (must happen before sim.setup())
4845

4946
current_dir = os.path.dirname(os.path.abspath(__file__))
5047
input_path = os.path.join(current_dir, "wb_cond_exp_neuron.nestml")
5148
celltype_cls = sim.nestml.nestml_cell_type("wb_cond_exp_neuron", input_path)
5249

50+
sim.setup(timestep=0.01, min_delay=1.0)
51+
5352
# add some variability between neurons
5453
rng = NumpyRNG(seed=1309463846)
5554
rnd = lambda min, max: RandomDistribution("uniform", (min, max), rng=rng)

pyNN/nest/control.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def setup(timestep=DEFAULT_TIMESTEP, min_delay=DEFAULT_MIN_DELAY,
7979
# Set min_delay and max_delay
8080
simulator.state.set_delays(min_delay, max_delay)
8181
nest.SetDefaults('spike_generator', {'precise_times': True})
82+
from . import nestml
83+
nestml._compile_and_resolve()
8284
return rank()
8385

8486

0 commit comments

Comments
 (0)