Simulating Neuron Models and Plotting Membrane Potential Dynamics in NEST

Configuring the NEST simulation kernel requires initializing the environment and defining the temporal resolution. The PyNEST interface operates on a discrete-event architecture, where computational load scales directly with network activity rather than static node count. Before instantiating any biological components, the kernel state must be cleared to prevent parameter leakage from previous runs.

import matplotlib.pyplot as plt
import nest

# Initialize simulation environment
nest.ResetKernel()
nest.resolution = 0.001

# Instantiate Hodgkin-Huxley cell and recording device
target_cell = nest.Create("hh_psc_alpha")
potential_monitor = nest.Create("voltmeter")

# Apply constant external current
target_cell.I_e = 376.0

# Wire monitor to cell
nest.Connect(potential_monitor, target_cell)

# Execute simulation
nest.Simulate(10.0)

# Extract recorded data
recorded_events = potential_monitor.get("events")
simulation_times = recorded_events["times"]
membrane_voltages = recorded_events["V_m"]

# Visualize dynamics
plt.figure(figsize=(8, 4))
plt.plot(simulation_times, membrane_voltages, linewidth=1.5, label="V_m")
plt.axhline(y=20.0, color="crimson", linestyle=":", label="Spike Threshold")
plt.xlabel("Simulation Time (ms)")
plt.ylabel("Membrane Potential (mV)")
plt.title("Hodgkin-Huxley Neuron Response")
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Data retrieval from recording devices in NEST 3.x utilizes the .get() method, which returns a dictionary containing timestamp arrays and corresponding signal values. Extracting the times and V_m keys allows direct integration with Matplotlib for custom visualization. The plotting routine maps the temporal evolution of the membrane potential against a defined spike threshold, providing immediate feedback on the neuron's firing regime under constant current injection.

Comparative analysis across multiple neuronal formalisms requires parallel instantiation and independent monitoring. By mapping model identifiers to their respective NEST implementations, simulation pipelines can be automated to reduce boilerplate connection logic.

import matplotlib.pyplot as plt
import nest

nest.ResetKernel()
nest.resolution = 0.001

# Define target models
model_configs = {
    "Hodgkin-Huxley": "hh_psc_alpha",
    "Leaky Integrate-and-Fire": "iaf_cond_alpha",
    "Izhikevich": "izhikevich"
}

cells = {}
monitors = {}

# Instantiate cells and attach individual voltmeters
for label, model_name in model_configs.items():
    cells[label] = nest.Create(model_name)
    cells[label].I_e = 376.0
    monitors[label] = nest.Create("voltmeter")
    nest.Connect(monitors[label], cells[label])

# Run network simulation
nest.Simulate(40.0)

# Plot comparative results
fig, axes = plt.subplots(1, 3, figsize=(14, 4), sharey=True)

for ax, (label, monitor) in zip(axes, monitors.items()):
    trace_data = monitor.get("events")
    ax.plot(trace_data["times"], trace_data["V_m"], linewidth=1.2)
    ax.set_title(label)
    ax.set_xlabel("Time (ms)")
    ax.grid(True, linestyle="--", alpha=0.5)

axes[0].set_ylabel("Membrane Potential (mV)")
plt.tight_layout()
plt.show()

The batch simulation approach attaches a dedicated voltmeter to each cell type, ensuring that event streams remain isolated. After executing the simulation step, iterating through the monitor collection enables synchronized subplot generation. Sharing the y-axis across panels standardizes the voltage scale, making it straightforward to contrast the spiking patterns, adaptation characteristics, and subthreshold dynamics inherent to each mathematical model.

Tags: NEST Simulator Computational Neuroscience PyNEST python Neuron Modeling

Posted on Mon, 24 Aug 2026 16:18:37 +0000 by jasons61