-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgeneral.py
More file actions
368 lines (318 loc) · 15.2 KB
/
Copy pathgeneral.py
File metadata and controls
368 lines (318 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
docstring needed
:copyright: Copyright 2010-2017 by the NineML Python team, see AUTHORS.
:license: BSD-3, see LICENSE for details.
"""
from past.builtins import basestring
from nineml.exceptions import NineMLUsageError, NineMLDimensionError
from nineml.abstraction.expressions.utils import is_valid_lhs_target
from nineml.abstraction.expressions import reserved_identifiers, Expression
from nineml.base import BaseNineMLObject
import operator
import sympy
from sympy import sympify
from nineml.base import SendPortBase
from sympy.logic.boolalg import BooleanTrue, BooleanFalse
from nineml.visitors import BaseVisitor, BaseVisitorWithContext
from functools import reduce
class AliasesAreNotRecursiveComponentValidator(BaseVisitor):
"""Check that aliases are not self-referential"""
def __init__(self, component_class, **kwargs): # @UnusedVariable
BaseVisitor.__init__(self)
self.visit(component_class)
def action_componentclass(self, component_class, **kwargs): # @UnusedVariable @IgnorePep8
unresolved_aliases = dict((a.lhs, a) for a in component_class.aliases)
def alias_contains_unresolved_symbols(alias):
unresolved = [sym for sym in alias.rhs_symbol_names
if sym in unresolved_aliases]
return len(unresolved) != 0
def get_resolved_aliases():
return [alias for alias in list(unresolved_aliases.values())
if not alias_contains_unresolved_symbols(alias)]
while(unresolved_aliases):
resolved_aliases = get_resolved_aliases()
if resolved_aliases:
for r in resolved_aliases:
del unresolved_aliases[r.lhs]
else:
raise NineMLUsageError(
"Unable to resolve all aliases, you may have a recursion "
"issue. Remaining Aliases: {}".format(
','.join(list(unresolved_aliases.keys()))))
def default_action(self, obj, nineml_cls, **kwargs):
pass
class NoUnresolvedSymbolsComponentValidator(BaseVisitor):
"""
Check that aliases and timederivatives are defined in terms of other
parameters, aliases, statevariables and ports
"""
def __init__(self, component_class, **kwargs): # @UnusedVariable @IgnorePep8
BaseVisitor.__init__(self)
self.available_symbols = []
self.aliases = []
self.time_derivatives = []
self.state_assignments = []
self.component_class = component_class
self.visit(component_class)
# Check Aliases:
for alias in self.aliases:
for rhs_atom in alias.rhs_symbol_names:
if rhs_atom in reserved_identifiers:
continue
if rhs_atom not in self.available_symbols:
raise NineMLUsageError(
"Unresolved Symbol in Alias: {} [{}]"
.format(rhs_atom, alias))
# Check TimeDerivatives:
for timederivative in self.time_derivatives:
for rhs_atom in timederivative.rhs_symbol_names:
if (rhs_atom not in self.available_symbols and
rhs_atom not in reserved_identifiers):
raise NineMLUsageError(
"Unresolved Symbol in Time Derivative: {} [{}]"
.format(rhs_atom, timederivative))
# Check StateAssignments
for state_assignment in self.state_assignments:
for rhs_atom in state_assignment.rhs_symbol_names:
if (rhs_atom not in self.available_symbols and
rhs_atom not in reserved_identifiers):
raise NineMLUsageError(
'Unresolved Symbol in Assignment: {} [{}]'
.format(rhs_atom, state_assignment))
def add_symbol(self, symbol):
if symbol in self.available_symbols:
raise NineMLUsageError(
"Duplicate Symbol '{}' found".format(symbol))
self.available_symbols.append(symbol)
def action_alias(self, alias, **kwargs): # @UnusedVariable
if alias in self.component_class.aliases:
self.add_symbol(symbol=alias.lhs)
self.aliases.append(alias)
def action_parameter(self, parameter, **kwargs): # @UnusedVariable @IgnorePep8
self.add_symbol(symbol=parameter.name)
def action_constant(self, constant, **kwargs): # @UnusedVariable @IgnorePep8
self.add_symbol(constant.name)
def default_action(self, obj, nineml_cls, **kwargs):
pass
class CheckNoLHSAssignmentsToMathsNamespaceComponentValidator(
BaseVisitor):
"""
This class checks that there is not a mathematical symbols, (e.g. pi, e)
on the left-hand-side of an equation
"""
def __init__(self, component_class, **kwargs): # @UnusedVariable
BaseVisitor.__init__(self)
self.visit(component_class)
def check_lhssymbol_is_valid(self, symbol):
assert isinstance(symbol, basestring)
if not is_valid_lhs_target(symbol):
err = 'Symbol: %s found on left-hand-side of an equation'
raise NineMLUsageError(err)
def action_parameter(self, parameter, **kwargs): # @UnusedVariable
self.check_lhssymbol_is_valid(parameter.name)
def action_alias(self, alias, **kwargs): # @UnusedVariable
self.check_lhssymbol_is_valid(alias.lhs)
def action_constant(self, constant, **kwargs): # @UnusedVariable
self.check_lhssymbol_is_valid(constant.name)
def default_action(self, obj, nineml_cls, **kwargs):
pass
class DimensionalityComponentValidator(BaseVisitorWithContext):
_RECURSION_MAX = 450
class DeclaredDimensionsVisitor(BaseVisitor):
"""
Inserts declared dimensions into dimensionality dictionary
before inferring dimensions from derived expressions
"""
def __init__(self, component_class, as_class, **kwargs):
BaseVisitor.__init__(self)
self._dimensions = {}
self.as_class = as_class
self.visit(component_class, **kwargs)
def default_action(self, obj, nineml_cls, **kwargs): # @UnusedVariable
if not isinstance(obj, SendPortBase):
try:
self._dimensions[obj.id] = sympify(obj.dimension)
except AttributeError:
# If element doesn't have dimension attribute
try:
self._dimensions[obj.id] = sympify(obj.units.dimension)
except AttributeError:
pass # If element doesn't have units attribute
@property
def dimensions(self):
return self._dimensions
def __init__(self, component_class, **kwargs): # @UnusedVariable @IgnorePep8
BaseVisitorWithContext.__init__(self)
self.component_class = component_class
self._dimensions = self.DeclaredDimensionsVisitor(
component_class, self.as_class, **kwargs).dimensions
self._recursion_count = 0
self.visit(component_class)
def _get_dimensions(self, element):
if isinstance(element, (sympy.Symbol, basestring)):
if element == sympy.Symbol('t'): # Reserved symbol 't'
return sympy.Symbol('t') # representation of the time dim.
name = Expression.symbol_to_str(element)
# Look back through the scope stack to find the referenced
# element
element = None
for context in reversed(self.contexts):
try:
element = context.parent.element(
name, child_types=context.parent_cls.nineml_children)
except KeyError:
pass
if element is None:
raise NineMLUsageError(
"Did not find '{}' in '{}' dynamics class (scopes: {})"
.format(name, self.component_class.name,
list(reversed([c.parent for c in self.contexts]))))
try:
expr = element.rhs
except AttributeError: # for basic sympy expressions
expr = element
try:
dims = self._dimensions[element.id]
self._recursion_count = 0
except (KeyError, AttributeError): # for derived dimensions
if self._recursion_count > self._RECURSION_MAX:
assert False, (
"'{}' is not defined.\nDefined symbols:\n{}"
"\n\nElements:\n{}".format(
expr, "\n".join(
str(e) for e in self._dimensions.keys()),
"\n".join(
str(e) for e in self.component_class.elements(
child_types=(
self.as_class.nineml_children)))
))
self._recursion_count += 1
dims = self._flatten_dims(expr, element)
self._dimensions[element.id] = dims
return dims
def _flatten_dims(self, expr, element):
if isinstance(expr, (sympy.Integer, sympy.Float, int, float)):
dims = 1
elif isinstance(expr, (BooleanTrue, BooleanFalse)):
dims = 0
elif isinstance(expr, sympy.Symbol):
dims = self._get_dimensions(expr)
elif isinstance(expr, sympy.Mul):
dims = reduce(operator.mul,
(self._flatten_dims(a, element) for a in expr.args))
if isinstance(dims, sympy.Basic):
dims = dims.powsimp()
elif isinstance(expr, sympy.Pow):
base = expr.args[0]
exponent = expr.args[1]
exp_dims = self._flatten_dims(exponent, element)
if exp_dims != 1:
raise NineMLDimensionError(self._construct_error_message(
"Exponents are required to be dimensionless arguments,"
" which was not the case in", exp_dims, expr, element))
base_dims = self._flatten_dims(base, element)
if base_dims != 1:
if not isinstance(exponent, (sympy.Integer, int,
sympy.numbers.NegativeOne)):
raise NineMLDimensionError(self._construct_error_message(
"Integer exponents are required for non-dimensionless "
"bases, which was not the case in", exp_dims, expr,
element))
dims = (self._flatten_dims(base, element) ** exponent)
elif isinstance(expr, sympy.Add):
dims = None
for arg in expr.args:
arg_dims = self._flatten_dims(arg, element)
if dims is None:
dims = arg_dims
elif arg_dims - dims != 0:
raise NineMLDimensionError(self._construct_error_message(
"Dimensions do not match within",
' + '.join(str(self._flatten_dims(a, element))
for a in expr.args), expr, element))
elif isinstance(expr, (sympy.GreaterThan, sympy.LessThan,
sympy.StrictGreaterThan, sympy.StrictLessThan)):
lhs_dims = self._flatten_dims(expr.args[0], element)
rhs_dims = self._flatten_dims(expr.args[1], element)
if lhs_dims - rhs_dims != 0:
raise NineMLDimensionError(self._construct_error_message(
"LHS/RHS dimensions of boolean expression",
lhs_dims - rhs_dims, expr, postamble="do not match"))
dims = 0 # boolean expression
elif isinstance(expr, (sympy.And, sympy.Or, sympy.Not)):
for arg in expr.args:
dims = self._flatten_dims(arg, element)
# boolean expression == 0
if dims != 0 and dims != 1:
raise NineMLDimensionError(self._construct_error_message(
"Logical expression provided non-boolean argument '{}'"
.format(arg), dims, expr))
elif isinstance(type(expr), sympy.FunctionClass):
for arg in expr.args:
arg_dims = self._flatten_dims(arg, element)
if arg_dims != 1:
raise NineMLDimensionError(self._construct_error_message(
"Dimensionless arguments required for function",
arg_dims, element=element, expr=arg))
dims = 1
elif (type(expr).__name__ in ('Pi',) or
isinstance(expr, sympy.Rational)):
dims = 1
elif isinstance(element, BaseNineMLObject):
assert False, ("{} was not added to pre-determined dimensions"
.format(element))
else:
raise NotImplementedError(
"Unrecognised type {} of expression '{}'"
.format(type(expr), expr))
return dims
def _compare_dimensionality(self, dimension, reference, element, ref_name):
if dimension - sympify(reference) != 0:
raise NineMLDimensionError(self._construct_error_message(
"Dimension of", dimension, element=element,
postamble=(" match that declared for '{}', {} ('{}')".format(
ref_name, sympify(reference), reference.name))))
def _check_send_port(self, port):
# Get the state variable or alias associated with the analog send
# port
element = self.component_class.element(
port.name, child_types=self.as_class.nineml_children)
try:
if element.dimension != port.dimension:
raise NineMLDimensionError(self._construct_error_message(
"Dimension of", sympify(element.dimension),
element=element, postamble=(
"does match attached send port dimension {} ('{}')"
.format(sympify(port.dimension),
port.dimension.name))))
except AttributeError: # If element doesn't have explicit dimension
self._compare_dimensionality(self._get_dimensions(element),
port.dimension, element, port.name)
def _construct_error_message(self, preamble, dimension, expr=None,
element=None, postamble=None):
if expr is None:
try:
expr = element.rhs
symbols = element.rhs_symbol_names
except AttributeError:
expr = ''
symbols = []
else:
symbols = expr.free_symbols
msg = preamble
if element is None:
msg += ' expression'
else:
msg += " {} '{}' in '{}'".format(
element.__class__.__name__, element.key,
self.component_class.name)
msg += ", {} [{}, with {}], ".format(
dimension, expr, ', '.join(
'{}={}'.format(a, self._get_dimensions(a)) for a in symbols))
if postamble is not None:
msg += postamble
return msg
def action_alias(self, alias, **kwargs): # @UnusedVariable
self._get_dimensions(alias)
def default_action(self, obj, nineml_cls, **kwargs):
pass