Models
Models are the core of neurolib
. The Model
superclass will help you to load, simulate, and analyse models. It also makes it very easy to implement your own neural mass model (see Example 0.6 custom model).
Loading a model
To load a model, we need to import the submodule of a model and instantiate it. This example shows how to load a single node of the ALNModel
. See Example 0 aln minimal on how to simulate a whole-brain network using this model.
from neurolib.models.aln import ALNModel # Import the model
model = ALNModel() # Create an instance
model.run() # Run it
Model base class methods
The Model base class runs models, manages their outputs, parameters and more. This class should serve as the base class for all implemented models.
Source code in neurolib/models/model.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 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 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 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 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 594 595 596 597 598 599 600 |
|
output
property
Returns value of default output as defined by self.default_output
.
Note that all outputs are saved in the attribute self.outputs
.
__getitem__(key)
Index outputs with a dictionary-like key, e.g., model['rates_exc']
.
Source code in neurolib/models/model.py
524 525 526 |
|
autochunk(inputs=None, chunksize=1, append_outputs=False, bold=False)
Executes a single chunk of integration, either for a given duration
or a single timestep dt
. Gathers all inputs to the model and resets
the initial conditions as a preparation for the next chunk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
list[np.ndarray|], optional
|
list of input values, ordered according to self.input_vars, defaults to None |
None
|
chunksize |
int, optional
|
length of a chunk to simulate in dt, defaults 1 |
1
|
append_outputs |
bool, optional
|
append the chunkwise outputs to the outputs attribute, defaults to False |
False
|
Source code in neurolib/models/model.py
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 |
|
checkChunkwise(chunksize)
Checks if the model fulfills requirements for chunkwise simulation. Checks whether the sampling rate for outputs fits to chunksize and duration. Throws errors if not.
Source code in neurolib/models/model.py
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 |
|
clearModelState()
Clears the model's state to create a fresh one
Source code in neurolib/models/model.py
297 298 299 300 301 302 303 |
|
getMaxDelay()
Computes the maximum delay of the model. This function should be overloaded
if the model has internal delays (additional to delay between nodes defined by Dmat)
such as the delay between an excitatory and inhibitory population within each brain area.
If this function is not overloaded, the maximum delay is assumed to be defined from the
global delay matrix Dmat
.
Note: Maxmimum delay is given in units of dt.
Returns:
Type | Description |
---|---|
int
|
maxmimum delay of the model in units of dt |
Source code in neurolib/models/model.py
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 |
|
getOutput(name)
Get an output of a given name (dot.semarated)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
A key, grouped outputs in the form group.subgroup.variable |
required |
Returns:
Type | Description |
---|---|
Output data |
Source code in neurolib/models/model.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
getOutputs(group='')
Get all outputs of an output group. Examples: getOutputs("BOLD")
or simply getOutputs()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group |
str
|
Group name, subgroups separated by dots. If left empty (default), all outputs of the root group are returned. |
''
|
Source code in neurolib/models/model.py
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 |
|
initializeBold()
Initialize BOLD model.
Source code in neurolib/models/model.py
54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
initializeRun(initializeBold=False)
Initialization before each run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initializeBold |
bool
|
initialize BOLD model |
False
|
Source code in neurolib/models/model.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
integrate(append_outputs=False, simulate_bold=False)
Calls each models integration
function and saves the state and the outputs of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
append |
bool, optional
|
append the chunkwise outputs to the outputs attribute, defaults to False |
required |
Source code in neurolib/models/model.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
integrateChunkwise(chunksize, bold=False, append_outputs=False)
Repeatedly calls the chunkwise integration for the whole duration of the simulation.
If bold==True
, the BOLD model is simulated after each chunk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunksize |
int
|
size of each chunk to simulate in units of dt |
required |
bold |
bool, optional
|
simulate BOLD model after each chunk, defaults to False |
False
|
append_outputs |
bool, optional
|
append the chunkwise outputs to the outputs attribute, defaults to False |
False
|
Source code in neurolib/models/model.py
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 |
|
randomICs(min=0, max=1)
Generates a new set of uniformly-distributed random initial conditions for the model.
TODO: All parameters are drawn from the same distribution / range. Allow for independent ranges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
min |
float
|
Minium of uniform distribution |
0
|
max |
float
|
Maximum of uniform distribution |
1
|
Source code in neurolib/models/model.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
run(chunkwise=False, chunksize=None, bold=False, append_outputs=False, continue_run=False)
Main interfacing function to run a model.
The model can be run in three different ways:
1) model.run()
starts a new run.
2) model.run(chunkwise=True)
runs the simulation in chunks of length chunksize
.
3) mode.run(continue_run=True)
continues the simulation of a previous run. This has no effect during the first run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
list[np.ndarray|]
|
list of inputs to the model, must have the same order as model.input_vars. Note: no sanity check is performed for performance reasons. Take care of the inputs yourself. |
required |
chunkwise |
bool, optional
|
simulate model chunkwise or in one single run, defaults to False |
False
|
chunksize |
int, optional
|
size of the chunk to simulate in dt, if set will imply chunkwise=True, defaults to 2s |
None
|
bold |
bool, optional
|
simulate BOLD signal (only for chunkwise integration), defaults to False |
False
|
append_outputs |
bool, optional
|
append new and chunkwise outputs to the outputs attribute, defaults to False. Note: BOLD outputs are always appended. |
False
|
continue_run |
bool
|
continue a simulation by using the initial values from a previous simulation. This has no effect during the first run. |
False
|
Source code in neurolib/models/model.py
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 |
|
setInitialValuesToLastState()
Reads the last state of the model and sets the initial conditions to that state for continuing a simulation.
Source code in neurolib/models/model.py
324 325 326 327 328 329 330 331 332 333 334 335 |
|
setInputs(inputs)
Take inputs from a list and store it in the appropriate model parameter for external input. TODO: This is not safe yet, checks should be implemented whether the model has inputs defined or not for example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
list[np.ndarray(), ...]
|
list of inputs |
required |
Source code in neurolib/models/model.py
353 354 355 356 357 358 359 360 361 |
|
setOutput(name, data, append=False, removeICs=False)
Adds an output to the model, typically a simulation result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the output in dot.notation, a la "outputgroup.output" |
required |
data |
`numpy.ndarray`
|
Output data, can't be a dictionary! |
required |
Source code in neurolib/models/model.py
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 |
|
setSamplingDt()
Checks if sampling_dt is set correctly and sets self.sample_every
1) Check if sampling_dt is multiple of dt
2) Check if semplind_dt is greater than duration
Source code in neurolib/models/model.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
setStateVariables(name, data)
Saves the models current state variables.
TODO: Cut state variables to length of self.maxDelay However, this could be time-memory tradeoff
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the state variable |
required |
data |
np.ndarray
|
value of the variable |
required |
Source code in neurolib/models/model.py
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
simulateBold(bold_variable, append=True)
Gets the default output of the model and simulates the BOLD model. Adds the simulated BOLD signal to outputs.
Source code in neurolib/models/model.py
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 |
|
storeOutputsAndStates(t, variables, append=False)
Takes the simulated variables of the integration and stores it to the appropriate model output and state object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t |
list
|
time vector |
required |
variables |
numpy.ndarray
|
variable from time integration |
required |
append |
bool, optional
|
append output to existing output or overwrite, defaults to False |
False
|
Source code in neurolib/models/model.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
xr(group='')
Converts a group of outputs to xarray. Output group needs to contain an element that starts with the letter "t" or it will not recognize any time axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group |
str
|
Output group name, example: "BOLD". Leave empty for top group. |
''
|
Source code in neurolib/models/model.py
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 594 595 596 597 598 599 600 |
|