Physical quantities
Physical quantities, that is numbers accompanied by a physical unit, are represented by the PhysicalQuantity
parameter type. One main feature of that parameter type is that it handles any necessary unit conversion from specified unit to declared unit automatically.
As can be seen from the example above physical quantities are specified using the following syntax: "<magnitude> [<unit>]"
. The specified value is then loaded and converted to the declared unit. Note that the unit is only used for loading the parameter and for applying the appropriate conversions while the parameter's resulting value is represented by a float
with the declared unit assumed implicitly.
Alternative ways to declare units
Using a string for the unit declaration is in fact a shorthand. Behind the scenes runs a unit engine which is based on a specific python package. Different engines are backed by different packages and are intended to support a different range of features. The user can choose the unit engine they want to use. The default engine is backed by Pint.
In order to use a specific unit engine (also the default engine!) you need to manually install the corresponding package. This behavior intends to keep the dependencies of the framework at a minimum (since not necessarily every user wants to use physical quantities). You can check the backend of each engine by looking at the Engine.backend_url
attribute.
So suppose you have the Pint package installed then you can use Pint's native units for declaration of physical quantities:
The unit of a PhysicalQuantity
can be changed later on via Diode.threshold.unit = 'mV'
(instead of str
you can also use one the above alternatives).
Selecting the unit engine
Three different engines are available (in the module hanna.physics.units
), each backed by a different Python package:
Pint
(backed by Pint; default)NumericalUnits
(backed by numericalunits)Units
(backed by units)
We can change the active engine in the following way:
Last updated