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

performance optimize _generate_fourier_series_np #516

Merged
merged 8 commits into from
Sep 10, 2024

Conversation

mathause
Copy link
Member

@mathause mathause commented Sep 3, 2024

Performance optimization of _generate_fourier_series_np. Builds on #512 so the diff is actually smaller. This does two main things:

  1. cos(alpha) is periodic - so we only need to compute it once for each month (instead of n_years * 12 times). We then reshape the yearly_predictor to n_years x 12 such that broadcasting ensures each element is multiplied with the coefficients
  2. Change the order of operation: sum the coefficients before multiplying - this avoids a lot of multiplication and sum operations. So instead of
    $$\sum\left((a_i \cdot T + b_i) \cdot \cos(\alpha) + (c_i \cdot T + d_i) \cdot \sin(\alpha)\right)$$
    we do
    $$\sum\left(a_i \cdot \cos(\alpha) + c_i \cdot \sin(\alpha)\right) \cdot T + \sum\left(b_i \cdot \cos(\alpha) + d_i \cdot \sin(\alpha)\right)$$

This speeds up _generate_fourier_series_np about 9 times and fit_harmonic_model about 4 times (on my machine). We do pay this with a small loss in precision (see changes in tests)


What we do additionally:

  1. Combine the $\cos$ and $\sin$ array into one so we can directly multiply them with the coeffs
  2. use @ to compute the product-sum
  3. optimize the creation of the alpha array for the cost of some readability

What else could we do:

  1. Split the function into two to pre-allocate the cos_sin array similar yeo-johnson: performance optimization #496
  2. use fft to get better inital conditions (fourier series: more consistent with standard definition #512 (comment))

Copy link

codecov bot commented Sep 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 49.85%. Comparing base (444c682) to head (75b6f70).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #516      +/-   ##
==========================================
+ Coverage   49.73%   49.85%   +0.12%     
==========================================
  Files          50       50              
  Lines        3557     3566       +9     
==========================================
+ Hits         1769     1778       +9     
  Misses       1788     1788              
Flag Coverage Δ
unittests 49.85% <100.00%> (+0.12%) ⬆️

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.

Comment on lines +186 to +187
# assume mse smaller eps is 'perfect' - only relevant for noiseless test data
mse = mse if mse > np.finfo(float).eps else 0.0
Copy link
Member Author

Choose a reason for hiding this comment

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

mesmer/stats/_harmonic_model.py Outdated Show resolved Hide resolved
CHANGELOG.rst Outdated
`#415 <https://github.com/MESMER-group/mesmer/pull/415>`_,
`#424 <https://github.com/MESMER-group/mesmer/pull/424>`_, and
`#433 <https://github.com/MESMER-group/mesmer/pull/433>`_)
- Refactor variable names, small code improvements, fixes and clean docstring
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- Refactor variable names, small code improvements, fixes and clean docstring
- Refactor variable names, small code improvements, optimization, fixes and clean docstring

@mathause mathause marked this pull request as ready for review September 10, 2024 07:50
@mathause
Copy link
Member Author

Thanks for the review. Let's merge.

@mathause mathause merged commit 41fdd9c into MESMER-group:main Sep 10, 2024
9 checks passed
@mathause mathause deleted the performance_opt_fourier_series branch September 10, 2024 07:57
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.

optimize _generate_fourier_series_np
2 participants