From 6e8a08e419ddbd44cf63bfa4ff3ad8cff0d8dea1 Mon Sep 17 00:00:00 2001 From: Diego Prada-Gracia Date: Sat, 3 Dec 2022 09:23:30 -0600 Subject: [PATCH 1/4] fixing units translation --- pyunitwizard/forms/api_openmm_unit.py | 8 ++++---- pyunitwizard/main.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pyunitwizard/forms/api_openmm_unit.py b/pyunitwizard/forms/api_openmm_unit.py index a7a3440..d75a8e2 100644 --- a/pyunitwizard/forms/api_openmm_unit.py +++ b/pyunitwizard/forms/api_openmm_unit.py @@ -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 @@ -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 @@ -140,7 +140,7 @@ def string_to_quantity(string): raise LibraryWithoutParserError('openmm.unit') -def to_string(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.Quantity]) -> str: +def to_string(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.unit]) -> str: """ Convert a quantity to string. Parameters diff --git a/pyunitwizard/main.py b/pyunitwizard/main.py index 625a174..132dd40 100644 --- a/pyunitwizard/main.py +++ b/pyunitwizard/main.py @@ -401,7 +401,7 @@ def quantity(value: Union[int, float, ArrayLike], output = None form = digest_form(form) - + if type(value) is str: if unit is None: output = convert(value, to_form=form, parser=parser) @@ -416,7 +416,7 @@ def quantity(value: Union[int, float, ArrayLike], if unit is None: raise BadCallError('unit') - unit = convert(unit, to_form=form, parser=parser) + unit = convert(unit, to_form=form, parser=parser, to_type='unit') try: output = dict_make_quantity[form](value, unit) From 11f265a2881228456d92ec63ec58e0e39d231442 Mon Sep 17 00:00:00 2001 From: Diego Prada Date: Mon, 5 Dec 2022 14:06:08 -0600 Subject: [PATCH 2/4] Adding mamba to list of dev required packages --- devtools/conda-envs/development_env.yaml | 3 +++ devtools/conda-envs/docs_env.yaml | 2 ++ devtools/requirements.yaml | 1 + pyunitwizard/forms/api_openmm_unit.py | 12 ++++++------ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/devtools/conda-envs/development_env.yaml b/devtools/conda-envs/development_env.yaml index 3bf50e7..b232cef 100644 --- a/devtools/conda-envs/development_env.yaml +++ b/devtools/conda-envs/development_env.yaml @@ -13,6 +13,9 @@ dependencies: - sphinx - sphinx_rtd_theme - sphinxcontrib-bibtex +- sphinx-remove-toctrees +- sphinx-copybutton - myst-nb - jupyterlab - pyyaml +- mamba diff --git a/devtools/conda-envs/docs_env.yaml b/devtools/conda-envs/docs_env.yaml index 61a2be4..8f31bc2 100644 --- a/devtools/conda-envs/docs_env.yaml +++ b/devtools/conda-envs/docs_env.yaml @@ -7,6 +7,8 @@ dependencies: - sphinx - sphinx_rtd_theme - sphinxcontrib-bibtex +- sphinx-remove-toctrees +- sphinx-copybutton - myst-nb - openmm - unyt diff --git a/devtools/requirements.yaml b/devtools/requirements.yaml index 9529fb0..cc79778 100644 --- a/devtools/requirements.yaml +++ b/devtools/requirements.yaml @@ -50,6 +50,7 @@ development: - *docs_dependencies - jupyterlab - pyyaml + - mamba conda-build: channels: diff --git a/pyunitwizard/forms/api_openmm_unit.py b/pyunitwizard/forms/api_openmm_unit.py index d75a8e2..69914d9 100644 --- a/pyunitwizard/forms/api_openmm_unit.py +++ b/pyunitwizard/forms/api_openmm_unit.py @@ -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.unit]) -> 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 @@ -89,8 +89,8 @@ def dimensionality(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.uni return dimensionality_dict -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: +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 @@ -117,7 +117,7 @@ def compatibility(quantity_or_unit_1: Union[openmm_unit.Quantity, openmm_unit.un 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 @@ -125,7 +125,7 @@ def make_quantity(value: Union[int, float, ArrayLike], value: int, float or ArrayLike The value of the quantity. - unit : openmm_unit.unit + unit : openmm_unit.Unit The unit. Returns @@ -140,7 +140,7 @@ def string_to_quantity(string): raise LibraryWithoutParserError('openmm.unit') -def to_string(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.unit]) -> str: +def to_string(quantity_or_unit: Union[openmm_unit.Quantity, openmm_unit.Unit]) -> str: """ Convert a quantity to string. Parameters From f832079d251dd4f1aa0decb95ae861bc50eb33af Mon Sep 17 00:00:00 2001 From: Diego Prada Date: Mon, 5 Dec 2022 15:53:11 -0600 Subject: [PATCH 3/4] in process --- pyunitwizard/forms/api_openmm_unit.py | 137 +++++++++++++++++----- pyunitwizard/forms/api_pint.py | 99 +++++++++++++--- sandbox/Test.ipynb | 161 ++++++++++++++++++++++++++ 3 files changed, 351 insertions(+), 46 deletions(-) create mode 100644 sandbox/Test.ipynb diff --git a/pyunitwizard/forms/api_openmm_unit.py b/pyunitwizard/forms/api_openmm_unit.py index 69914d9..7d13e68 100644 --- a/pyunitwizard/forms/api_openmm_unit.py +++ b/pyunitwizard/forms/api_openmm_unit.py @@ -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.Unit]) -> 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: @@ -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 @@ -229,7 +261,29 @@ def to_pint(quantity: openmm_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 @@ -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) + diff --git a/pyunitwizard/forms/api_pint.py b/pyunitwizard/forms/api_pint.py index b8806a3..11d0f45 100644 --- a/pyunitwizard/forms/api_pint.py +++ b/pyunitwizard/forms/api_pint.py @@ -170,6 +170,28 @@ def change_value(quantity: pint.Quantity, return make_quantity(value, get_unit(quantity)) + +def convert(quantity: pint.Quantity, unit_name: str) -> pint.Quantity: + """ Converts the quantity to a different unit. + + Parameters + ----------- + quantity : pint.Quantity + A quanitity or a unit. + + unit : str + The unit to convert to. + + Returns + ------- + pint.Quantity + The converted quantity. + """ + return quantity.to(unit_name) + + +## Parser + def string_to_quantity(string: str) -> pint.Quantity: """ Get a quantity from a string. @@ -185,7 +207,25 @@ def string_to_quantity(string: str) -> pint.Quantity: """ return Q_(string) -def to_string(quantity_or_item) -> str: +def string_to_unit(string: str) -> pint.Unit: + """ Get a unit from a string. + + Parameters + ---------- + string : str + A string with the unit. + + Returns + ------- + pint.Unit + The unit. + """ + return U_(string) + + +# To string + +def quantity_to_string(quantity_or_item) -> str: """ Convert a quantity to string. Parameters @@ -200,25 +240,25 @@ def to_string(quantity_or_item) -> str: """ return quantity_or_item.__str__() -def convert(quantity: pint.Quantity, unit_name: str) -> pint.Quantity: - """ Converts the quantity to a different unit. +def unit_to_string(unit_or_item) -> str: + """ Convert a unit to string. Parameters ----------- - quantity : pint.Quantity - A quanitity or a unit. - - unit : str - The unit to convert to. - + unit_or_item : pint.Unit + A unit. + Returns ------- - pint.Quantity - The converted quantity. + str + The unit as a string. """ - return quantity.to(unit_name) + return unit_or_item.__str__() + -def to_openmm_unit(quantity: pint.Quantity): +## To openmm.unit + +def quantity_to_openmm_unit(quantity: pint.Quantity): """ Transform a quantity from a pint quantity to a openmm.unit quantity. Parameters @@ -228,7 +268,7 @@ def to_openmm_unit(quantity: pint.Quantity): Returns ------- - openmm.unit.Quantity + openmm_unit.Quantity The quantity. """ from pint.util import ParserHelper as PintParserHelper @@ -250,6 +290,37 @@ def to_openmm_unit(quantity: pint.Quantity): return tmp_quantity +def unit_to_openmm_unit(unit: pint.Unit): + """ Transform a unit from a pint unit to a openmm.unit unit. + + Parameters + ----------- + unit : pint.Unit + A unit. + + Returns + ------- + openmm_unit.Unit + The unit. + """ + from pint.util import ParserHelper as PintParserHelper + try: + import openmm.unit as openmm_unit + except: + raise LibraryNotFoundError('openmm') + + pint_parser = PintParserHelper.from_string(unit.__str__()) + tmp_ = 1 + for unit_name, exponent in pint_parser.items(): + if unit_name == 'unified_atomic_mass_unit': + unit_name = 'amu' + tmp_quantity *= getattr(openmm_unit, unit_name)**exponent + + return tmp_unit + + + + def to_unyt(quantity: pint.Quantity): """ Transform a quantity from a pint quantity to a unyt quantity. diff --git a/sandbox/Test.ipynb b/sandbox/Test.ipynb new file mode 100644 index 0000000..9fdc52a --- /dev/null +++ b/sandbox/Test.ipynb @@ -0,0 +1,161 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "f963cf9e-03b3-40b3-9675-f7eec5b8de74", + "metadata": {}, + "outputs": [], + "source": [ + "import pyunitwizard as puw" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "0ba781fe-b700-4562-bd96-3ad441e062e8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['pint', 'openmm.unit', 'unyt']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "puw.configure.get_libraries_supported()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3cb5ad41-1066-4da5-9a0d-113388630232", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['pint', 'openmm.unit', 'unyt']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "puw.configure.get_libraries_loaded()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a128a665-aec6-4f19-b8a6-ddec4279d055", + "metadata": {}, + "outputs": [], + "source": [ + "puw.configure.reset()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ded768cf-3188-42b7-9583-5cc87484d609", + "metadata": {}, + "outputs": [], + "source": [ + "puw.forms.load_library('openmm.unit')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "e7ab9ab2-aaab-4bca-b1bc-bcd034e46d64", + "metadata": {}, + "outputs": [], + "source": [ + "aa = puw.unit('nm', form='openmm.unit')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "aa492746-f2aa-4cde-85bf-f1cdb695e207", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'nanometer'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aa.__str__()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6aa0fb8b-59ad-492e-a4d3-e0283b1eb45e", + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'Unit' object has no attribute '_value'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [7]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mpuw\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconvert\u001b[49m\u001b[43m(\u001b[49m\u001b[43maa\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mto_form\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mpint\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/projects@dprada/PyUnitWizard/pyunitwizard/main.py:553\u001b[0m, in \u001b[0;36mconvert\u001b[0;34m(quantity_or_unit, to_unit, to_form, parser, to_type)\u001b[0m\n\u001b[1;32m 551\u001b[0m output \u001b[38;5;241m=\u001b[39m quantity_or_unit\n\u001b[1;32m 552\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 553\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[43mdict_translate\u001b[49m\u001b[43m[\u001b[49m\u001b[43mform_in\u001b[49m\u001b[43m]\u001b[49m\u001b[43m[\u001b[49m\u001b[43mto_form\u001b[49m\u001b[43m]\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquantity_or_unit\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 555\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m to_unit \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 556\u001b[0m output \u001b[38;5;241m=\u001b[39m dict_convert[to_form](output, to_unit)\n", + "File \u001b[0;32m~/projects@dprada/PyUnitWizard/pyunitwizard/forms/api_openmm_unit.py:227\u001b[0m, in \u001b[0;36mto_pint\u001b[0;34m(quantity)\u001b[0m\n\u001b[1;32m 213\u001b[0m \u001b[38;5;124;03m\"\"\" Transform a quantity from openmm.unit to a pint quantity.\u001b[39;00m\n\u001b[1;32m 214\u001b[0m \u001b[38;5;124;03m \u001b[39;00m\n\u001b[1;32m 215\u001b[0m \u001b[38;5;124;03m Parameters\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 223\u001b[0m \u001b[38;5;124;03m The quantity.\u001b[39;00m\n\u001b[1;32m 224\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 225\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mapi_pint\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m make_quantity \u001b[38;5;28;01mas\u001b[39;00m make_pint_quantity\n\u001b[0;32m--> 227\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[43mget_value\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquantity\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 228\u001b[0m unit_name \u001b[38;5;241m=\u001b[39m to_string(get_unit(quantity))\n\u001b[1;32m 230\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m make_pint_quantity(value, unit_name)\n", + "File \u001b[0;32m~/projects@dprada/PyUnitWizard/pyunitwizard/forms/api_openmm_unit.py:190\u001b[0m, in \u001b[0;36mget_value\u001b[0;34m(quantity)\u001b[0m\n\u001b[1;32m 177\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_value\u001b[39m(quantity: openmm_unit\u001b[38;5;241m.\u001b[39mQuantity) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[\u001b[38;5;28mint\u001b[39m, \u001b[38;5;28mfloat\u001b[39m, ArrayLike]:\n\u001b[1;32m 178\u001b[0m \u001b[38;5;124;03m\"\"\" Returns the value of the quantity.\u001b[39;00m\n\u001b[1;32m 179\u001b[0m \u001b[38;5;124;03m \u001b[39;00m\n\u001b[1;32m 180\u001b[0m \u001b[38;5;124;03m Parameters\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 188\u001b[0m \u001b[38;5;124;03m The value.\u001b[39;00m\n\u001b[1;32m 189\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 190\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mquantity\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_value\u001b[49m\n", + "\u001b[0;31mAttributeError\u001b[0m: 'Unit' object has no attribute '_value'" + ] + } + ], + "source": [ + "puw.convert(aa, to_form='pint')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1906c419-f856-42bb-a68d-445691ba3d72", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 1d12d3d13cdca150f1c21de984dd2128d76c8f39 Mon Sep 17 00:00:00 2001 From: Diego Prada-Gracia Date: Mon, 5 Dec 2022 20:21:24 -0600 Subject: [PATCH 4/4] Bug fixed converting units to units --- pyunitwizard/forms/__init__.py | 44 +++-- pyunitwizard/forms/api_openmm_unit.py | 2 +- pyunitwizard/forms/api_pint.py | 42 +++-- pyunitwizard/forms/api_string.py | 69 +++++-- pyunitwizard/forms/api_unyt.py | 170 ++++++++++++------ pyunitwizard/main.py | 17 +- pyunitwizard/parse.py | 14 +- .../test_convert_with_openmm_unit.py | 5 + .../tests/pint/test_convert_with_pint.py | 7 +- sandbox/Test.ipynb | 161 ----------------- 10 files changed, 259 insertions(+), 272 deletions(-) delete mode 100644 sandbox/Test.ipynb diff --git a/pyunitwizard/forms/__init__.py b/pyunitwizard/forms/__init__.py index b453957..2c20d28 100644 --- a/pyunitwizard/forms/__init__.py +++ b/pyunitwizard/forms/__init__.py @@ -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={} @@ -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) @@ -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 diff --git a/pyunitwizard/forms/api_openmm_unit.py b/pyunitwizard/forms/api_openmm_unit.py index 7d13e68..91f091e 100644 --- a/pyunitwizard/forms/api_openmm_unit.py +++ b/pyunitwizard/forms/api_openmm_unit.py @@ -257,7 +257,7 @@ def quantity_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) diff --git a/pyunitwizard/forms/api_pint.py b/pyunitwizard/forms/api_pint.py index 11d0f45..b8e99ac 100644 --- a/pyunitwizard/forms/api_pint.py +++ b/pyunitwizard/forms/api_pint.py @@ -303,31 +303,22 @@ def unit_to_openmm_unit(unit: pint.Unit): openmm_unit.Unit The unit. """ - from pint.util import ParserHelper as PintParserHelper - try: - import openmm.unit as openmm_unit - except: - raise LibraryNotFoundError('openmm') - - pint_parser = PintParserHelper.from_string(unit.__str__()) - tmp_ = 1 - for unit_name, exponent in pint_parser.items(): - if unit_name == 'unified_atomic_mass_unit': - unit_name = 'amu' - tmp_quantity *= getattr(openmm_unit, unit_name)**exponent - return tmp_unit + from .api_openmm_unit import get_unit as get_openmm_unit_unit + quantity = quantity_to_openmm_unit(1.0*unit) + return get_openmm_unit_unit(quantity) +## To Unyt -def to_unyt(quantity: pint.Quantity): +def quantity_to_unyt(quantity: pint.Quantity): """ Transform a quantity from a pint quantity to a unyt quantity. Parameters ----------- quantity : pint.Quantity - A quanitity. + A quantity. Returns ------- @@ -338,3 +329,24 @@ def to_unyt(quantity: pint.Quantity): return unyt_array.from_pint(quantity) +def unit_to_unyt(unit: pint.Unit): + """ Transform a unit from a pint unit to a unyt unit. + + Parameters + ----------- + unit : pint.Unit + A unit. + + Returns + ------- + unyt_array or unyt_quantity + The unyt quantity + """ + + from .api_unyt import get_unit as get_unyt_unit + + quantity = quantity_to_unyt(1.0*unit) + + return get_unyt_unit(quantity) + + diff --git a/pyunitwizard/forms/api_string.py b/pyunitwizard/forms/api_string.py index a745d82..d9093b8 100644 --- a/pyunitwizard/forms/api_string.py +++ b/pyunitwizard/forms/api_string.py @@ -158,14 +158,6 @@ def change_value(quantity: str, return make_quantity(value, get_unit(quantity)) -def string_to_quantity(string: str) -> str: - """ Returns the same string. """ - return string - -def to_string(quantity_or_item: str) -> str: - """ Returns the same string. """ - return quantity_or_item - def convert(quantity: str, unit_name: str) -> str: """ Converts the quantity to a different unit. @@ -189,16 +181,41 @@ def convert(quantity: str, unit_name: str) -> str: tmp_quantity_or_unit = _convert(tmp_quantity_or_unit, to_unit=unit_name, parser=default_parser) return _convert(tmp_quantity_or_unit, to_form='string') -def to_openmm_unit(quantity: str): + +## Parser + +#def string_to_quantity(string: str) -> str: +# """ Returns the same string. """ +# return string +# +#def string_to_unit(string: str) -> str: +# """ Returns the same string. """ +# return string + + +## To openmm.unit + +def quantity_to_openmm_unit(quantity: str): # This function will raise an error. - from .api_openmm_unit import string_to_quantity as _string_to_quantity + from .api_openmm_unit import string_to_quantity as string_to_openmm_unit_quantity - tmp_quantity_or_unit = _string_to_quantity(quantity) + tmp_quantity_or_unit = string_to_openmm_unit_quantity(quantity) return tmp_quantity_or_unit -def to_pint(quantity: str): +def unit_to_openmm_unit(unit: str): + + from .api_openmm_unit import get_unit as get_openmm_unit_unit + + quantity = quantity_to_openmm_unit(unit) + + return get_openmm_unit_unit(quantity) + + +## To pint + +def quantity_to_pint(quantity: str): """ Transform a quantity from a string quantity to a pint quantity. Parameters @@ -215,5 +232,31 @@ def to_pint(quantity: str): return _string_to_quantity(quantity) -def to_unyt(quantity: str): +def unit_to_pint(unit: str): + """ Transform a quantity from a string quantity to a pint quantity. + + Parameters + ----------- + quantity : str + A quanitity. + + Returns + ------- + pint.Quantity + The quantity. + """ + from .api_pint import get_unit as get_pint_unit + + quantity = quantity_to_pint(unit) + + return get_pint_unit(quantity) + + +## To unyt + +def quantity_to_unyt(quantity: str): + raise NotImplementedError + +def unit_to_unyt(quantity: str): raise NotImplementedError + diff --git a/pyunitwizard/forms/api_unyt.py b/pyunitwizard/forms/api_unyt.py index 7669826..831eb8b 100644 --- a/pyunitwizard/forms/api_unyt.py +++ b/pyunitwizard/forms/api_unyt.py @@ -5,7 +5,8 @@ try: import unyt - from unyt import unyt_array, unyt_quantity, Unit + from unyt import unyt_array, unyt_quantity + from unyt import Unit as unyt_unit except: raise LibraryNotFoundError('unyt') @@ -15,7 +16,7 @@ is_form = { unyt_array: form_name, unyt_quantity: form_name, - Unit: form_name, + unyt_unit: form_name, } @@ -45,11 +46,11 @@ def is_unit(quantity_or_unit: Any) -> bool: Returns ------- bool - True if it is a unyt.Unit. + True if it is a unyt_unit. """ - return isinstance(quantity_or_unit, Unit) + return isinstance(quantity_or_unit, unyt_unit) -def dimensionality(quantity_or_unit: Union[unyt_array, unyt_quantity, Unit] +def dimensionality(quantity_or_unit: Union[unyt_array, unyt_quantity, unyt_unit] ) -> Dict[str, int]: """ Returns the dimensionality of the quantity or unit. @@ -79,17 +80,17 @@ def dimensionality(quantity_or_unit: Union[unyt_array, unyt_quantity, Unit] return dimensionality_pint(temp_quantity) -def compatibility(quantity_or_unit_1: Union[unyt_array, unyt_quantity, Unit], - quantity_or_unit_2: Union[unyt_array, unyt_quantity, Unit] +def compatibility(quantity_or_unit_1: Union[unyt_array, unyt_quantity, unyt_unit], + quantity_or_unit_2: Union[unyt_array, unyt_quantity, unyt_unit] ) -> bool: """ Check whether two quantities or units are compatible. Parameters ---------- - quantity_or_unit_1 : unyt_array or unyt_quantity or Unit + quantity_or_unit_1 : unyt_array or unyt_quantity or unyt_unit A quanitity or a unit. - quantity_or_unit_2 : unyt_array or unyt_quantity or Unit + quantity_or_unit_2 : unyt_array or unyt_quantity or unyt_unit A quanitity or a unit. Returns @@ -110,7 +111,7 @@ def compatibility(quantity_or_unit_1: Union[unyt_array, unyt_quantity, Unit], return unit_1.same_dimensions_as(unit_2) def make_quantity(value: Union[int, float, ArrayLike], - unit: Union[str, Unit]) -> Union[unyt_array, unyt_quantity]: + unit: Union[str, unyt_unit]) -> Union[unyt_array, unyt_quantity]: """ Returns a unyt quantity. Parmeters @@ -118,7 +119,7 @@ def make_quantity(value: Union[int, float, ArrayLike], value: int, float or ArrayLike The value of the quantity. - unit : Unit or str + unit : unyt_unit or str The unit. Returns @@ -131,26 +132,42 @@ def make_quantity(value: Union[int, float, ArrayLike], else: return unyt_array(value, unit) -def string_to_quantity(string): - - raise LibraryWithoutParserError("Unyt library has no string parser") - -def to_string(quantity_or_unit: Union[unyt_array, - unyt_quantity, - Unit]) -> str: - """ Convert a quantity to string. - +def get_value(quantity: Union[unyt_array, + unyt_quantity]) -> Union[int, float, ArrayLike]: + """ Returns the value of the quantity. + Parameters ----------- - quantity_or_unit : unyt_array or unyt_quantity or Unit + quantity : unyt_array or unyt_quantity A quanitity or a unit. + + Returns + ------- + int, float or ArrrayLike + The value. + """ + return quantity.value +def get_unit(quantity: Union[unyt_array, + unyt_quantity]) -> unyt_unit: + """ Returns the units of the quantity. + + Parameters + ----------- + quantity : unyt_array or unyt_quantity + A quanitity or a unit. + Returns ------- - str - The quantitity as a string. + unyt_unit + The unit. """ - return str(quantity_or_unit) + return quantity.units + +def change_value(quantity: Union[unyt_quantity, unyt_array], + value: Union[int, float, ArrayLike]) -> Union[unyt_array, unyt_quantity]: + + return make_quantity(value, get_unit(quantity)) def convert(quantity: Union[unyt_array, unyt_quantity], unit_name: str) -> Union[unyt_array, unyt_quantity]: @@ -171,44 +188,55 @@ def convert(quantity: Union[unyt_array, unyt_quantity], """ return quantity.to(unit_name) -def get_value(quantity: Union[unyt_array, - unyt_quantity]) -> Union[int, float, ArrayLike]: - """ Returns the value of the quantity. - + +## Parser + +def string_to_quantity(string): + + raise LibraryWithoutParserError("Unyt library has no string parser") + +def string_to_unit(string): + + raise LibraryWithoutParserError("Unyt library has no string parser") + + +## To string + +def quantity_to_string(quantity: Union[unyt_array, + unyt_quantity]) -> str: + """ Convert a quantity to string. + Parameters ----------- - quantity : unyt_array or unyt_quantity + quantity: unyt_array or unyt_quantity A quanitity or a unit. - + Returns ------- - int, float or ArrrayLike - The value. + str + The quantitity as a string. """ - return quantity.value + return str(quantity) + +def unit_to_string(unit: unyt_unit) -> str: + """ Convert a unit to string. -def get_unit(quantity: Union[unyt_array, - unyt_quantity]) -> Unit: - """ Returns the units of the quantity. - Parameters ----------- - quantity : unyt_array or unyt_quantity - A quanitity or a unit. - + unit: unyt_unit + A unit. + Returns ------- - Unit - The unit. + str + The unit as a string. """ - return quantity.units + return str(unit) -def change_value(quantity: Union[unyt_quantity, unyt_array], - value: Union[int, float, ArrayLike]) -> Union[unyt_array, unyt_quantity]: - return make_quantity(value, get_unit(quantity)) +## To Pint -def to_pint(quantity: Union[unyt_array, +def quantity_to_pint(quantity: Union[unyt_array, unyt_quantity]): """ Transform a quantity from unyt to a pint quantity. @@ -225,7 +253,29 @@ def to_pint(quantity: Union[unyt_array, from .api_pint import ureg return quantity.to_pint(unit_registry=ureg) -def to_openmm_unit(quantity: Union[unyt_array, unyt_quantity]): +def unit_to_pint(unit: unyt_unit): + """ Transform a unit from unyt to a pint unit. + + Parameters + ----------- + unit : unyt_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 openmm.unit + +def quantity_to_openmm_unit(quantity: Union[unyt_array, unyt_quantity]): """ Transform a quantity from unyt to an openmm.unit quantity. Parameters @@ -239,7 +289,27 @@ def to_openmm_unit(quantity: Union[unyt_array, unyt_quantity]): The quantity. """ # Convert to pint quantity first and then to openmm. Temporary solution - from .api_pint import to_openmm_unit as pint_to_openmm + from .api_pint import quantity_to_openmm_unit as pint_quantity_to_openmm + + return pint_quantity_to_openmm(quantity.to_pint()) + +def unit_to_openmm_unit(unit: unyt_unit): + """ Transform a unit from unyt to a openmm.unit unit. + + Parameters + ----------- + unit : unyt_unit + A unit. + + Returns + ------- + openmm_unit.Unit + The unit. + """ + from .api_openmm_unit import get_unit as get_openmm_unit_unit + + quantity = quantity_to_openmm_unit(1.0*unit) + + return get_openmm_unit_unit(quantity) - return pint_to_openmm(quantity.to_pint()) diff --git a/pyunitwizard/main.py b/pyunitwizard/main.py index 132dd40..6eda6ba 100644 --- a/pyunitwizard/main.py +++ b/pyunitwizard/main.py @@ -4,7 +4,7 @@ from ._private.quantity_or_unit import ArrayLike, QuantityOrUnit, QuantityLike, UnitLike from .forms import dict_is_form, dict_is_unit, dict_is_quantity, dict_dimensionality, dict_compatibility from .forms import dict_get_unit, dict_get_value, dict_make_quantity -from .forms import dict_convert, dict_translate, dict_to_string, dict_change_value +from .forms import dict_convert, dict_translate_quantity, dict_translate_unit, dict_change_value from .import kernel from .parse import parse as _parse import numpy as np @@ -504,12 +504,12 @@ def convert(quantity_or_unit: Any, output=output else: output = dict_get_unit[parser](output) - output = dict_to_string[parser](output) + output = dict_translate_quantity[parser]['string'](output) elif to_type == 'value': output = dict_get_value[parser](output) output = str(output) else: - output = dict_to_string[parser](output) + output = dict_translate_quantity[parser]['string'](output) else: @@ -533,27 +533,32 @@ def convert(quantity_or_unit: Any, if to_unit is not None: output = dict_convert[form_in](output, to_unit) + if to_type == 'unit': if is_unit(output): output=output else: output = dict_get_unit[form_in](output) - output = dict_to_string[form_in](output) + output = dict_translate_unit[form_in]['string'](output) elif to_type == 'value': output = dict_get_value[form_in](output) output = str(output) else: - output = dict_to_string[form_in](output) + output = dict_translate_quantity[form_in]['string'](output) else: if form_in == to_form: output = quantity_or_unit else: - output = dict_translate[form_in][to_form](quantity_or_unit) + if is_unit(quantity_or_unit): + output = dict_translate_unit[form_in][to_form](quantity_or_unit) + else: + output = dict_translate_quantity[form_in][to_form](quantity_or_unit) if to_unit is not None: output = dict_convert[to_form](output, to_unit) + if to_type == 'unit': if is_unit(output): output = output diff --git a/pyunitwizard/parse.py b/pyunitwizard/parse.py index 4ac79d1..81749d1 100644 --- a/pyunitwizard/parse.py +++ b/pyunitwizard/parse.py @@ -1,9 +1,7 @@ from ._private.exceptions import * from ._private.forms import digest_to_form from ._private.parsers import digest_parser -from .forms import dict_string_to_quantity -from .forms import dict_to_string -from .forms import dict_translate +from .forms import dict_translate_quantity import ast from typing import Optional @@ -27,9 +25,9 @@ def _parse_with_pint(string: str): value_string = string[:end_list] unit_string = string[end_list:] - return ast.literal_eval(value_string)*dict_string_to_quantity['pint'](unit_string) + return ast.literal_eval(value_string)*dict_translate_quantity['string']['pint'](unit_string) else: - return dict_string_to_quantity['pint'](string) + return dict_translate_quantity['string']['pint'](string) def parse(string: str, parser: Optional[str]=None, to_form: Optional[str]=None): """ Parses a string and returns a quantity. @@ -63,15 +61,15 @@ def parse(string: str, parser: Optional[str]=None, to_form: Optional[str]=None): elif to_form == 'openmm.unit': pint_quantity = _parse_with_pint(string) - return dict_translate['pint']['openmm.unit'](pint_quantity) + return dict_translate_quantity['pint']['openmm.unit'](pint_quantity) elif to_form == 'string': pint_quantity = _parse_with_pint(string) - return dict_to_string['pint'](pint_quantity) + return dict_translate_quantity['pint']['string'](pint_quantity) elif to_form == 'unyt': pint_quantity = _parse_with_pint(string) - return dict_translate['pint']['unyt'](pint_quantity) + return dict_translate_quantity['pint']['unyt'](pint_quantity) else: raise NotImplementedParsingError(parser, to_form) diff --git a/pyunitwizard/tests/openmm_unit/test_convert_with_openmm_unit.py b/pyunitwizard/tests/openmm_unit/test_convert_with_openmm_unit.py index 5932479..700836b 100644 --- a/pyunitwizard/tests/openmm_unit/test_convert_with_openmm_unit.py +++ b/pyunitwizard/tests/openmm_unit/test_convert_with_openmm_unit.py @@ -6,6 +6,7 @@ def test_convert_from_openmm_to_pint(): puw.configure.reset() puw.configure.load_library(['openmm.unit', 'pint']) + puw.configure.set_default_parser('pint') quantity = puw.convert(1*openmm_unit.meter, to_form='pint') assert puw.get_form(quantity) == 'pint' @@ -17,6 +18,10 @@ def test_convert_from_openmm_to_pint(): assert np.all(puw.get_value(quantity) == np.array([1, 2])) assert puw.get_unit(quantity) == "meter" + unit_openmm = puw.unit('nm', form='openmm.unit') + unit_pint = puw.unit('nm', form='pint') + assert unit_pint == puw.convert(unit_openmm, to_form='pint') + def test_convert_from_openmm_to_openmm(): puw.configure.reset() puw.configure.load_library(['openmm.unit', 'pint']) diff --git a/pyunitwizard/tests/pint/test_convert_with_pint.py b/pyunitwizard/tests/pint/test_convert_with_pint.py index bcf82f4..d80b56a 100644 --- a/pyunitwizard/tests/pint/test_convert_with_pint.py +++ b/pyunitwizard/tests/pint/test_convert_with_pint.py @@ -64,6 +64,11 @@ def test_convert_from_pint_to_openmm_unit(): quantity = puw.convert(quantity, to_form='openmm.unit') assert quantity == 2.5 * openmm_unit.nanometer/openmm_unit.picosecond + + unit_openmm = puw.unit('nm', form='openmm.unit') + unit_pint = puw.unit('nm', form='pint') + assert unit_openmm == puw.convert(unit_pint, to_form='openmm.unit') + def test_convert_from_pint_to_openmm_array(): puw.configure.reset() puw.configure.load_library(['pint','openmm.unit']) @@ -102,4 +107,4 @@ def test_convert_from_pint_to_unyt_array(): quantity = ureg.Quantity([[0,0], [0,0]], 'nanometers/picoseconds') quantity = puw.convert(quantity, to_form='unyt') q_true = [[0,0], [0,0]] * unyt.nm/unyt.ps - assert np.all(quantity == q_true) \ No newline at end of file + assert np.all(quantity == q_true) diff --git a/sandbox/Test.ipynb b/sandbox/Test.ipynb deleted file mode 100644 index 9fdc52a..0000000 --- a/sandbox/Test.ipynb +++ /dev/null @@ -1,161 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "f963cf9e-03b3-40b3-9675-f7eec5b8de74", - "metadata": {}, - "outputs": [], - "source": [ - "import pyunitwizard as puw" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "0ba781fe-b700-4562-bd96-3ad441e062e8", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['pint', 'openmm.unit', 'unyt']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "puw.configure.get_libraries_supported()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "3cb5ad41-1066-4da5-9a0d-113388630232", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['pint', 'openmm.unit', 'unyt']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "puw.configure.get_libraries_loaded()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a128a665-aec6-4f19-b8a6-ddec4279d055", - "metadata": {}, - "outputs": [], - "source": [ - "puw.configure.reset()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ded768cf-3188-42b7-9583-5cc87484d609", - "metadata": {}, - "outputs": [], - "source": [ - "puw.forms.load_library('openmm.unit')" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "e7ab9ab2-aaab-4bca-b1bc-bcd034e46d64", - "metadata": {}, - "outputs": [], - "source": [ - "aa = puw.unit('nm', form='openmm.unit')" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "aa492746-f2aa-4cde-85bf-f1cdb695e207", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'nanometer'" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "aa.__str__()" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "6aa0fb8b-59ad-492e-a4d3-e0283b1eb45e", - "metadata": {}, - "outputs": [ - { - "ename": "AttributeError", - "evalue": "'Unit' object has no attribute '_value'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [7]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mpuw\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconvert\u001b[49m\u001b[43m(\u001b[49m\u001b[43maa\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mto_form\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mpint\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/projects@dprada/PyUnitWizard/pyunitwizard/main.py:553\u001b[0m, in \u001b[0;36mconvert\u001b[0;34m(quantity_or_unit, to_unit, to_form, parser, to_type)\u001b[0m\n\u001b[1;32m 551\u001b[0m output \u001b[38;5;241m=\u001b[39m quantity_or_unit\n\u001b[1;32m 552\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 553\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[43mdict_translate\u001b[49m\u001b[43m[\u001b[49m\u001b[43mform_in\u001b[49m\u001b[43m]\u001b[49m\u001b[43m[\u001b[49m\u001b[43mto_form\u001b[49m\u001b[43m]\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquantity_or_unit\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 555\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m to_unit \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 556\u001b[0m output \u001b[38;5;241m=\u001b[39m dict_convert[to_form](output, to_unit)\n", - "File \u001b[0;32m~/projects@dprada/PyUnitWizard/pyunitwizard/forms/api_openmm_unit.py:227\u001b[0m, in \u001b[0;36mto_pint\u001b[0;34m(quantity)\u001b[0m\n\u001b[1;32m 213\u001b[0m \u001b[38;5;124;03m\"\"\" Transform a quantity from openmm.unit to a pint quantity.\u001b[39;00m\n\u001b[1;32m 214\u001b[0m \u001b[38;5;124;03m \u001b[39;00m\n\u001b[1;32m 215\u001b[0m \u001b[38;5;124;03m Parameters\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 223\u001b[0m \u001b[38;5;124;03m The quantity.\u001b[39;00m\n\u001b[1;32m 224\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 225\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mapi_pint\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m make_quantity \u001b[38;5;28;01mas\u001b[39;00m make_pint_quantity\n\u001b[0;32m--> 227\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[43mget_value\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquantity\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 228\u001b[0m unit_name \u001b[38;5;241m=\u001b[39m to_string(get_unit(quantity))\n\u001b[1;32m 230\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m make_pint_quantity(value, unit_name)\n", - "File \u001b[0;32m~/projects@dprada/PyUnitWizard/pyunitwizard/forms/api_openmm_unit.py:190\u001b[0m, in \u001b[0;36mget_value\u001b[0;34m(quantity)\u001b[0m\n\u001b[1;32m 177\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_value\u001b[39m(quantity: openmm_unit\u001b[38;5;241m.\u001b[39mQuantity) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[\u001b[38;5;28mint\u001b[39m, \u001b[38;5;28mfloat\u001b[39m, ArrayLike]:\n\u001b[1;32m 178\u001b[0m \u001b[38;5;124;03m\"\"\" Returns the value of the quantity.\u001b[39;00m\n\u001b[1;32m 179\u001b[0m \u001b[38;5;124;03m \u001b[39;00m\n\u001b[1;32m 180\u001b[0m \u001b[38;5;124;03m Parameters\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 188\u001b[0m \u001b[38;5;124;03m The value.\u001b[39;00m\n\u001b[1;32m 189\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 190\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mquantity\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_value\u001b[49m\n", - "\u001b[0;31mAttributeError\u001b[0m: 'Unit' object has no attribute '_value'" - ] - } - ], - "source": [ - "puw.convert(aa, to_form='pint')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1906c419-f856-42bb-a68d-445691ba3d72", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.15" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}