| 1 |
""" |
| 2 |
Implementation of `PoissonGroup`. |
| 3 |
""" |
| 4 |
|
| 5 |
import numpy as np |
| 6 |
|
| 7 |
from brian2.core.spikesource import SpikeSource |
| 8 |
from brian2.core.variables import Subexpression, Variables |
| 9 |
from brian2.groups.group import Group |
| 10 |
from brian2.groups.neurongroup import Thresholder |
| 11 |
from brian2.groups.subgroup import Subgroup |
| 12 |
from brian2.parsing.expressions import parse_expression_dimensions |
| 13 |
from brian2.units.fundamentalunits import check_units, fail_for_dimension_mismatch |
| 14 |
from brian2.units.stdunits import Hz |
| 15 |
from brian2.utils.stringtools import get_identifiers |
| 16 |
|
| 17 |
__all__ = ["PoissonGroup"] |
| 18 |
|
| 19 |
|
| 20 |
class PoissonGroup(Group, SpikeSource): |
| 21 |
""" |
| 22 |
Poisson spike source |
| 23 |
|
| 24 |
Parameters |
| 25 |
---------- |
| 26 |
N : int |
| 27 |
Number of neurons |
| 28 |
rates : `Quantity`, str |
| 29 |
Single rate, array of rates of length N, or a string expression |
| 30 |
evaluating to a rate. This string expression will be evaluated at every |
| 31 |
time step, it can therefore be time-dependent (e.g. refer to a |
| 32 |
`TimedArray`). |
| 33 |
dt : `Quantity`, optional |
| 34 |
The time step to be used for the simulation. Cannot be combined with |
| 35 |
the `clock` argument. |
| 36 |
clock : `Clock`, optional |
| 37 |
The update clock to be used. If neither a clock, nor the `dt` argument |
| 38 |
is specified, the `defaultclock` will be used. |
| 39 |
when : str, optional |
| 40 |
When to run within a time step, defaults to the ``'thresholds'`` slot. |
| 41 |
See :ref:`scheduling` for possible values. |
| 42 |
order : int, optional |
| 43 |
The priority of of this group for operations occurring at the same time |
| 44 |
step and in the same scheduling slot. Defaults to 0. |
| 45 |
name : str, optional |
| 46 |
Unique name, or use poissongroup, poissongroup_1, etc. |
| 47 |
""" |
| 48 |
|
| 49 |
add_to_magic_network = True |
| 50 |
|
| 51 |
@check_units(rates=Hz) |
| 52 |
def __init__( |
| 53 |
self, |
| 54 |
N, |
| 55 |
rates, |
| 56 |
dt=None, |
| 57 |
clock=None, |
| 58 |
when="thresholds", |
| 59 |
order=0, |
| 60 |
namespace=None, |
| 61 |
name="poissongroup*", |
| 62 |
codeobj_class=None, |
| 63 |
): |
| 64 |
Group.__init__( |
| 65 |
self, |
| 66 |
dt=dt, |
| 67 |
clock=clock, |
| 68 |
when=when, |
| 69 |
order=order, |
| 70 |
namespace=namespace, |
| 71 |
name=name, |
| 72 |
) |
| 73 |
|
| 74 |
self.codeobj_class = codeobj_class |
| 75 |
|
| 76 |
self._N = N = int(N) |
| 77 |
|
| 78 |
# TODO: In principle, it would be nice to support Poisson groups with |
| 79 |
# refactoriness, but we can't currently, since the refractoriness |
| 80 |
# information is reset in the state updater which we are not using |
| 81 |
# We could either use a specific template or simply not bother and make |
| 82 |
# users write their own NeuronGroup (with threshold rand() < rates*dt) |
| 83 |
# for more complex use cases. |
| 84 |
|
| 85 |
self.variables = Variables(self) |
| 86 |
# standard variables |
| 87 |
self.variables.add_constant("N", value=self._N) |
| 88 |
self.variables.add_arange("i", self._N, constant=True, read_only=True) |
| 89 |
self.variables.add_array("_spikespace", size=N + 1, dtype=np.int32) |
| 90 |
self.variables.create_clock_variables(self._clock) |
| 91 |
|
| 92 |
# The firing rates |
| 93 |
if isinstance(rates, str): |
| 94 |
self.variables.add_subexpression("rates", dimensions=Hz.dim, expr=rates) |
| 95 |
else: |
| 96 |
self.variables.add_array("rates", size=N, dimensions=Hz.dim) |
| 97 |
self._rates = rates |
| 98 |
|
| 99 |
self.start = 0 |
| 100 |
self.stop = N |
| 101 |
|
| 102 |
self._refractory = False |
| 103 |
|
| 104 |
self.events = {"spike": "rand() < rates * dt"} |
| 105 |
self.thresholder = {"spike": Thresholder(self)} |
| 106 |
self.contained_objects.append(self.thresholder["spike"]) |
| 107 |
|
| 108 |
self._enable_group_attributes() |
| 109 |
|
| 110 |
if not isinstance(rates, str): |
| 111 |
self.rates = rates |
| 112 |
|
| 113 |
def __getitem__(self, item): |
| 114 |
if not isinstance(item, slice): |
| 115 |
raise TypeError("Subgroups can only be constructed using slicing syntax") |
| 116 |
start, stop, step = item.indices(self._N) |
| 117 |
if step != 1: |
| 118 |
raise IndexError("Subgroups have to be contiguous") |
| 119 |
if start >= stop: |
| 120 |
raise IndexError( |
| 121 |
f"Illegal start/end values for subgroup, {int(start)}>={int(stop)}" |
| 122 |
) |
| 123 |
|
| 124 |
return Subgroup(self, start, stop) |
| 125 |
|
| 126 |
def before_run(self, run_namespace=None): |
| 127 |
rates_var = self.variables["rates"] |
| 128 |
if isinstance(rates_var, Subexpression): |
| 129 |
# Check that the units of the expression make sense |
| 130 |
expr = rates_var.expr |
| 131 |
identifiers = get_identifiers(expr) |
| 132 |
variables = self.resolve_all( |
| 133 |
identifiers, run_namespace, user_identifiers=identifiers |
| 134 |
) |
| 135 |
unit = parse_expression_dimensions(rates_var.expr, variables) |
| 136 |
fail_for_dimension_mismatch( |
| 137 |
unit, |
| 138 |
Hz, |
| 139 |
"The expression provided for " |
| 140 |
"PoissonGroup's 'rates' " |
| 141 |
"argument, has to have units " |
| 142 |
"of Hz", |
| 143 |
) |
| 144 |
super().before_run(run_namespace) |
| 145 |
|
| 146 |
@property |
| 147 |
def spikes(self): |
| 148 |
""" |
| 149 |
The spikes returned by the most recent thresholding operation. |
| 150 |
""" |
| 151 |
# Note that we have to directly access the ArrayVariable object here |
| 152 |
# instead of using the Group mechanism by accessing self._spikespace |
| 153 |
# Using the latter would cut _spikespace to the length of the group |
| 154 |
spikespace = self.variables["_spikespace"].get_value() |
| 155 |
return spikespace[: spikespace[-1]] |
| 156 |
|
| 157 |
def __repr__(self): |
| 158 |
classname = self.__class__.__name__ |
| 159 |
return f"{classname}({self.N}, rates={self._rates!r})" |