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

Conflicting function variable names fail to give resolution conflict warning. #1341

Open
denisalevi opened this issue Sep 2, 2021 · 1 comment

Comments

@denisalevi
Copy link
Member

Hi there,

I just ran into the following issue. When I define a variable in the namespace of an equation and the variable is also an internal variable, I get a warning that the internal variable is used. E.g.

from brian2 import *

num_neurons = 10
num_time_steps = 10

xi_custom = 1

eqs = """
dV/dt = (-V + sqrt(ms) * xi_custom)/ms : 1
"""
group = NeuronGroup(num_neurons, eqs)

run(num_time_steps * defaultclock.dt)

Gives

WARNING    "xi_custom" is an internal variable of group "neurongroup", but also exists in the run namespace with the value 1. The internal variable will be used. [brian2.groups.group.Group.resolve.resolution_conflict]

But if that namespace variable is a function instead, the dimensions check raises an error before the resolution conflict warning is given. E.g.:

from brian2 import *

num_neurons = 10
num_time_steps = 10
xi_custom = TimedArray(
    np.random.rand(num_time_steps, num_neurons) / np.sqrt(defaultclock.dt),
    dt=defaultclock.dt
)

eqs = """
dV/dt = (-V + sqrt(ms) * xi_custom(t, i))/ms : 1
"""
group = NeuronGroup(num_neurons, eqs)

run(num_time_steps * defaultclock.dt)

does not give the helpful resolution conflict warning but instead raises the following error:

ERROR      Brian 2 encountered an unexpected error. If you think this is bug in Brian 2, please report this issue either to the mailing list at <http://groups.google.com/group/brian-development/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: /tmp/brian_debug_6m7aulf4.log  Additionally, you can also include a copy of the script that was run, available at: /tmp/brian_script_ju25mfpx.py Thanks! [brian2]
Traceback (most recent call last):
  File "/path/to/brian2/brian2/core/network.py", line 897, in before_run
    obj.before_run(run_namespace)
  File "/path/to/brian2/brian2/groups/neurongroup.py", line 884, in before_run
    self.equations.check_units(self, run_namespace=run_namespace)
  File "/path/to/brian2/brian2/equations/equations.py", line 956, in check_units
    check_dimensions(str(eq.expr), self.dimensions[var] / second.dim,
  File "/path/to/brian2/brian2/equations/unitcheck.py", line 41, in check_dimensions
    expr_dims = parse_expression_dimensions(expression, variables)
  File "/path/to/brian2/brian2/parsing/expressions.py", line 323, in parse_expression_dimensions
    left_dim = parse_expression_dimensions(expr.left, variables)
  File "/path/to/brian2/brian2/parsing/expressions.py", line 324, in parse_expression_dimensions
    right_dim = parse_expression_dimensions(expr.right, variables)
  File "/path/to/brian2/brian2/parsing/expressions.py", line 324, in parse_expression_dimensions
    right_dim = parse_expression_dimensions(expr.right, variables)
  File "/path/to/brian2/brian2/parsing/expressions.py", line 279, in parse_expression_dimensions
    raise ValueError(('Function %s does not specify how it '
ValueError: Function xi_custom does not specify how it deals with units.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/bla.py", line 15, in <module>
    run(num_time_steps * defaultclock.dt)
  File "/path/to/brian2/brian2/units/fundamentalunits.py", line 2385, in new_f
    result = f(*args, **kwds)
  File "/path/to/brian2/brian2/core/magic.py", line 373, in run
    return magic_network.run(duration, report=report, report_period=report_period,
  File "/path/to/brian2/brian2/core/magic.py", line 231, in run
    Network.run(self, duration, report=report, report_period=report_period,
  File "/path/to/brian2/brian2/core/base.py", line 276, in device_override_decorated_function
    return func(*args, **kwds)
  File "/path/to/brian2/brian2/units/fundamentalunits.py", line 2385, in new_f
    result = f(*args, **kwds)
  File "/path/to/brian2/brian2/core/network.py", line 1008, in run
    self.before_run(namespace)
  File "/path/to/brian2/brian2/core/base.py", line 276, in device_override_decorated_function
    return func(*args, **kwds)
  File "/path/to/brian2/brian2/core/network.py", line 899, in before_run
    raise brian_object_exception("An error occurred when preparing an object.", obj, ex)
brian2.core.base.BrianObjectException: Original error and traceback:
Traceback (most recent call last):
  File "/path/to/brian2/brian2/core/network.py", line 897, in before_run
    obj.before_run(run_namespace)
  File "/path/to/brian2/brian2/groups/neurongroup.py", line 884, in before_run
    self.equations.check_units(self, run_namespace=run_namespace)
  File "/path/to/brian2/brian2/equations/equations.py", line 956, in check_units
    check_dimensions(str(eq.expr), self.dimensions[var] / second.dim,
  File "/path/to/brian2/brian2/equations/unitcheck.py", line 41, in check_dimensions
    expr_dims = parse_expression_dimensions(expression, variables)
  File "/path/to/brian2/brian2/parsing/expressions.py", line 323, in parse_expression_dimensions
    left_dim = parse_expression_dimensions(expr.left, variables)
  File "/path/to/brian2/brian2/parsing/expressions.py", line 324, in parse_expression_dimensions
    right_dim = parse_expression_dimensions(expr.right, variables)
  File "/path/to/brian2/brian2/parsing/expressions.py", line 324, in parse_expression_dimensions
    right_dim = parse_expression_dimensions(expr.right, variables)
  File "/path/to/brian2/brian2/parsing/expressions.py", line 279, in parse_expression_dimensions
    raise ValueError(('Function %s does not specify how it '
ValueError: Function xi_custom does not specify how it deals with units.

Error encountered with object named "neurongroup".
Object was created here (most recent call only, full details in debug log):
  File "/tmp/bla.py", line 13, in <module>
    group = NeuronGroup(num_neurons, eqs)

An error occurred when preparing an object. ValueError: Function xi_custom does not specify how it deals with units.
(See above for original error message and traceback.)

Two issues here:

  1. Is there a way to have the resolution conflict run before the unit expression raises an error? It is rather tricky to figure out why things are failing in the second example, because the function defined in namespace does correctly define how it deals with functions, but brian uses the internal xi_custom variable and fails to warn the user about that.
  2. I couldn't really find anything about xi_<custom_name> variables in the user documentation. I came across is only by searching the source code. Could it be that it isn't documented that one can use xi_<custom_name> for noise variables? Or did I just not find it?
@denisalevi
Copy link
Member Author

Found the xi_ documented. And very prominently... Don't know why I didn't see it. So forget about my 2. point :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant