In Brian 1, a distinction is made between scalars and numpy arrays (including scalar arrays): Scalars could be multiplied with a unit, resulting in a Quantity object whereas the multiplication of an array with a unit resulted in a (unitless) array. Accordingly, scalars were considered as dimensionless quantities for the purpose of unit checking (e.g.. 1 + 1 * mV raised an error) whereas arrays were not (e.g. array(1) + 1 * mV resulted in 1.001 without any errors). Brian 2 no longer makes this distinction and treats both scalars and arrays as dimensionless for unit checking and make all operations involving quantities return a quantity.:
>>> 1 + 1*second Traceback (most recent call last): ... DimensionMismatchError: Cannot calculate 1. s + 1, units do not match (units are second and 1). >>> np.array() + 1*second Traceback (most recent call last): ... DimensionMismatchError: Cannot calculate 1. s + , units do not match (units are second and 1). >>> 1*second + 1*second 2. * second >>> np.array()*second + 1*second array([ 2.]) * second
As one exception from this rule, a scalar or array
0 is considered as having
“any unit”, i.e.
0 + 1 * second will result in
1 * second without a
dimension mismatch error and
0 == 0 * mV will evaluate to
seems reasonable from a mathematical viewpoint and makes some sources of error
disappear. For example, the Python builtin
sum (not numpy’s version) adds
the value of the optional argument
start, which defaults to 0, to its
main argument. Without this exception,
sum([1 * mV, 2 * mV]) would therefore
raise an error.
The above rules also apply to all comparisons (e.g.
<) with one
-inf also have “any unit”, therefore an
v <= inf will never raise an exception (and always return
Functions and units¶
All methods that make sense on quantities should work, i.e. they check for the correct units of their arguments and return quantities with units were appropriate. Most of the methods are overwritten using thin function wrappers:
- Strips away the units before giving the array to the method of
ndarray, then reattaches the unit to the result (examples: sum, mean, max)
- Changes the dimensions in a simple way that is independent of function arguments, the shape of the array, etc. (examples: sqrt, var, power)
- Raises an error if the method is called on a quantity with dimensions (i.e. it works on dimensionless quantities).
List of methods
- Methods directly working on the internal data buffer (
newbyteorder) ignore the dimensions of the quantity.
- The type of a quantity cannot be int, therefore
astypedoes not quite work when trying to convert the array into integers.
chooseis only defined for integer arrays and therefore does not work
tofileonly return/save the pure array data without the unit (but you can use
dumpsto pickle a quantity array)
resizedoes not work:
ValueError: cannot resize this array: it does not own its data
cumprodwould result in different dimensions for different elements and is therefore forbidden
itemreturns a pure Python float by definition
itemsetdoes not check for units
All of the standard numpy ufuncs (functions that operate element-wise on numpy
arrays) are supported, meaning that they check for correct units and return
appropriate arrays. These functions are often called implicitly, for example
when using operators like
- Math operations:
- Trigonometric functions:
- Bitwise functions:
- Comparison functions:
- Floating functions:
Not taken care of yet:
- Everything involving
exp, as well as trigonometric functions only works on dimensionless array (for
hypotthis is questionable, though)
- Unit arrays can only be raised to a scalar power, not to an array of exponents as this would lead to differing dimensions across entries. For simplicity, this is enforced even for dimensionless quantities.
- Bitwise functions never works on quantities (numpy will by itself throw a
TypeErrorbecause they are floats not integers).
- All comparisons only work for matching dimensions (with the exception of always allowing comparisons to 0) and return a pure boolean array.
- All logical functions treat quantities as boolean values in the same way as floats are treated as boolean: Any non-zero value is True.
Many numpy functions are functional versions of ndarray methods (e.g.
clip). They therefore work automatically when called on quantities,
as numpy propagates the call to the respective method.
There are some functions in numpy that do not propagate their call to the
corresponding method (because they use np.asarray instead of np.asanyarray,
which might actually be a bug in numpy):
dot. For these, wrapped functions in
unitsafefunctions.py are provided.
Wrapped numpy functions in unitsafefunctions.py
These functions are thin wrappers around the numpy functions to correctly check for units and return quantities when appropriate:
numpy functions that work unchanged
This includes all functional counterparts of the methods mentioned above (with the exceptions mentioned above). Some other functions also work correctly, as they are only using functions/methods that work with quantities:
|||(1, 2) But does not care about the units of its input.|
numpy functions that return a pure numpy array instead of quantities
numpy functions that do something wrong
delete(return a quantity array but without units)
correlate(returns a quantity with wrong units)