Fix CompoundModel.input_units to be independent of operand order#20020
Open
PranavAchar01 wants to merge 4 commits into
Open
Fix CompoundModel.input_units to be independent of operand order#20020PranavAchar01 wants to merge 4 commits into
PranavAchar01 wants to merge 4 commits into
Conversation
For arithmetic operators the inputs are shared by both operands, but inputs_map only records the left one, so input_units ignored the right operand. An expression like Const1D() + Gaussian1D() (whose left operand has no input units) returned None, while the reversed order returned the correct units. Fill in the units of any input the left operand does not constrain from the right operand. Closes astropy#17040
Contributor
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
There was a problem hiding this comment.
Pull request overview
This PR fixes CompoundModel.input_units for arithmetic compound models so the computed input units no longer depend on operand order, addressing the case where only the right-hand operand constrains input units (e.g., Const1D() + Gaussian1D()).
Changes:
- Update
CompoundModel.input_unitsto fill unconstrained input units from the right operand for arithmetic operators (+ - * / **). - Add a regression test covering operand-order independence for
input_units. - Add a changelog fragment documenting the bugfix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
astropy/modeling/core.py |
Ensures arithmetic compound models can derive missing input-unit constraints from the right operand. |
astropy/modeling/tests/test_quantities_model.py |
Adds a regression test to prevent operand-order-dependent input_units. |
docs/changes/modeling/20020.bugfix.rst |
Documents the bugfix in the modeling changelog. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+3900
to
+3905
| if self.op in ("+", "-", "*", "/", "**"): | ||
| right_units = self.right.input_units | ||
| if right_units: | ||
| for left_key, right_key in zip(self.inputs, self.right.inputs): | ||
| if left_key not in input_units_dict and right_key in right_units: | ||
| input_units_dict[left_key] = right_units[right_key] |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This pull request fixes
CompoundModel.input_unitsreturning a result that depends on the operand order for arithmetic operators.Fixes #17040.
For arithmetic operators (
+ - * / **) the inputs are shared by both operands, butinputs_map()only records the left operand.input_unitstherefore never consulted the right operand, so an expression whose left operand has no input units (e.g.Const1D) returnedNone, while the reversed order returned the correct units:The fix fills in the units of any input the left operand does not constrain from the right operand, so the result is independent of operand order. If neither operand constrains the units the result is still
None. Composition (|) and join (&) are unaffected.A regression test is added in
astropy/modeling/tests/test_quantities_model.py. Theastropy/modelingtest suite passes locally.Note: this change was prepared with the assistance of Claude Code; it has been reviewed and tested locally by the submitter.