-
Notifications
You must be signed in to change notification settings - Fork 1
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
Quantity
initialization does not call jax.numpy.asarray when mantissa is a python number
#94
Conversation
…sa is a python number
Reviewer's Guide by SourceryThis PR addresses a bug in the Sequence diagram for Quantity initialization with Python numbersequenceDiagram
participant Client
participant Quantity
participant mantissa
Client->>Quantity: __init__(mantissa=python_number)
Note over Quantity: Check if mantissa is a number
alt mantissa is a number
Quantity->>mantissa: Keep original value
else mantissa is other type
Quantity->>mantissa: Convert to array
end
Quantity-->>Client: Return Quantity instance
Class diagram for Quantity class changesclassDiagram
class Quantity {
+mantissa
+unit
+__init__(mantissa, unit)
+tolist()
+get(indices_are_sorted, unique_indices, fill_value)
}
note for Quantity "Modified mantissa handling:
- Skip jnp.array for numbers
- Updated tolist() for number types
- Modified get() fill_value handling"
Flow diagram for mantissa type handlingflowchart TD
A[Input mantissa] --> B{Is Python/JAX number?}
B -->|Yes| C[Keep original value]
B -->|No| D[Convert to array]
C --> E[Create Quantity]
D --> E
E --> F[Use in operations]
F --> G{Need conversion?}
G -->|Yes| H[tolist/get methods]
G -->|No| I[Direct use]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @chaoming0625 - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
for c in constants_list: | ||
q_c = getattr(quantity_constants, c) | ||
u_c = getattr(unit_constants, c) | ||
assert q_c.to_decimal(q_c.unit) == (1. * u_c).to_decimal( | ||
q_c.unit), f"Mismatch between {c} in quantity_constants and unit_constants" | ||
assert u.math.isclose( | ||
q_c.to_decimal(q_c.unit), | ||
(1. * u_c).to_decimal(q_c.unit) | ||
), f"Mismatch between {c} in quantity_constants and unit_constants" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): Avoid loops in tests. (no-loop-in-tests
)
Explanation
Avoid complex code, like loops, in test functions.Google's software engineering guidelines says:
"Clear tests are trivially correct upon inspection"
To reach that avoid complex code in tests:
- loops
- conditionals
Some ways to fix this:
- Use parametrized tests to get rid of the loop.
- Move the complex logic into helpers.
- Move the complex part into pytest fixtures.
Complexity is most often introduced in the form of logic. Logic is defined via the imperative parts of programming languages such as operators, loops, and conditionals. When a piece of code contains logic, you need to do a bit of mental computation to determine its result instead of just reading it off of the screen. It doesn't take much logic to make a test more difficult to reason about.
Software Engineering at Google / Don't Put Logic in Tests
This pull request includes several changes to improve the handling of numerical operations and enhance the accuracy of unit tests in the
brainunit
package. The most important changes involve modifying the handling of numerical types, updating test assertions for better precision, and importing necessary libraries.Numerical operations improvements:
brainunit/_base.py
: Updated the__init__
,tolist
, andget
methods to handle numerical types more accurately and avoid unnecessary conversions. [1] [2] [3]Test precision enhancements:
brainunit/_base_test.py
: Modified assertions intest_unit_with_factor
andtest_display
methods to useu.math.isclose
for better precision in floating-point comparisons. [1] [2]brainunit/_celsius_test.py
: Updated assertions intest1
andtest2
methods to useu.math.allclose
andnp.isclose
for improved accuracy.brainunit/constants_test.py
: Enhanced thetest_quantity_constants_and_unit_constants
method to useu.math.isclose
for precise comparison of constants.Library imports:
brainunit/_celsius_test.py
: Added an import fornumpy
to support the use ofnp.isclose
.Summary by Sourcery
Fix quantity initialization for Python numbers and improve test assertions.
Bug Fixes:
Quantity
initialization did not correctly handle Python numbers.u.math.isclose
andnp.isclose
.