DimensionMismatchError: Inconsistent Units Error

Inconsistent Units Error

Hi, I am a new user of brian so maybe its an amateur question but i was hoping if someone could help me figure out where I am going wrong in writing the following equations. I keep getting inconsistent unit error and my hunch is it is coming from the expression ‘’’ ((2.0mV)exp((-53.5mV-Vd)/(27.0mV)) ‘’’ in betacd however I’m not sure what I have written incorrectly.
Here are the equations:
image

If anyone can help me spot it, it would be a great help!

Minimal code to reproduce problem

# KC potassium current

alphacd = (((1.0*mV-Vd-10.0*mV)/mV)*exp((Vd+50.0*mV)/(11*mV)-(Vd+53.5*mV)/(27*mV))/18.975)/ms+(Vd+10.0*mV)*(2.0/mV)*exp((-53.5*mV-Vd)/(27.0*mV))/ms: Hz

betacd= int(Vd <= -10*mV)*((2.0*mV)*exp((-53.5*mV-Vd)/(27.0*mV))-alphacd): Hz

What you have already tried

Expected output (if relevant)

Actual output (if relevant)

Full traceback of error (if relevant)

Here is the traceback,

---------------------------------------------------------------------------
DimensionMismatchError                    Traceback (most recent call last)
File c:\Users\parih\anaconda3\lib\site-packages\brian2\equations\equations.py:1189, in Equations.check_units(self, group, run_namespace)
   1188 try:
-> 1189     check_dimensions(str(eq.expr), self.dimensions[var], all_variables)
   1190 except DimensionMismatchError as ex:

File c:\Users\parih\anaconda3\lib\site-packages\brian2\equations\unitcheck.py:41, in check_dimensions(expression, dimensions, variables)
     20 """
     21 Compares the physical dimensions of an expression to expected dimensions in
     22 a given namespace.
   (...)
     39     If an unit mismatch occurs during the evaluation.
     40 """
---> 41 expr_dims = parse_expression_dimensions(expression, variables)
     42 expected = repr(get_unit(dimensions))

File c:\Users\parih\anaconda3\lib\site-packages\brian2\parsing\expressions.py:382, in parse_expression_dimensions(expr, variables, orig_expr)
    379 left_dim = parse_expression_dimensions(
    380     expr.left, variables, orig_expr=orig_expr
    381 )
--> 382 right_dim = parse_expression_dimensions(
    383     expr.right, variables, orig_expr=orig_expr
    384 )
    385 if op in ["Add", "Sub", "Mod"]:
    386     # dimensions should be the same

File c:\Users\parih\anaconda3\lib\site-packages\brian2\parsing\expressions.py:399, in parse_expression_dimensions(expr, variables, orig_expr)
    393     error_msg = (
    394         f"Expression '{left_str} {op_symbol} {right_str}' uses "
    395         f"inconsistent units ('{left_str}' has unit "
    396         f"{left_unit}; '{right_str}' "
    397         f"has unit {right_unit})."
    398     )
--> 399     raise DimensionMismatchError(error_msg)
    400 u = left_dim

DimensionMismatchError: Expression '(2.0 * mV) * exp((((- 53.5) * mV) - Vd) / (27.0 * mV)) - alphacd' uses inconsistent units ('(2.0 * mV) * exp((((- 53.5) * mV) - Vd) / (27.0 * mV))' has unit V; 'alphacd' has unit Hz).

Hi,

It is hard to understand what the units are supposed to be without the rest of the equations, but the left term of your \beta_c is in mV (because you multiplied 2.0*mV), whereas the term on the right is in Hz (because you divided everyone by ms when calculating \alpha_c). You just need to ensure that the left term of \beta_c is also in Hz, like you did for \alpha_c. Just be careful not to change your original equations by simply removing units! The units are there for a reason.

Cheers

1 Like

Thank you for your suggestion. I actually updated the equations as I realized I had made some small errors in writing them previously. The updated equations are:

alphacd = (((1.0 - (Vd + 10.0 * mV) / mV) * exp((Vd + 50.0 * mV) / (11 * mV) - (Vd + 53.5 * mV) / (27.0 * mV)) / 18.975) +
           (Vd + 10.0 * mV) * (2.0 / mV) * exp((-53.5 * mV - Vd) / (27.0 * mV))) / ms : Hz

betacd = (1.0 - (Vd + 10.0 * mV) / mV) * (2.0 / mV) * exp((-53.5 * mV - Vd) / (27.0 * mV))/ms - alphacd : Hz

Although, I’m unsure how to check the units of the values of the variables inside these equations. Because while getting the same dimensionmismatch error, I still think the mismatch occurs in the middle part of betacd ((2.0*mV)*exp((-53.5*mV-Vd)/(27.0*mV)). By checking the units inside the equations, I mean once the variable names with ‘eqs’ is set and run, how can we check their values as the simulation runs.
If it helps, Vd is the membrane potential with unit in mV and the equations describe rate of calcium induced potassium current. I don’t mind sharing the rest of the equations but they are a very long list.

Here is the traceback if it helps:

Traceback (most recent call last):
An error occurred when preparing an object. (See above for original error message and traceback.)
(base) PS C:\Users\parih\OneDrive\Documents\GitHub\Microcircuit_Model> & C:/Users/parih/anaconda3/python.exe c:/Users/parih/OneDrive/Documents/GitHub/Microcircuit_Model/Model.py
ERROR      Brian 2 encountered an unexpected error. If you think this is a bug in Brian 2, please report this issue either to the discourse forum at <http://brian.discourse.group/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: C:\Users\parih\AppData\Local\Temp\brian_debug_4cqx6e03.log  Additionally, you can also include a copy of the script that was run, available at: C:\Users\parih\AppData\Local\Temp\brian_script_uv4w4uch.py Thanks! [brian2]
Traceback (most recent call last):
  File "C:\Users\parih\anaconda3\lib\site-packages\brian2\equations\equations.py", line 1189, in check_units
    check_dimensions(str(eq.expr), self.dimensions[var], all_variables)
  File "C:\Users\parih\anaconda3\lib\site-packages\brian2\equations\unitcheck.py", line 41, in check_dimensions
    expr_dims = parse_expression_dimensions(expression, variables)
  File "C:\Users\parih\anaconda3\lib\site-packages\brian2\parsing\expressions.py", line 399, in parse_expression_dimensions        
    raise DimensionMismatchError(error_msg)
brian2.units.fundamentalunits.DimensionMismatchError: Expression '(((1.0 - ((Vd + (10.0 * mV)) / mV)) * (2.0 / mV)) * exp((((- 53.5) * mV) - Vd) / (27.0 * mV))) / ms - alphacd' uses inconsistent units ('(((1.0 - ((Vd + (10.0 * mV)) / mV)) * (2.0 / mV)) * exp((((- 53.5) * mV) - Vd) / (27.0 * mV))) / ms' has unit m^-2 kg^-1 s^2 A; 'alphacd' has unit Hz).

Hi @bindok,
the error message states that

(((1.0 - ((Vd + (10.0 * mV)) / mV)) * (2.0 / mV)) * exp((((- 53.5) * mV) - Vd) / (27.0 * mV))) / ms - alphacd

has inconsistent units, since alphacd is a rate function with units of 1/second (or Hz), and the first part (i.e. (((1.0 - ((Vd + (10.0 * mV)) / mV)) * (2.0 / mV)) * exp((((- 53.5) * mV) - Vd) / (27.0 * mV))) / ms) has units m^-2 kg^-1 s^2 A. Since the two units do not match, you cannot subtract one from the other.
Unfortunately, m^-2 kg^-1 s^2 A is not that helpful, a more helpful description would have been the equivalent 1/(volt*second). So for your definition to be consistent, you need something in 1/second but you have something in 1/(volt*second). In other words, you have an incorrect division by volt somewhere, From your equations, this is the 2.0/mV expression, which should just be 2.0 (or one could argue that it would be clearer if it were 2.0/ms, and you’d remove the division by ms in the end). Note that if you fix this error, you will get the same error for alphacd, if I am not mistaken.

A final remark: in your equations, you convert the seemingly dimensionless quantities such as -53.5 to their actual unit of volt by multiplying them by mV. This is correct (and it is unfortunate that many papers gloss over these details). However, it is often easier to do the opposite, i.e make the variable with units (in your case, Vd) dimensionless. Instead of

betacd = (1.0 - (Vd + 10.0 * mV) / mV) * 2.0 * exp((-53.5 * mV - Vd) / (27.0 * mV))/ms - alphacd : Hz

you’d write

betacd = (1.0 - Vd/mV + 10.0) * 2.0 * exp((-53.5 - Vd/mV) / 27.0)/ms - alphacd : Hz

If you want to get even closer to “unitless” equations, you can also define a new variable that is the same as Vd, but as a dimensionless value in mV:

Vd_2 = Vd/mV : 1
betacd = (1.0 - Vd_2 + 10.0) * 2.0 * exp((-53.5 - Vd_2) / 27.0)/ms - alphacd : Hz

Hope that makes things clearer. But I also agree that it would be very useful to have some tool that makes it easier to debug these kind of issues. If you have any concrete idea of what this could look like, please let me know :slight_smile: