%load_ext autoreload
%autoreload 2

Quantities and Units

-PyUnitWizard provides with a unique simple API to work with quantities and units with different pythonic forms-

Since there are many libraries to work with physical quantities, the same quantity can be stored as different python objects, or in the language of PyUniWizard, with different forms, thanks to the method pyunitwizard.quantity():

import pyunitwizard as puw
import numpy as np
q = puw.quantity(value=3.0, unit='joules', form='openmm.unit')

The form name of a quantity variable can be obtained with the method pyunitwizard.get_form():

puw.get_form(q)
'openmm.unit'

And the value and unit of a quantity can be obtained with pyunitwizard.get_value() and pyunitwizard.get_unit():

puw.get_value(q)
3.0
puw.get_unit(q)
Unit({ScaledUnit(factor=1.0, master=meter*newton, name='joule', symbol='J'): 1.0})

Let’s see other examples of how to use pyunitwizard.quantity() to make quantities:

q = puw.quantity([0,1,2], 'angstroms', form='pint')
q
Magnitude
[0 1 2]
Unitsangstrom
q = puw.quantity(np.zeros(shape=[2,6,3]), 'nm/ps', form='pint')
q
Magnitude
[[[0.0 0.0 0.0]
[0.0 0.0 0.0]
[0.0 0.0 0.0]
[0.0 0.0 0.0]
[0.0 0.0 0.0]
[0.0 0.0 0.0]]

[[0.0 0.0 0.0]
[0.0 0.0 0.0]
[0.0 0.0 0.0]
[0.0 0.0 0.0]
[0.0 0.0 0.0]
[0.0 0.0 0.0]]]
Unitsnanometer/picosecond
q = puw.quantity('1.0 nm/ps', form='openmm.unit')
q
Quantity(value=1, unit=nanometer/picosecond)

Units can be created in a similar way with pyunitwizard.unit():

u = puw.unit('kcal/mol', form='pint')
u
kilocalorie/mole
puw.get_form(u)
'pint'
u = puw.unit('N/nm**2', form='openmm.unit')
u
Unit({BaseUnit(base_dim=BaseDimension("length"), name="nanometer", symbol="nm"): -2.0, ScaledUnit(factor=1.0, master=kilogram*meter/(second**2), name='newton', symbol='N'): 1.0})

And two auxiliary methods, pyunitwizard.is_quantity() and pyunitwizard.is_unit(), can be used to check if a variable is a quantity or a unit object no matter their form:

puw.is_quantity(q)
True
puw.is_quantity(u)
False
puw.is_unit(u)
True

Finnally, quantities and units with different forms can be translated into strings:

q
Quantity(value=1, unit=nanometer/picosecond)
puw.convert(q, to_form='string')
'1 nm/ps'
puw.convert(u, to_form='string')
'newton/(nanometer**2)'

Additionally, given that some of the libraries supported by PyUnitWizard, as pint, can parse strings to make quantities, this wizard takes advantage of this great functionality to convert strings to any quantity form (see section ‘Working with strings’):

puw.convert('10.0 Å -0.5 nm', to_form='openmm.unit')
Quantity(value=5.0, unit=angstrom)
puw.convert('1.0 mol/L + 3.5 mol/dL', to_form='pint')
36.0 mole/liter

Or to any unit form:

puw.convert('1.0 (mol/L)/picosecond', to_form='pint', to_type='unit')
mole/(liter picosecond)

Quantities can be concatenated into a list, tuple or numpy ndarray:

puw.concatenate([puw.quantity(1, 'nm'), puw.quantity(2, 'nm'), puw.quantity(3, 'nm')],
               to_object='list')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[22], line 1
----> 1 puw.concatenate([puw.quantity(1, 'nm'), puw.quantity(2, 'nm'), puw.quantity(3, 'nm')],
      2                to_object='list')

TypeError: concatenate() got an unexpected keyword argument 'to_object'

The method concatenate has some useful input arguments:

puw.concatenate([puw.quantity(1, 'nm'), puw.quantity(2, 'nm'), puw.quantity(3, 'nm')],
               to_unit='angstroms', to_form='string')
'[10.0 20.0 30.0] angstrom'