Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an internal allclose function #35

Merged
merged 7 commits into from
Feb 17, 2019
Merged

Add an internal allclose function #35

merged 7 commits into from
Feb 17, 2019

Conversation

mattwthompson
Copy link
Member

This is born out of an issue using numpy.allclose on two unyt arrays, the critical part here:

If the following equation is element-wise True, then allclose returns True.
absolute(a - b) <= (atol + rtol * absolute(b))

When the arguments are unyt arrays, the expression on the right attempts to add a unyt array (b) to a unitless float (atol). This function attempts to infer a common unit (loosely defined here by which array has units closest to an order of magnitude of 0, i.e. 10^0 = 1) and multiple atol through by that unit.

This is also the start of an internal testing module, where we should try and develop our own functions to compare things according to own preferred criteria.

topology/testing/utils.py Outdated Show resolved Hide resolved
-------
common_unit : u.Unit
"""

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this is mainly just an internal testing function, and so any developer will probably have a better sense of the units/quantities s/he will be dealing with, but there could just be a basic sanity check that dimensionality is the same? Just compare that a.in_base() == b.in_base()?

if atol is None:
atol = 1e-8 * common_unit

if rtol is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, a slight nitpick because people using this function are probably going to be fairly familiar with the syntax for atol and rtol, but if rtol is not None and we use the provided-argument for rtol, we could check unit consistency for the provided rtol? If you pass a unit-less rtol, then you get a somewhat vague unyt error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I tried to handle the cases of unitless rtol and/or atol. I think if a value is passed in for either and it's not necessarily with the same unit, unyt should handle it. Let me know what you think of the changes.

@mattwthompson mattwthompson merged commit e3f24d6 into master Feb 17, 2019
@mattwthompson mattwthompson deleted the add-testing branch February 28, 2020 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants