Skip to content
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

Speed up recursions #307

Open
wants to merge 38 commits into
base: development
Choose a base branch
from
Open

Speed up recursions #307

wants to merge 38 commits into from

Conversation

BalzaniEdoardo
Copy link
Collaborator

@BalzaniEdoardo BalzaniEdoardo commented Feb 12, 2025

In this PR:

  • New accept integer for __mul__: basis * 3 will be equivalent to basis + basis+basis
  • Add an __rmul__ enabling right multiplicationn * basis
  • Improved the efficiency of __pow__ by bisect multiply (log2(n) multiplication, less deep copy)
  • Add a mechanism that do not perform a deep-copy when cloning a bases

Old clone performance

bas = nmo.basis.BSplineEval(5)**500
%timeit bas.__sklearn_clone__()
2.47 s ± 107 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

New clone performance

bas = nmo.basis.BSplineEval(5)**500
%timeit bas.__sklearn_clone__()
10.9 ms ± 22 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Old Power Performance

bas = nmo.basis.BSplineEval(5)
%timeit bas ** 500
2.49 s ± 76.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

New Power Performance

bas = nmo.basis.BSplineEval(5)
%timeit bas ** 500
8.46 ms ± 79.3 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

@BalzaniEdoardo BalzaniEdoardo marked this pull request as ready for review February 28, 2025 16:46
@codecov-commenter
Copy link

codecov-commenter commented Feb 28, 2025

Codecov Report

Attention: Patch coverage is 96.82540% with 2 lines in your changes missing coverage. Please review.

Project coverage is 97.00%. Comparing base (a510ef3) to head (54f9c97).
Report is 361 commits behind head on development.

Files with missing lines Patch % Lines
src/nemos/basis/_basis.py 95.34% 2 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##           development     #307      +/-   ##
===============================================
+ Coverage        96.13%   97.00%   +0.86%     
===============================================
  Files               34       36       +2     
  Lines             2642     3208     +566     
===============================================
+ Hits              2540     3112     +572     
+ Misses             102       96       -6     

☔ 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
Member

@billbrod billbrod left a comment

Choose a reason for hiding this comment

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

Generally looks good! Just a couple of small changes:

  • Need to update labels at the end of the mul/pow recursion (and add test).
bas = nmo.basis.BSplineEval(5)
add = bas*3
add.basis1.basis1.label == add.basis2.label
  • pow should have the same check about label as mul
  • I think it's worth changing the references to _shallow_copy and _set_shallow_copy_temporarily to self.__class__ or similar, to make it clear that it's class-level behavior we're changing.
  • I also think you can remove the temporarily part of that method: it's a context manager, so temporarily is implied.

@billbrod
Copy link
Member

billbrod commented Mar 5, 2025

While reviewing this PR, I looked into how to use not-quite-global variables, like the new shallow_copy flag. That flag affects the behavior of some functions of all composite classes, so it can't be instance level (i.e., self._shallow_copy). Right now, it's class level (self.__class__._shallow_copy), which I think is right. While looking into this, came across this thread, which is discussing how stdlib's warnings handles filters, and they're set at the module-level, using sys.modules["warnings"].filters. So if we had flags that affected the behavior across the module (not just within a single class/mixin), then that would be the way to go.

@BalzaniEdoardo
Copy link
Collaborator Author

BalzaniEdoardo commented Mar 5, 2025

I think I should have addressed everything, including adding missing tests!
@billbrod note that I added a call to the function that recomputes all default labels after the shallow copy composition is completed, and I realized tan extra corner case that I fixed

add = nmo.basis.BSplineEval(5)*2

# this would have called deep-copy in original implementation but it would copy the parent as well.
# now it prints nothing
out = add.basis1 * 1

# originally would have printed the same as print(add)
print(out._parent)

@BalzaniEdoardo BalzaniEdoardo requested a review from billbrod March 5, 2025 23:14
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.

4 participants