Refactor math module for Quantity/CustomArray; add dtype aliases#50
Refactor math module for Quantity/CustomArray; add dtype aliases#50chaoming0625 merged 3 commits intomainfrom
Conversation
Reviewer's GuideThis PR refactors the math module to standardize quantity handling through custom array wrappers, adds a full set of dtype alias constants, extends gradient behavior with unit support and error handling, and augments coverage with new comprehensive tests. Class diagram for updated isscalar property in QuantityclassDiagram
class Quantity {
+isscalar: bool
}
Quantity : isscalar = ndim == 0
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
@sourcery-ai title |
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- Consider introducing a decorator or helper wrapper to apply maybe_custom_array/ maybe_custom_array_tree instead of calling it manually at the top of every function to reduce boilerplate and the risk of missing it in future additions.
- The extensive list of dtype aliases in all and as top-level assignments could be maintained programmatically (e.g. from a mapping or loop over jnp attributes) to improve readability and reduce manual errors when JAX adds or deprecates types.
- The change of isscalar to
ndim == 0may not exactly mirror the previous is_scalar_type behavior for all input types (e.g. Python scalars or booleans), so double-check that it covers all intended scalar cases.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider introducing a decorator or helper wrapper to apply maybe_custom_array/ maybe_custom_array_tree instead of calling it manually at the top of every function to reduce boilerplate and the risk of missing it in future additions.
- The extensive list of dtype aliases in __all__ and as top-level assignments could be maintained programmatically (e.g. from a mapping or loop over jnp attributes) to improve readability and reduce manual errors when JAX adds or deprecates types.
- The change of isscalar to `ndim == 0` may not exactly mirror the previous is_scalar_type behavior for all input types (e.g. Python scalars or booleans), so double-check that it covers all intended scalar cases.
## Individual Comments
### Comment 1
<location> `saiunit/math/_misc.py:388` </location>
<code_context>
+ a = maybe_custom_array(a)
if isinstance(a, Quantity):
- return jnp.finfo(a.value)
+ return jnp.finfo(a.mantissa)
else:
return jnp.finfo(a)
</code_context>
<issue_to_address>
Switching from a.value to a.mantissa in finfo may change behavior.
Verify that mantissa is consistently the appropriate attribute for dtype introspection, as differences from value could impact the returned information.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| a = maybe_custom_array(a) | ||
| if isinstance(a, Quantity): | ||
| return jnp.finfo(a.value) | ||
| return jnp.finfo(a.mantissa) |
There was a problem hiding this comment.
issue (bug_risk): Switching from a.value to a.mantissa in finfo may change behavior.
Verify that mantissa is consistently the appropriate attribute for dtype introspection, as differences from value could impact the returned information.
| if isinstance(a, Quantity): | ||
| return jnp.finfo(a.value) | ||
| return jnp.finfo(a.mantissa) | ||
| else: | ||
| return jnp.finfo(a) |
There was a problem hiding this comment.
suggestion (code-quality): Replace if statement with if expression (assign-if-exp)
| if isinstance(a, Quantity): | |
| return jnp.finfo(a.value) | |
| return jnp.finfo(a.mantissa) | |
| else: | |
| return jnp.finfo(a) | |
| return jnp.finfo(a.mantissa) if isinstance(a, Quantity) else jnp.finfo(a) |
| if isinstance(a, Quantity): | ||
| return jnp.iinfo(a.value) | ||
| return jnp.iinfo(a.mantissa) | ||
| else: | ||
| return jnp.iinfo(a) |
There was a problem hiding this comment.
suggestion (code-quality): Replace if statement with if expression (assign-if-exp)
| if isinstance(a, Quantity): | |
| return jnp.iinfo(a.value) | |
| return jnp.iinfo(a.mantissa) | |
| else: | |
| return jnp.iinfo(a) | |
| return jnp.iinfo(a.mantissa) if isinstance(a, Quantity) else jnp.iinfo(a) |
Summary by Sourcery
Register dtype aliases in the math module and refactor its functions to better support Quantity inputs by unwrapping CustomArray types, update the is_scalar check on Quantity, and expand tests to cover constants, type predicates, introspection utilities, gradient operations, and window functions.
New Features:
Enhancements:
Tests: