| 1 |
""" |
| 2 |
Module for analysing synaptic pre and post code for synapse order independence. |
| 3 |
""" |
| 4 |
|
| 5 |
from brian2.core.functions import Function |
| 6 |
from brian2.core.variables import Constant |
| 7 |
from brian2.utils.stringtools import get_identifiers |
| 8 |
|
| 9 |
__all__ = ["OrderDependenceError", "check_for_order_independence"] |
| 10 |
|
| 11 |
|
| 12 |
class OrderDependenceError(Exception): |
| 13 |
pass |
| 14 |
|
| 15 |
|
| 16 |
def check_for_order_independence(statements, variables, indices): |
| 17 |
""" |
| 18 |
Check that the sequence of statements doesn't depend on the order in which the indices are iterated through. |
| 19 |
""" |
| 20 |
# Remove stateless functions from variables (only bother with ones that are used) |
| 21 |
all_used_vars = set() |
| 22 |
for statement in statements: |
| 23 |
all_used_vars.update(get_identifiers(statement.expr)) |
| 24 |
variables = variables.copy() |
| 25 |
for var in set(variables.keys()).intersection(all_used_vars): |
| 26 |
val = variables[var] |
| 27 |
if isinstance(val, Function): |
| 28 |
if val.stateless: |
| 29 |
del variables[var] |
| 30 |
else: |
| 31 |
raise OrderDependenceError( |
| 32 |
"Function %s may have internal state, " |
| 33 |
"which can lead to order dependence." % var |
| 34 |
) |
| 35 |
all_variables = [v for v in variables if not isinstance(variables[v], Constant)] |
| 36 |
# Main index variables are those whose index corresponds to the main index being iterated through. By |
| 37 |
# assumption/definition, these indices are unique, and any order-dependence cannot come from their values, |
| 38 |
# only from the values of the derived indices. In the most common case of Synapses, the main index would be |
| 39 |
# the synapse index, and the derived indices would be pre and postsynaptic indices (which can be repeated). |
| 40 |
unique_index = lambda v: ( |
| 41 |
indices[v] != "0" and getattr(variables[indices[v]], "unique", False) |
| 42 |
) |
| 43 |
main_index_variables = { |
| 44 |
v for v in all_variables if indices[v] == "_idx" or unique_index(v) |
| 45 |
} |
| 46 |
different_index_variables = set(all_variables) - main_index_variables |
| 47 |
|
| 48 |
# At the start, we assume all the different/derived index variables are permutation independent and we continue |
| 49 |
# to scan through the list of statements checking whether or not permutation-dependence has been introduced |
| 50 |
# until the permutation_independent set has stopped changing. |
| 51 |
permutation_independent = list(different_index_variables) |
| 52 |
permutation_dependent_aux_vars = set() |
| 53 |
changed_permutation_independent = True |
| 54 |
for statement in statements: |
| 55 |
if statement.op == ":=" and statement.var not in all_variables: |
| 56 |
main_index_variables.add(statement.var) |
| 57 |
all_variables.append(statement.var) |
| 58 |
|
| 59 |
while changed_permutation_independent: |
| 60 |
changed_permutation_independent = False |
| 61 |
for statement in statements: |
| 62 |
vars_in_expr = get_identifiers(statement.expr).intersection(all_variables) |
| 63 |
# any time a statement involves a LHS and RHS which only depend on itself, this doesn't change anything |
| 64 |
if {statement.var} == vars_in_expr: |
| 65 |
continue |
| 66 |
nonsyn_vars_in_expr = vars_in_expr.intersection(different_index_variables) |
| 67 |
permdep = any( |
| 68 |
var not in permutation_independent for var in nonsyn_vars_in_expr |
| 69 |
) |
| 70 |
permdep = permdep or any( |
| 71 |
var in permutation_dependent_aux_vars for var in vars_in_expr |
| 72 |
) |
| 73 |
if statement.op == ":=": # auxiliary variable created |
| 74 |
if permdep: |
| 75 |
if statement.var not in permutation_dependent_aux_vars: |
| 76 |
permutation_dependent_aux_vars.add(statement.var) |
| 77 |
changed_permutation_independent = True |
| 78 |
continue |
| 79 |
elif statement.var in main_index_variables: |
| 80 |
if permdep: |
| 81 |
raise OrderDependenceError() |
| 82 |
elif statement.var in different_index_variables: |
| 83 |
if statement.op in ("+=", "*=", "-=", "/="): |
| 84 |
if permdep: |
| 85 |
raise OrderDependenceError() |
| 86 |
if statement.var in permutation_independent: |
| 87 |
permutation_independent.remove(statement.var) |
| 88 |
changed_permutation_independent = True |
| 89 |
elif statement.op == "=": |
| 90 |
otheridx = [ |
| 91 |
v |
| 92 |
for v in variables |
| 93 |
if indices[v] not in (indices[statement.var], "_idx", "0") |
| 94 |
] |
| 95 |
if any(var in otheridx for var in vars_in_expr): |
| 96 |
raise OrderDependenceError() |
| 97 |
if permdep: |
| 98 |
raise OrderDependenceError() |
| 99 |
if any(var in main_index_variables for var in vars_in_expr): |
| 100 |
raise OrderDependenceError() |
| 101 |
else: |
| 102 |
raise OrderDependenceError() |
| 103 |
else: |
| 104 |
raise AssertionError("Should never get here...") |