Skip to content

Commit

Permalink
Merge pull request #34 from dprada/main
Browse files Browse the repository at this point in the history
Bug fixed with units translation
  • Loading branch information
dprada authored Dec 6, 2022
2 parents 398b297 + 1d12d3d commit fc8e3d9
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 151 deletions.
3 changes: 3 additions & 0 deletions devtools/conda-envs/development_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ dependencies:
- sphinx
- sphinx_rtd_theme
- sphinxcontrib-bibtex
- sphinx-remove-toctrees
- sphinx-copybutton
- myst-nb
- jupyterlab
- pyyaml
- mamba
2 changes: 2 additions & 0 deletions devtools/conda-envs/docs_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ dependencies:
- sphinx
- sphinx_rtd_theme
- sphinxcontrib-bibtex
- sphinx-remove-toctrees
- sphinx-copybutton
- myst-nb
- openmm
- unyt
1 change: 1 addition & 0 deletions devtools/requirements.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ development:
- *docs_dependencies
- jupyterlab
- pyyaml
- mamba

conda-build:
channels:
Expand Down
44 changes: 27 additions & 17 deletions pyunitwizard/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
dict_get_unit={}
dict_change_value={}
dict_make_quantity={}
dict_translate={} # This contains a sub-dictionary for each loaded library. Contains functions such as to_pint
dict_translate_quantity={} # This contains a sub-dictionary for each loaded library. Contains functions such as to_pint
dict_translate_unit={} # This contains a sub-dictionary for each loaded library. Contains functions such as to_pint
dict_convert={}
dict_string_to_quantity={}
dict_to_string={}
dict_dimensionality={}
dict_compatibility={}

Expand All @@ -40,32 +39,44 @@ def load_library(library: str) -> None:
dict_change_value[library] = api.change_value
dict_make_quantity[library] = api.make_quantity
dict_convert[library] = api.convert
dict_translate[library] = {}
dict_string_to_quantity[library] = api.string_to_quantity
dict_to_string[library] = api.to_string
dict_translate_quantity[library] = {}
dict_translate_unit[library] = {}
dict_dimensionality[library] = api.dimensionality
dict_compatibility[library] = api.compatibility

dict_translate[library]['string'] = api.to_string
dict_translate_quantity[library]['string'] = api.quantity_to_string
dict_translate_unit[library]['string'] = api.unit_to_string
api_string = _import_module('.api_string', _base_package)
dict_translate['string'][library]= getattr(api_string, 'to_'+library.replace('.','_'))
dict_translate_quantity['string'][library]= getattr(api_string, 'quantity_to_'+library.replace('.','_'))
dict_translate_unit['string'][library]= getattr(api_string, 'unit_to_'+library.replace('.','_'))
del(api_string)

for method in api.__dict__.keys():
if method.startswith('to_'):
out_form = method.replace('to_','').replace('_','.')
if method.startswith('quantity_to_'):
out_form = method.replace('quantity_to_','').replace('_','.')
if out_form in loaded_libraries:
dict_translate[library][out_form] = getattr(api, method)
dict_translate_quantity[library][out_form] = getattr(api, method)
if method.startswith('unit_to_'):
out_form = method.replace('unit_to_','').replace('_','.')
if out_form in loaded_libraries:
dict_translate_unit[library][out_form] = getattr(api, method)

if api.parser:
loaded_parsers.append(library)

for library_loaded in loaded_libraries:
api = _import_module('.'+_forms_apis_modules[library_loaded], _base_package)
for method in api.__dict__.keys():
if method.startswith('to_'):
out_form=method.replace('to_','').replace('_','.')
if method.startswith('quantity_to_'):
out_form=method.replace('quantity_to_','').replace('_','.')
if out_form == library:
dict_translate_quantity[library_loaded][library]= getattr(api, method)
break
for method in api.__dict__.keys():
if method.startswith('unit_to_'):
out_form=method.replace('unit_to_','').replace('_','.')
if out_form == library:
dict_translate[library_loaded][library]= getattr(api, method)
dict_translate_unit[library_loaded][library]= getattr(api, method)
break

loaded_libraries.append(library)
Expand All @@ -85,9 +96,8 @@ def load_library(library: str) -> None:
dict_change_value['string'] = api.change_value
dict_make_quantity['string'] = api.make_quantity
dict_convert['string'] = api.convert
dict_translate['string']={}
dict_string_to_quantity['string'] = api.string_to_quantity
dict_to_string['string'] = api.to_string
dict_translate_quantity['string']={}
dict_translate_unit['string']={}
dict_dimensionality['string'] = api.dimensionality
dict_compatibility['string'] = api.compatibility

Expand Down
149 changes: 111 additions & 38 deletions pyunitwizard/forms/api_openmm_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def is_unit(quantity_or_unit: Any) -> bool:
'luminous intensity' : '[Cd]'
}

def dimensionality(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.Quantity]) -> Dict[str, int]:
def dimensionality(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.Unit]) -> Dict[str, int]:
""" Returns the dimensionality of the quantity or unit.
Parameters
Expand Down Expand Up @@ -89,8 +89,8 @@ def dimensionality(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.Qua

return dimensionality_dict

def compatibility(quantity_or_unit_1: Union[openmm_unit.Quantity, openmm_unit.Quantity],
quantity_or_unit_2: Union[openmm_unit.Quantity, openmm_unit.Quantity]) -> bool:
def compatibility(quantity_or_unit_1: Union[openmm_unit.Quantity, openmm_unit.Unit],
quantity_or_unit_2: Union[openmm_unit.Quantity, openmm_unit.Unit]) -> bool:
""" Check whether two quantities or units are compatible.
Parameters
Expand All @@ -117,15 +117,15 @@ def compatibility(quantity_or_unit_1: Union[openmm_unit.Quantity, openmm_unit.Qu


def make_quantity(value: Union[int, float, ArrayLike],
unit: openmm_unit.unit) -> openmm_unit.Quantity:
unit: openmm_unit.Unit) -> openmm_unit.Quantity:
""" Returns an openmm quantity.
Parmeters
---------
value: int, float or ArrayLike
The value of the quantity.
unit : openmm_unit.unit
unit : openmm_unit.Unit
The unit.
Returns
Expand All @@ -136,24 +136,44 @@ def make_quantity(value: Union[int, float, ArrayLike],

return openmm_unit.Quantity(value, unit)

def string_to_quantity(string):

raise LibraryWithoutParserError('openmm.unit')
def get_value(quantity: openmm_unit.Quantity) -> Union[int, float, ArrayLike]:
""" Returns the value of the quantity.
Parameters
-----------
quantity : openmm.unit.Quantity
A quanitity or a unit.
Returns
-------
int, float or ArrrayLike
The value.
"""
return quantity._value

def to_string(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.Quantity]) -> str:
""" Convert a quantity to string.

def get_unit(quantity: openmm_unit.Quantity) -> openmm_unit.Unit:
""" Returns the units of the quantity.
Parameters
-----------
quantity_or_unit : openmm_unit.Quantity or openmm_unit.Quantity
quantity : openmm.unit.Quantity
A quanitity or a unit.
Returns
-------
str
The quantitity as a string.
openmm.unit.Unit
The unit.
"""
return quantity_or_unit.__str__()
return quantity.unit


def change_value(quantity: openmm_unit.Quantity,
value: Union[int, float, ArrayLike]) -> openmm_unit.Quantity:

return make_quantity(value, get_unit(quantity))


def convert(quantity: openmm_unit.Quantity,
unit: openmm_unit.Unit) -> openmm_unit.Quantity:
Expand All @@ -174,42 +194,54 @@ def convert(quantity: openmm_unit.Quantity,
"""
return quantity.in_units_of(unit)

def get_value(quantity: openmm_unit.Quantity) -> Union[int, float, ArrayLike]:
""" Returns the value of the quantity.

## Parser

def string_to_quantity(string):

raise LibraryWithoutParserError('openmm.unit')

def string_to_unit(string):

raise LibraryWithoutParserError('openmm.unit')


## To string

def quantity_to_string(quantity: openmm_unit.Quantity) -> str:
""" Convert a quantity to string.
Parameters
-----------
quantity : openmm.unit.Quantity
A quanitity or a unit.
quantity : openmm_unit.Quantity
A quanitity.
Returns
-------
int, float or ArrrayLike
The value.
str
The quantitity as a string.
"""
return quantity._value
return quantity.__str__()

def unit_to_string(unit: openmm_unit.Unit) -> str:
""" Convert a unit to string.
def get_unit(quantity: openmm_unit.Quantity) -> openmm_unit.Unit:
""" Returns the units of the quantity.
Parameters
-----------
quantity : openmm.unit.Quantity
A quanitity or a unit.
unit : openmm_unit.Unit
A unit.
Returns
-------
openmm.unit.Unit
The unit.
str
The quantitity as a string.
"""
return quantity.unit
return unit.__str__()

def change_value(quantity: openmm_unit.Quantity,
value: Union[int, float, ArrayLike]) -> openmm_unit.Quantity:

return make_quantity(value, get_unit(quantity))
## To Pint

def to_pint(quantity: openmm_unit.Quantity):
def quantity_to_pint(quantity: openmm_unit.Quantity):
""" Transform a quantity from openmm.unit to a pint quantity.
Parameters
Expand All @@ -225,11 +257,33 @@ def to_pint(quantity: openmm_unit.Quantity):
from .api_pint import make_quantity as make_pint_quantity

value = get_value(quantity)
unit_name = to_string(get_unit(quantity))
unit_name = unit_to_string(get_unit(quantity))

return make_pint_quantity(value, unit_name)

def to_unyt(quantity: openmm_unit.Quantity):
def unit_to_pint(unit: openmm_unit.Unit):
""" Transform a unit from openmm.unit to a pint unit.
Parameters
-----------
unit : openmm.unit.Unit
A unit.
Returns
-------
pint.Unit
The unit.
"""
from .api_pint import get_unit as get_pint_unit

quantity = quantity_to_pint(1.0*unit)

return get_pint_unit(quantity)


## To Unyt

def quantity_to_unyt(quantity: openmm_unit.Quantity):
""" Transform a quantity from openmm.unit to a unyt quantity.
Parameters
Expand All @@ -249,3 +303,22 @@ def to_unyt(quantity: openmm_unit.Quantity):

return make_unyt_quantity(value, unit_name)

def unit_to_unyt(unit: openmm_unit.Unit):
""" Transform a unit from openmm.unit to a unyt unit.
Parameters
-----------
unit : openmm.unit.Unit
A unit.
Returns
-------
unyt_unit
The unit.
"""
from .api_unyt import get_unit as get_unyt_unit

quantity = quantity_to_unyt(1.0*unit)

return get_unyt_unit(quantity)

Loading

0 comments on commit fc8e3d9

Please sign in to comment.