%load_ext autoreload
%autoreload 2

Standardization

-If quantities have to be handled in your project with a default set of dimensions, PyUnitWizard can help you defining a set of standard units-

Let’s suppose we are working with observables and magnitudes encoded as pythonic quantities coming from different tools -with different forms-. PyUnitWizard, in addition to work with a default form defined at any moment, can also standardize quantities to a set of units of the users election:

import pyunitwizard as puw

With the method puw.configure.set_standard_units() the list of units as standards can be set:

puw.configure.set_standard_units(['nm', 'ps', 'K', 'mole', 'amu', 'e',
                                 'kcal/mol', 'N/nm**2', 'N', 'degrees'])
puw.configure.get_standard_units()
{'nm': {'[L]': 1,
  '[M]': 0,
  '[T]': 0,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'ps': {'[L]': 0,
  '[M]': 0,
  '[T]': 1,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'K': {'[L]': 0,
  '[M]': 0,
  '[T]': 0,
  '[K]': 1,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'mole': {'[L]': 0,
  '[M]': 0,
  '[T]': 0,
  '[K]': 0,
  '[mol]': 1,
  '[A]': 0,
  '[Cd]': 0},
 'amu': {'[L]': 0,
  '[M]': 1,
  '[T]': 0,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'e': {'[L]': 0,
  '[M]': 0,
  '[T]': 1,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 1,
  '[Cd]': 0},
 'kcal/mol': {'[L]': 2,
  '[M]': 1,
  '[T]': -2,
  '[K]': 0,
  '[mol]': -1,
  '[A]': 0,
  '[Cd]': 0},
 'N/nm**2': {'[L]': -1,
  '[M]': 1,
  '[T]': -2,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'N': {'[L]': 1,
  '[M]': 1,
  '[T]': -2,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'degrees': {'[L]': 0,
  '[M]': 0,
  '[T]': 0,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0}}

Some of these standards are fundamental units, some are combinations, and others can be adimensional:

puw.kernel.dimensional_fundamental_standards
{'nm': array([1., 0., 0., 0., 0., 0., 0.]),
 'ps': array([0., 0., 1., 0., 0., 0., 0.]),
 'K': array([0., 0., 0., 1., 0., 0., 0.]),
 'mole': array([0., 0., 0., 0., 1., 0., 0.]),
 'amu': array([0., 1., 0., 0., 0., 0., 0.])}
puw.kernel.dimensional_combinations_standards
{'e': array([0., 0., 1., 0., 0., 1., 0.]),
 'kcal/mol': array([ 2.,  1., -2.,  0., -1.,  0.,  0.]),
 'N/nm**2': array([-1.,  1., -2.,  0.,  0.,  0.,  0.]),
 'N': array([ 1.,  1., -2.,  0.,  0.,  0.,  0.])}
puw.kernel.adimensional_standards
{'degrees': array([0., 0., 0., 0., 0., 0., 0.])}
puw.kernel.tentative_base_standards
{'nm': array([1., 0., 0., 0., 0., 0., 0.]),
 'ps': array([0., 0., 1., 0., 0., 0., 0.]),
 'K': array([0., 0., 0., 1., 0., 0., 0.]),
 'mole': array([0., 0., 0., 0., 1., 0., 0.]),
 'amu': array([0., 1., 0., 0., 0., 0., 0.]),
 'e': array([0., 0., 1., 0., 0., 1., 0.])}

This way, the method pyunitwizard.standardize() converts any quantity to the default form with the standard units:

q = puw.quantity(450, 'km/hour', form='openmm.unit')
output = puw.standardize(q)
print('{} as {} quantity'.format(output, puw.get_form(output)))
0.12499999999999997 nanometer / picosecond as pint quantity
q = puw.quantity(1.4, 'kJ/mol')
output = puw.standardize(q)
print('{} as {} quantity'.format(output, puw.get_form(output)))
0.3346080305927342 kilocalorie / mole as pint quantity
q = puw.quantity(0.250, 'kN/nm**2')
output = puw.standardize(q)
print('{} as {} quantity'.format(output, puw.get_form(output)))
250.0 newton / nanometer ** 2 as pint quantity
q = puw.quantity(9.8, 'amu*m/s**2')
output = puw.standardize(q)
print('{} as {} quantity'.format(output, puw.get_form(output)))
1.627328285268e-26 newton as pint quantity
puw.configure.set_standard_units(['nm', 'ps', 'K', 'kcal', 'mol', 'radians'])
puw.configure.get_standard_units()
{'nm': {'[L]': 1,
  '[M]': 0,
  '[T]': 0,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'ps': {'[L]': 0,
  '[M]': 0,
  '[T]': 1,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'K': {'[L]': 0,
  '[M]': 0,
  '[T]': 0,
  '[K]': 1,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'kcal': {'[L]': 2,
  '[M]': 1,
  '[T]': -2,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0},
 'mol': {'[L]': 0,
  '[M]': 0,
  '[T]': 0,
  '[K]': 0,
  '[mol]': 1,
  '[A]': 0,
  '[Cd]': 0},
 'radians': {'[L]': 0,
  '[M]': 0,
  '[T]': 0,
  '[K]': 0,
  '[mol]': 0,
  '[A]': 0,
  '[Cd]': 0}}
puw.kernel.dimensional_fundamental_standards
{'nm': array([1., 0., 0., 0., 0., 0., 0.]),
 'ps': array([0., 0., 1., 0., 0., 0., 0.]),
 'K': array([0., 0., 0., 1., 0., 0., 0.]),
 'mol': array([0., 0., 0., 0., 1., 0., 0.])}
puw.kernel.tentative_base_standards
{'nm': array([1., 0., 0., 0., 0., 0., 0.]),
 'ps': array([0., 0., 1., 0., 0., 0., 0.]),
 'K': array([0., 0., 0., 1., 0., 0., 0.]),
 'mol': array([0., 0., 0., 0., 1., 0., 0.]),
 'kcal': array([ 2.,  1., -2.,  0.,  0.,  0.,  0.])}
q = puw.quantity(1.4, 'kJ/mol')
output = puw.standardize(q)
print('{} as {} quantity'.format(output, puw.get_form(output)))
0.3346080305927342 kilocalorie / mole as pint quantity
q = puw.quantity(30, 'degrees')
output = puw.standardize(q)
print('{} as {} quantity'.format(output, puw.get_form(output)))
0.5235987755982988 radian as pint quantity