You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: implement __sub__ and __rsub__ for Loss class - #152
Enables subtraction syntax: SomeLoss() - OtherLoss() and
constant - SomeLoss().
Previously users had to hack around this with (-1) * SomeLoss().
Closesjankrepl#68
The reason will be displayed to describe this comment to others. Learn more.
Copilot review overview
🟡 Changes recommended
Unresolved moderate issues affect unary negation and nested-subtraction repr correctness; reverse-subtraction coverage and documentation also need updates.
Get a fresh assessment by requesting another Copilot review.
Adds subtraction support to Loss, including reflected constant subtraction, with expanded arithmetic and representation tests.
Changes:
Implements Loss.__sub__ and Loss.__rsub__.
Adds subtraction arithmetic, repr, and invalid-type test coverage.
File
Review summary
tests/test_losses.py
Adds subtraction coverage, but lacks valid reverse-subtraction evaluation and representation tests.
deepdow/losses.py
Adds subtraction operators, but unary negation is unsupported, nested subtraction repr loses grouping, and the docstring does not match supported operand types.
…b tests
- Add __neg__ for unary -SomeLoss() (the issue jankrepl#68 requirement)
- Parenthesize right operand in __sub__ repr: a - (b) instead of a - b
so eval(repr(...)) round-trip preserves semantics for nested subtraction
- Parenthesize both operands in __rsub__ repr for consistency
- Add test_neg for unary negation
- Add test_rsub for constant - loss path with repr round-trip
- Add test_sub_repr_roundtrip for nested subtraction grouping
Unary negation: Added __neg__ so -SomeLoss() now works. repr uses -() wrapping.
Repr grouping: Right operand in __sub__ is now parenthesized: a - (b) instead of a - b. This preserves eval(repr(...)) round-trip for nested subtraction like a - (b - c).
rsub test coverage: Added test_rsub that exercises constant - loss with evaluation and repr round-trip checks.
Also added test_sub_repr_roundtrip specifically for nested subtraction grouping.
__rsub__ rejects Loss operands, but Python can dispatch the reflected method for left_loss - right_loss when right_loss is a subclass of left_loss. In that case a valid Loss–Loss subtraction reaches this branch and raises TypeError; handle Loss by delegating to other.__sub__(self) before the numeric case (and add a regression test with a derived loss class).
…asses in __rsub__
- Fix test_sub_repr_roundtrip: use correct class names (StandardDeviation,
MaximumDrawdown) instead of undefined (Variance, MaxDrawdown)
- Handle Loss subclasses in __rsub__: when a Loss subclass is on the right
side of subtraction, Python dispatches to __rsub__ on the subclass. Now
handles Loss operands by delegating to other.__sub__(self) equivalent.
- Add test_rsub_with_loss_subclass regression test
Undefined class names: Fixed Variance → StandardDeviation and MaxDrawdown → MaximumDrawdown in test_sub_repr_roundtrip. Test now uses the actual imported class names.
Loss subclass dispatch in rsub: Added isinstance(other, Loss) check to __rsub__. When base_loss - subclass_loss and the subclass doesn't define __sub__, Python dispatches to subclass.__rsub__(base_loss). Now handles this case correctly. Added test_rsub_with_loss_subclass regression test.
Hi! Just checking in on this PR — happy to make any changes if needed. Thanks for your time!
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
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.
What
Implements
__sub__and__rsub__on theLossclass so users can write:combined = SomeLoss() - OtherLoss()
constant_sub = 5 - SomeLoss()
Previously the only way to achieve this was SomeLoss() + (-1) * OtherLoss().
Closes #68
Changes
__sub__method toLossclass (follows__add__pattern exactly)__rsub__method forconstant - losssyntaxsubto parametrized arithmetic testssubto repr arithmetic testsTesting
All existing tests pass. New tests cover:
__rsub__)