Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured equations with equation templates #1244

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ff28463
Dummy implementation for discussion
mstimberg Oct 13, 2020
27c2cb8
Example using templates
mstimberg Oct 15, 2020
76c5d42
WIP: exploring a few more options for modular template equations
mstimberg Oct 22, 2020
3f2d253
Better support nested equations/expressions
mstimberg Oct 23, 2020
d0945bf
Merge Equations and EquationTemplate
mstimberg Oct 23, 2020
74341eb
Merge Expression and ExpressionTemplate
mstimberg Oct 23, 2020
bd5b5e8
Fix a minor replacement bug
mstimberg Oct 23, 2020
9b8ed84
Use the template approach for all currents in Rothman&Manis example
mstimberg Oct 23, 2020
1d58e0e
fix a few replacement bugs
mstimberg Oct 26, 2020
068fdb5
Give access to template identifiers in expressions/equations
mstimberg Oct 26, 2020
ab7d29a
WIP: replace dependencies in order
mstimberg Oct 26, 2020
f934614
Refactor/simplify equation substitution code
mstimberg Oct 28, 2020
e20fb64
Fix a minor replacement issue
mstimberg Oct 28, 2020
f294931
Raise an error for an unused replacement argument
mstimberg Oct 29, 2020
2820567
Raise a warning for ambiguous replacements
mstimberg Oct 29, 2020
a2085e0
Make Expression substitution consistent with Equations
mstimberg Oct 29, 2020
ecee6cb
Raise an error if templates are used in NeuronGroup/Synapses
mstimberg Oct 29, 2020
a0d0f24
Fix replacements with equations without expressions (i.e. parameters)
mstimberg Nov 3, 2020
28e3f46
Store original equations before substitution
mstimberg Dec 31, 2020
deb9139
Merge branch 'master' into equation_template
mstimberg Dec 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP: replace dependencies in order
  • Loading branch information
mstimberg committed Oct 26, 2020
commit ab7d29ae6685c9da008007e9bdf9249040a2daf5
21 changes: 20 additions & 1 deletion brian2/equations/equations.py
Original file line number Diff line number Diff line change
@@ -612,6 +612,23 @@ def _substitute(self, replacements):
if len(replacements) == 0:
return self._equations

dependencies = {}
for to_replace, replacement in replacements.items():
if not isinstance(replacement, Sequence) or isinstance(replacement, str):
replacement = [replacement]
for one_replacement in replacement:
dependencies[to_replace] = set()
if not isinstance(one_replacement, (numbers.Number, Quantity, str, Expression, Equations)):
raise TypeError(f'Cannot use an object of type \'{type(one_replacement)}\''
f'to replace \'{to_replace}\'')
if isinstance(one_replacement, (Expression, Equations)):
dependencies[to_replace] |= one_replacement.template_identifiers
# We only care about dependencies to values that are replaced at the same time
for dep_key, deps in dependencies.items():
dependencies[dep_key] = {d for d in deps if d in dependencies}

replacements_in_order = topsort(dependencies)[::-1]

new_equations = {}
# First, do replacements for equations/expressions to allow other values to
# replace values in them
@@ -695,13 +712,15 @@ def _substitute(self, replacements):
# Replace the name of a model variable (works only for strings)
new_varname = eq.varname
for to_replace, replacement in replacements.items():
if isinstance(replacement, (Expression, Equations)) or isinstance(replacement, Sequence) and not isinstance(replacement, str):
continue
if '{' + to_replace + '}' in eq.varname:
if not isinstance(replacement, str):
raise ValueError(f'Cannot replace variable name \'{eq.varname}\' with value '
f'\'{repr(replacement)}\'')
new_varname = new_varname.replace('{' + to_replace + '}', replacement)
elif eq.varname == to_replace:
if not isinstance(replacement, str):
if isinstance(replacement, (str)):
raise ValueError(f'Cannot replace variable name \'{eq.varname}\' with value '
f'\'{repr(replacement)}\'')
new_varname = replacement