# Synapses class

(Shortest import: `from brian2 import Synapses)`

class brian2.synapses.synapses.Synapses(*args, **kw)

  Bases: `Group`

  Class representing synaptic connections.

  Creating a new `Synapses` object does by default not create any synapses, you have to call the `Synapses.connect` method for that.

  Parameters:

    source : `SpikeSource`

    > The source of spikes, e.g. a `NeuronGroup`.

    target : `Group`, optional

    > The target of the spikes, typically a `NeuronGroup`. If none is given, the same as `source`

    model : `str`, `Equations`, optional

    > The model equations for the synapses.

    on_pre : str, dict, optional

    > The code that will be executed after every pre-synaptic spike. Can be either a single (possibly multi-line) string, or a dictionary mapping pathway names to code strings. In the first case, the pathway will be called `pre` and made available as an attribute of the same name. In the latter case, the given names will be used as the pathway/attribute names. Each pathway has its own code and its own delays.

    pre : str, dict, optional

    > Deprecated. Use `on_pre` instead.

    on_post : str, dict, optional

    > The code that will be executed after every post-synaptic spike. Same conventions as for `on_pre``, the default name for the pathway is `post`.

    post : str, dict, optional

    > Deprecated. Use `on_post` instead.

    delay : `Quantity`, dict, optional

    > The delay for the “pre” pathway (same for all synapses) or a dictionary mapping pathway names to delays. If a delay is specified in this way for a pathway, it is stored as a single scalar value. It can still be changed afterwards, but only to a single scalar value. If you want to have delays that vary across synapses, do not use the keyword argument, but instead set the delays via the attribute of the pathway, e.g. `S.pre.delay = ...` (or `S.delay = ...` as an abbreviation), `S.post.delay = ...`, etc.

    on_event : str or dict, optional

    > Define the events which trigger the pre and post pathways. By default, both pathways are triggered by the `'spike'` event, i.e. the event that is triggered by the `threshold` condition in the connected groups.

    multisynaptic_index : str, optional

    > The name of a variable (which will be automatically created) that stores the “synapse number”. This number enumerates all synapses between the same source and target so that they can be distinguished. For models where each source-target pair has only a single connection, this number only wastes memory (it would always default to 0), it is therefore not stored by default. Defaults to `None` (no variable).

    namespace : dict, optional

    > A dictionary mapping identifier names to objects. If not given, the namespace will be filled in at the time of the call of `Network.run`, with either the values from the `namespace` argument of the `Network.run` method or from the local context, if no such argument is given.

    dtype : `dtype`, dict, optional

    > The `numpy.dtype` that will be used to store the values, or a dictionary specifying the type for variable names. If a value is not provided for a variable (or no value is provided at all), the preference setting core.default_float_dtype is used.

    codeobj_class : class, optional

    > The `CodeObject` class to use to run code.

    dt : `Quantity`, optional

    > The time step to be used for the update of the state variables. Cannot be combined with the `clock` argument.

    clock : `Clock`, optional

    > The update clock to be used. If neither a clock, nor the `dt` argument is specified, the `defaultclock` will be used.

    order : int, optional

    > The priority of of this group for operations occurring at the same time step and in the same scheduling slot. Defaults to 0.

    method : str, `StateUpdateMethod`, optional

    > The numerical integration method to use. If none is given, an appropriate one is automatically determined.

    name : str, optional

    > The name for this object. If none is given, a unique name of the form `synapses`, `synapses_1`, etc. will be automatically chosen.

  Attributes

  `N_incoming_post` | The number of incoming synapses for each neuron in the post-synaptic group.
  `N_outgoing_pre` | The number of outgoing synapses for each neuron in the pre-synaptic group.
  `_connect_called` | remember whether connect was called to raise an error if an assignment to a synaptic variable is attempted without a preceding connect.
  `_pathways` | List of all `SynapticPathway` objects
  `_registered_variables` | Set of `Variable` objects that should be resized when the number of synapses changes
  `_synaptic_updaters` | List of names of all updaters, e.g. ['pre', 'post'].
  `delay` | The presynaptic delay (if a pre-synaptic pathway exists).
  `delay_` | The presynaptic delay without unit information (if apre-synaptic pathway exists).
  `events` | "Events" for all the pathways
  `state_updater` | Performs numerical integration step
  `subexpression_updater` | Update the "constant over a time step" subexpressions
  `summed_updaters` | "Summed variable" mechanism -- sum over all synapses of a pre-/postsynaptic target

  Methods

  `before_run`(run_namespace) | Optional method to prepare the object before a run.
  `check_variable_write`(variable) | Checks that `Synapses.connect` has been called before setting a synaptic variable.
  `connect`([condition, i, j, p, n, ...]) | Add synapses.
  `register_variable`(variable) | Register a `DynamicArray` to be automatically resized when the size of the indices change.
  `unregister_variable`(variable) | Unregister a `DynamicArray` from the automatic resizing mechanism.
  `verify_dependencies`(eq, eq_type, deps, ...) | Helper function to verify that event-driven equations do not depend on clock-driven equations and the other way round.

  Details

  N_incoming_post

    The number of incoming synapses for each neuron in the post-synaptic group.

  N_outgoing_pre

    The number of outgoing synapses for each neuron in the pre-synaptic group.

  _connect_called

    remember whether connect was called to raise an error if an assignment to a synaptic variable is attempted without a preceding connect.

  _pathways

    List of all `SynapticPathway` objects

  _registered_variables

    Set of `Variable` objects that should be resized when the number of synapses changes

  _synaptic_updaters

    List of names of all updaters, e.g. [‘pre’, ‘post’]

  delay

    The presynaptic delay (if a pre-synaptic pathway exists).

  delay_

    The presynaptic delay without unit information (if apre-synaptic pathway exists).

  events

    “Events” for all the pathways

  state_updater

    Performs numerical integration step

  subexpression_updater

    Update the “constant over a time step” subexpressions

  summed_updaters

    “Summed variable” mechanism – sum over all synapses of a pre-/postsynaptic target

  before_run(run_namespace)

    Optional method to prepare the object before a run.

    Called by `Network.after_run` before the main simulation loop starts.

  check_variable_write(variable)

    Checks that `Synapses.connect` has been called before setting a synaptic variable.

    Parameters:

      variable : `Variable`

      > The variable that the user attempts to set.

    Raises

    `TypeError`

      If `Synapses.connect` has not been called yet.

  connect(condition=None, i=None, j=None, p=1.0, n=1, skip_if_invalid=False, namespace=None, level=0)

    Add synapses.

    See Synapses for details.

    Parameters:

      condition : str, bool, optional

      > A boolean or string expression that evaluates to a boolean. The expression can depend on indices `i` and `j` and on pre- and post-synaptic variables. Can be combined with arguments `n`, and `p` but not `i` or `j`.

      i : int, ndarray of int, str, optional

      > The presynaptic neuron indices It can be an index or array of indices if combined with the `j` argument, or it can be a string generator expression.

      j : int, ndarray of int, str, optional

      > The postsynaptic neuron indices. It can be an index or array of indices if combined with the `i` argument, or it can be a string generator expression.

      p : float, str, optional

      > The probability to create `n` synapses wherever the `condition` evaluates to true. Cannot be used with generator syntax for `j`.

      n : int, str, optional

      > The number of synapses to create per pre/post connection pair. Defaults to 1.

      skip_if_invalid : bool, optional

      > If set to True, rather than raising an error if you try to create an invalid/out of range pair (i, j) it will just quietly skip those synapses.

      namespace : dict-like, optional

      > A namespace that will be used in addition to the group-specific namespaces (if defined). If not specified, the locals and globals around the run function will be used.

      level : int, optional

      > How deep to go up the stack frame to look for the locals/global (see `namespace` argument).

    Examples

        >>> from brian2 import *
        >>> import numpy as np
        >>> G = NeuronGroup(10, 'dv/dt = -v / tau : 1', threshold='v>1', reset='v=0')
        >>> S = Synapses(G, G, 'w:1', on_pre='v+=w')
        >>> S.connect(condition='i != j') # all-to-all but no self-connections
        >>> S.connect(i=0, j=0) # connect neuron 0 to itself
        >>> S.connect(i=np.array([1, 2]), j=np.array([2, 1])) # connect 1->2 and 2->1
        >>> S.connect() # connect all-to-all
        >>> S.connect(condition='i != j', p=0.1)  # Connect neurons with 10% probability, exclude self-connections
        >>> S.connect(j='i', n=2)  # Connect all neurons to themselves with 2 synapses
        >>> S.connect(j='k for k in range(i+1)') # Connect neuron i to all j with 0<=j<=i
        >>> S.connect(j='i+(-1)**k for k in range(2) if i>0 and i<N_pre-1') # connect neuron i to its neighbours if it has both neighbours
        >>> S.connect(j='k for k in sample(N_post, p=i*1.0/(N_pre-1))') # neuron i connects to j with probability i/(N-1)
        >>> S.connect(j='k for k in sample(N_post, size=i//2)') # Each neuron connects to i//2 other neurons (chosen randomly)

  register_variable(variable)

    Register a `DynamicArray` to be automatically resized when the size of the indices change. Called automatically when a `SynapticArrayVariable` specifier is created.

  unregister_variable(variable)

    Unregister a `DynamicArray` from the automatic resizing mechanism.

  static verify_dependencies(eq, eq_type, deps, should_not_depend_on, should_not_depend_on_name)

    Helper function to verify that event-driven equations do not depend on clock-driven equations and the other way round.

    Parameters:

      eq : `SingleEquation`

      > The equation to verify

      eq_type : str

      > The type of the equation (for the error message)

      deps : list

      > A list of dependencies

      should_not_depend_on : list

      > A list of equations to verify against the dependencies

      should_not_depend_on_name : str

      > The name of the list of equations (for the error message)

    Raises

    :exc:`EquationError`

      If the given equation depends on something in the other set of equations.

## Tutorials and examples using this

- Tutorial 1-intro-to-brian-neurons

- Tutorial 2-intro-to-brian-synapses

- Tutorial 3-intro-to-brian-simulations

- Example COBAHH

- Example CUBA

- Example adaptive_threshold

- Example advanced/custom_events

- Example advanced/float_32_64_benchmark

- Example compartmental/bipolar_with_inputs

- Example compartmental/bipolar_with_inputs2

- Example compartmental/lfp

- Example coupled_oscillators

- Example frompapers/Brette_2012/Fig5A

- Example frompapers/Brunel_2000

- Example frompapers/Brunel_Hakim_1999

- Example frompapers/Brunel_Wang_2001

- Example frompapers/Clopath_et_al_2010_homeostasis

- Example frompapers/Clopath_et_al_2010_no_homeostasis

- Example frompapers/Diehl_Cook_2015

- Example frompapers/Diesmann_et_al_1999

- Example frompapers/Graupner_Brunel_2012

- Example frompapers/Izhikevich_2003

- Example frompapers/Izhikevich_2007

- Example frompapers/Kremer_et_al_2011_barrel_cortex

- Example frompapers/Maass_Natschlaeger_Markram_2002

- Example frompapers/Nicola_Clopath_2017

- Example frompapers/Spreizer_et_al_2019/Spreizer_et_al_2019

- Example frompapers/Stimberg_et_al_2018/example_1_COBA

- Example frompapers/Stimberg_et_al_2018/example_2_gchi_astrocyte

- Example frompapers/Stimberg_et_al_2018/example_3_io_synapse

- Example frompapers/Stimberg_et_al_2018/example_4_rsmean

- Example frompapers/Stimberg_et_al_2018/example_4_synrel

- Example frompapers/Stimberg_et_al_2018/example_5_astro_ring

- Example frompapers/Stimberg_et_al_2018/example_6_COBA_with_astro

- Example frompapers/Sturzl_et_al_2000

- Example frompapers/Tetzlaff_2015

- Example frompapers/Tsodyks_Pawelzik_Markram_1998

- Example frompapers/Tsodyks_Uziel_Markram_2000

- Example frompapers/Vogels_et_al_2011

- Example frompapers/Wang_2002

- Example standalone/STDP_standalone

- Example standalone/cuba_openmp

- Example standalone/standalone_multiple_processes

- Example standalone/standalone_multiplerun

- Example synapses/STDP

- Example synapses/continuous_interaction

- Example synapses/efficient_gaussian_connectivity

- Example synapses/gapjunctions

- Example synapses/homeostatic_stdp_at_inhibitory_synapes

- Example synapses/jeffress

- Example synapses/licklider

- Example synapses/nonlinear

- Example synapses/spatial_connections

- Example synapses/spike_based_homeostasis

- Example synapses/state_variables

- Example synapses/synapses