From bea32bdb12e37304d1b9076c4e37a8c6fc591f15 Mon Sep 17 00:00:00 2001 From: "Matthew W. Thompson" Date: Thu, 2 Jun 2022 16:11:37 -0500 Subject: [PATCH 1/3] ENH: Add `Quantity.to_openmm` --- openff/units/tests/test_units.py | 15 +++++++++++++++ openff/units/units.py | 18 ++++++++++++++++++ setup.cfg | 3 +++ 3 files changed, 36 insertions(+) diff --git a/openff/units/tests/test_units.py b/openff/units/tests/test_units.py index 4b7ffa4..9074ffe 100644 --- a/openff/units/tests/test_units.py +++ b/openff/units/tests/test_units.py @@ -6,6 +6,21 @@ from openff.units import unit +class TestQuantity: + @skip_if_missing("openmm.unit") + def test_to_openmm_method(self): + """ + Test the basic behavior of `Quantity.to_openmm` as an API call. Testing of the underlying + behavior of the standalone `to_openmm` function is in opeff/units/tests/test_openmm.py. + """ + from openmm import unit as openmm_unit + + quantity = unit.Quantity(0.5, "nanometer") + converted = quantity.to_openmm() + + assert converted == openmm_unit.Quantity(0.5, openmm_unit.nanometer) + + class TestPickle: """Test pickle-based serialization of Quantity, Unit, and Measurement objects diff --git a/openff/units/units.py b/openff/units/units.py index 96bba78..0ed93b4 100644 --- a/openff/units/units.py +++ b/openff/units/units.py @@ -4,14 +4,19 @@ import uuid import warnings +from typing import TYPE_CHECKING import pint +from openff.utilities import requires_package from pint.measurement import _Measurement from pint.quantity import _Quantity from pint.unit import _Unit from openff.units.utilities import get_defaults_path +if TYPE_CHECKING: + from openmm.unit import Quantity as OpenMMQuantity + __all__ = [ "DEFAULT_UNIT_REGISTRY", "Quantity", @@ -63,6 +68,19 @@ def _dask_finalize(results, func, args, units): values = func(results, *args) return Quantity(values, units) + @requires_package("openmm") + def to_openmm(self) -> "OpenMMQuantity": + """Convert the quantity to an `openmm.unit.Quantity`. + + Returns + ------- + openmm_unit : str + The OpenMM compatible unit. + """ + from openff.units.openmm import to_openmm + + return to_openmm(self) + class Measurement(_Measurement): """A value with associated units and uncertainty.""" diff --git a/setup.cfg b/setup.cfg index 60fd42f..514fb3b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -50,5 +50,8 @@ ignore_missing_imports = True [mypy-openmm] ignore_missing_imports = True +[mypy-openmm.unit] +ignore_missing_imports = True + [mypy-openmm.app.element] ignore_missing_imports = True From cc76b0844a50c9c36ed36961dd04ee4825af7f94 Mon Sep 17 00:00:00 2001 From: "Matthew W. Thompson" Date: Fri, 3 Jun 2022 09:09:06 -0500 Subject: [PATCH 2/3] Update Napoleon settings --- docs/conf.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/conf.py b/docs/conf.py index 3a7b27a..e47177b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -96,6 +96,8 @@ napoleon_google_docstring = True napoleon_use_param = False napoleon_use_ivar = True +napoleon_use_rtype = False +napoleon_preprocess_types = True myst_enable_extensions = [ "deflist", From e43884e298e0c2a1011fbdaf799c68b1a2fed9c0 Mon Sep 17 00:00:00 2001 From: Matt Thompson Date: Fri, 3 Jun 2022 09:09:32 -0500 Subject: [PATCH 3/3] Update openff/units/units.py Co-authored-by: Josh A. Mitchell --- openff/units/units.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openff/units/units.py b/openff/units/units.py index 0ed93b4..332425f 100644 --- a/openff/units/units.py +++ b/openff/units/units.py @@ -70,12 +70,12 @@ def _dask_finalize(results, func, args, units): @requires_package("openmm") def to_openmm(self) -> "OpenMMQuantity": - """Convert the quantity to an `openmm.unit.Quantity`. + """Convert the quantity to an ``openmm.unit.Quantity``. Returns ------- - openmm_unit : str - The OpenMM compatible unit. + openmm_quantity : openmm.unit.quantity.Quantity + The OpenMM compatible quantity. """ from openff.units.openmm import to_openmm