You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's a rough edge I found while debugging some code in which I can't trust the type of the object I'm interacting with:
In [1]: fromopenff.units.openmmimportensure_quantityIn [2]: importnumpyIn [3]: partial_charges=numpy.linspace(-1, 1, 9, dtype=float)
In [4]: ensure_quantity(partial_charges, "openff")
---------------------------------------------------------------------------ExceptionTraceback (mostrecentcalllast)
CellIn [4], line1---->1ensure_quantity(partial_charges, "openff")
File~/mambaforge/envs/openmmforcefields-test/lib/python3.9/site-packages/openff/units/openmm.py:240, inensure_quantity(unknown_quantity, type_to_ensure)
238return_ensure_openmm_quantity(unknown_quantity)
239eliftype_to_ensure=="openff":
-->240return_ensure_openff_quantity(unknown_quantity)
241else:
242raiseValueError(
243f"Unsupported `type_to_ensure` found. Given {type_to_ensure}, "244"expected 'openff' or 'openmm'."245 )
File~/mambaforge/envs/openmmforcefields-test/lib/python3.9/site-packages/openff/units/openmm.py:230, in_ensure_openff_quantity(unknown_quantity)
226raiseValueError(
227f"Failed to process input of type {type(unknown_quantity)}."228 )
229else:
-->230raiseExceptionException:
In #44 I only considered that openmm.unit.Quantity or openff.units.Quantity would be passed through. I think some users would expect ensure_quantity to make some effort to coerce things into those types if asked. I'm less passionate about coercing int/float but the behavior should be similar.
The main downside of this approach is that it the resulting object must be a unitless quantity. Pint handles explicitly unitless quantities in a way that feels naturally to me via carrying around a dimensioness unit. OpenMM does a pretty good job with a similar solution but has a couple of gotchas, i.e. openmm/openmm#2247:
The text was updated successfully, but these errors were encountered:
mattwthompson
changed the title
ensure_quantity should wrap arrays if providedensure_quantity should attempt to wrap arrays and primitives
Sep 19, 2022
Here's a rough edge I found while debugging some code in which I can't trust the type of the object I'm interacting with:
In #44 I only considered that
openmm.unit.Quantity
oropenff.units.Quantity
would be passed through. I think some users would expectensure_quantity
to make some effort to coerce things into those types if asked. I'm less passionate about coercingint
/float
but the behavior should be similar.The main downside of this approach is that it the resulting object must be a unitless quantity. Pint handles explicitly unitless quantities in a way that feels naturally to me via carrying around a
dimensioness
unit. OpenMM does a pretty good job with a similar solution but has a couple of gotchas, i.e. openmm/openmm#2247:The text was updated successfully, but these errors were encountered: