| 1 |
""" |
| 2 |
Utility functions for handling the units in `Equations`. |
| 3 |
""" |
| 4 |
|
| 5 |
import re |
| 6 |
|
| 7 |
from brian2.core.variables import Variable |
| 8 |
from brian2.parsing.expressions import parse_expression_dimensions |
| 9 |
from brian2.parsing.statements import parse_statement |
| 10 |
from brian2.units.fundamentalunits import ( |
| 11 |
fail_for_dimension_mismatch, |
| 12 |
get_dimensions, |
| 13 |
get_unit, |
| 14 |
) |
| 15 |
|
| 16 |
__all__ = ["check_dimensions", "check_units_statements"] |
| 17 |
|
| 18 |
|
| 19 |
def check_dimensions(expression, dimensions, variables): |
| 20 |
""" |
| 21 |
Compares the physical dimensions of an expression to expected dimensions in |
| 22 |
a given namespace. |
| 23 |
|
| 24 |
Parameters |
| 25 |
---------- |
| 26 |
expression : str |
| 27 |
The expression to evaluate. |
| 28 |
dimensions : `Dimension` |
| 29 |
The expected physical dimensions for the `expression`. |
| 30 |
variables : dict |
| 31 |
Dictionary of all variables (including external constants) used in |
| 32 |
the `expression`. |
| 33 |
|
| 34 |
Raises |
| 35 |
------ |
| 36 |
KeyError |
| 37 |
In case on of the identifiers cannot be resolved. |
| 38 |
DimensionMismatchError |
| 39 |
If an unit mismatch occurs during the evaluation. |
| 40 |
""" |
| 41 |
expr_dims = parse_expression_dimensions(expression, variables) |
| 42 |
expected = repr(get_unit(dimensions)) |
| 43 |
err_msg = ( |
| 44 |
f"Expression '{expression.strip()}' does not have the expected unit {expected}" |
| 45 |
) |
| 46 |
fail_for_dimension_mismatch(expr_dims, dimensions, err_msg) |
| 47 |
|
| 48 |
|
| 49 |
def check_units_statements(code, variables): |
| 50 |
""" |
| 51 |
Check the units for a series of statements. Setting a model variable has to |
| 52 |
use the correct unit. For newly introduced temporary variables, the unit |
| 53 |
is determined and used to check the following statements to ensure |
| 54 |
consistency. |
| 55 |
|
| 56 |
Parameters |
| 57 |
---------- |
| 58 |
code : str |
| 59 |
The statements as a (multi-line) string |
| 60 |
variables : dict of `Variable` objects |
| 61 |
The information about all variables used in `code` (including |
| 62 |
`Constant` objects for external variables) |
| 63 |
|
| 64 |
Raises |
| 65 |
------ |
| 66 |
KeyError |
| 67 |
In case on of the identifiers cannot be resolved. |
| 68 |
DimensionMismatchError |
| 69 |
If an unit mismatch occurs during the evaluation. |
| 70 |
""" |
| 71 |
variables = dict(variables) |
| 72 |
# Avoid a circular import |
| 73 |
from brian2.codegen.translation import analyse_identifiers |
| 74 |
|
| 75 |
newly_defined, _, unknown = analyse_identifiers(code, variables) |
| 76 |
|
| 77 |
if len(unknown): |
| 78 |
raise AssertionError( |
| 79 |
"Encountered unknown identifiers, this should not " |
| 80 |
f"happen at this stage. Unknown identifiers: {unknown}" |
| 81 |
) |
| 82 |
|
| 83 |
code = re.split(r"[;\n]", code) |
| 84 |
for line in code: |
| 85 |
line = line.strip() |
| 86 |
if not len(line): |
| 87 |
continue # skip empty lines |
| 88 |
|
| 89 |
varname, op, expr, comment = parse_statement(line) |
| 90 |
if op in ("+=", "-=", "*=", "/=", "%="): |
| 91 |
# Replace statements such as "w *=2" by "w = w * 2" |
| 92 |
expr = f"{varname} {op[0]} {expr}" |
| 93 |
elif op == "=": |
| 94 |
pass |
| 95 |
else: |
| 96 |
raise AssertionError(f'Unknown operator "{op}"') |
| 97 |
|
| 98 |
expr_unit = parse_expression_dimensions(expr, variables) |
| 99 |
|
| 100 |
if varname in variables: |
| 101 |
expected_unit = variables[varname].dim |
| 102 |
fail_for_dimension_mismatch( |
| 103 |
expr_unit, |
| 104 |
expected_unit, |
| 105 |
"The right-hand-side of code " |
| 106 |
'statement "%s" does not have the ' |
| 107 |
"expected unit {expected}" % line, |
| 108 |
expected=expected_unit, |
| 109 |
) |
| 110 |
elif varname in newly_defined: |
| 111 |
# note the unit for later |
| 112 |
variables[varname] = Variable( |
| 113 |
name=varname, dimensions=get_dimensions(expr_unit), scalar=False |
| 114 |
) |
| 115 |
else: |
| 116 |
raise AssertionError( |
| 117 |
f"Variable '{varname}' is neither in the variables " |
| 118 |
"dictionary nor in the list of undefined " |
| 119 |
"variables." |
| 120 |
) |