| 1 |
""" |
| 2 |
Implementation of `PoissonInput`. |
| 3 |
""" |
| 4 |
|
| 5 |
from brian2.core.variables import Variables |
| 6 |
from brian2.groups.group import CodeRunner |
| 7 |
from brian2.units.fundamentalunits import ( |
| 8 |
DimensionMismatchError, |
| 9 |
check_units, |
| 10 |
get_dimensions, |
| 11 |
have_same_dimensions, |
| 12 |
) |
| 13 |
from brian2.units.stdunits import Hz |
| 14 |
|
| 15 |
from .binomial import BinomialFunction |
| 16 |
|
| 17 |
__all__ = ["PoissonInput"] |
| 18 |
|
| 19 |
|
| 20 |
class PoissonInput(CodeRunner): |
| 21 |
""" |
| 22 |
PoissonInput(target, target_var, N, rate, weight, when='synapses', order=0) |
| 23 |
|
| 24 |
Adds independent Poisson input to a target variable of a `Group`. For large |
| 25 |
numbers of inputs, this is much more efficient than creating a |
| 26 |
`PoissonGroup`. The synaptic events are generated randomly during the |
| 27 |
simulation and are not preloaded and stored in memory. All the inputs must |
| 28 |
target the same variable, have the same frequency and same synaptic weight. |
| 29 |
All neurons in the target `Group` receive independent realizations of |
| 30 |
Poisson spike trains. |
| 31 |
|
| 32 |
Parameters |
| 33 |
---------- |
| 34 |
target : `Group` |
| 35 |
The group that is targeted by this input. |
| 36 |
target_var : str |
| 37 |
The variable of `target` that is targeted by this input. |
| 38 |
N : int |
| 39 |
The number of inputs |
| 40 |
rate : `Quantity` |
| 41 |
The rate of each of the inputs |
| 42 |
weight : str or `Quantity` |
| 43 |
Either a string expression (that can be interpreted in the context of |
| 44 |
`target`) or a `Quantity` that will be added for every event to |
| 45 |
the `target_var` of `target`. The unit has to match the unit of |
| 46 |
`target_var` |
| 47 |
when : str, optional |
| 48 |
When to update the target variable during a time step. Defaults to |
| 49 |
the `synapses` scheduling slot. See :ref:`scheduling` for possible values. |
| 50 |
order : int, optional |
| 51 |
The priority of of the update compared to other operations occurring at |
| 52 |
the same time step and in the same scheduling slot. Defaults to 0. |
| 53 |
|
| 54 |
""" |
| 55 |
|
| 56 |
@check_units(N=1, rate=Hz) |
| 57 |
def __init__(self, target, target_var, N, rate, weight, when="synapses", order=0): |
| 58 |
if target_var not in target.variables: |
| 59 |
raise KeyError(f"{target_var} is not a variable of {target.name}") |
| 60 |
|
| 61 |
self._weight = weight |
| 62 |
self._target_var = target_var |
| 63 |
|
| 64 |
if isinstance(weight, str): |
| 65 |
weight = f"({weight})" |
| 66 |
else: |
| 67 |
weight_dims = get_dimensions(weight) |
| 68 |
target_dims = target.variables[target_var].dim |
| 69 |
# This will be checked automatically in the abstract code as well |
| 70 |
# but doing an explicit check here allows for a clearer error |
| 71 |
# message |
| 72 |
if not have_same_dimensions(weight_dims, target_dims): |
| 73 |
raise DimensionMismatchError( |
| 74 |
"The provided weight does not " |
| 75 |
"have the same unit as the " |
| 76 |
f"target variable '{target_var}'", |
| 77 |
weight_dims, |
| 78 |
target_dims, |
| 79 |
) |
| 80 |
weight = repr(weight) |
| 81 |
self._N = N |
| 82 |
self._rate = rate |
| 83 |
binomial_sampling = BinomialFunction( |
| 84 |
N, rate * target.clock.dt, name="poissoninput_binomial*" |
| 85 |
) |
| 86 |
|
| 87 |
code = f"{target_var} += {binomial_sampling.name}()*{weight}" |
| 88 |
self._stored_dt = target.dt_[:] # make a copy |
| 89 |
# FIXME: we need an explicit reference here for on-the-fly subgroups |
| 90 |
# For example: PoissonInput(group[:N], ...) |
| 91 |
self._group = target |
| 92 |
CodeRunner.__init__( |
| 93 |
self, |
| 94 |
group=target, |
| 95 |
template="stateupdate", |
| 96 |
code=code, |
| 97 |
user_code="", |
| 98 |
when=when, |
| 99 |
order=order, |
| 100 |
name="poissoninput*", |
| 101 |
clock=target.clock, |
| 102 |
) |
| 103 |
self.variables = Variables(self) |
| 104 |
self.variables._add_variable(binomial_sampling.name, binomial_sampling) |
| 105 |
|
| 106 |
rate = property(fget=lambda self: self._rate, doc="The rate of each input") |
| 107 |
N = property(fget=lambda self: self._N, doc="The number of inputs") |
| 108 |
target_var = property( |
| 109 |
fget=lambda self: self._target_var, doc="The targetted variable" |
| 110 |
) |
| 111 |
weight = property(fget=lambda self: self._weight, doc="The synaptic weight") |
| 112 |
|
| 113 |
def before_run(self, run_namespace): |
| 114 |
if self._group.dt_ != self._stored_dt: |
| 115 |
raise NotImplementedError( |
| 116 |
f"The dt used for simulating {self.group.name} " |
| 117 |
"changed after the PoissonInput source was " |
| 118 |
"created." |
| 119 |
) |
| 120 |
CodeRunner.before_run(self, run_namespace=run_namespace) |