Top-level methods#
These methods are directly available in the main namespace when importing ANNarchy:
from ANNarchy import *
Note that numpy is automatically imported as:
import numpy as np
Configuration and compilation#
Contrary to other simulators, ANNarchy is entirely based on code generation. It provides a set of first-level functions to ensure the network is correctly created. It is important to call these functions in the right order.
ANNarchy.setup(**keyValueArgs)
#
The setup function is used to configure ANNarchy simulation environment. It takes various optional arguments:
- dt: simulation step size (default: 1.0 ms).
- paradigm: parallel framework for code generation. Accepted values: "openmp" or "cuda" (default: "openmp").
- method: default method to numerize ODEs. Default is the explicit forward Euler method ('explicit').
- sparse_matrix_format: the default matrix format for projections in ANNarchy (by default: List-In-List for CPUs and Compressed Sparse Row). Note that affects only the C++ data structures.
- sparse_matrix_storage_order: encodes whether the row in a connectivity matrix encodes pre-synaptic neurons (post_to_pre, default) or post-synaptic neurons (pre_to_post). Note that affects only the C++ data structures.
- precision: default floating precision for variables in ANNarchy. Accepted values: "float" or "double" (default: "double")
- num_threads: number of treads used by openMP (overrides the environment variable
OMP_NUM_THREADS
when set, default = None). - visible_cores: allows a fine-grained control which cores are useable for the created threads (default = [] for no limitation). It can be used to limit created openMP threads to a physical socket.
- structural_plasticity: allows synapses to be dynamically added/removed during the simulation (default: False).
- seed: the seed (integer) to be used in the random number generators (default = -1 is equivalent to time(NULL)).
The following parameters are mainly for debugging and profiling, and should be ignored by most users:
- verbose: shows details about compilation process on console (by default False). Additional some information of the network construction will be shown.
- suppress_warnings: if True, warnings (e. g. from the mathematical parser) are suppressed.
- show_time: if True, initialization times are shown. Attention: verbose should be set to True additionally.
- disable_shared_library_time_offset: by default False. If set to True, the shared library generated by ANNarchy will not be extended by time offset.
Note:
This function should be used before any other functions of ANNarchy (including importing a network definition), right after from ANNarchy import *
:
from ANNarchy import *
setup(dt=1.0, method='midpoint', num_threads=2)
Source code in ANNarchy/core/Global.py
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 |
|
ANNarchy.compile(directory='annarchy', clean=False, populations=None, projections=None, compiler='default', compiler_flags='default', add_sources='', extra_libs='', cuda_config={'device': 0}, annarchy_json='', silent=False, debug_build=False, profile_enabled=False, net_id=0)
#
This method uses the network architecture to generate optimized C++ code and compile a shared library that will perform the simulation.
The compiler
, compiler_flags
and part of cuda_config
take their default value from the configuration file ~/.config/ANNarchy/annarchy.json
.
The following arguments are for internal development use only:
- debug_build: creates a debug version of ANNarchy, which logs the creation of objects and some other data (default: False).
- profile_enabled: creates a profilable version of ANNarchy, which logs several computation timings (default: False).
Parameters:
-
directory
–name of the subdirectory where the code will be generated and compiled. Must be a relative path. Default: "annarchy/".
-
clean
–boolean to specifying if the library should be recompiled entirely or only the changes since last compilation (default: False).
-
populations
–list of populations which should be compiled. If set to None, all available populations will be used.
-
projections
–list of projection which should be compiled. If set to None, all available projections will be used.
-
compiler
–C++ compiler to use. Default: g++ on GNU/Linux, clang++ on OS X. Valid compilers are [g++, clang++].
-
compiler_flags
–platform-specific flags to pass to the compiler. Default: "-march=native -O2". Warning: -O3 often generates slower code and can cause linking problems, so it is not recommended.
-
cuda_config
–dictionary defining the CUDA configuration for each population and projection.
-
annarchy_json
–compiler flags etc can be stored in a .json file normally placed in the home directory (see comment below). With this flag one can directly assign a file location.
-
silent
–defines if status message like "Compiling... OK" should be printed.
Source code in ANNarchy/generator/Compiler.py
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 |
|
ANNarchy.clear(functions=True, neurons=True, synapses=True, constants=True)
#
Clears all variables (erasing already defined populations, projections, monitors and constants), as if you had just imported ANNarchy.
- functions: if True (default), all functions defined with
add_function
are erased. - neurons: if True (default), all neurons defined with
Neuron
are erased. - synapses: if True (default), all synapses defined with
Synapse
are erased. - constants: if True (default), all constants defined with
Constant
are erased.
Useful when re-running Jupyter/IPython notebooks multiple times:
from ANNarchy import *
clear()
Source code in ANNarchy/core/Global.py
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 |
|
Simulation#
Different methods are available to run the simulation:
ANNarchy.simulate(duration, measure_time=False, progress_bar=False, callbacks=True, net_id=0)
#
Simulates the network for the given duration in milliseconds.
The number of simulation steps is computed relative to the discretization step dt
declared in setup()
(default: 1ms):
simulate(1000.0)
Parameters:
-
duration
–the duration in milliseconds.
-
measure_time
–defines whether the simulation time should be printed. Default: False.
-
progress_bar
–defines whether a progress bar should be printed. Default: False
-
callbacks
–defines if the callback method (decorator
every
should be called). Default: True.
Source code in ANNarchy/core/Simulate.py
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 |
|
ANNarchy.simulate_until(max_duration, population, operator='and', measure_time=False, net_id=0)
#
Runs the network for the maximal duration in milliseconds. If the stop_condition
defined in the population becomes true during the simulation, it is stopped.
One can specify several populations. If the stop condition is true for any of the populations, the simulation will stop ('or' function).
Example:
pop1 = Population( ..., stop_condition = "r > 1.0 : any")
compile()
simulate_until(max_duration=1000.0, population=pop1)
Parameters:
-
max_duration
–the maximum duration of the simulation in milliseconds.
-
population
–the (list of) population whose
stop_condition
should be checked to stop the simulation. -
operator
–operator to be used ('and' or 'or') when multiple populations are provided (default: 'and').
-
measure_time
–defines whether the simulation time should be printed (default=False).
Returns:
-
–
the actual duration of the simulation in milliseconds.
Source code in ANNarchy/core/Simulate.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 |
|
ANNarchy.step(net_id=0)
#
Performs a single simulation step (duration = dt
).
Source code in ANNarchy/core/Simulate.py
122 123 124 125 126 127 128 129 130 |
|
ANNarchy.every
#
Decorator to declare a callback method that will be called periodically during the simulation.
Example of setting increasing inputs to a population every 100 ms, with an offset of 90 ms (or -10 ms relative to the period):
@every(period=100., offset=-10.)
def step_input(n):
pop.I = float(n) / 100.
simulate(10000.)
step_input()
will be called at times 90, 190, ..., 9990 ms during the call to simulate()
.
The method must accept only n
as parameter (an integer being 0 the first time the method is called, and incremented afterwards) and can not return anything.
The times at which the method is called are relative to the time when simulate()
is called (if t
is already 150 before calling simulate()
, the first call will then be made at t=240
with the previous example).
If multiple callbacks are defined, they will be called in the order of their declaration if they occur at the same time.
Source code in ANNarchy/core/Simulate.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 |
|
__init__(period, offset=0.0, wait=0.0, net_id=0)
#
Parameters:
-
period
–interval in ms between two calls to the function. If less than
dt
, will be called every step. -
offset
–by default, the first call to the method will be made at the start of the simulation. The offset delays the call within the period (default: 0.0). Can be negative, in which case it will be counted from the end of the period.
-
wait
–allows to wait for a certain amount of time (in ms) before starting to call the method.
wait
can be combined withoffset
, so ifperiod=100.
,offset=50.
andwait=500.
, the first call will be made 550 ms after the call tosimulate()
Source code in ANNarchy/core/Simulate.py
188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
ANNarchy.enable_callbacks(net_id=0)
#
Enables all declared callbacks for the network.
Source code in ANNarchy/core/Simulate.py
149 150 151 152 153 |
|
ANNarchy.disable_callbacks(net_id=0)
#
Disables all callbacks for the network.
Source code in ANNarchy/core/Simulate.py
143 144 145 146 147 |
|
ANNarchy.clear_all_callbacks(net_id=0)
#
Clears the list of declared callbacks for the network.
Cannot be undone!
Source code in ANNarchy/core/Simulate.py
155 156 157 158 159 160 161 |
|
Reset the network#
If you want to run multiple experiments with the same network, or if your experiment setup requires a pre-learning phase, you can reset selectively neural or synaptic variables to their initial values.
ANNarchy.reset(populations=True, projections=False, synapses=False, monitors=True, net_id=0)
#
Reinitialises the network to its state before the call to compile. The network time will be set to 0ms.
All monitors are emptied.
Parameters:
-
populations
–if True (default), the neural parameters and variables will be reset to their initial value.
-
projections
–if True, the synaptic parameters and variables (except the connections) will be reset (default=False).
-
synapses
–if True, the synaptic weights will be erased and recreated (default=False).
-
monitors
–if True, the monitors will be emptied and reset (default=True).
Source code in ANNarchy/core/Global.py
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 |
|
Access to populations#
ANNarchy.get_population(name, net_id=0)
#
Returns the population with the given name
.
Parameters:
-
name
–name of the population.
Returns:
-
–
The requested
Population
object if existing,None
otherwise.
Source code in ANNarchy/core/Global.py
320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
ANNarchy.get_projection(name, net_id=0)
#
Returns the projection with the given name.
Parameters:
-
name
–name of the projection.
Returns:
-
–
The requested
Projection
object if existing,None
otherwise.
Source code in ANNarchy/core/Global.py
334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
Functions#
ANNarchy.add_function(function)
#
Defines a global function which can be used by all neurons and synapses.
The function must have only one return value and use only the passed arguments.
Examples of valid functions:
logistic(x) = 1 / (1 + exp(-x))
piecewise(x, a, b) = if x < a:
a
else:
if x > b :
b
else:
x
Please refer to the manual to know the allowed mathematical functions.
Source code in ANNarchy/core/Global.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
|
ANNarchy.functions(name, net_id=0)
#
Allows to access a global function defined with add_function
and use it from Python using arrays after compilation.
The name of the function is not added to the global namespace to avoid overloading.
add_function("logistic(x) = 1. / (1. + exp(-x))")
compile()
result = functions('logistic')([0., 1., 2., 3., 4.])
Only lists or 1D Numpy arrays can be passed as arguments, not single values nor multidimensional arrays.
When passing several arguments, make sure they have the same size.
Source code in ANNarchy/core/Global.py
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 |
|
Constants#
ANNarchy.Constant
#
Bases: float
Constant parameter that can be used by all neurons and synapses.
The class Constant
derives from float
, so any legal operation on floats (addition, multiplication) can be used.
If a Neuron/Synapse defines a parameter with the same name, the constant parameters will not be visible.
Example:
tau = Constant('tau', 20)
factor = Constant('factor', 0.1)
real_tau = Constant('real_tau', tau*factor)
neuron = Neuron(
equations='''
real_tau*dr/dt + r =1.0
'''
)
The value of the constant can be changed anytime with the set()
method. Assignments will have no effect (e.g. tau = 10.0
only creates a new float).
The value of constants defined as combination of other constants (real_tau
) is not updated if the value of these constants changes (changing tau
with tau.set(10.0)
will not modify the value of real_tau
).
Source code in ANNarchy/core/Global.py
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 |
|
__init__(name, value, net_id=0)
#
Parameters:
-
name
–name of the constant (unique), which can be used in equations.
-
value
–the value of the constant, which must be a float, or a combination of Constants.
Source code in ANNarchy/core/Global.py
488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
set(value)
#
Changes the value of the constant.
Source code in ANNarchy/core/Global.py
505 506 507 508 509 |
|
Learning#
ANNarchy.enable_learning(projections=None, period=None, offset=None, net_id=0)
#
Enables learning for all projections. Optionally period and offset can be changed for all projections.
Parameters:
-
projections
–the projections whose learning should be enabled. By default, all the existing projections are enabled.
-
period
–determines how often the synaptic variables will be updated.
-
offset
–determines the offset at which the synaptic variables will be updated relative to the current time.
Source code in ANNarchy/core/Global.py
572 573 574 575 576 577 578 579 580 581 582 583 584 |
|
ANNarchy.disable_learning(projections=None, net_id=0)
#
Disables learning for all projections.
Parameters:
-
projections
–the projections whose learning should be disabled. By default, all the existing projections are disabled.
Source code in ANNarchy/core/Global.py
586 587 588 589 590 591 592 593 594 595 |
|
Access to simulation times#
ANNarchy.get_time(net_id=0)
#
Returns the current time in ms.
Source code in ANNarchy/core/Global.py
600 601 602 603 604 605 606 |
|
ANNarchy.set_time(t, net_id=0)
#
Sets the current time in ms.
Warning: can be dangerous for some spiking models.
Source code in ANNarchy/core/Global.py
608 609 610 611 612 613 614 615 616 617 |
|
ANNarchy.get_current_step(net_id=0)
#
Returns the current simulation step.
Source code in ANNarchy/core/Global.py
619 620 621 622 623 624 625 |
|
ANNarchy.set_current_step(t, net_id=0)
#
Sets the current simulation step (integer).
Warning: can be dangerous for some spiking models.
Source code in ANNarchy/core/Global.py
627 628 629 630 631 632 633 634 635 636 |
|
ANNarchy.dt()
#
Returns the simulation step size dt
used in the simulation.
Source code in ANNarchy/core/Global.py
638 639 640 |
|