Skip to content

Commit

Permalink
Merge pull request #51 from openforcefield/pint-0.20
Browse files Browse the repository at this point in the history
Update for Pint 0.20
  • Loading branch information
mattwthompson authored Nov 2, 2022
2 parents 0b50def + 7b0d187 commit 878083a
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
- name: Run docexamples
if: ${{ matrix.openmm == 'true' }}
run: |
pytest --doctest-modules $PYTEST_ARGS $COV openff
pytest --doctest-modules $PYTEST_ARGS $COV openff --ignore=openff/units/tests
- name: Codecov
uses: codecov/codecov-action@v1
Expand Down
2 changes: 1 addition & 1 deletion devtools/conda-envs/docs_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dependencies:
- python
- pip
- numpy
- pint =0.19
- pint >=0.20.1
- openff-utilities >=0.1.3
- sphinx >=4.5,<5
- myst-parser>=0.13.6
Expand Down
2 changes: 1 addition & 1 deletion devtools/conda-envs/test_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
- python
- pip
- numpy
- pint =0.19
- pint >=0.20.1
- openff-utilities >=0.1.3

# Tests
Expand Down
10 changes: 7 additions & 3 deletions openff/units/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from pint import UnitRegistry

from openff.units._version import get_versions # type: ignore
from openff.units.openmm import ensure_quantity
from openff.units.units import DEFAULT_UNIT_REGISTRY, Measurement, Quantity, Unit
from openff.units.units import (
DEFAULT_UNIT_REGISTRY,
Measurement,
Quantity,
Unit,
UnitRegistry,
)

__all__ = [
"unit",
Expand Down
3 changes: 2 additions & 1 deletion openff/units/data/defaults.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ hertz = 1 / second = Hz
reciprocal_centimeter = 1 / cm = cm_1 = kayser

# Velocity
[velocity] = [length] / [time] = [speed]
# As of 0.20, derived dimensions cannot have aliases
[velocity] = [length] / [time]

# Acceleration
[acceleration] = [velocity] / [time]
Expand Down
2 changes: 1 addition & 1 deletion openff/units/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"SYMBOLS",
]

MASSES: Dict[int, Quantity[float]] = {
MASSES: Dict[int, Quantity] = {
index + 1: Quantity(mass, unit.dalton)
for index, mass in enumerate(
[
Expand Down
4 changes: 3 additions & 1 deletion openff/units/openmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ def to_openmm_inner(quantity) -> "openmm_unit.Quantity":

return value * openmm_unit_

assert isinstance(quantity, Quantity)

try:
return to_openmm_inner(quantity)
except MissingOpenMMUnitError:
Expand Down Expand Up @@ -262,7 +264,7 @@ def _ensure_openff_quantity(
)
else:
try:
return unit.Quantity( # type: ignore
return unit.Quantity(
unknown_quantity,
unit.dimensionless,
)
Expand Down
4 changes: 2 additions & 2 deletions openff/units/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ def test_pickle_unit(self):

assert x == y

def test_pick_quantity(self):
def test_pickle_quantity(self):
x = 1.0 * unit.kelvin
y = pickle.loads(pickle.dumps(x))

assert x == y

@skip_if_missing("uncertainties")
def test_pickle_quantity(self):
def test_pickle_quantity_uncertainties(self):
x = (1.0 * unit.kelvin).plus_minus(0.05)
y = pickle.loads(pickle.dumps(x))

Expand Down
62 changes: 18 additions & 44 deletions openff/units/units.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""
Core classes for OpenFF Units
"""

import uuid
import warnings
from typing import TYPE_CHECKING, TypeVar
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 pint import Measurement as _Measurement
from pint import Quantity as _Quantity
from pint import Unit as _Unit
from pint import UnitRegistry as _UnitRegistry

from openff.units.utilities import get_defaults_path

Expand All @@ -24,45 +24,16 @@
"Unit",
]

DEFAULT_UNIT_REGISTRY = pint.UnitRegistry(get_defaults_path())
"""The default unit registry provided by OpenFF Units"""


def _unpickle_quantity(cls, *args):
"""Rebuild quantity upon unpickling using the application registry."""
return pint._unpickle(DEFAULT_UNIT_REGISTRY.Quantity, *args)


def _unpickle_unit(cls, *args):
"""Rebuild unit upon unpickling using the application registry."""
return pint._unpickle(DEFAULT_UNIT_REGISTRY.Unit, *args)


def _unpickle_measurement(cls, *args):
"""Rebuild measurement upon unpickling using the application registry."""
return pint._unpickle(DEFAULT_UNIT_REGISTRY.Measurement, *args)


class Unit(_Unit):
"""A unit of measure."""

_REGISTRY = DEFAULT_UNIT_REGISTRY

def __reduce__(self):
return _unpickle_unit, (Unit, self._units)
pass


_MagnitudeType = TypeVar("_MagnitudeType")


class Quantity(_Quantity[_MagnitudeType]):
class Quantity(_Quantity):
"""A value with associated units."""

_REGISTRY = DEFAULT_UNIT_REGISTRY

def __reduce__(self):
return _unpickle_quantity, (Quantity, self.magnitude, self._units)

def __dask_tokenize__(self):
return uuid.uuid4().hex

Expand All @@ -88,11 +59,6 @@ def to_openmm(self) -> "OpenMMQuantity":
class Measurement(_Measurement):
"""A value with associated units and uncertainty."""

_REGISTRY = DEFAULT_UNIT_REGISTRY

def __reduce__(self):
return _unpickle_measurement, (Measurement, self.magnitude, self._units)

def __dask_tokenize__(self):
return uuid.uuid4().hex

Expand All @@ -102,9 +68,17 @@ def _dask_finalize(results, func, args, units):
return Measurement(values, units)


DEFAULT_UNIT_REGISTRY.Unit = Unit
DEFAULT_UNIT_REGISTRY.Quantity = Quantity
DEFAULT_UNIT_REGISTRY.Measurement = Measurement
class UnitRegistry(_UnitRegistry):
_quantity_class = Quantity
_unit_class = Unit
_measurement_class = Measurement


DEFAULT_UNIT_REGISTRY = UnitRegistry(get_defaults_path())

Unit: _Unit = DEFAULT_UNIT_REGISTRY.Unit # type: ignore[no-redef]
Quantity: _Quantity = DEFAULT_UNIT_REGISTRY.Quantity # type: ignore[no-redef]
Measurement: _Measurement = DEFAULT_UNIT_REGISTRY.Measurement # type: ignore[no-redef]

pint.set_application_registry(DEFAULT_UNIT_REGISTRY)

Expand Down
8 changes: 2 additions & 6 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ exclude_lines =
[flake8]
max-line-length = 119
ignore = E203,W503
per-file-ignores =
openff/units/units.py:F811

[isort]
multi_line_output=3
Expand Down Expand Up @@ -47,14 +49,8 @@ ignore_missing_imports = True
[mypy-pint.*]
ignore_missing_imports = True

[mypy-openff.utilities.*]
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

0 comments on commit 878083a

Please sign in to comment.