Skip to content

Refactor math module for Quantity/CustomArray; add dtype aliases#50

Merged
chaoming0625 merged 3 commits intomainfrom
misc
Sep 14, 2025
Merged

Refactor math module for Quantity/CustomArray; add dtype aliases#50
chaoming0625 merged 3 commits intomainfrom
misc

Conversation

@chaoming0625
Copy link
Member

@chaoming0625 chaoming0625 commented Sep 14, 2025

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:

  • Expose JAX dtype constants (e.g., float32, int64, bfloat16) and aliases in saiunit.math

Enhancements:

  • Refactor math functions (is_quantity, result_type, ndim, shape, size, finfo, iinfo, get_dtype, isfinite, isinf, isnan, isreal, isscalar, is_float, is_int, gradient) to uniformly handle Quantity and CustomArray inputs via maybe_custom_array and maybe_custom_array_tree
  • Update Quantity.is_scalar property to check ndim == 0

Tests:

  • Add tests for math constants, dtype aliases, array vs quantity predicates, shape and dtype introspection, finfo/iinfo behavior with quantities, gradient operations with unit handling, and window function outputs

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Sep 14, 2025

Reviewer's Guide

This 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 Quantity

classDiagram
    class Quantity {
        +isscalar: bool
    }
    Quantity : isscalar = ndim == 0
Loading

File-Level Changes

Change Details Files
Introduce dtype alias constants in the math module
  • Extended all with new dtype alias names
  • Defined aliases mapping to jnp fundamental types (e.g., float32, complex128)
  • Added shorthand names (single, double, csingle, cdouble)
saiunit/math/_misc.py
Inject custom array unwrapping in quantity-aware functions
  • Imported maybe_custom_array and maybe_custom_array_tree
  • Wrapped inputs to functions (is_quantity, result_type, ndim, isreal, isscalar, isfinite, isinf, isnan, shape, size, finfo, iinfo, get_dtype, is_float, is_int) to handle Quantity uniformly
saiunit/math/_misc.py
Enhance gradient to support quantities and multi-axis
  • Unpack and unwrap f and spacing via maybe_custom_array_tree
  • Handle unit spacing by extracting mantissa and reconstructing Quantity outputs
  • Raise NotImplementedError for unsupported edge_order
saiunit/math/_misc.py
Adjust CustomArray scalar detection
  • Replaced is_scalar_type check with ndim == 0 in isscalar property
saiunit/_base.py
Fix custom array tree mapping predicate
  • Corrected is_leaf lambda parameter name in maybe_custom_array_tree
saiunit/_misc.py
Add comprehensive tests for constants, dtype aliases, and quantity operations
  • New tests for math constants and dtype alias lookups
  • Predicates (isreal, isfinite, isnan, etc.) on arrays and quantities
  • Broadcast, result_type, dtype detection, gradient and window function behaviors
saiunit/math/_misc_test.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@chaoming0625
Copy link
Member Author

@sourcery-ai title

@sourcery-ai sourcery-ai bot changed the title Refactor quantity handling and add dtype aliases in math module Refactor math module for Quantity/CustomArray; add dtype aliases Sep 14, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

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 == 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines 387 to 390
if isinstance(a, Quantity):
return jnp.finfo(a.value)
return jnp.finfo(a.mantissa)
else:
return jnp.finfo(a)
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (code-quality): Replace if statement with if expression (assign-if-exp)

Suggested change
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)

Comment on lines 396 to 399
if isinstance(a, Quantity):
return jnp.iinfo(a.value)
return jnp.iinfo(a.mantissa)
else:
return jnp.iinfo(a)
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (code-quality): Replace if statement with if expression (assign-if-exp)

Suggested change
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)

@chaoming0625 chaoming0625 merged commit 1b42fde into main Sep 14, 2025
19 checks passed
@chaoming0625 chaoming0625 deleted the misc branch September 14, 2025 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant