simulation¶
Simulation execution and results handling.
toric_spines_sim.simulation
¶
Simulation execution and results handling.
SimulationResults(results_dict)
¶
Container for simulation results with analysis methods.
This class wraps the raw simulation results and provides convenient methods for analyzing voltage traces, including integration by segment tag with optional surface-area weighting.
Attributes: voltage_traces: TsdFrame with voltage data (rows=time, columns=probes) input_events: TsGroup mapping stream indices to event timestamps synapses: Dict of synapse specifications gap_junctions: List of gap junction specifications record_points: Dict mapping probe labels to (x, y, z) coordinates cell: arbor.cable_cell object morphology: arbor.morphology object segment_tree: arbor.segment_tree object decor: arbor.decor object labels: arbor.label_dict object cvp: arbor.cv_policy object swc_filepath: Path to SWC morphology file synpts_filepath: Path to synapse points file
Initialize from a simulation results dictionary.
Source code in toric_spines_sim/simulation/results.py
integrate_voltages_by_tag(tags, method='average')
¶
Integrate voltage traces from all segments with specified tag(s).
Args: tags: Single tag or collection of tags to integrate over. method: Integration method: - "average": Simple arithmetic mean of all voltage traces - "surface_weighted": Weighted average by segment surface area
Returns: Pynapple Tsd with time in seconds and integrated voltage in mV.
Example: >>> results = SimulationResults(raw_results) >>> # Get average voltage across all spine segments (tag 3) >>> v_spine = results.integrate_voltages_by_tag(3, method="average") >>> >>> # Get surface-weighted voltage across sink segments (tag 5) >>> v_sink = results.integrate_voltages_by_tag(5, method="surface_weighted") >>> >>> # Combine multiple tags >>> v_combined = results.integrate_voltages_by_tag([3, 5])
Source code in toric_spines_sim/simulation/results.py
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 | |
get_tags()
¶
get_segments_by_tag(tag)
¶
Get list of segment indices with the specified tag.
Source code in toric_spines_sim/simulation/results.py
to_dict()
¶
Convert back to a dictionary (for saving).
Source code in toric_spines_sim/simulation/results.py
to_serializable_dict()
¶
Convert to a dictionary containing only serializable data.
Excludes Arbor C++ objects (cell, morphology, segment_tree, decor, labels, cvp) and SynapsePoint/GapJunctionPoint objects which cannot be pickled. These can be reconstructed from the SWC and synapse points files if needed.
Source code in toric_spines_sim/simulation/results.py
save(filepath)
¶
Save simulation results to a pickle file.
Note: Only serializable data is saved (voltages, events, record_points, metadata). Arbor C++ objects (cell, morphology, segment_tree, etc.) and SynapsePoint/GapJunctionPoint objects cannot be pickled and are excluded. To access these objects later, you'll need to keep the original SimulationResults instance or rebuild them from the SWC and synapse files.
Args: filepath: Path where to save the results (.pkl file).
Example: >>> results.save("simulations/ts1/results.pkl")
Source code in toric_spines_sim/simulation/results.py
load(filepath)
classmethod
¶
Load simulation results from a pickle file.
Note: Loaded results will not contain Arbor objects (cell, morphology, etc.) or synapse/gap junction objects as these cannot be serialized. Only voltage_traces (TsdFrame), input_events (TsGroup), record_points, and metadata are loaded. Methods that require Arbor objects (like integrate_voltages_by_tag) will not work on loaded results.
Args: filepath: Path to the saved results file (.pkl).
Returns: SimulationResults instance with limited functionality.
Raises: FileNotFoundError: If the file does not exist.
Example: >>> results = SimulationResults.load("simulations/ts1/results.pkl") >>> voltage_trace = results.voltage_traces["probe_seg_0"]
Source code in toric_spines_sim/simulation/results.py
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 | |
TSSimulator(swc_filepath, synpts_filepath, events, parameters, record_points='all')
¶
Run an Arbor simulation of a toric-spine SWC with AMPA synapses.
All configuration is provided at construction time. Intermediate pipeline
objects (synapses, cell, recipe, etc.) are computed on first use, then
reused for the lifetime of the instance. run() may be called multiple
times; each call reuses cached pipeline objects and creates a fresh Arbor
simulation.
Synapses are always built as AMPA from the points file (syn_0,
syn_1, … in file order). For other receptor types, build a
TSModel / SynapsePopulation yourself.
Event channels are mapped by TsGroup index to that synapse order.
Generators emit axon-order channels (A0S0, …). If axon assignments are
not already point-file order, call
remap_axon_channel_events_to_synapses before passing events here.
When using stochastic event generators, pass parameters["seed"] to the
generator so that repeated run() calls with the same instance are
deterministic.
Examples:
>>> from toric_spines_sim.simulation import TSSimulator
>>> from toric_spines_sim.simulation import make_default_parameter_bank
>>> from toric_spines_sim.events import StochasticEventGenerator, FlatRateCurve
>>>
>>> parameter_bank = make_default_parameter_bank()
>>> parameters = parameter_bank.sample()
>>> events = StochasticEventGenerator(
... rate_curves=[FlatRateCurve(rate_hz=50.0)],
... n_synapses_per_axon=[25],
... T_ms=parameters["T_ms"],
... seed=int(parameters["seed"]),
... ).generate()
>>> sim = TSSimulator(
... "data/swc/microns/TS1_wsink_r10um.swc",
... "data/pointsets/microns/TS1_synpts.txt",
... events,
... parameters,
... )
>>> results = sim.run()
Source code in toric_spines_sim/simulation/core.py
swc_filepath
property
¶
Path to the SWC morphology file.
synpts_filepath
property
¶
Path to the synapse points file.
parameters
property
¶
Sampled simulation parameters.
record_points_spec
property
¶
Recording point specification passed at construction.
synapses
property
¶
Synapse specifications (builds if needed).
gap_junctions
property
¶
Gap junction specifications (builds synapses if needed).
record_points
property
¶
Resolved record points (builds if needed).
events
property
¶
Input event timestamps.
cell
property
¶
The built Arbor cable_cell (builds if needed).
morphology
property
¶
The cell morphology (builds cell if needed).
segment_tree
property
¶
The segment tree (builds cell if needed).
decor
property
¶
The cell decor (builds cell if needed).
labels
property
¶
The label dictionary (builds cell if needed).
cvp
property
¶
The control volume policy (builds cell if needed).
recipe
property
¶
The Arbor recipe (builds if needed).
build_synapses()
¶
Build synapses and gap junctions from morphology files.
Returns: Dictionary of synapse specifications.
Source code in toric_spines_sim/simulation/core.py
build_record_points()
¶
Resolve record points (either "all" or explicit dict).
Returns: Dictionary mapping probe labels to (x, y, z) coordinates.
Source code in toric_spines_sim/simulation/core.py
build_events()
¶
Return input events.
Returns: TsGroup mapping stream indices to Ts objects (timestamps in ms).
build_cell()
¶
Build the Arbor cable cell.
Returns: The built Arbor cable_cell.
Source code in toric_spines_sim/simulation/core.py
build_recipe()
¶
Build the Arbor recipe.
Returns: The built TSRecipe.
Source code in toric_spines_sim/simulation/core.py
write_cell(filename)
¶
Write the cable cell to file using Arbor's write_component.
Args: filename: Path to the output file (should have .acc extension)
Source code in toric_spines_sim/simulation/core.py
run()
¶
Run the simulation and return results.
May be called multiple times on the same instance. Pipeline objects are reused; a fresh Arbor simulation is created on each call.
Returns: SimulationResults containing all simulation data.
Source code in toric_spines_sim/simulation/core.py
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 | |
make_default_parameter_bank()
¶
Returns a ParameterBank with default values.
Call bank.sample() to obtain a ParameterSet for simulation.
Arbor ion channel defaults: Na: revpot = 50.0 mV, int_conc = 10.0 mM, ext_conc = 140.0 mM K: revpot = -77.0 mV, int_conc = 54.4 mM, ext_conc = 2.5 mM Ca: revpot = 132.458 mV, int_conc = 0.00005 mM, ext_conc = 2 mM
Source code in toric_spines_sim/simulation/parameters.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
make_icx_parameter_bank_invitro()
¶
ICx bank for comparison to Sanculi et al. patch-clamp (in vitro).
- Longitudinal resistivity (ohm cm): ~100 ohm cm, typical values 30 - 200
- Membrane capacitance (uF/cm^2): Cm = 8.559 +/- 50.475 (Sanculi; no in-vivo Cm)
- AMPA: gmax = 0.2-2.0 nS, tau = 2 ms, revpot = 0 mV
- NMDA: gmax = 0.2-2.0 nS, tau_r = 5 ms, tau_d = 50 ms, revpot = 0 mV
- GABA_A: gmax = 0.5-2.0 nS, tau = 2 ms, revpot = -70 mV
Arbor ion channel defaults: Na: revpot = 50.0 mV, int_conc = 10.0 mM, ext_conc = 140.0 mM K: revpot = -77.0 mV, int_conc = 54.4 mM, ext_conc = 2.5 mM Ca: revpot = 132.458 mV, int_conc = 0.00005 mM, ext_conc = 2 mM
Source code in toric_spines_sim/simulation/parameters.py
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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
make_icx_parameter_bank_invivo()
¶
ICx bank for comparison to living-owl intracellular recordings. Peña and Konishi (2002, Journal of Neuroscience): mean rest potential = -67.6 ± 9.3 mV (n = 75 ICx neurons, sharp electrode). Body temperature = ~313 K. Input resistance is lower in vivo than in slice (~3×); leak = Sanculi g_pas × 3 = 0.00042 S/cm². Cm is still the Sanculi value (no in-vivo capacitance).
- Longitudinal resistivity (ohm cm): ~100 ohm cm, typical values 30 - 200
- Membrane capacitance (uF/cm^2): Cm = 8.559 +/- 50.475 (Sanculi; no in-vivo Cm)
- AMPA: gmax = 0.2-2.0 nS, tau = 2 ms, revpot = 0 mV
- NMDA: gmax = 0.2-2.0 nS, tau_r = 5 ms, tau_d = 50 ms, revpot = 0 mV
- GABA_A: gmax = 0.5-2.0 nS, tau = 2 ms, revpot = -70 mV
Arbor ion channel defaults: Na: revpot = 50.0 mV, int_conc = 10.0 mM, ext_conc = 140.0 mM K: revpot = -77.0 mV, int_conc = 54.4 mM, ext_conc = 2.5 mM Ca: revpot = 132.458 mV, int_conc = 0.00005 mM, ext_conc = 2 mM
Source code in toric_spines_sim/simulation/parameters.py
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 260 261 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 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 | |
random_axon_events(n_synapses, n_axons, mod_freq_hz, peak_rate_hz=None, peak_rate_range_hz=None, phase_range_rad=(0.0, 2.0 * np.pi), seed=42)
¶
Create random sine rate curves and a random synapse-to-axon partition.
Provide exactly one of peak_rate_hz or peak_rate_range_hz.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_synapses
|
int
|
Total number of synapses. |
required |
n_axons
|
int
|
Number of axons (must be |
required |
mod_freq_hz
|
float
|
Common sine frequency (Hz). |
required |
peak_rate_hz
|
float
|
Peak rate for every axon (Hz). |
None
|
peak_rate_range_hz
|
tuple of float
|
|
None
|
phase_range_rad
|
tuple of float
|
Phase draw range in radians. Default |
(0.0, 2.0 * pi)
|
seed
|
int
|
RNG seed. |
42
|
Returns:
| Name | Type | Description |
|---|---|---|
rate_curves |
list of SineRateCurve
|
|
n_synapses_per_axon |
list of int
|
|
Source code in toric_spines_sim/simulation/input.py
load_axon_events_from_file(axon_assignment_file, axon_rates_hz)
¶
Build per-axon flat rate curves from a synapse assignment file.
Each line is one axon: comma-separated 1-based synapse indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axon_assignment_file
|
path - like
|
Assignment file (one axon per line). |
required |
axon_rates_hz
|
list of float
|
Rate in Hz for each axon. Length must match the number of lines.
Use |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rate_curves |
list of FlatRateCurve
|
|
n_synapses_per_axon |
list of int
|
|
axon_synapses |
list of list of int
|
0-based synapse indices per axon (for |
Source code in toric_spines_sim/simulation/input.py
remap_axon_channel_events_to_synapses(events_tsgroup, axon_synapses, n_synapses=None)
¶
Map axon-ordered event channels onto synapse point-file indices.
Event generators fan out channels in axon order (axon 0 synapses, then
axon 1, …), but TSRecipe maps TsGroup index i to synapse
syn_i. This scatters axon-channel events onto the correct indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events_tsgroup
|
TsGroup
|
Channels ordered by axon assignment (generator output). |
required |
axon_synapses
|
list of list of int
|
Per-axon 0-based synapse indices (from |
required |
n_synapses
|
int
|
Total synapses. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
TsGroup
|
Indexed |
Examples:
>>> # Axon 0 hits synapses 2 then 0; axon 1 hits synapse 1
>>> axon_synapses = [[2, 0], [1]]
>>> remapped = remap_axon_channel_events_to_synapses(events, axon_synapses)
>>> remapped.get_info("label")[0]
'syn_0'