Skip to content

Refactor power derivative functions #294

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

Merged
merged 18 commits into from
Apr 17, 2025
Merged

Conversation

jagerber48
Copy link
Contributor

@jagerber48 jagerber48 commented Mar 14, 2025

  • Closes Clean up power derivative calculations #293
  • Executed pre-commit run --all-files with no errors
  • The change is fully covered by automated unit tests
  • Documented in docs/ as appropriate
  • Added an entry to the CHANGES file

uncertainties allows calculating x**y where one or both of x and y are UFloat objects. For this we need to calculate the x and y derivatives of x**y. This PR refactors the functions that calculate those derivatives to more explicitly and clearly handle non-typical cases. Moving those functions to the module level also allows these functions to be directly tested.

Copy link

codecov bot commented Mar 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.54%. Comparing base (3bd3b9d) to head (f10d77a).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #294      +/-   ##
==========================================
+ Coverage   86.47%   86.54%   +0.07%     
==========================================
  Files          18       18              
  Lines        1893     1903      +10     
==========================================
+ Hits         1637     1647      +10     
  Misses        256      256              
Flag Coverage Δ
macos-latest-3.10 95.58% <100.00%> (+<0.01%) ⬆️
macos-latest-3.11 95.58% <100.00%> (+<0.01%) ⬆️
macos-latest-3.12 95.58% <100.00%> (+<0.01%) ⬆️
macos-latest-3.8 95.58% <100.00%> (+<0.01%) ⬆️
macos-latest-3.9 95.58% <100.00%> (+<0.01%) ⬆️
no-numpy 78.71% <100.00%> (+0.11%) ⬆️
ubuntu-latest-3.10 95.58% <100.00%> (+<0.01%) ⬆️
ubuntu-latest-3.11 95.58% <100.00%> (+<0.01%) ⬆️
ubuntu-latest-3.12 95.58% <100.00%> (+<0.01%) ⬆️
ubuntu-latest-3.8 95.58% <100.00%> (+<0.01%) ⬆️
ubuntu-latest-3.9 95.58% <100.00%> (+<0.01%) ⬆️
windows-latest-3.10 95.58% <100.00%> (+<0.01%) ⬆️
windows-latest-3.11 95.58% <100.00%> (+<0.01%) ⬆️
windows-latest-3.12 95.58% <100.00%> (+<0.01%) ⬆️
windows-latest-3.8 95.58% <100.00%> (+<0.01%) ⬆️
windows-latest-3.9 95.58% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

codspeed-hq bot commented Mar 14, 2025

CodSpeed Performance Report

Merging #294 will not alter performance

Comparing jagerber48:pow_derivs_funcs (f10d77a) with master (3bd3b9d)

Summary

✅ 5 untouched benchmarks

newville
newville previously approved these changes Apr 5, 2025
Copy link
Member

@newville newville left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for adding those tests.

@@ -119,29 +141,6 @@ def get_ops_with_reflection():
eval("lambda y, x: %s" % expr) for expr in reversed(derivatives)
]

# The derivatives of pow() are more complicated:

# The case x**y is constant one the line x = 0 and in y = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

it's a shame to lose this comment. is there somewhere it could be moved to? I would say a docstring but that's not useful for __pow__.

maybe it could be added to umath.pow's docstring?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me take a look. I actually spent a VERY long time puzzling over various special cases and I had some rewrites of this same comment.

Part of the problem is that the old function handled different special cases using different approaches. For example, the old function allowed complex results to be returned but, instead, relied on some other part of the codebase to prevent complex results.

Under this PR the comment is not actually correct anymore but I can come up with replacement text. But I'm curious, do you want a comment for the developer's sake (like the old comment used to be) or for the user's sake? For the latter we can add some description under the umath.pow docstring. We can also add documentation to the docs webpage. If we want a comment for the developers sake then I can update the old comments with some notes about the various special cases and how they're handled.

Also happy to do all of the above, though it ends up being a lot of text because there's a few different cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's most useful for users so a docstring or section of the documentation, either works.

@jagerber48
Copy link
Contributor Author

@andrewgsavage @newville I've included words and codeblocks in the user guide documentation near the nan/inf discussion. Let me know what you think. There are more cases that could be demonstrated. I also intentionally left out examples with std_dev=0 because those confuse the story greatly re #283.

Are more cases needed to show the different situations? Do we want any of this to appear on the docstring for umath.pow()?

I'm relatively happy where it is, but open to suggestions.

@andrewgsavage
Copy link
Contributor

seems good

@jagerber48
Copy link
Contributor Author

Cleaned it up a little more. Think it's ready for approval and merging if others agree.

@jagerber48
Copy link
Contributor Author

Can someone please approve and merge #302 and then merge this PR? @newville @andrewgsavage? After that we are ready to release version 3.2.3 and begin work on the 4.0.0 release.

Copy link
Member

@newville newville left a comment

Choose a reason for hiding this comment

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

@jagerber48 @andrewgsavage This looks good to me... Any objections to merging?

@andrewgsavage
Copy link
Contributor

go for it

@newville newville merged commit 9b1844d into lmfit:master Apr 17, 2025
22 checks passed
@jagerber48 jagerber48 deleted the pow_derivs_funcs branch April 18, 2025 03:27
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.

Clean up power derivative calculations
3 participants