Last active
August 29, 2015 14:26
-
-
Save mstimberg/9c1706e9af67154c03fc to your computer and use it in GitHub Desktop.
Print Brian2 unit error messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from brian2 import * | |
# A little script to get an overview over the new error messages for unit | |
# errors in Brian 2 (in the form of a table that can be pasted into github) | |
if __name__ == '__main__': | |
@check_units(x=volt, y=1, result=amp) | |
def myfunc(x, y): | |
if y == 0: | |
return 1*mA | |
else: | |
return 1*ms | |
from __builtin__ import sum as python_sum | |
expressions = ['3*mV+5*mA', | |
'sin(5*mV)', | |
'np.where(True, (np.arange(10)*mV), (np.arange(10)*mA))', | |
'3*mV -1', | |
'1 - 3*mV', | |
'2 ** (3*mV)', | |
'Quantity(2, force_quantity=True) ** (3*mV)', | |
'3 % (3*mV)', | |
'3*mV % 5*mA', | |
'max(1*mV, 2*mV, 3*mA)', | |
'max([1*mV, 2*mV, 3*mA])', | |
'3*mV < 5*mA', | |
'3*mV >= 5*mA', | |
'3 >= 5*mA', | |
'3*mV >= 5', | |
'5*second == 3*volt', | |
'5*second != 3*volt', | |
'python_sum([3*mV, 4*mV, 5*mA])', | |
'hypot(3*um, 4*um)', | |
'hypot(3*um, 4)', | |
'hypot(3, 4*um)', | |
'myfunc(3*mA, 0)', | |
'myfunc(3*mV, 1*ms)', | |
'myfunc(3*mA, 1*ms)', | |
'myfunc(3*mV, 1)', | |
] | |
G = NeuronGroup(10, '''x : 1 | |
y : volt''', threshold='True') | |
# If G.x would only have zeros, it could be used in the "0 has any units" way | |
G.x = np.arange(10) | |
statements = ['G.x = 3*mV', | |
'G.x = np.arange(10)*mV', | |
'G.y = 2*mA', | |
'G.y = 2', | |
'G.y = np.arange(10)*mA', | |
'G.y = np.arange(10)', | |
'G.y += G.x', | |
'G.y = G.x + G.y', | |
'G.x += G.y', | |
'G.x = G.x + G.y', | |
'x = np.arange(3)*mV; x += 1', | |
'x = np.arange(3)*mV; x += 1*mA', | |
'S = Synapses(G, G, "", pre="x+=1", delay=5*amp)', | |
'G2 = NeuronGroup(3, "dv/dt = -v/(10*ms) : 1 (unless refractory)", refractory=5*amp)'] | |
print 'Expression/Statement | Error message' | |
print '-------------------- | -------------' | |
for expr in expressions: | |
try: | |
result = eval(expr) | |
except DimensionMismatchError as ex: | |
print '`%s` | `%s`' % (expr, ex) | |
for statement in statements: | |
try: | |
exec(statement) | |
except DimensionMismatchError as ex: | |
print '`%s` | `%s`' % (statement, ex) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment