-
Notifications
You must be signed in to change notification settings - Fork 52
[WIP] Addition of UnitOperationWarning and strict kwarg to UnitRegistry
#166
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
7a1553f
fb05c4d
152ba95
b433c19
ed298ad
b19e139
7230006
04e48f7
05f0763
d64b089
0d0bf7d
be41182
e5b139a
0943e71
b15d0ad
2533b4f
d3436be
f87b4bf
8f20ac7
75e89d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,6 +125,7 @@ | |
| UnitConversionError, | ||
| UnitsNotReducible, | ||
| SymbolNotFoundError, | ||
| UnitOperationWarning, | ||
| ) | ||
| from unyt.equivalencies import equivalence_registry | ||
| from unyt._on_demand_imports import _astropy, _pint | ||
|
|
@@ -140,6 +141,7 @@ | |
|
|
||
| NULL_UNIT = Unit() | ||
| POWER_SIGN_MAPPING = {multiply: 1, divide: -1} | ||
| STRICT = True | ||
mkhorton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| __doctest_requires__ = { | ||
| ("unyt_array.from_pint", "unyt_array.to_pint"): ["pint"], | ||
|
|
@@ -166,6 +168,15 @@ def _iterable(obj): | |
| return True | ||
|
|
||
|
|
||
| def _unit_operation_error_raise_or_warn(ufunc, u0, u1, func, *inputs): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am hesitant about how this function implements the proposed functionality. Specifically, it feels weird to replace a raise statement with a return statement (where this function is used).
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I share this uneasiness but I'm not sure of a better solution. It seems clear to me that some users will want this to fail explicitly and raise, whereas others will just want the warning, so there has to be a conditional somewhere that switches between that behaviour?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To your second comment, makes sense, I'll remove the |
||
| if u0.registry.strict and u1.registry.strict: # True by default | ||
| raise UnitOperationError(ufunc, u0, u1) | ||
| else: | ||
| warnings.warn(UnitOperationWarning(ufunc, u0, u1)) | ||
| unwrapped_inputs = [i.value if isinstance(i, unyt_array) else i for i in inputs] | ||
| return func(*unwrapped_inputs) | ||
mkhorton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @lru_cache(maxsize=128, typed=False) | ||
| def _sqrt_unit(unit): | ||
| return 1, unit ** 0.5 | ||
|
|
@@ -1759,12 +1770,16 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): | |
| elif ufunc is power: | ||
| u1 = inp1 | ||
| if inp0.shape != () and inp1.shape != (): | ||
| raise UnitOperationError(ufunc, u0, u1) | ||
| return _unit_operation_error_raise_or_warn( | ||
| ufunc, u0, u1, func, *inputs | ||
| ) | ||
| if isinstance(u1, unyt_array): | ||
| if u1.units.is_dimensionless: | ||
| pass | ||
| else: | ||
| raise UnitOperationError(ufunc, u0, u1.units) | ||
| return _unit_operation_error_raise_or_warn( | ||
| ufunc, u0, u1.units, func, *inputs | ||
| ) | ||
| if u1.shape == (): | ||
| u1 = float(u1) | ||
| else: | ||
|
|
@@ -1814,9 +1829,13 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): | |
| ret = bool(ret) | ||
| return ret | ||
| else: | ||
| raise UnitOperationError(ufunc, u0, u1) | ||
| return _unit_operation_error_raise_or_warn( | ||
| ufunc, u0, u1, func, *inputs | ||
| ) | ||
| else: | ||
| raise UnitOperationError(ufunc, u0, u1) | ||
| return _unit_operation_error_raise_or_warn( | ||
| ufunc, u0, u1, func, *inputs | ||
| ) | ||
| conv, offset = u1.get_conversion_factor(u0, inp1.dtype) | ||
| new_dtype = np.dtype("f" + str(inp1.dtype.itemsize)) | ||
| conv = new_dtype.type(conv) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.