| 1 |
""" |
| 2 |
Module implementing Brian's refractory mechanism. |
| 3 |
""" |
| 4 |
|
| 5 |
from brian2.units.allunits import second |
| 6 |
from brian2.units.fundamentalunits import DIMENSIONLESS |
| 7 |
|
| 8 |
from .equations import ( |
| 9 |
BOOLEAN, |
| 10 |
DIFFERENTIAL_EQUATION, |
| 11 |
PARAMETER, |
| 12 |
Equations, |
| 13 |
Expression, |
| 14 |
SingleEquation, |
| 15 |
) |
| 16 |
|
| 17 |
__all__ = ["add_refractoriness"] |
| 18 |
|
| 19 |
|
| 20 |
def check_identifier_refractory(identifier): |
| 21 |
""" |
| 22 |
Check that the identifier is not using a name reserved for the refractory |
| 23 |
mechanism. The reserved names are `not_refractory`, `refractory`, |
| 24 |
`refractory_until`. |
| 25 |
|
| 26 |
Parameters |
| 27 |
---------- |
| 28 |
identifier : str |
| 29 |
The identifier to check. |
| 30 |
|
| 31 |
Raises |
| 32 |
------ |
| 33 |
ValueError |
| 34 |
If the identifier is a variable name used for the refractory mechanism. |
| 35 |
""" |
| 36 |
if identifier in ("not_refractory", "refractory", "refractory_until"): |
| 37 |
raise SyntaxError( |
| 38 |
f"The name '{identifier}' is used in the refractory mechanism " |
| 39 |
" and should not be used as a variable " |
| 40 |
"name." |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
Equations.register_identifier_check(check_identifier_refractory) |
| 45 |
|
| 46 |
|
| 47 |
def add_refractoriness(eqs): |
| 48 |
""" |
| 49 |
Extends a given set of equations with the refractory mechanism. New |
| 50 |
parameters are added and differential equations with the "unless refractory" |
| 51 |
flag are changed so that their right-hand side is 0 when the neuron is |
| 52 |
refractory (by multiplication with the ``not_refractory`` variable). |
| 53 |
|
| 54 |
Parameters |
| 55 |
---------- |
| 56 |
eqs : `Equations` |
| 57 |
The equations without refractory mechanism. |
| 58 |
|
| 59 |
Returns |
| 60 |
------- |
| 61 |
new_eqs : `Equations` |
| 62 |
New equations, with added parameters and changed differential |
| 63 |
equations having the "unless refractory" flag. |
| 64 |
""" |
| 65 |
new_equations = [] |
| 66 |
|
| 67 |
# replace differential equations having the active flag |
| 68 |
for eq in eqs.values(): |
| 69 |
if eq.type == DIFFERENTIAL_EQUATION and "unless refractory" in eq.flags: |
| 70 |
# the only case where we have to change anything |
| 71 |
new_code = f"int(not_refractory)*({eq.expr.code})" |
| 72 |
new_equations.append( |
| 73 |
SingleEquation( |
| 74 |
DIFFERENTIAL_EQUATION, |
| 75 |
eq.varname, |
| 76 |
eq.dim, |
| 77 |
expr=Expression(new_code), |
| 78 |
flags=eq.flags, |
| 79 |
) |
| 80 |
) |
| 81 |
else: |
| 82 |
new_equations.append(eq) |
| 83 |
|
| 84 |
# add new parameters |
| 85 |
new_equations.append( |
| 86 |
SingleEquation(PARAMETER, "not_refractory", DIMENSIONLESS, var_type=BOOLEAN) |
| 87 |
) |
| 88 |
new_equations.append(SingleEquation(PARAMETER, "lastspike", second.dim)) |
| 89 |
|
| 90 |
return Equations(new_equations) |