Neuron injection & I/O¶
Current injection¶
inject_currents_into_populations ¶
inject_currents_into_populations(populations: Sequence[_Pool], input_current__AnalogSignal: CURRENT__AnalogSignal) -> None
Injects input currents into the specified populations.
Sets up time-varying current injection using NEURON's IClamp and Vector.play() mechanisms. This function only sets up the current injection - spike recording and simulation execution must be handled separately by the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
populations
|
Sequence[_Pool]
|
The populations of neurons to inject current into. |
required |
input_current__AnalogSignal
|
CURRENT__AnalogSignal
|
The analog signal of input currents to inject into the population. Shape should be (time_points, n_pools) where n_pools matches len(populations). |
required |
Returns:
| Type | Description |
|---|---|
None
|
Current injection mechanisms are attached to the neurons as side effects. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of populations does not match the number of current channels. |
Notes
- Current injection vectors and IClamp objects are stored on each cell as
cell._stim_vectorsto prevent garbage collection - The user is responsible for setting up spike recording and calling h.run()
Source code in myogen/utils/neuron/inject_currents_into_populations.py
inject_currents_and_simulate_spike_trains ¶
inject_currents_and_simulate_spike_trains(populations: Sequence[_Pool], input_current__AnalogSignal: CURRENT__AnalogSignal, spike_detection_thresholds__mV: Quantity__mV | Sequence[Quantity__mV] = -10.0 * mV) -> SPIKE_TRAIN__Block
Injects input currents into populations and returns recorded spike trains.
This is a complete pipeline function that sets up current injection, spike recording, runs the NEURON simulation, and returns the results as a properly formatted neo.Block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
populations
|
Sequence[_Pool]
|
The populations of neurons to inject current into. |
required |
input_current__AnalogSignal
|
CURRENT__AnalogSignal
|
The analog signal of input currents to inject into the population. Shape should be (time_points, n_pools) where n_pools matches len(populations). |
required |
spike_detection_thresholds__mV
|
float | Sequence[float]
|
Thresholds for spike detection in millivolts, by default -10.0. If a sequence is provided, it must match the number of populations. |
-10.0 * mV
|
Returns:
| Type | Description |
|---|---|
SPIKE_TRAIN__Block
|
Neo Block containing spike trains organized as segments (pools) with spiketrains (neurons). Each segment represents a motor unit pool, each spiketrain represents a neuron. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of populations does not match the number of current channels. |
Notes
This function performs the complete simulation pipeline: 1. Sets up current injection (same as inject_currents_into_populations) 2. Sets up spike recording for all neurons 3. Runs the NEURON simulation via h.run() 4. Converts recorded spikes to neo.Block format
Source code in myogen/utils/neuron/inject_currents_into_populations.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
Persistence¶
ContinuousSaver ¶
ContinuousSaver(save_path: Path, chunk_duration__ms: Quantity__ms = 10000.0 * ms, populations: Optional[dict] = None, recording_config: Optional[dict] = None, verbose: bool = True)
Manages continuous saving of simulation data in chunks to prevent memory overflow.
Instead of accumulating all data in RAM, this class periodically saves chunks to disk and clears memory. Data can be loaded and combined afterward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_path
|
Path
|
Directory where chunks will be saved |
required |
chunk_duration__ms
|
float
|
Duration of each chunk in milliseconds (default: 10000 ms = 10 seconds) |
10000.0 * ms
|
populations
|
dict
|
Dictionary of populations to record from |
None
|
recording_config
|
dict
|
Configuration like {"aMN": [0, 10, 20, ...]} for which cells to record |
None
|
Source code in myogen/utils/continuous_saver.py
record_step ¶
record_step(timestep__ms: float) -> None
Record data for current simulation timestep.
Call this from your step callback at each timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestep__ms
|
float
|
Integration timestep in milliseconds |
required |
Source code in myogen/utils/continuous_saver.py
record_spike ¶
Record a spike event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pop_name
|
str
|
Population name |
required |
cell_id
|
int
|
Cell ID within population |
required |
spike_time
|
float
|
Time of spike in milliseconds |
required |
Source code in myogen/utils/continuous_saver.py
finalize ¶
finalize(timestep__ms: Quantity__ms, spike_results=None) -> None
Save final chunk and spike data.
Call this after simulation completes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestep__ms
|
Quantity__ms
|
Integration timestep in milliseconds |
required |
spike_results
|
NEO Block
|
NEO Block containing spike trains from SimulationRunner. If provided, spike data will be extracted and saved to chunks. |
None
|
Source code in myogen/utils/continuous_saver.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
convert_chunks_to_neo ¶
convert_chunks_to_neo(save_path: Path, duration__ms: Optional[float] = None, spike_data_file: Optional[Path] = None, verbose: bool = True) -> Block
Load chunks and convert to NEO Block format (compatible with SimulationRunner output).
This function creates a NEO Block that's identical in structure to what SimulationRunner.run() would return, making it compatible with existing analysis code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_path
|
Path
|
Directory where chunks were saved |
required |
duration__ms
|
float
|
Total simulation duration in ms (if None, inferred from data) |
None
|
spike_data_file
|
Path
|
Path to SimulationRunner spike results file (e.g., 'watanabe__spikes_only.pkl') If provided, spike data will be loaded from this NEO Block instead of chunks |
None
|
verbose
|
bool
|
If True, display progress bars and status messages. Set to False to disable. |
True
|
Returns:
| Type | Description |
|---|---|
Block
|
NEO Block containing spike trains and analog signals |
Source code in myogen/utils/continuous_saver.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
NWB export¶
Note
NWB export requires optional dependencies: pip install myogen[nwb].
export_to_nwb ¶
export_to_nwb(block: Block, filepath: str | Path, session_description: str = 'MyoGen simulation', identifier: str | None = None, session_start_time: datetime | None = None, experimenter: str | list[str] | None = None, institution: str | None = None, lab: str | None = None, experiment_description: str | None = None, keywords: list[str] | None = None, subject_id: str | None = None, species: str = 'Homo sapiens', age: str | None = None, sex: str | None = None, subject_description: str | None = None, **kwargs) -> Path
Export a Neo Block to NWB format.
This function uses Neo's NWBIO to write simulation data to an NWB file. The Block should contain AnalogSignals with grid annotations (created via create_grid_signal) for electrode array data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block
|
Block
|
Neo Block containing simulation data. Can be spike trains, EMG, or MUAP data from MyoGen simulations. |
required |
filepath
|
str or Path
|
Output file path. Should end with '.nwb'. |
required |
session_description
|
str
|
Description of the simulation session. |
"MyoGen simulation"
|
identifier
|
str
|
Unique identifier for this NWB file. If None, a UUID is generated. |
None
|
session_start_time
|
datetime
|
Start time of the session. If None, current time is used. |
None
|
experimenter
|
str or list[str]
|
Name(s) of experimenter(s). |
None
|
institution
|
str
|
Institution where the simulation was performed. |
None
|
lab
|
str
|
Lab where the simulation was performed. |
None
|
experiment_description
|
str
|
Description of the experiment/simulation. |
None
|
keywords
|
list[str]
|
Keywords describing the data. |
None
|
subject_id
|
str
|
Subject identifier (recommended for DANDI). |
None
|
species
|
str
|
Species of the subject. Use Latin binomial (e.g., "Homo sapiens", "Mus musculus"). For simulations, defaults to human. |
"Homo sapiens"
|
age
|
str
|
Age of subject in ISO 8601 duration format (e.g., "P30Y" for 30 years). |
None
|
sex
|
str
|
Sex of subject. One of: "M", "F", "U" (unknown), "O" (other). |
None
|
subject_description
|
str
|
Description of the subject. |
None
|
**kwargs
|
Additional keyword arguments passed to NWBIO. |
{}
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created NWB file. |
Examples:
>>> from myogen.utils.nwb import export_to_nwb
>>>
>>> # Export spike trains to NWB
>>> export_to_nwb(
... spike_train__Block,
... "simulation_spikes.nwb",
... session_description="Motor neuron pool simulation",
... institution="My University",
... )
>>>
>>> # Export surface EMG to NWB
>>> export_to_nwb(
... surface_emg__Block,
... "simulation_emg.nwb",
... session_description="Surface EMG simulation",
... experimenter="John Doe",
... )
Notes
For electrode array data (surface EMG, MUAPs), the grid structure is preserved via electrode_positions in annotations, which map to NWB's electrode table.
See Also
create_grid_signal : Create grid-annotated AnalogSignals validate_nwb : Validate NWB file with NWBInspector
Source code in myogen/utils/nwb.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
export_simulation_to_nwb ¶
export_simulation_to_nwb(filepath: str | Path, spike_train__Block: Block | None = None, surface_emg__Block: Block | None = None, surface_muap__Block: Block | None = None, intramuscular_emg__Block: Block | None = None, intramuscular_muap__Block: Block | None = None, session_description: str = 'MyoGen neuromuscular simulation', identifier: str | None = None, session_start_time: datetime | None = None, **kwargs) -> Path
Export all simulation data to a single NWB file.
This is a convenience function that combines multiple Neo Blocks (spike trains, EMG, MUAPs) into a single NWB file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str or Path
|
Output file path. |
required |
spike_train__Block
|
SPIKE_TRAIN__Block
|
Block containing spike train data. |
None
|
surface_emg__Block
|
SURFACE_EMG__Block
|
Block containing surface EMG data. |
None
|
surface_muap__Block
|
SURFACE_MUAP__Block
|
Block containing surface MUAP templates. |
None
|
intramuscular_emg__Block
|
INTRAMUSCULAR_EMG__Block
|
Block containing intramuscular EMG data. |
None
|
intramuscular_muap__Block
|
INTRAMUSCULAR_MUAP__Block
|
Block containing intramuscular MUAP templates. |
None
|
session_description
|
str
|
Description of the simulation session. |
"MyoGen neuromuscular simulation"
|
identifier
|
str
|
Unique identifier for this NWB file. |
None
|
session_start_time
|
datetime
|
Start time of the session. |
None
|
**kwargs
|
Additional metadata passed to export_to_nwb. |
{}
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created NWB file. |
Examples:
>>> from myogen.utils.nwb import export_simulation_to_nwb
>>>
>>> # Export complete simulation
>>> export_simulation_to_nwb(
... "full_simulation.nwb",
... spike_train__Block=simulation.get_spike_train__Block(),
... surface_emg__Block=surface_emg.surface_emg__Block,
... session_description="Biceps brachii simulation",
... institution="University",
... )
Source code in myogen/utils/nwb.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
validate_nwb ¶
Validate an NWB file using NWBInspector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str or Path
|
Path to the NWB file to validate. |
required |
verbose
|
bool
|
If True, print validation results. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if validation passed with no errors, False otherwise. |
Examples:
>>> from myogen.utils.nwb import validate_nwb
>>>
>>> is_valid = validate_nwb("simulation.nwb")
>>> if is_valid:
... print("File is valid!")